singleSelector.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <!-- 单选选择器 -->
  2. <template>
  3. <view>
  4. <!-- 表单输入框 -->
  5. <view v-if="formInput">
  6. <uni-easyinput
  7. v-model="inputText"
  8. type="text"
  9. :placeholder="placeholder"
  10. :clearable="clearable"
  11. :readonly="readonly"
  12. @clear="handleClear"
  13. @focus.stop="handleOpen('focus')"
  14. ></uni-easyinput>
  15. </view>
  16. <!-- 插槽 -->
  17. <view v-else @tap="handleOpen('slot')">
  18. <slot></slot>
  19. </view>
  20. <!-- 弹窗 -->
  21. <uni-popup ref="popup">
  22. <view class="popup-content" :style="popupStyle">
  23. <view class="popup-header">
  24. <view class="popup-title">{{ label }}</view>
  25. <uni-icons
  26. type="closeempty"
  27. size="24"
  28. color="#999"
  29. @tap="closePopup"
  30. />
  31. </view>
  32. <view class="popup-search" v-if="filter">
  33. <uni-easyinput
  34. v-model="inputKeyword"
  35. type="text"
  36. :placeholder="searchPlaceholder"
  37. @input="inputChange"
  38. suffixIcon="search"
  39. clearable
  40. ></uni-easyinput>
  41. </view>
  42. <scroll-view class="popup-body" scroll-y :style="{ height: bodyHeight }">
  43. <view class="level-container">
  44. <view v-if="!showList?.length" class="empty-state">
  45. <uni-icons type="info" size="24" color="#999" />
  46. <text class="empty-text">{{ inputKeyword ? '没有匹配的数据' : '暂无数据' }}</text>
  47. </view>
  48. <template v-else>
  49. <view
  50. v-for="item in showList"
  51. :key="item[itemValue]"
  52. class="item-container"
  53. :class="calcClass(item)"
  54. @tap="handleItemClick(item)"
  55. >
  56. <text class="item-label">{{ item[itemLabel] }}</text>
  57. <view class="item-icons">
  58. <uni-icons
  59. v-if="isItemActive(item)"
  60. type="checkmarkempty"
  61. color="#00B760"
  62. size="16"
  63. />
  64. </view>
  65. </view>
  66. </template>
  67. </view>
  68. </scroll-view>
  69. </view>
  70. </uni-popup>
  71. </view>
  72. </template>
  73. <script setup>
  74. import { ref, watch, computed } from 'vue'
  75. import { debounce } from 'lodash-es'
  76. const emit = defineEmits(['update:modelValue', 'change', 'search'])
  77. const props = defineProps({
  78. // 基础属性
  79. modelValue: [String, Number, Array, Object],
  80. text: String,
  81. items: {
  82. type: Array,
  83. default: () => []
  84. },
  85. label: {
  86. type: String,
  87. default: '请选择'
  88. },
  89. placeholder: {
  90. type: String,
  91. default: '请选择'
  92. },
  93. // 字段映射
  94. itemLabel: {
  95. type: String,
  96. default: 'label'
  97. },
  98. itemValue: {
  99. type: String,
  100. default: 'value'
  101. },
  102. children: {
  103. type: String,
  104. default: 'children'
  105. },
  106. bodyHeight: {
  107. type: String,
  108. default: '60vh'
  109. },
  110. // 功能开关
  111. multiple: Boolean,
  112. filter: Boolean, // 可检索
  113. clearable: Boolean,
  114. readonly: Boolean,
  115. hideChildren: Boolean, // 不展示子级
  116. footer: Boolean, // 显示底部按钮
  117. // 表单右侧输入框
  118. formInput: {
  119. type: Boolean,
  120. default: true
  121. },
  122. // 搜索相关
  123. searchPlaceholder: {
  124. type: String,
  125. default: '请输入'
  126. },
  127. searchDebounceTime: {
  128. type: Number,
  129. default: 500
  130. },
  131. // 样式
  132. popupStyle: [String, Object],
  133. // 文本自定义
  134. clearText: {
  135. type: String,
  136. default: '清除'
  137. },
  138. submitText: {
  139. type: String,
  140. default: '确定'
  141. }
  142. })
  143. // 组件引用
  144. const popup = ref()
  145. // 数据状态
  146. const inputText = ref('')
  147. const inputValue = ref(null)
  148. const inputKeyword = ref('')
  149. const showList = ref([])
  150. // 计算属性
  151. const filteredItems = computed(() => {
  152. if (!inputKeyword.value) {
  153. return props.items
  154. }
  155. return props.items?.filter(item =>
  156. item[props.itemLabel]?.toLowerCase().includes(inputKeyword.value.toLowerCase())
  157. )
  158. })
  159. // 使用 lodash 的防抖
  160. const debouncedSearch = debounce(() => {
  161. showList.value = filteredItems.value
  162. emit('search', inputKeyword.value)
  163. }, props.searchDebounceTime)
  164. const inputChange = () => {
  165. debouncedSearch()
  166. }
  167. const isItemActive = (item) => {
  168. return inputValue.value === item[props.itemValue]
  169. }
  170. // 方法
  171. const calcClass = (item) => {
  172. return {
  173. active: isItemActive(item),
  174. // 'has-children': item[props.children]?.length > 0
  175. }
  176. }
  177. const handleOpen = async () => {
  178. popup.value.open('bottom')
  179. }
  180. const handleClear = () => {
  181. inputValue.value = null
  182. inputText.value = ''
  183. emit('update:modelValue', null)
  184. emit('change', null, null)
  185. }
  186. const closePopup = () => {
  187. popup.value.close()
  188. }
  189. const handleItemClick = (item) => {
  190. inputText.value = item[props.itemLabel]
  191. inputValue.value = item[props.itemValue]
  192. emit('update:modelValue', item[props.itemValue])
  193. emit('change', item[props.itemValue], item)
  194. closePopup()
  195. }
  196. // 回显
  197. const getInputText = () => {
  198. if (inputValue.value && props.items?.length) {
  199. const item = props.items.find(i => i[props.itemValue] === inputValue.value)
  200. inputText.value = item?.[props.itemLabel] || ''
  201. }
  202. }
  203. const initShowList = () => {
  204. showList.value = filteredItems.value
  205. getInputText()
  206. }
  207. // 监听props变化
  208. watch(() => props.items, initShowList, { immediate: true })
  209. // 实现回显逻辑
  210. watch(() => props.modelValue, (newVal) => {
  211. inputValue.value = newVal || null
  212. getInputText()
  213. }, { immediate: true })
  214. </script>
  215. <style lang="scss" scoped>
  216. .popup-content {
  217. background: #fff;
  218. border-radius: 16rpx 16rpx 0 0;
  219. padding: 24rpx;
  220. box-sizing: border-box;
  221. display: flex;
  222. flex-direction: column;
  223. }
  224. .popup-header {
  225. display: flex;
  226. justify-content: space-between;
  227. align-items: center;
  228. padding-bottom: 16rpx;
  229. border-bottom: 1rpx solid #eee;
  230. }
  231. .popup-title {
  232. font-size: 32rpx;
  233. font-weight: bold;
  234. color: #333;
  235. }
  236. .popup-search {
  237. padding: 24rpx 0;
  238. }
  239. .level-container {
  240. margin-bottom: 16rpx;
  241. }
  242. .empty-state {
  243. display: flex;
  244. flex-direction: column;
  245. align-items: center;
  246. justify-content: center;
  247. padding: 40rpx 0;
  248. .empty-text {
  249. margin-top: 16rpx;
  250. color: #999;
  251. font-size: 28rpx;
  252. }
  253. }
  254. .item-container {
  255. display: flex;
  256. justify-content: space-between;
  257. align-items: center;
  258. padding: 20rpx 16rpx;
  259. border-radius: 8rpx;
  260. margin-bottom: 8rpx;
  261. transition: all 0.3s ease;
  262. &.active {
  263. background-color: #f5fff9;
  264. color: #00B760;
  265. }
  266. }
  267. .item-label {
  268. flex: 1;
  269. font-size: 28rpx;
  270. }
  271. .item-icons {
  272. margin-left: 16rpx;
  273. }
  274. .popup-footer {
  275. display: flex;
  276. justify-content: space-between;
  277. padding-top: 24rpx;
  278. border-top: 1rpx solid #eee;
  279. .btn {
  280. flex: 1;
  281. height: 80rpx;
  282. line-height: 80rpx;
  283. font-size: 28rpx;
  284. border-radius: 8rpx;
  285. text-align: center;
  286. &.cancel {
  287. background: #f5f5f5;
  288. color: #666;
  289. margin-right: 16rpx;
  290. }
  291. &.confirm {
  292. background: #00B760;
  293. color: #fff;
  294. &[disabled] {
  295. opacity: 0.6;
  296. }
  297. }
  298. }
  299. }
  300. </style>