cwg-filter-select.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <!-- 显示区域 -->
  3. <view class="filter-select" :class="{ disabled }">
  4. <view class="picker-value">
  5. <uni-data-select v-model="value1" :localdata="optionList" :clear="false"
  6. @change="onConfirm"></uni-data-select>
  7. </view>
  8. </view>
  9. </template>
  10. <script setup lang="ts">
  11. import { computed, ref, watch } from 'vue'
  12. import { useI18n } from 'vue-i18n'
  13. type Option = {
  14. value: string | number | undefined
  15. text: string
  16. }
  17. const props = defineProps<{
  18. modelValue?: string | number | null
  19. options: Record<string, string> | Option[]
  20. placeholder?: string
  21. disabled?: boolean
  22. showAll?: boolean
  23. }>()
  24. const emit = defineEmits<{
  25. (e: 'update:modelValue', value: string | number | undefined): void
  26. }>()
  27. const value1 = ref(props.modelValue)
  28. watch(
  29. () => props.modelValue,
  30. (val) => {
  31. value1.value = val === null ? undefined : val
  32. },
  33. )
  34. const { t } = useI18n()
  35. const optionList = computed<Option[]>(() => {
  36. let list: Option[] = []
  37. if (Array.isArray(props.options)) {
  38. list = props.options.map(i => ({
  39. value: i.value,
  40. text: i.label
  41. }))
  42. } else {
  43. list = Object.entries(props.options).map(([value, label]) => ({
  44. value: isNaN(Number(value)) ? value : Number(value),
  45. text:
  46. typeof label === 'string' && label.includes('.')
  47. ? t(label)
  48. : String(label)
  49. }))
  50. }
  51. if (props.showAll !== true) {
  52. list.unshift({
  53. value: -1,
  54. text: props.placeholder || t('State.All')
  55. })
  56. }
  57. return list
  58. })
  59. function onConfirm(e: any) {
  60. emit('update:modelValue', e)
  61. }
  62. </script>