FluAutoSuggestBox.qml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import QtQuick
  2. import QtQuick.Controls
  3. import FluentUI
  4. FluTextBox{
  5. property var items:[]
  6. property string emptyText: "没有找到结果"
  7. property int autoSuggestBoxReplacement: FluentIcons.Search
  8. signal itemClicked(var data)
  9. signal handleClicked
  10. id:control
  11. width: 300
  12. Component.onCompleted: {
  13. loadData()
  14. }
  15. QtObject{
  16. id:d
  17. property bool flagVisible: true
  18. }
  19. Popup{
  20. id:control_popup
  21. y:control.height
  22. focus: false
  23. enter: Transition {
  24. NumberAnimation {
  25. property: "y"
  26. from:0
  27. to:control_popup.y
  28. duration: 150
  29. }
  30. NumberAnimation {
  31. property: "opacity"
  32. from:0
  33. to:1
  34. duration: 150
  35. }
  36. }
  37. onVisibleChanged: {
  38. if(visible){
  39. list_view.currentIndex = -1
  40. }
  41. }
  42. background: Rectangle{
  43. width: control.width
  44. radius: 4
  45. FluShadow{
  46. radius: 4
  47. }
  48. color: FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(248/255,250/255,253/255,1)
  49. height: 38*Math.min(Math.max(list_view.count,1),8)
  50. ListView{
  51. id:list_view
  52. anchors.fill: parent
  53. clip: true
  54. currentIndex: -1
  55. ScrollBar.vertical: FluScrollBar {}
  56. header: Item{
  57. width: control.width
  58. height: visible ? 38 : 0
  59. visible: list_view.count === 0
  60. FluText{
  61. text:emptyText
  62. anchors{
  63. verticalCenter: parent.verticalCenter
  64. left: parent.left
  65. leftMargin: 10
  66. }
  67. }
  68. }
  69. delegate:Control{
  70. width: control.width
  71. padding:10
  72. background: Rectangle{
  73. color: {
  74. if(list_view.currentIndex === index){
  75. return FluTheme.dark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
  76. }
  77. if(hovered){
  78. return FluTheme.dark ? Qt.rgba(63/255,60/255,61/255,1) : Qt.rgba(237/255,237/255,242/255,1)
  79. }
  80. return FluTheme.dark ? Qt.rgba(51/255,48/255,48/255,1) : Qt.rgba(0,0,0,0)
  81. }
  82. MouseArea{
  83. id:mouse_area
  84. anchors.fill: parent
  85. Connections{
  86. target: control
  87. function onHandleClicked(){
  88. if((list_view.currentIndex === index)){
  89. handleClick(modelData)
  90. }
  91. }
  92. }
  93. onClicked: handleClick(modelData)
  94. }
  95. Rectangle{
  96. width: 3
  97. color:FluTheme.primaryColor.dark
  98. visible: list_view.currentIndex === index
  99. radius: 3
  100. height: 20
  101. anchors{
  102. left: parent.left
  103. verticalCenter: parent.verticalCenter
  104. }
  105. }
  106. }
  107. contentItem: FluText{
  108. text:modelData.title
  109. anchors{
  110. verticalCenter: parent.verticalCenter
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. onTextChanged: {
  118. loadData()
  119. if(d.flagVisible){
  120. control_popup.visible = true
  121. }
  122. }
  123. function handleClick(modelData){
  124. control_popup.visible = false
  125. control.itemClicked(modelData)
  126. updateText(modelData.title)
  127. }
  128. function updateText(text){
  129. d.flagVisible = false
  130. control.text = text
  131. d.flagVisible = true
  132. }
  133. function loadData(){
  134. var result = []
  135. if(items==null){
  136. list_view.model = result
  137. return
  138. }
  139. items.map(function(item){
  140. if(item.title.indexOf(control.text)!==-1){
  141. result.push(item)
  142. }
  143. })
  144. list_view.model = result
  145. }
  146. }