SelectValue.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <a-select
  3. :value="selectValue"
  4. @update:value="onUpdateValue"
  5. :disabled="disabled"
  6. v-bind="customProps"
  7. >
  8. <a-select-option v-for="it in options" :key="it.text" :value="it.text">
  9. {{it.text}}
  10. </a-select-option>
  11. </a-select>
  12. </template>
  13. <script lang="ts" setup>
  14. /**
  15. * 下拉框表单控件,用于解决 a-select 不能选择对象的问题
  16. */
  17. import type { SelectProps } from 'ant-design-vue/lib/vc-select';
  18. import { type PropType, ref, watch, onMounted } from 'vue';
  19. import type { IDynamicFormItemSelectValueOption } from './SelectValue';
  20. const props = defineProps({
  21. /**
  22. * 是否禁用
  23. */
  24. disabled: {
  25. type: Boolean,
  26. default: false
  27. },
  28. /**
  29. * 选项数据
  30. */
  31. options: {
  32. type: Object as PropType<IDynamicFormItemSelectValueOption[]>,
  33. default: null,
  34. },
  35. /**
  36. * 选择值
  37. */
  38. value: {
  39. },
  40. /**
  41. * a-select 其他自定义参数
  42. */
  43. customProps: {
  44. type: Object as PropType<SelectProps>,
  45. default: null,
  46. },
  47. });
  48. const emits = defineEmits([
  49. 'update:value',
  50. ]);
  51. const selectValue = ref<string|null>('');
  52. function setSelectValue() {
  53. selectValue.value = props.options.find(k => (k.value === props.value))?.text || null;
  54. if (selectValue.value === null)
  55. selectValue.value = props.options.find(k => (typeof k.value === typeof props.value))?.text || null;
  56. }
  57. watch(() => props.value, () => {
  58. setSelectValue();
  59. });
  60. onMounted(() => {
  61. setSelectValue();
  62. });
  63. function onUpdateValue(v : unknown) {
  64. emits('update:value', props.options.find(k => k.text === v)?.value);
  65. }
  66. </script>