|
1 | | -import { provide, inject, nextTick, defineComponent } from 'vue'; |
| 1 | +import { provide, nextTick, defineComponent, ref, watch, onBeforeMount } from 'vue'; |
| 2 | +import type { PropType, ExtractPropTypes } from 'vue'; |
2 | 3 | import classNames from '../_util/classNames'; |
3 | 4 | import PropTypes from '../_util/vue-types'; |
4 | 5 | import Radio from './Radio'; |
5 | | -import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util'; |
6 | | -import { defaultConfigProvider } from '../config-provider'; |
| 6 | +import useConfigInject from '../_util/hooks/useConfigInject'; |
7 | 7 | import { tuple } from '../_util/type'; |
8 | 8 | import type { RadioChangeEvent } from './interface'; |
9 | 9 | import { useInjectFormItemContext } from '../form/FormItemContext'; |
10 | 10 |
|
| 11 | +const RadioGroupSizeTypes = tuple('large', 'default', 'small'); |
| 12 | + |
| 13 | +export type RadioGroupSize = typeof RadioGroupSizeTypes[number]; |
| 14 | + |
| 15 | +const RadioGroupOptionTypes = tuple('default', 'button'); |
| 16 | + |
| 17 | +export type RadioGroupOption = typeof RadioGroupOptionTypes[number]; |
| 18 | + |
| 19 | +export type RadioGroupChildOption = { |
| 20 | + label: string; |
| 21 | + value: string; |
| 22 | + disabled?: boolean; |
| 23 | +}; |
| 24 | + |
| 25 | +const radioGroupProps = { |
| 26 | + prefixCls: PropTypes.string, |
| 27 | + value: PropTypes.any, |
| 28 | + size: PropTypes.oneOf(RadioGroupSizeTypes).def('default'), |
| 29 | + options: { |
| 30 | + type: Array as PropType<Array<String | RadioGroupChildOption>>, |
| 31 | + }, |
| 32 | + disabled: PropTypes.looseBool, |
| 33 | + name: PropTypes.string, |
| 34 | + buttonStyle: PropTypes.string.def('outline'), |
| 35 | + id: PropTypes.string, |
| 36 | + optionType: PropTypes.oneOf(RadioGroupOptionTypes).def('default'), |
| 37 | +}; |
| 38 | + |
| 39 | +export type RadioGroupProps = Partial<ExtractPropTypes<typeof radioGroupProps>>; |
| 40 | + |
11 | 41 | export default defineComponent({ |
12 | 42 | name: 'ARadioGroup', |
13 | | - props: { |
14 | | - prefixCls: PropTypes.string, |
15 | | - defaultValue: PropTypes.any, |
16 | | - value: PropTypes.any, |
17 | | - size: PropTypes.oneOf(tuple('large', 'default', 'small')).def('default'), |
18 | | - options: PropTypes.array, |
19 | | - disabled: PropTypes.looseBool, |
20 | | - name: PropTypes.string, |
21 | | - buttonStyle: PropTypes.string.def('outline'), |
22 | | - onChange: PropTypes.func, |
23 | | - id: PropTypes.string, |
24 | | - }, |
| 43 | + props: radioGroupProps, |
25 | 44 | emits: ['update:value', 'change'], |
26 | | - setup() { |
| 45 | + setup(props, { slots, emit }) { |
27 | 46 | const formItemContext = useInjectFormItemContext(); |
28 | | - return { |
29 | | - formItemContext, |
30 | | - updatingValue: false, |
31 | | - configProvider: inject('configProvider', defaultConfigProvider), |
32 | | - radioGroupContext: null, |
33 | | - }; |
34 | | - }, |
35 | | - data() { |
36 | | - const { value, defaultValue } = this; |
37 | | - return { |
38 | | - stateValue: value === undefined ? defaultValue : value, |
39 | | - }; |
40 | | - }, |
41 | | - watch: { |
42 | | - value(val) { |
43 | | - this.updatingValue = false; |
44 | | - this.stateValue = val; |
45 | | - }, |
46 | | - }, |
47 | | - // computed: { |
48 | | - // radioOptions() { |
49 | | - // const { disabled } = this; |
50 | | - // return this.options.map(option => { |
51 | | - // return typeof option === 'string' |
52 | | - // ? { label: option, value: option } |
53 | | - // : { ...option, disabled: option.disabled === undefined ? disabled : option.disabled }; |
54 | | - // }); |
55 | | - // }, |
56 | | - // }, |
57 | | - created() { |
58 | | - this.radioGroupContext = provide('radioGroupContext', this); |
59 | | - }, |
60 | | - methods: { |
61 | | - onRadioChange(ev: RadioChangeEvent) { |
62 | | - const lastValue = this.stateValue; |
| 47 | + const { prefixCls } = useConfigInject('radio', props); |
| 48 | + const stateValue = ref(props.value === undefined ? props.defaultValue : props.value); |
| 49 | + const updatingValue = ref<boolean>(false); |
| 50 | + watch( |
| 51 | + () => props.value, |
| 52 | + val => { |
| 53 | + stateValue.value = val; |
| 54 | + updatingValue.value = false; |
| 55 | + }, |
| 56 | + ); |
| 57 | + |
| 58 | + const onRadioChange = (ev: RadioChangeEvent) => { |
| 59 | + const lastValue = stateValue.value; |
63 | 60 | const { value } = ev.target; |
64 | | - if (!hasProp(this, 'value')) { |
65 | | - this.stateValue = value; |
| 61 | + |
| 62 | + if (!('value' in props)) { |
| 63 | + stateValue.value = value; |
66 | 64 | } |
67 | 65 | // nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280 |
68 | | - if (!this.updatingValue && value !== lastValue) { |
69 | | - this.updatingValue = true; |
70 | | - this.$emit('update:value', value); |
71 | | - this.$emit('change', ev); |
72 | | - this.formItemContext.onFieldChange(); |
| 66 | + if (!updatingValue.value && value !== lastValue) { |
| 67 | + updatingValue.value = true; |
| 68 | + emit('update:value', value); |
| 69 | + emit('change', ev); |
| 70 | + formItemContext.onFieldChange(); |
73 | 71 | } |
74 | 72 | nextTick(() => { |
75 | | - this.updatingValue = false; |
| 73 | + updatingValue.value = false; |
76 | 74 | }); |
77 | | - }, |
78 | | - }, |
79 | | - render() { |
80 | | - const props = getOptionProps(this); |
81 | | - const { |
82 | | - prefixCls: customizePrefixCls, |
83 | | - options, |
84 | | - buttonStyle, |
85 | | - id = this.formItemContext.id.value, |
86 | | - } = props; |
87 | | - const { getPrefixCls } = this.configProvider; |
88 | | - const prefixCls = getPrefixCls('radio', customizePrefixCls); |
89 | | - |
90 | | - const groupPrefixCls = `${prefixCls}-group`; |
91 | | - const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, { |
92 | | - [`${groupPrefixCls}-${props.size}`]: props.size, |
| 75 | + }; |
| 76 | + |
| 77 | + provide('radioGroupContext', { |
| 78 | + onRadioChange, |
| 79 | + stateValue, |
| 80 | + props, |
93 | 81 | }); |
94 | 82 |
|
95 | | - let children = filterEmpty(getSlot(this)); |
| 83 | + return () => { |
| 84 | + const { options, optionType, buttonStyle, id = formItemContext.id.value } = props; |
| 85 | + |
| 86 | + const groupPrefixCls = `${prefixCls.value}-group`; |
96 | 87 |
|
97 | | - // 如果存在 options, 优先使用 |
98 | | - if (options && options.length > 0) { |
99 | | - children = options.map(option => { |
100 | | - if (typeof option === 'string') { |
| 88 | + const classString = classNames(groupPrefixCls, `${groupPrefixCls}-${buttonStyle}`, { |
| 89 | + [`${groupPrefixCls}-${props.size}`]: props.size, |
| 90 | + }); |
| 91 | + |
| 92 | + let children = null; |
| 93 | + if (options && options.length > 0) { |
| 94 | + const optionsPrefixCls = |
| 95 | + optionType === 'button' ? `${prefixCls.value}-button` : prefixCls.value; |
| 96 | + children = options.map(option => { |
| 97 | + if (typeof option === 'string') { |
| 98 | + return ( |
| 99 | + <Radio |
| 100 | + key={option} |
| 101 | + prefixCls={optionsPrefixCls} |
| 102 | + disabled={props.disabled} |
| 103 | + value={option} |
| 104 | + checked={stateValue.value === option} |
| 105 | + > |
| 106 | + {option} |
| 107 | + </Radio> |
| 108 | + ); |
| 109 | + } |
| 110 | + const { value, disabled, label } = option as RadioGroupChildOption; |
101 | 111 | return ( |
102 | 112 | <Radio |
103 | | - key={option} |
104 | | - prefixCls={prefixCls} |
105 | | - disabled={props.disabled} |
106 | | - value={option} |
107 | | - checked={this.stateValue === option} |
| 113 | + key={`radio-group-value-options-${value}`} |
| 114 | + prefixCls={optionsPrefixCls} |
| 115 | + disabled={disabled || props.disabled} |
| 116 | + value={value} |
| 117 | + checked={stateValue.value === value} |
108 | 118 | > |
109 | | - {option} |
| 119 | + {label} |
110 | 120 | </Radio> |
111 | 121 | ); |
112 | | - } |
113 | | - return ( |
114 | | - <Radio |
115 | | - key={`radio-group-value-options-${option.value}`} |
116 | | - prefixCls={prefixCls} |
117 | | - disabled={option.disabled || props.disabled} |
118 | | - value={option.value} |
119 | | - checked={this.stateValue === option.value} |
120 | | - > |
121 | | - {option.label} |
122 | | - </Radio> |
123 | | - ); |
124 | | - }); |
125 | | - } |
126 | | - |
127 | | - return ( |
128 | | - <div class={classString} id={id}> |
129 | | - {children} |
130 | | - </div> |
131 | | - ); |
| 122 | + }); |
| 123 | + } else { |
| 124 | + children = slots.default?.(); |
| 125 | + } |
| 126 | + return ( |
| 127 | + <div class={classString} id={id}> |
| 128 | + {children} |
| 129 | + </div> |
| 130 | + ); |
| 131 | + }; |
132 | 132 | }, |
133 | 133 | }); |
0 commit comments