DictSelection.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="dict-selection">
  3. <view class="select-input" :class="{ disabled: disabled }" @click="handleClick">
  4. <text class="value-text">{{ displayValue || placeholder }}</text>
  5. <text class="arrow" v-if="!disabled">›</text>
  6. </view>
  7. <u-picker
  8. :show="showPicker"
  9. :columns="[dictList]"
  10. keyName="label"
  11. @confirm="onConfirm"
  12. @cancel="showPicker = false"
  13. ></u-picker>
  14. </view>
  15. </template>
  16. <script>
  17. import dictEnum from '@/enum/dict';
  18. import { mapActions, mapGetters } from 'vuex';
  19. export default {
  20. model: {
  21. prop: 'value',
  22. event: 'updateVal'
  23. },
  24. props: {
  25. value: {
  26. type: [String, Number],
  27. default: ''
  28. },
  29. dictName: {
  30. type: String,
  31. required: true
  32. },
  33. labelName: {
  34. type: String,
  35. default: 'dictValue'
  36. },
  37. valueName: {
  38. type: String,
  39. default: 'dictCode'
  40. },
  41. listFormatte: Function,
  42. placeholder: {
  43. type: String,
  44. default: '请选择'
  45. },
  46. disabled: {
  47. type: Boolean,
  48. default: false
  49. }
  50. },
  51. data() {
  52. return {
  53. showPicker: false
  54. };
  55. },
  56. computed: {
  57. ...mapGetters(['dict', 'getDict']),
  58. dictList() {
  59. const list =
  60. (this.listFormatte &&
  61. this.listFormatte(this.dict[dictEnum[this.dictName]] || [])) ||
  62. this.dict[dictEnum[this.dictName]] ||
  63. [];
  64. return list.map(item => ({
  65. label: item[this.labelName],
  66. value: item[this.valueName],
  67. ...item
  68. }));
  69. },
  70. displayValue() {
  71. if (!this.value && this.value !== 0) return '';
  72. const item = this.dictList.find(d => d.value == this.value);
  73. return item ? item.label : '';
  74. }
  75. },
  76. created() {
  77. if (this.dictName) {
  78. this.requestDict(this.dictName);
  79. }
  80. },
  81. methods: {
  82. ...mapActions('dict', ['requestDict']),
  83. handleClick() {
  84. if (!this.disabled) {
  85. this.showPicker = true;
  86. }
  87. },
  88. onConfirm(e) {
  89. const value = e.value[0].value;
  90. this.$emit('updateVal', value);
  91. this.$emit('itemChange', this.getDict(this.dictName, value));
  92. this.showPicker = false;
  93. }
  94. }
  95. };
  96. </script>
  97. <style lang="scss" scoped>
  98. .dict-selection {
  99. width: 100%;
  100. }
  101. .select-input {
  102. display: flex;
  103. align-items: center;
  104. justify-content: space-between;
  105. padding: 20rpx 0;
  106. min-height: 70rpx;
  107. border-bottom: 1rpx solid #e5e5e5;
  108. &.disabled {
  109. opacity: 0.6;
  110. }
  111. .value-text {
  112. flex: 1;
  113. font-size: 28rpx;
  114. color: #333;
  115. &.empty {
  116. color: #999;
  117. }
  118. }
  119. .arrow {
  120. font-size: 40rpx;
  121. color: #999;
  122. line-height: 1;
  123. margin-left: 20rpx;
  124. }
  125. }
  126. </style>