2021-06-26 01:35:40 +00:00
|
|
|
import type { ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent, inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import VcCheckbox from '../vc-checkbox';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-06-13 14:32:10 +00:00
|
|
|
import { getOptionProps } from '../_util/props-util';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { RadioChangeEvent } from './interface';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2020-10-20 08:58:08 +00:00
|
|
|
export const radioProps = {
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
defaultChecked: PropTypes.looseBool,
|
|
|
|
checked: PropTypes.looseBool,
|
|
|
|
disabled: PropTypes.looseBool,
|
|
|
|
isGroup: PropTypes.looseBool,
|
|
|
|
value: PropTypes.any,
|
|
|
|
name: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
type: PropTypes.string.def('radio'),
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onFocus: PropTypes.func,
|
|
|
|
onBlur: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type RadioProps = Partial<ExtractPropTypes<typeof radioProps>>;
|
|
|
|
|
|
|
|
export default defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ARadio',
|
2020-10-20 08:58:08 +00:00
|
|
|
props: radioProps,
|
|
|
|
emits: ['update:checked', 'update:value', 'change', 'blur', 'focus'],
|
2020-06-13 14:32:10 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-08-03 08:47:12 +00:00
|
|
|
radioGroupContext: inject('radioGroupContext', null),
|
2020-06-13 14:32:10 +00:00
|
|
|
};
|
2017-11-07 03:58:47 +00:00
|
|
|
},
|
2017-10-27 06:04:48 +00:00
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-11-13 02:51:12 +00:00
|
|
|
(this.$refs.vcCheckbox as HTMLInputElement).focus();
|
2018-06-17 08:14:49 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-11-13 02:51:12 +00:00
|
|
|
(this.$refs.vcCheckbox as HTMLInputElement).blur();
|
2017-11-07 03:58:47 +00:00
|
|
|
},
|
2020-10-20 08:58:08 +00:00
|
|
|
handleChange(event: RadioChangeEvent) {
|
2020-03-07 11:45:13 +00:00
|
|
|
const targetChecked = event.target.checked;
|
2020-08-03 08:47:12 +00:00
|
|
|
this.$emit('update:checked', targetChecked);
|
2020-06-13 14:32:10 +00:00
|
|
|
this.$emit('update:value', targetChecked);
|
2020-03-07 11:45:13 +00:00
|
|
|
this.$emit('change', event);
|
|
|
|
},
|
2020-10-20 08:58:08 +00:00
|
|
|
onChange2(e: RadioChangeEvent) {
|
2019-09-18 12:03:13 +00:00
|
|
|
this.$emit('change', e);
|
|
|
|
if (this.radioGroupContext && this.radioGroupContext.onRadioChange) {
|
|
|
|
this.radioGroupContext.onRadioChange(e);
|
|
|
|
}
|
|
|
|
},
|
2017-11-02 04:07:20 +00:00
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-06-13 14:49:32 +00:00
|
|
|
const { $slots, radioGroupContext: radioGroup } = this;
|
2019-01-12 03:33:27 +00:00
|
|
|
const props = getOptionProps(this);
|
2019-03-18 12:00:00 +00:00
|
|
|
const { prefixCls: customizePrefixCls, ...restProps } = props;
|
2020-10-20 08:58:08 +00:00
|
|
|
const { getPrefixCls } = this.configProvider;
|
2019-03-18 12:00:00 +00:00
|
|
|
const prefixCls = getPrefixCls('radio', customizePrefixCls);
|
|
|
|
|
2020-10-20 08:58:08 +00:00
|
|
|
const rProps: RadioProps = {
|
2020-06-13 14:32:10 +00:00
|
|
|
prefixCls,
|
|
|
|
...restProps,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
if (radioGroup) {
|
2020-10-20 08:58:08 +00:00
|
|
|
rProps.name = radioGroup.name;
|
|
|
|
rProps.onChange = this.onChange2;
|
|
|
|
rProps.checked = props.value === radioGroup.stateValue;
|
|
|
|
rProps.disabled = props.disabled || radioGroup.disabled;
|
2018-09-05 13:28:54 +00:00
|
|
|
} else {
|
2020-10-20 08:58:08 +00:00
|
|
|
rProps.onChange = this.handleChange;
|
2018-06-17 09:22:26 +00:00
|
|
|
}
|
2020-06-20 14:22:00 +00:00
|
|
|
const wrapperClassString = classNames({
|
2018-06-17 09:46:09 +00:00
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2020-10-20 08:58:08 +00:00
|
|
|
[`${prefixCls}-wrapper-checked`]: rProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: rProps.disabled,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-06-17 09:46:09 +00:00
|
|
|
|
2018-03-19 01:43:31 +00:00
|
|
|
return (
|
2020-06-20 14:22:00 +00:00
|
|
|
<label class={wrapperClassString}>
|
2020-10-20 08:58:08 +00:00
|
|
|
<VcCheckbox {...rProps} ref="vcCheckbox" />
|
2020-06-13 14:49:32 +00:00
|
|
|
{$slots.default && <span>{$slots.default()}</span>}
|
2018-03-19 01:43:31 +00:00
|
|
|
</label>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-19 01:43:31 +00:00
|
|
|
},
|
2020-10-20 08:58:08 +00:00
|
|
|
});
|