|
18 | 18 | </div>
|
19 | 19 | </template>
|
20 | 20 |
|
21 |
| -<script lang="ts"> |
22 |
| -import {computed, defineComponent, PropType} from 'vue' |
| 21 | +<script setup lang="ts"> |
| 22 | +// import type {BFormRadioGroupEmits, BFormRadioGroupProps} from '@/types/components' |
| 23 | +import type {ButtonVariant, Size} from '@/types' |
| 24 | +import {computed, useSlots} from 'vue' |
23 | 25 | import BFormRadio from './BFormRadio.vue'
|
24 |
| -import {ColorVariant, Size} from '../../types' |
25 | 26 | import {
|
26 | 27 | bindGroupProps,
|
27 | 28 | getGroupAttr,
|
28 | 29 | getGroupClasses,
|
29 | 30 | optionToElement,
|
30 | 31 | slotsToElements,
|
31 |
| -} from '../../composables/useFormCheck' |
32 |
| -import useId from '../../composables/useId' |
| 32 | +} from '@/composables/useFormCheck' |
| 33 | +import useId from '@/composables/useId' |
33 | 34 |
|
34 |
| -export default defineComponent({ |
35 |
| - name: 'BFormRadioGroup', |
36 |
| - components: {BFormRadio}, |
37 |
| - props: { |
38 |
| - modelValue: {type: [String, Boolean, Array, Object, Number], default: ''}, |
39 |
| - ariaInvalid: {type: [Boolean, String], default: null}, |
40 |
| - autofocus: {type: Boolean, default: false}, |
41 |
| - buttonVariant: {type: String as PropType<ColorVariant>, default: 'secondary'}, |
42 |
| - buttons: {type: Boolean, default: false}, |
43 |
| - disabled: {type: Boolean, default: false}, |
44 |
| - disabledField: {type: String, default: 'disabled'}, |
45 |
| - form: {type: String}, |
46 |
| - htmlField: {type: String, default: 'html'}, |
47 |
| - id: {type: String}, |
48 |
| - name: {type: String}, |
49 |
| - options: {type: Array, default: () => []}, // Objects are not supported yet |
50 |
| - plain: {type: Boolean, default: false}, |
51 |
| - required: {type: Boolean, default: false}, |
52 |
| - size: {type: String as PropType<Size>}, |
53 |
| - stacked: {type: Boolean, default: false}, |
54 |
| - state: {type: Boolean, default: null}, |
55 |
| - textField: {type: String, default: 'text'}, |
56 |
| - validated: {type: Boolean, default: false}, |
57 |
| - valueField: {type: String, default: 'value'}, |
58 |
| - }, |
59 |
| - emits: ['update:modelValue', 'input', 'change'], |
60 |
| - setup(props, {emit, slots}) { |
61 |
| - const slotsName = 'BFormRadio' |
62 |
| - const computedId = useId(props.id, 'radio') |
63 |
| - const computedName = useId(props.name, 'checkbox') |
| 35 | +interface BFormRadioGroupProps { |
| 36 | + size?: Size |
| 37 | + form?: string |
| 38 | + id?: string |
| 39 | + name?: string |
| 40 | + modelValue?: string | boolean | Array<unknown> | Record<string, unknown> | number |
| 41 | + ariaInvalid?: boolean | string |
| 42 | + autofocus?: boolean |
| 43 | + buttonVariant?: ButtonVariant |
| 44 | + buttons?: boolean |
| 45 | + disabled?: boolean |
| 46 | + disabledField?: string |
| 47 | + htmlField?: string |
| 48 | + options?: Array<unknown> // Objects are not supported yet |
| 49 | + plain?: boolean |
| 50 | + required?: boolean |
| 51 | + stacked?: boolean |
| 52 | + state?: boolean |
| 53 | + textField?: string |
| 54 | + validated?: boolean |
| 55 | + valueField?: string |
| 56 | +} |
| 57 | +
|
| 58 | +const props = withDefaults(defineProps<BFormRadioGroupProps>(), { |
| 59 | + modelValue: '', |
| 60 | + ariaInvalid: undefined, |
| 61 | + autofocus: false, |
| 62 | + buttonVariant: 'secondary', |
| 63 | + buttons: false, |
| 64 | + disabled: false, |
| 65 | + disabledField: 'disabled', |
| 66 | + htmlField: 'html', |
| 67 | + options: () => [], |
| 68 | + plain: false, |
| 69 | + required: false, |
| 70 | + stacked: false, |
| 71 | + state: undefined, |
| 72 | + textField: 'text', |
| 73 | + validated: false, |
| 74 | + valueField: 'value', |
| 75 | +}) |
64 | 76 |
|
65 |
| - const localValue = computed({ |
66 |
| - get: () => props.modelValue, |
67 |
| - set: (newValue: any) => { |
68 |
| - emit('input', newValue) |
69 |
| - emit('update:modelValue', newValue) |
70 |
| - emit('change', newValue) |
71 |
| - }, |
72 |
| - }) |
| 77 | +interface BFormRadioGroupEmits { |
| 78 | + (e: 'input', value: unknown): void |
| 79 | + (e: 'update:modelValue', value: unknown): void |
| 80 | + (e: 'change', value: unknown): void |
| 81 | +} |
73 | 82 |
|
74 |
| - const checkboxList = computed(() => |
75 |
| - (slots.first ? slotsToElements(slots.first(), slotsName, props.disabled) : []) |
76 |
| - .concat(props.options.map((e) => optionToElement(e, props))) |
77 |
| - .concat(slots.default ? slotsToElements(slots.default(), slotsName, props.disabled) : []) |
78 |
| - .map((e, idx) => bindGroupProps(e, idx, props, computedName, computedId)) |
79 |
| - .map((e) => ({ |
80 |
| - ...e, |
81 |
| - })) |
82 |
| - ) |
| 83 | +const emit = defineEmits<BFormRadioGroupEmits>() |
83 | 84 |
|
84 |
| - const attrs = getGroupAttr(props) |
85 |
| - const classes = getGroupClasses(props) |
| 85 | +const slots = useSlots() |
86 | 86 |
|
87 |
| - // TODO: make tests compatible with the v-focus directive |
| 87 | +const slotsName = 'BFormRadio' |
| 88 | +const computedId = useId(props.id, 'radio') |
| 89 | +const computedName = useId(props.name, 'checkbox') |
88 | 90 |
|
89 |
| - return { |
90 |
| - attrs, |
91 |
| - classes, |
92 |
| - checkboxList, |
93 |
| - computedId, |
94 |
| - localValue, |
95 |
| - } |
| 91 | +const localValue = computed<string | boolean | Array<unknown> | Record<string, unknown> | number>({ |
| 92 | + get: () => props.modelValue, |
| 93 | + set: (newValue: any) => { |
| 94 | + emit('input', newValue) |
| 95 | + emit('update:modelValue', newValue) |
| 96 | + emit('change', newValue) |
96 | 97 | },
|
97 | 98 | })
|
| 99 | +
|
| 100 | +const checkboxList = computed<Array<any>>(() => |
| 101 | + (slots.first ? slotsToElements(slots.first(), slotsName, props.disabled) : []) |
| 102 | + .concat(props.options.map((e) => optionToElement(e, props))) |
| 103 | + .concat(slots.default ? slotsToElements(slots.default(), slotsName, props.disabled) : []) |
| 104 | + .map((e, idx) => bindGroupProps(e, idx, props, computedName, computedId)) |
| 105 | + .map((e) => ({ |
| 106 | + ...e, |
| 107 | + })) |
| 108 | +) |
| 109 | +
|
| 110 | +const attrs = getGroupAttr(props) |
| 111 | +const classes = getGroupClasses(props) |
| 112 | +
|
| 113 | +// TODO: make tests compatible with the v-focus directive |
98 | 114 | </script>
|
0 commit comments