FluentUI/src/controls/FluAutoSuggestBox.qml

160 lines
4.9 KiB
QML
Raw Normal View History

2023-03-30 21:52:55 +08:00
import QtQuick
import QtQuick.Controls
import FluentUI
2023-03-09 15:49:37 +08:00
2023-04-02 14:05:54 +08:00
FluTextBox{
2023-03-30 17:16:57 +08:00
property var items:[]
2023-04-01 21:01:46 +08:00
property string emptyText: "没有找到结果"
2023-04-10 18:17:22 +08:00
property int autoSuggestBoxReplacement: FluentIcons.Zoom
2023-03-30 17:16:57 +08:00
signal itemClicked(var data)
2023-03-29 21:43:01 +08:00
signal handleClicked
2023-03-30 18:23:33 +08:00
QtObject{
id:d
property bool flagVisible: true
}
2023-04-01 21:01:46 +08:00
id:control
2023-03-09 15:49:37 +08:00
width: 300
Component.onCompleted: {
2023-04-01 21:01:46 +08:00
loadData()
2023-03-09 15:49:37 +08:00
}
Popup{
2023-04-01 21:01:46 +08:00
id:control_popup
y:control.height
2023-03-30 18:23:33 +08:00
focus: false
2023-03-28 18:14:14 +08:00
enter: Transition {
NumberAnimation {
property: "y"
from:0
2023-04-01 21:01:46 +08:00
to:control_popup.y
2023-03-28 18:14:14 +08:00
duration: 150
}
2023-03-29 15:43:23 +08:00
NumberAnimation {
property: "opacity"
from:0
to:1
duration: 150
}
2023-03-28 18:14:14 +08:00
}
2023-03-12 14:26:03 +08:00
onVisibleChanged: {
if(visible){
list_view.currentIndex = -1
}
}
2023-03-09 15:49:37 +08:00
background: Rectangle{
2023-04-01 21:01:46 +08:00
width: control.width
2023-03-09 15:49:37 +08:00
radius: 4
FluShadow{
radius: 4
}
2023-03-28 21:37:10 +08:00
color: FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(248/255,250/255,253/255,1)
2023-03-09 15:49:37 +08:00
height: 38*Math.min(Math.max(list_view.count,1),8)
ListView{
id:list_view
anchors.fill: parent
clip: true
2023-03-12 14:26:03 +08:00
currentIndex: -1
2023-03-14 18:23:12 +08:00
ScrollBar.vertical: FluScrollBar {}
2023-03-09 15:49:37 +08:00
header: Item{
2023-04-01 21:01:46 +08:00
width: control.width
2023-03-09 15:49:37 +08:00
height: visible ? 38 : 0
visible: list_view.count === 0
FluText{
2023-04-01 21:01:46 +08:00
text:emptyText
2023-03-09 15:49:37 +08:00
anchors{
verticalCenter: parent.verticalCenter
left: parent.left
2023-03-09 23:11:59 +08:00
leftMargin: 10
2023-03-09 15:49:37 +08:00
}
}
}
2023-03-12 14:26:03 +08:00
delegate:Control{
2023-04-01 21:01:46 +08:00
width: control.width
2023-03-12 14:26:03 +08:00
padding:10
background: Rectangle{
2023-03-09 15:49:37 +08:00
color: {
2023-03-12 14:26:03 +08:00
if(list_view.currentIndex === index){
2023-03-28 21:37:10 +08:00
return FluTheme.dark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
2023-03-12 14:26:03 +08:00
}
if(hovered){
2023-03-28 21:37:10 +08:00
return FluTheme.dark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
2023-03-09 15:49:37 +08:00
}
2023-03-28 21:37:10 +08:00
return FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(0,0,0,0)
2023-03-09 15:49:37 +08:00
}
MouseArea{
2023-03-12 14:26:03 +08:00
id:mouse_area
2023-03-09 15:49:37 +08:00
anchors.fill: parent
2023-03-12 14:26:03 +08:00
Connections{
2023-04-01 21:01:46 +08:00
target: control
2023-03-12 14:26:03 +08:00
function onHandleClicked(){
if((list_view.currentIndex === index)){
2023-03-30 19:58:57 +08:00
handleClick(modelData)
2023-03-12 14:26:03 +08:00
}
}
}
2023-03-30 18:23:33 +08:00
onClicked: handleClick(modelData)
2023-03-09 15:49:37 +08:00
}
2023-03-12 14:26:03 +08:00
Rectangle{
width: 3
color:FluTheme.primaryColor.dark
visible: list_view.currentIndex === index
radius: 3
height: 20
2023-03-09 15:49:37 +08:00
anchors{
left: parent.left
2023-03-12 14:26:03 +08:00
verticalCenter: parent.verticalCenter
2023-03-09 15:49:37 +08:00
}
}
}
2023-03-12 14:26:03 +08:00
contentItem: FluText{
2023-03-30 17:16:57 +08:00
text:modelData.title
2023-03-12 14:26:03 +08:00
anchors{
verticalCenter: parent.verticalCenter
}
}
2023-03-09 15:49:37 +08:00
}
}
}
}
onTextChanged: {
2023-04-01 21:01:46 +08:00
loadData()
2023-03-30 18:23:33 +08:00
if(d.flagVisible){
2023-04-01 21:01:46 +08:00
control_popup.visible = true
2023-03-30 18:23:33 +08:00
}
}
TapHandler {
acceptedButtons: Qt.RightButton
2023-04-01 21:01:46 +08:00
onTapped: control.echoMode !== TextInput.Password && menu.popup()
2023-03-30 18:23:33 +08:00
}
2023-04-01 21:01:46 +08:00
FluTextBoxMenu{
2023-03-30 18:23:33 +08:00
id:menu
2023-04-01 21:01:46 +08:00
inputItem: control
}
function handleClick(modelData){
control_popup.visible = false
control.itemClicked(modelData)
updateText(modelData.title)
}
function updateText(text){
d.flagVisible = false
control.text = text
d.flagVisible = true
2023-03-09 15:49:37 +08:00
}
2023-04-01 21:01:46 +08:00
function loadData(){
2023-03-09 15:49:37 +08:00
var result = []
2023-03-30 17:16:57 +08:00
if(items==null){
2023-03-11 20:33:29 +08:00
list_view.model = result
return
}
2023-03-30 17:16:57 +08:00
items.map(function(item){
2023-04-01 21:01:46 +08:00
if(item.title.indexOf(control.text)!==-1){
2023-03-09 15:49:37 +08:00
result.push(item)
}
})
list_view.model = result
}
}