single.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. formInput: Boolean, // 表单右侧输入框
  113. filter: Boolean, // 可检索
  114. clearable: Boolean,
  115. readonly: Boolean,
  116. hideChildren: Boolean, // 不展示子级
  117. footer: Boolean, // 显示底部按钮
  118. // 搜索相关
  119. searchPlaceholder: {
  120. type: String,
  121. default: '请输入'
  122. },
  123. searchDebounceTime: {
  124. type: Number,
  125. default: 500
  126. },
  127. // 样式
  128. popupStyle: [String, Object],
  129. // 文本自定义
  130. clearText: {
  131. type: String,
  132. default: '清除'
  133. },
  134. submitText: {
  135. type: String,
  136. default: '确定'
  137. }
  138. })
  139. // 组件引用
  140. const popup = ref()
  141. // 数据状态
  142. const inputText = ref('')
  143. const inputValue = ref(null)
  144. const inputKeyword = ref('')
  145. const showList = ref([])
  146. // 计算属性
  147. const filteredItems = computed(() => {
  148. if (!inputKeyword.value) {
  149. return props.items
  150. }
  151. return props.items?.filter(item =>
  152. item[props.itemLabel]?.toLowerCase().includes(inputKeyword.value.toLowerCase())
  153. )
  154. })
  155. // 使用 lodash 的防抖
  156. const debouncedSearch = debounce(() => {
  157. showList.value = filteredItems.value
  158. emit('search', inputKeyword.value)
  159. }, props.searchDebounceTime)
  160. const inputChange = () => {
  161. debouncedSearch()
  162. }
  163. const isItemActive = (item) => {
  164. return inputValue.value === item[props.itemValue]
  165. }
  166. // 方法
  167. const calcClass = (item) => {
  168. return {
  169. active: isItemActive(item),
  170. // 'has-children': item[props.children]?.length > 0
  171. }
  172. }
  173. const handleOpen = async () => {
  174. popup.value.open('bottom')
  175. }
  176. const handleClear = () => {
  177. inputValue.value = null
  178. inputText.value = ''
  179. emit('update:modelValue', null)
  180. emit('change', null, null)
  181. }
  182. const closePopup = () => {
  183. popup.value.close()
  184. }
  185. const handleItemClick = (item) => {
  186. inputText.value = item[props.itemLabel]
  187. inputValue.value = item[props.itemValue]
  188. emit('update:modelValue', item[props.itemValue])
  189. emit('change', item[props.itemValue], item)
  190. closePopup()
  191. }
  192. // 回显
  193. const getInputText = () => {
  194. if (inputValue.value && props.items?.length) {
  195. const item = props.items.find(i => i[props.itemValue] === inputValue.value)
  196. inputText.value = item?.[props.itemLabel] || ''
  197. }
  198. }
  199. const initShowList = () => {
  200. showList.value = filteredItems.value
  201. getInputText()
  202. }
  203. // 监听props变化
  204. watch(() => props.items, initShowList, { immediate: true })
  205. // 实现回显逻辑
  206. watch(() => props.modelValue, (newVal) => {
  207. inputValue.value = newVal || null
  208. getInputText()
  209. }, { immediate: true })
  210. </script>
  211. <style lang="scss" scoped>
  212. .popup-content {
  213. background: #fff;
  214. border-radius: 16rpx 16rpx 0 0;
  215. padding: 24rpx;
  216. box-sizing: border-box;
  217. display: flex;
  218. flex-direction: column;
  219. }
  220. .popup-header {
  221. display: flex;
  222. justify-content: space-between;
  223. align-items: center;
  224. padding-bottom: 16rpx;
  225. border-bottom: 1rpx solid #eee;
  226. }
  227. .popup-title {
  228. font-size: 32rpx;
  229. font-weight: bold;
  230. color: #333;
  231. }
  232. .popup-search {
  233. padding: 24rpx 0;
  234. }
  235. .level-container {
  236. margin-bottom: 16rpx;
  237. }
  238. .empty-state {
  239. display: flex;
  240. flex-direction: column;
  241. align-items: center;
  242. justify-content: center;
  243. padding: 40rpx 0;
  244. .empty-text {
  245. margin-top: 16rpx;
  246. color: #999;
  247. font-size: 28rpx;
  248. }
  249. }
  250. .item-container {
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. padding: 20rpx 16rpx;
  255. border-radius: 8rpx;
  256. margin-bottom: 8rpx;
  257. transition: all 0.3s ease;
  258. &.active {
  259. background-color: #f5fff9;
  260. color: #00B760;
  261. }
  262. }
  263. .item-label {
  264. flex: 1;
  265. font-size: 28rpx;
  266. }
  267. .item-icons {
  268. margin-left: 16rpx;
  269. }
  270. .popup-footer {
  271. display: flex;
  272. justify-content: space-between;
  273. padding-top: 24rpx;
  274. border-top: 1rpx solid #eee;
  275. .btn {
  276. flex: 1;
  277. height: 80rpx;
  278. line-height: 80rpx;
  279. font-size: 28rpx;
  280. border-radius: 8rpx;
  281. text-align: center;
  282. &.cancel {
  283. background: #f5f5f5;
  284. color: #666;
  285. margin-right: 16rpx;
  286. }
  287. &.confirm {
  288. background: #00B760;
  289. color: #fff;
  290. &[disabled] {
  291. opacity: 0.6;
  292. }
  293. }
  294. }
  295. }
  296. </style>