Skip to content

Commit d465356

Browse files
committed
refactor: radio to ts
1 parent 4abfd7e commit d465356

File tree

5 files changed

+84
-72
lines changed

5 files changed

+84
-72
lines changed

components/radio/Group.jsx renamed to components/radio/Group.tsx

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,57 @@ import PropTypes from '../_util/vue-types';
44
import Radio from './Radio';
55
import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util';
66
import { defaultConfigProvider } from '../config-provider';
7+
import { tuple } from '../_util/type';
8+
import { RadioChangeEvent } from './interface';
79

810
export default defineComponent({
911
name: 'ARadioGroup',
1012
props: {
1113
prefixCls: PropTypes.string,
1214
defaultValue: PropTypes.any,
1315
value: PropTypes.any,
14-
size: {
15-
default: 'default',
16-
validator(value) {
17-
return ['large', 'default', 'small'].includes(value);
18-
},
19-
},
20-
options: {
21-
default: () => [],
22-
type: Array,
23-
},
16+
size: PropTypes.oneOf(tuple('large', 'default', 'small')).def('default'),
17+
options: PropTypes.array,
2418
disabled: PropTypes.looseBool,
25-
name: String,
19+
name: PropTypes.string,
2620
buttonStyle: PropTypes.string.def('outline'),
2721
onChange: PropTypes.func,
28-
'onUpdate:value': PropTypes.func,
2922
},
23+
emits: ['update:value', 'change'],
3024
data() {
3125
const { value, defaultValue } = this;
32-
this.updatingValue = false;
3326
return {
3427
stateValue: value === undefined ? defaultValue : value,
3528
};
3629
},
3730
setup() {
3831
return {
32+
updatingValue: false,
3933
configProvider: inject('configProvider', defaultConfigProvider),
34+
radioGroupContext: null,
4035
};
4136
},
42-
computed: {
43-
radioOptions() {
44-
const { disabled } = this;
45-
return this.options.map(option => {
46-
return typeof option === 'string'
47-
? { label: option, value: option }
48-
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
49-
});
50-
},
37+
// computed: {
38+
// radioOptions() {
39+
// const { disabled } = this;
40+
// return this.options.map(option => {
41+
// return typeof option === 'string'
42+
// ? { label: option, value: option }
43+
// : { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
44+
// });
45+
// },
46+
// },
47+
created() {
48+
this.radioGroupContext = provide('radioGroupContext', this);
5149
},
5250
watch: {
5351
value(val) {
5452
this.updatingValue = false;
5553
this.stateValue = val;
5654
},
5755
},
58-
created() {
59-
this.radioGroupContext = provide('radioGroupContext', this);
60-
},
6156
methods: {
62-
onRadioChange(ev) {
57+
onRadioChange(ev: RadioChangeEvent) {
6358
const lastValue = this.stateValue;
6459
const { value } = ev.target;
6560
if (!hasProp(this, 'value')) {
@@ -79,7 +74,7 @@ export default defineComponent({
7974
render() {
8075
const props = getOptionProps(this);
8176
const { prefixCls: customizePrefixCls, options, buttonStyle } = props;
82-
const getPrefixCls = this.configProvider.getPrefixCls;
77+
const { getPrefixCls } = this.configProvider;
8378
const prefixCls = getPrefixCls('radio', customizePrefixCls);
8479

8580
const groupPrefixCls = `${prefixCls}-group`;
Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
import { inject } from 'vue';
1+
import { defineComponent, ExtractPropTypes, inject } from 'vue';
22
import PropTypes from '../_util/vue-types';
33
import VcCheckbox from '../vc-checkbox';
44
import classNames from '../_util/classNames';
55
import { getOptionProps } from '../_util/props-util';
66
import { defaultConfigProvider } from '../config-provider';
7+
import { RadioChangeEvent } from './interface';
78

8-
export default {
9+
export const radioProps = {
10+
prefixCls: PropTypes.string,
11+
defaultChecked: PropTypes.looseBool,
12+
checked: PropTypes.looseBool,
13+
disabled: PropTypes.looseBool,
14+
isGroup: PropTypes.looseBool,
15+
value: PropTypes.any,
16+
name: PropTypes.string,
17+
id: PropTypes.string,
18+
autofocus: PropTypes.looseBool,
19+
type: PropTypes.string.def('radio'),
20+
onChange: PropTypes.func,
21+
onFocus: PropTypes.func,
22+
onBlur: PropTypes.func,
23+
};
24+
25+
export type RadioProps = Partial<ExtractPropTypes<typeof radioProps>>;
26+
27+
export default defineComponent({
928
name: 'ARadio',
1029
model: {
1130
prop: 'checked',
1231
},
13-
props: {
14-
prefixCls: PropTypes.string,
15-
defaultChecked: PropTypes.looseBool,
16-
checked: PropTypes.looseBool,
17-
disabled: PropTypes.looseBool,
18-
isGroup: PropTypes.looseBool,
19-
value: PropTypes.any,
20-
name: String,
21-
id: String,
22-
autofocus: PropTypes.looseBool,
23-
type: PropTypes.string.def('radio'),
24-
onChange: PropTypes.func,
25-
onFocus: PropTypes.func,
26-
onBlur: PropTypes.func,
27-
'onUpdate:checked': PropTypes.func,
28-
'onUpdate:value': PropTypes.func,
29-
},
32+
props: radioProps,
33+
emits: ['update:checked', 'update:value', 'change', 'blur', 'focus'],
3034
setup() {
3135
return {
3236
configProvider: inject('configProvider', defaultConfigProvider),
@@ -35,18 +39,18 @@ export default {
3539
},
3640
methods: {
3741
focus() {
38-
this.$refs.vcCheckbox.focus();
42+
(this.$refs.vcCheckbox as any).focus();
3943
},
4044
blur() {
41-
this.$refs.vcCheckbox.blur();
45+
(this.$refs.vcCheckbox as any).blur();
4246
},
43-
handleChange(event) {
47+
handleChange(event: RadioChangeEvent) {
4448
const targetChecked = event.target.checked;
4549
this.$emit('update:checked', targetChecked);
4650
this.$emit('update:value', targetChecked);
4751
this.$emit('change', event);
4852
},
49-
onChange2(e) {
53+
onChange2(e: RadioChangeEvent) {
5054
this.$emit('change', e);
5155
if (this.radioGroupContext && this.radioGroupContext.onRadioChange) {
5256
this.radioGroupContext.onRadioChange(e);
@@ -58,33 +62,33 @@ export default {
5862
const { $slots, radioGroupContext: radioGroup } = this;
5963
const props = getOptionProps(this);
6064
const { prefixCls: customizePrefixCls, ...restProps } = props;
61-
const getPrefixCls = this.configProvider.getPrefixCls;
65+
const { getPrefixCls } = this.configProvider;
6266
const prefixCls = getPrefixCls('radio', customizePrefixCls);
6367

64-
const radioProps = {
68+
const rProps: RadioProps = {
6569
prefixCls,
6670
...restProps,
6771
};
6872

6973
if (radioGroup) {
70-
radioProps.name = radioGroup.name;
71-
radioProps.onChange = this.onChange2;
72-
radioProps.checked = props.value === radioGroup.stateValue;
73-
radioProps.disabled = props.disabled || radioGroup.disabled;
74+
rProps.name = radioGroup.name;
75+
rProps.onChange = this.onChange2;
76+
rProps.checked = props.value === radioGroup.stateValue;
77+
rProps.disabled = props.disabled || radioGroup.disabled;
7478
} else {
75-
radioProps.onChange = this.handleChange;
79+
rProps.onChange = this.handleChange;
7680
}
7781
const wrapperClassString = classNames({
7882
[`${prefixCls}-wrapper`]: true,
79-
[`${prefixCls}-wrapper-checked`]: radioProps.checked,
80-
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled,
83+
[`${prefixCls}-wrapper-checked`]: rProps.checked,
84+
[`${prefixCls}-wrapper-disabled`]: rProps.disabled,
8185
});
8286

8387
return (
8488
<label class={wrapperClassString}>
85-
<VcCheckbox {...radioProps} ref="vcCheckbox" />
89+
<VcCheckbox {...rProps} ref="vcCheckbox" />
8690
{$slots.default && <span>{$slots.default()}</span>}
8791
</label>
8892
);
8993
},
90-
};
94+
});
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import { defineComponent, inject } from 'vue';
2-
import Radio from './Radio';
2+
import Radio, { radioProps, RadioProps } from './Radio';
33
import { getOptionProps, getSlot } from '../_util/props-util';
44
import { defaultConfigProvider } from '../config-provider';
55

66
export default defineComponent({
77
name: 'ARadioButton',
88
props: {
9-
...Radio.props,
9+
...radioProps,
1010
},
1111
setup() {
1212
return {
1313
configProvider: inject('configProvider', defaultConfigProvider),
14-
radioGroupContext: inject('radioGroupContext', {}),
14+
radioGroupContext: inject<any>('radioGroupContext', {}),
1515
};
1616
},
1717
render() {
18-
const props = getOptionProps(this);
18+
const props = getOptionProps(this) as RadioProps;
1919
const { prefixCls: customizePrefixCls, ...otherProps } = props;
20-
const getPrefixCls = this.configProvider.getPrefixCls;
20+
const { getPrefixCls } = this.configProvider;
2121
const prefixCls = getPrefixCls('radio-button', customizePrefixCls);
2222

23-
const radioProps = {
23+
const rProps: RadioProps = {
2424
prefixCls,
2525
...otherProps,
2626
};
2727
if (this.radioGroupContext) {
28-
radioProps.onChange = this.radioGroupContext.onRadioChange;
29-
radioProps.checked = props.value === this.radioGroupContext.stateValue;
30-
radioProps.disabled = props.disabled || this.radioGroupContext.disabled;
28+
rProps.onChange = this.radioGroupContext.onRadioChange;
29+
rProps.checked = props.value === this.radioGroupContext.stateValue;
30+
rProps.disabled = props.disabled || this.radioGroupContext.disabled;
3131
}
32-
return <Radio {...radioProps}>{getSlot(this)}</Radio>;
32+
return <Radio {...rProps}>{getSlot(this)}</Radio>;
3333
},
3434
});

components/radio/index.js renamed to components/radio/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { App } from 'vue';
12
import Radio from './Radio';
23
import Group from './Group';
34
import Button from './RadioButton';
@@ -6,7 +7,7 @@ Radio.Group = Group;
67
Radio.Button = Button;
78

89
/* istanbul ignore next */
9-
Radio.install = function(app) {
10+
Radio.install = function(app: App) {
1011
app.component(Radio.name, Radio);
1112
app.component(Radio.Group.name, Radio.Group);
1213
app.component(Radio.Button.name, Radio.Button);

components/radio/interface.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { RadioProps } from './Radio';
2+
3+
export interface RadioChangeEventTarget extends RadioProps {
4+
checked: boolean;
5+
}
6+
7+
export interface RadioChangeEvent {
8+
target: RadioChangeEventTarget;
9+
stopPropagation: () => void;
10+
preventDefault: () => void;
11+
nativeEvent: MouseEvent;
12+
}

0 commit comments

Comments
 (0)