index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view class="dict-select" @click="openPicker">
  3. <text :class="['select-text', { placeholder: !displayText }]">
  4. {{ displayText || placeholder }}
  5. </text>
  6. <text class="select-arrow">▼</text>
  7. </view>
  8. </template>
  9. <script setup>
  10. import { ref, computed, watch } from 'vue'
  11. import dictUtil from '../../utils/dict.js'
  12. const props = defineProps({
  13. modelValue: {
  14. type: [String, Number],
  15. default: ''
  16. },
  17. type: {
  18. type: String,
  19. required: true
  20. },
  21. dataRange: {
  22. type: Array,
  23. default: () => []
  24. },
  25. placeholder: {
  26. type: String,
  27. default: '请选择'
  28. },
  29. disabled: {
  30. type: Boolean,
  31. default: false
  32. }
  33. })
  34. const emit = defineEmits(['update:modelValue', 'change'])
  35. const options = computed(() => {
  36. if (props.dataRange && props.dataRange.length > 0) {
  37. return props.dataRange
  38. }
  39. return dictUtil.getDictOptions(props.type)
  40. })
  41. const displayText = computed(() => {
  42. const opt = options.value.find(k => k.value == props.modelValue)
  43. return opt ? opt.label : ''
  44. })
  45. const openPicker = () => {
  46. if (props.disabled) return
  47. if (options.value.length === 0) {
  48. uni.showToast({ title: '暂无选项', icon: 'none' })
  49. return
  50. }
  51. const range = options.value.map(k => k.label)
  52. const currentIndex = options.value.findIndex(k => k.value == props.modelValue)
  53. uni.showActionSheet({
  54. itemList: range,
  55. success: (res) => {
  56. const selected = options.value[res.tapIndex]
  57. emit('update:modelValue', selected.value)
  58. emit('change', selected.value)
  59. }
  60. })
  61. }
  62. </script>
  63. <style scoped>
  64. .dict-select {
  65. display: flex;
  66. align-items: center;
  67. justify-content: space-between;
  68. padding: 16rpx 20rpx;
  69. background: #F5F7FA;
  70. border: 2rpx solid #E8E8E8;
  71. border-radius: 16rpx;
  72. font-size: 28rpx;
  73. }
  74. .select-text {
  75. color: #1A1A1A;
  76. }
  77. .select-text.placeholder {
  78. color: #CCCCCC;
  79. }
  80. .select-arrow {
  81. font-size: 20rpx;
  82. color: #999999;
  83. }
  84. </style>