-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathGroup.tsx
148 lines (131 loc) · 4.48 KB
/
Group.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { nextTick, defineComponent, ref, watch, computed } from 'vue';
import type { ExtractPropTypes } from 'vue';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import Radio from './Radio';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import { booleanType, stringType, arrayType, functionType } from '../_util/type';
import type { RadioChangeEvent, RadioGroupButtonStyle, RadioGroupOptionType } from './interface';
import { useInjectFormItemContext } from '../form/FormItemContext';
import { useProvideRadioGroupContext } from './context';
// CSSINJS
import useStyle from './style';
const RadioGroupSizeTypes = ['large', 'default', 'small'] as const;
export type RadioGroupSize = (typeof RadioGroupSizeTypes)[number];
export type RadioGroupOption = RadioGroupOptionType;
export type RadioGroupChildOption = {
label?: any;
value: any;
disabled?: boolean;
};
export const radioGroupProps = () => ({
prefixCls: String,
value: PropTypes.any,
size: stringType<RadioGroupSize>(),
options: arrayType<Array<string | RadioGroupChildOption | number>>(),
disabled: booleanType(),
name: String,
buttonStyle: stringType<RadioGroupButtonStyle>('outline'),
id: String,
optionType: stringType<RadioGroupOptionType>('default'),
onChange: functionType<(e: RadioChangeEvent) => void>(),
'onUpdate:value': functionType<(val: any) => void>(),
});
export type RadioGroupProps = Partial<ExtractPropTypes<ReturnType<typeof radioGroupProps>>>;
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'ARadioGroup',
inheritAttrs: false,
props: radioGroupProps(),
// emits: ['update:value', 'change'],
setup(props, { slots, emit, attrs }) {
const formItemContext = useInjectFormItemContext();
const { prefixCls, direction, size } = useConfigInject('radio', props);
// Style
const [wrapSSR, hashId] = useStyle(prefixCls);
const stateValue = ref(props.value);
const updatingValue = ref<boolean>(false);
watch(
() => props.value,
val => {
stateValue.value = val;
updatingValue.value = false;
},
);
const onRadioChange = (ev: RadioChangeEvent) => {
const lastValue = stateValue.value;
const { value } = ev.target;
if (!('value' in props)) {
stateValue.value = value;
}
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
if (!updatingValue.value && value !== lastValue) {
updatingValue.value = true;
emit('update:value', value);
emit('change', ev);
formItemContext.onFieldChange();
}
nextTick(() => {
updatingValue.value = false;
});
};
useProvideRadioGroupContext({
onChange: onRadioChange,
value: stateValue,
disabled: computed(() => props.disabled),
name: computed(() => props.name),
optionType: computed(() => props.optionType),
});
return () => {
const { options, buttonStyle, id = formItemContext.id.value } = props;
const groupPrefixCls = `${prefixCls.value}-group`;
const classString = classNames(
groupPrefixCls,
`${groupPrefixCls}-${buttonStyle}`,
{
[`${groupPrefixCls}-${size.value}`]: size.value,
[`${groupPrefixCls}-rtl`]: direction.value === 'rtl',
},
attrs.class,
hashId.value,
);
let children = null;
if (options && options.length > 0) {
children = options.map(option => {
if (typeof option === 'string' || typeof option === 'number') {
return (
<Radio
key={option}
prefixCls={prefixCls.value}
disabled={props.disabled}
value={option}
checked={stateValue.value === option}
>
{option}
</Radio>
);
}
const { value, disabled, label } = option as RadioGroupChildOption;
return (
<Radio
key={`radio-group-value-options-${value}`}
prefixCls={prefixCls.value}
disabled={disabled || props.disabled}
value={value}
checked={stateValue.value === value}
>
{label}
</Radio>
);
});
} else {
children = slots.default?.();
}
return wrapSSR(
<div {...attrs} class={classString} id={id}>
{children}
</div>,
);
};
},
});