RadioValue.vue 1.6 KB

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