refactor: radio to ts

feat-dayjs^2
Amour1688 2020-10-20 16:58:08 +08:00
parent 4abfd7ea52
commit d465356f6d
5 changed files with 84 additions and 72 deletions

View File

@ -4,6 +4,8 @@ import PropTypes from '../_util/vue-types';
import Radio from './Radio'; import Radio from './Radio';
import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util'; import { getOptionProps, filterEmpty, hasProp, getSlot } from '../_util/props-util';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import { tuple } from '../_util/type';
import { RadioChangeEvent } from './interface';
export default defineComponent({ export default defineComponent({
name: 'ARadioGroup', name: 'ARadioGroup',
@ -11,43 +13,39 @@ export default defineComponent({
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
defaultValue: PropTypes.any, defaultValue: PropTypes.any,
value: PropTypes.any, value: PropTypes.any,
size: { size: PropTypes.oneOf(tuple('large', 'default', 'small')).def('default'),
default: 'default', options: PropTypes.array,
validator(value) {
return ['large', 'default', 'small'].includes(value);
},
},
options: {
default: () => [],
type: Array,
},
disabled: PropTypes.looseBool, disabled: PropTypes.looseBool,
name: String, name: PropTypes.string,
buttonStyle: PropTypes.string.def('outline'), buttonStyle: PropTypes.string.def('outline'),
onChange: PropTypes.func, onChange: PropTypes.func,
'onUpdate:value': PropTypes.func,
}, },
emits: ['update:value', 'change'],
data() { data() {
const { value, defaultValue } = this; const { value, defaultValue } = this;
this.updatingValue = false;
return { return {
stateValue: value === undefined ? defaultValue : value, stateValue: value === undefined ? defaultValue : value,
}; };
}, },
setup() { setup() {
return { return {
updatingValue: false,
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
radioGroupContext: null,
}; };
}, },
computed: { // computed: {
radioOptions() { // radioOptions() {
const { disabled } = this; // const { disabled } = this;
return this.options.map(option => { // return this.options.map(option => {
return typeof option === 'string' // return typeof option === 'string'
? { label: option, value: option } // ? { label: option, value: option }
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled }; // : { ...option, disabled: option.disabled === undefined ? disabled : option.disabled };
}); // });
}, // },
// },
created() {
this.radioGroupContext = provide('radioGroupContext', this);
}, },
watch: { watch: {
value(val) { value(val) {
@ -55,11 +53,8 @@ export default defineComponent({
this.stateValue = val; this.stateValue = val;
}, },
}, },
created() {
this.radioGroupContext = provide('radioGroupContext', this);
},
methods: { methods: {
onRadioChange(ev) { onRadioChange(ev: RadioChangeEvent) {
const lastValue = this.stateValue; const lastValue = this.stateValue;
const { value } = ev.target; const { value } = ev.target;
if (!hasProp(this, 'value')) { if (!hasProp(this, 'value')) {
@ -79,7 +74,7 @@ export default defineComponent({
render() { render() {
const props = getOptionProps(this); const props = getOptionProps(this);
const { prefixCls: customizePrefixCls, options, buttonStyle } = props; const { prefixCls: customizePrefixCls, options, buttonStyle } = props;
const getPrefixCls = this.configProvider.getPrefixCls; const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('radio', customizePrefixCls); const prefixCls = getPrefixCls('radio', customizePrefixCls);
const groupPrefixCls = `${prefixCls}-group`; const groupPrefixCls = `${prefixCls}-group`;

View File

@ -1,32 +1,36 @@
import { inject } from 'vue'; import { defineComponent, ExtractPropTypes, inject } from 'vue';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import VcCheckbox from '../vc-checkbox'; import VcCheckbox from '../vc-checkbox';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import { getOptionProps } from '../_util/props-util'; import { getOptionProps } from '../_util/props-util';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
import { RadioChangeEvent } from './interface';
export default { export const radioProps = {
name: 'ARadio',
model: {
prop: 'checked',
},
props: {
prefixCls: PropTypes.string, prefixCls: PropTypes.string,
defaultChecked: PropTypes.looseBool, defaultChecked: PropTypes.looseBool,
checked: PropTypes.looseBool, checked: PropTypes.looseBool,
disabled: PropTypes.looseBool, disabled: PropTypes.looseBool,
isGroup: PropTypes.looseBool, isGroup: PropTypes.looseBool,
value: PropTypes.any, value: PropTypes.any,
name: String, name: PropTypes.string,
id: String, id: PropTypes.string,
autofocus: PropTypes.looseBool, autofocus: PropTypes.looseBool,
type: PropTypes.string.def('radio'), type: PropTypes.string.def('radio'),
onChange: PropTypes.func, onChange: PropTypes.func,
onFocus: PropTypes.func, onFocus: PropTypes.func,
onBlur: PropTypes.func, onBlur: PropTypes.func,
'onUpdate:checked': PropTypes.func, };
'onUpdate:value': PropTypes.func,
export type RadioProps = Partial<ExtractPropTypes<typeof radioProps>>;
export default defineComponent({
name: 'ARadio',
model: {
prop: 'checked',
}, },
props: radioProps,
emits: ['update:checked', 'update:value', 'change', 'blur', 'focus'],
setup() { setup() {
return { return {
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
@ -35,18 +39,18 @@ export default {
}, },
methods: { methods: {
focus() { focus() {
this.$refs.vcCheckbox.focus(); (this.$refs.vcCheckbox as any).focus();
}, },
blur() { blur() {
this.$refs.vcCheckbox.blur(); (this.$refs.vcCheckbox as any).blur();
}, },
handleChange(event) { handleChange(event: RadioChangeEvent) {
const targetChecked = event.target.checked; const targetChecked = event.target.checked;
this.$emit('update:checked', targetChecked); this.$emit('update:checked', targetChecked);
this.$emit('update:value', targetChecked); this.$emit('update:value', targetChecked);
this.$emit('change', event); this.$emit('change', event);
}, },
onChange2(e) { onChange2(e: RadioChangeEvent) {
this.$emit('change', e); this.$emit('change', e);
if (this.radioGroupContext && this.radioGroupContext.onRadioChange) { if (this.radioGroupContext && this.radioGroupContext.onRadioChange) {
this.radioGroupContext.onRadioChange(e); this.radioGroupContext.onRadioChange(e);
@ -58,33 +62,33 @@ export default {
const { $slots, radioGroupContext: radioGroup } = this; const { $slots, radioGroupContext: radioGroup } = this;
const props = getOptionProps(this); const props = getOptionProps(this);
const { prefixCls: customizePrefixCls, ...restProps } = props; const { prefixCls: customizePrefixCls, ...restProps } = props;
const getPrefixCls = this.configProvider.getPrefixCls; const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('radio', customizePrefixCls); const prefixCls = getPrefixCls('radio', customizePrefixCls);
const radioProps = { const rProps: RadioProps = {
prefixCls, prefixCls,
...restProps, ...restProps,
}; };
if (radioGroup) { if (radioGroup) {
radioProps.name = radioGroup.name; rProps.name = radioGroup.name;
radioProps.onChange = this.onChange2; rProps.onChange = this.onChange2;
radioProps.checked = props.value === radioGroup.stateValue; rProps.checked = props.value === radioGroup.stateValue;
radioProps.disabled = props.disabled || radioGroup.disabled; rProps.disabled = props.disabled || radioGroup.disabled;
} else { } else {
radioProps.onChange = this.handleChange; rProps.onChange = this.handleChange;
} }
const wrapperClassString = classNames({ const wrapperClassString = classNames({
[`${prefixCls}-wrapper`]: true, [`${prefixCls}-wrapper`]: true,
[`${prefixCls}-wrapper-checked`]: radioProps.checked, [`${prefixCls}-wrapper-checked`]: rProps.checked,
[`${prefixCls}-wrapper-disabled`]: radioProps.disabled, [`${prefixCls}-wrapper-disabled`]: rProps.disabled,
}); });
return ( return (
<label class={wrapperClassString}> <label class={wrapperClassString}>
<VcCheckbox {...radioProps} ref="vcCheckbox" /> <VcCheckbox {...rProps} ref="vcCheckbox" />
{$slots.default && <span>{$slots.default()}</span>} {$slots.default && <span>{$slots.default()}</span>}
</label> </label>
); );
}, },
}; });

View File

@ -1,34 +1,34 @@
import { defineComponent, inject } from 'vue'; import { defineComponent, inject } from 'vue';
import Radio from './Radio'; import Radio, { radioProps, RadioProps } from './Radio';
import { getOptionProps, getSlot } from '../_util/props-util'; import { getOptionProps, getSlot } from '../_util/props-util';
import { defaultConfigProvider } from '../config-provider'; import { defaultConfigProvider } from '../config-provider';
export default defineComponent({ export default defineComponent({
name: 'ARadioButton', name: 'ARadioButton',
props: { props: {
...Radio.props, ...radioProps,
}, },
setup() { setup() {
return { return {
configProvider: inject('configProvider', defaultConfigProvider), configProvider: inject('configProvider', defaultConfigProvider),
radioGroupContext: inject('radioGroupContext', {}), radioGroupContext: inject<any>('radioGroupContext', {}),
}; };
}, },
render() { render() {
const props = getOptionProps(this); const props = getOptionProps(this) as RadioProps;
const { prefixCls: customizePrefixCls, ...otherProps } = props; const { prefixCls: customizePrefixCls, ...otherProps } = props;
const getPrefixCls = this.configProvider.getPrefixCls; const { getPrefixCls } = this.configProvider;
const prefixCls = getPrefixCls('radio-button', customizePrefixCls); const prefixCls = getPrefixCls('radio-button', customizePrefixCls);
const radioProps = { const rProps: RadioProps = {
prefixCls, prefixCls,
...otherProps, ...otherProps,
}; };
if (this.radioGroupContext) { if (this.radioGroupContext) {
radioProps.onChange = this.radioGroupContext.onRadioChange; rProps.onChange = this.radioGroupContext.onRadioChange;
radioProps.checked = props.value === this.radioGroupContext.stateValue; rProps.checked = props.value === this.radioGroupContext.stateValue;
radioProps.disabled = props.disabled || this.radioGroupContext.disabled; rProps.disabled = props.disabled || this.radioGroupContext.disabled;
} }
return <Radio {...radioProps}>{getSlot(this)}</Radio>; return <Radio {...rProps}>{getSlot(this)}</Radio>;
}, },
}); });

View File

@ -1,3 +1,4 @@
import { App } from 'vue';
import Radio from './Radio'; import Radio from './Radio';
import Group from './Group'; import Group from './Group';
import Button from './RadioButton'; import Button from './RadioButton';
@ -6,7 +7,7 @@ Radio.Group = Group;
Radio.Button = Button; Radio.Button = Button;
/* istanbul ignore next */ /* istanbul ignore next */
Radio.install = function(app) { Radio.install = function(app: App) {
app.component(Radio.name, Radio); app.component(Radio.name, Radio);
app.component(Radio.Group.name, Radio.Group); app.component(Radio.Group.name, Radio.Group);
app.component(Radio.Button.name, Radio.Button); app.component(Radio.Button.name, Radio.Button);

View File

@ -0,0 +1,12 @@
import { RadioProps } from './Radio';
export interface RadioChangeEventTarget extends RadioProps {
checked: boolean;
}
export interface RadioChangeEvent {
target: RadioChangeEventTarget;
stopPropagation: () => void;
preventDefault: () => void;
nativeEvent: MouseEvent;
}