RadioValue.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <FlexView :direction="vertical ? 'column' : 'row'" align="center" :gap="10" wrap>
  3. <RadioGroup
  4. :value="selectValue"
  5. @update:value="onUpdateValue"
  6. :disabled="disabled"
  7. v-bind="$attrs"
  8. >
  9. <Radio
  10. v-for="it in options"
  11. :key="(it.value as string)"
  12. :name="(it.value as string)"
  13. :text="it.text"
  14. />
  15. </RadioGroup>
  16. </FlexView>
  17. </template>
  18. <script lang="ts" setup>
  19. import { type PropType, ref, watch, onMounted } from 'vue';
  20. import RadioGroup from '@/components/form/RadioGroup.vue';
  21. import Radio from '@/components/form/Radio.vue';
  22. import type { RadioValueOption } from './RadioValue';
  23. import FlexView from '@/components/layout/FlexView.vue';
  24. const props = defineProps({
  25. /**
  26. * 是否禁用
  27. */
  28. disabled: {
  29. type: Boolean,
  30. default: false
  31. },
  32. /**
  33. * 是否垂直方向
  34. */
  35. vertical: {
  36. type: Boolean,
  37. default: false
  38. },
  39. /**
  40. * 选项数据
  41. */
  42. options: {
  43. type: Object as PropType<RadioValueOption[]>,
  44. default: null,
  45. },
  46. modelValue: {
  47. },
  48. });
  49. const emits = defineEmits([
  50. 'update:modelValue',
  51. ]);
  52. const selectValue = ref<string|null>('');
  53. function setRadioValue() {
  54. selectValue.value = props.options.find(k => (k.value === props.modelValue))?.text || null;
  55. if (selectValue.value === null)
  56. selectValue.value = props.options.find(k => (typeof k.value === typeof props.modelValue))?.text || null;
  57. }
  58. watch(() => props.modelValue, () => {
  59. setRadioValue();
  60. });
  61. onMounted(() => {
  62. setRadioValue();
  63. });
  64. function onUpdateValue(v : unknown) {
  65. emits('update:modelValue', props.options.find(k => k.text === v)?.value);
  66. }
  67. </script>