fuzzy-select.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="fuzzy-select-container">
  3. <!-- 搜索输入框和删除按钮 -->
  4. <view class="input-container">
  5. <uni-easyinput ref="inputRef" v-model="searchKeyword" :placeholder="placeholder" :clearable="true"
  6. :disabled="disabled" @input="handleInput" @focus="showDropdown = true" @blur="handleBlur" />
  7. <!-- 删除按钮 -->
  8. <view v-if="value && !disabled" class="delete-btn" @click="handleDelete">
  9. <text class="delete-icon">×</text>
  10. </view>
  11. </view>
  12. <!-- 下拉选项列表 -->
  13. <view v-if="showDropdown && filteredOptions.length > 0" class="dropdown-list">
  14. <scroll-view scroll-y="true" class="dropdown-scroll">
  15. <view v-for="(option, index) in filteredOptions" :key="index" class="dropdown-item"
  16. @click="selectOption(option)">
  17. {{ option[dataText] }}
  18. </view>
  19. </scroll-view>
  20. </view>
  21. <!-- 无匹配结果提示 -->
  22. <view v-if="showDropdown && filteredOptions.length === 0 && searchKeyword" class="no-result">
  23. 无匹配结果
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. name: "FuzzySelect",
  30. props: {
  31. // 绑定值
  32. value: {
  33. type: [String, Number],
  34. default: ''
  35. },
  36. // 选项列表
  37. options: {
  38. type: Array,
  39. default: () => []
  40. },
  41. // 占位符
  42. placeholder: {
  43. type: String,
  44. default: '请输入搜索'
  45. },
  46. // 值字段名
  47. dataValue: {
  48. type: String,
  49. default: 'value'
  50. },
  51. // 显示文本字段名
  52. dataText: {
  53. type: String,
  54. default: 'text'
  55. },
  56. // 是否开启搜索
  57. filterable: {
  58. type: Boolean,
  59. default: true
  60. },
  61. // 是否禁用
  62. disabled: {
  63. type: Boolean,
  64. default: false
  65. }
  66. },
  67. data() {
  68. return {
  69. searchKeyword: '',
  70. showDropdown: false,
  71. filteredOptions: this.options,
  72. selectedOption: null
  73. }
  74. },
  75. watch: {
  76. options(newOptions) {
  77. this.filterOptions(this.searchKeyword, newOptions)
  78. // 当options更新后,需要重新根据当前value设置选中项
  79. this.updateSelectedOption(this.value)
  80. },
  81. value(newValue) {
  82. // 只有当value确实变化时才更新选中项
  83. if (newValue !== this.selectedOption?.[this.dataValue]) {
  84. this.updateSelectedOption(newValue)
  85. }
  86. }
  87. },
  88. mounted() {
  89. this.updateSelectedOption(this.value)
  90. },
  91. methods: {
  92. // 根据value值更新选中的选项
  93. updateSelectedOption(value) {
  94. if (!value) {
  95. this.searchKeyword = ''
  96. this.selectedOption = null
  97. return
  98. }
  99. const foundOption = this.options.find(option =>
  100. String(option[this.dataValue]) === String(value)
  101. )
  102. if (foundOption) {
  103. this.selectedOption = foundOption
  104. this.searchKeyword = foundOption[this.dataText]
  105. // 确保filteredOptions包含当前选中的选项
  106. if (!this.filteredOptions.some(opt => String(opt[this.dataValue]) === String(value))) {
  107. this.filteredOptions = this.options
  108. }
  109. } else {
  110. this.searchKeyword = ''
  111. this.selectedOption = null
  112. }
  113. },
  114. // 处理输入
  115. handleInput(value) {
  116. if (this.disabled) return;
  117. this.searchKeyword = value
  118. this.filterOptions(value)
  119. this.showDropdown = true
  120. },
  121. // 过滤选项
  122. filterOptions(keyword = '', options = this.options) {
  123. if (!this.filterable || !keyword.trim()) {
  124. this.filteredOptions = options
  125. return
  126. }
  127. const lowerKeyword = keyword.toLowerCase()
  128. this.filteredOptions = options.filter(option => {
  129. const text = String(option[this.dataText] || '').toLowerCase()
  130. const value = String(option[this.dataValue] || '').toLowerCase()
  131. return text.includes(lowerKeyword) || value.includes(lowerKeyword)
  132. })
  133. },
  134. // 选择选项
  135. selectOption(option) {
  136. if (this.disabled) return;
  137. this.selectedOption = option
  138. this.searchKeyword = option[this.dataText]
  139. this.$emit('input', option[this.dataValue])
  140. this.$emit('change', option[this.dataValue])
  141. this.showDropdown = false
  142. },
  143. // 处理失焦
  144. handleBlur() {
  145. // 延迟隐藏下拉框,确保点击选项能触发
  146. setTimeout(() => {
  147. this.showDropdown = false
  148. }, 200)
  149. },
  150. // 清空搜索
  151. clearSearch() {
  152. this.searchKeyword = ''
  153. this.filteredOptions = this.options
  154. this.selectedOption = null
  155. this.$emit('input', '')
  156. this.$emit('change', '')
  157. },
  158. // 聚焦输入框
  159. focus() {
  160. if (this.$refs.inputRef) {
  161. this.$refs.inputRef.focus()
  162. }
  163. },
  164. // 失焦输入框
  165. blur() {
  166. if (this.$refs.inputRef) {
  167. this.$refs.inputRef.blur()
  168. }
  169. },
  170. // 处理删除
  171. handleDelete() {
  172. if (this.disabled) return;
  173. this.clearSearch()
  174. this.$emit('delete')
  175. },
  176. }
  177. }
  178. </script>
  179. <style scoped>
  180. .fuzzy-select-container {
  181. position: relative;
  182. width: 100%;
  183. }
  184. .input-container {
  185. position: relative;
  186. display: flex;
  187. align-items: center;
  188. }
  189. .delete-btn {
  190. position: absolute;
  191. right: 8px;
  192. top: 50%;
  193. transform: translateY(-50%);
  194. width: 20px;
  195. height: 20px;
  196. border-radius: 50%;
  197. background-color: #f0f0f0;
  198. display: flex;
  199. align-items: center;
  200. justify-content: center;
  201. cursor: pointer;
  202. z-index: 10;
  203. }
  204. .delete-btn:hover {
  205. background-color: #e0e0e0;
  206. }
  207. .delete-icon {
  208. font-size: 16px;
  209. color: #999;
  210. font-weight: bold;
  211. line-height: 1;
  212. }
  213. .dropdown-list {
  214. position: absolute;
  215. top: 100%;
  216. left: 0;
  217. right: 0;
  218. max-height: 200px;
  219. background-color: #fff;
  220. border: 1px solid #e5e5e5;
  221. border-radius: 4px;
  222. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  223. z-index: 999;
  224. margin-top: 4px;
  225. }
  226. .dropdown-scroll {
  227. max-height: 200px;
  228. }
  229. .dropdown-item {
  230. padding: 12px 16px;
  231. font-size: 14px;
  232. color: #333;
  233. border-bottom: 1px solid #f0f0f0;
  234. cursor: pointer;
  235. }
  236. .dropdown-item:hover {
  237. background-color: #f5f5f5;
  238. }
  239. .dropdown-item:last-child {
  240. border-bottom: none;
  241. }
  242. .no-result {
  243. position: absolute;
  244. top: 100%;
  245. left: 0;
  246. right: 0;
  247. padding: 12px 16px;
  248. background-color: #fff;
  249. border: 1px solid #e5e5e5;
  250. border-radius: 4px;
  251. font-size: 14px;
  252. color: #999;
  253. text-align: center;
  254. z-index: 999;
  255. margin-top: 4px;
  256. }
  257. </style>