2022-03-26 14:52:54 +00:00
|
|
|
import type { ExtractPropTypes, PropType } from 'vue';
|
2021-10-01 06:53:37 +00:00
|
|
|
import { defineComponent, inject, ref } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-12-28 08:47:52 +00:00
|
|
|
import VcCheckbox from '../vc-checkbox/Checkbox';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2021-10-01 06:53:37 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import type { RadioChangeEvent, RadioGroupContext } from './interface';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
2022-03-26 14:52:54 +00:00
|
|
|
import omit from '../_util/omit';
|
|
|
|
import type { FocusEventHandler, MouseEventHandler } from '../_util/EventInterface';
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export const radioProps = () => ({
|
|
|
|
prefixCls: String,
|
|
|
|
checked: { type: Boolean, default: undefined },
|
|
|
|
disabled: { type: Boolean, default: undefined },
|
|
|
|
isGroup: { type: Boolean, default: undefined },
|
2020-10-20 08:58:08 +00:00
|
|
|
value: PropTypes.any,
|
2022-03-26 14:52:54 +00:00
|
|
|
name: String,
|
|
|
|
id: String,
|
|
|
|
autofocus: { type: Boolean, default: undefined },
|
|
|
|
onChange: Function as PropType<(event: RadioChangeEvent) => void>,
|
|
|
|
onFocus: Function as PropType<FocusEventHandler>,
|
|
|
|
onBlur: Function as PropType<FocusEventHandler>,
|
|
|
|
onClick: Function as PropType<MouseEventHandler>,
|
|
|
|
'onUpdate:checked': Function as PropType<(checked: boolean) => void>,
|
|
|
|
'onUpdate:value': Function as PropType<(checked: boolean) => void>,
|
|
|
|
});
|
2020-10-20 08:58:08 +00:00
|
|
|
|
2022-03-26 14:52:54 +00:00
|
|
|
export type RadioProps = Partial<ExtractPropTypes<ReturnType<typeof radioProps>>>;
|
2020-10-20 08:58:08 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ARadio',
|
2022-03-26 14:52:54 +00:00
|
|
|
props: radioProps(),
|
|
|
|
// emits: ['update:checked', 'update:value', 'change', 'blur', 'focus'],
|
2021-10-01 06:53:37 +00:00
|
|
|
setup(props, { emit, expose, slots }) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2021-10-01 06:53:37 +00:00
|
|
|
const vcCheckbox = ref<HTMLElement>();
|
2021-10-01 07:06:18 +00:00
|
|
|
const radioGroupContext = inject<RadioGroupContext>('radioGroupContext', undefined);
|
|
|
|
const { prefixCls, direction } = useConfigInject('radio', props);
|
2021-10-01 06:53:37 +00:00
|
|
|
|
|
|
|
const focus = () => {
|
|
|
|
vcCheckbox.value.focus();
|
2020-06-13 14:32:10 +00:00
|
|
|
};
|
2021-10-01 06:53:37 +00:00
|
|
|
|
|
|
|
const blur = () => {
|
|
|
|
vcCheckbox.value.blur();
|
|
|
|
};
|
|
|
|
|
|
|
|
expose({ focus, blur });
|
|
|
|
|
|
|
|
const handleChange = (event: RadioChangeEvent) => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const targetChecked = event.target.checked;
|
2021-10-01 06:53:37 +00:00
|
|
|
emit('update:checked', targetChecked);
|
|
|
|
emit('update:value', targetChecked);
|
|
|
|
emit('change', event);
|
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
|
|
|
|
|
|
|
const onChange = (e: RadioChangeEvent) => {
|
|
|
|
emit('change', e);
|
|
|
|
if (radioGroupContext && radioGroupContext.onRadioChange) {
|
|
|
|
radioGroupContext.onRadioChange(e);
|
2019-09-18 12:03:13 +00:00
|
|
|
}
|
2021-10-01 06:53:37 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2021-10-01 06:53:37 +00:00
|
|
|
return () => {
|
|
|
|
const radioGroup = radioGroupContext;
|
2022-03-24 06:07:41 +00:00
|
|
|
const { prefixCls: customizePrefixCls, id = formItemContext.id.value, ...restProps } = props;
|
2019-03-18 12:00:00 +00:00
|
|
|
|
2021-10-01 06:53:37 +00:00
|
|
|
const rProps: RadioProps = {
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
id,
|
2022-03-26 14:52:54 +00:00
|
|
|
...omit(restProps, ['onUpdate:checked', 'onUpdate:value']),
|
2021-10-01 06:53:37 +00:00
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2021-10-01 06:53:37 +00:00
|
|
|
if (radioGroup) {
|
|
|
|
rProps.name = radioGroup.props.name;
|
|
|
|
rProps.onChange = onChange;
|
|
|
|
rProps.checked = props.value === radioGroup.stateValue.value;
|
|
|
|
rProps.disabled = props.disabled || radioGroup.props.disabled;
|
|
|
|
} else {
|
|
|
|
rProps.onChange = handleChange;
|
|
|
|
}
|
|
|
|
const wrapperClassString = classNames({
|
|
|
|
[`${prefixCls.value}-wrapper`]: true,
|
|
|
|
[`${prefixCls.value}-wrapper-checked`]: rProps.checked,
|
|
|
|
[`${prefixCls.value}-wrapper-disabled`]: rProps.disabled,
|
2021-10-01 07:06:18 +00:00
|
|
|
[`${prefixCls.value}-wrapper-rtl`]: direction.value === 'rtl',
|
2021-10-01 06:53:37 +00:00
|
|
|
});
|
2018-06-17 09:46:09 +00:00
|
|
|
|
2021-10-01 06:53:37 +00:00
|
|
|
return (
|
2022-03-24 06:07:41 +00:00
|
|
|
<label class={wrapperClassString}>
|
2022-03-12 01:56:32 +00:00
|
|
|
<VcCheckbox {...rProps} type="radio" ref={vcCheckbox} />
|
2021-10-01 06:53:37 +00:00
|
|
|
{slots.default && <span>{slots.default()}</span>}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
};
|
2018-03-19 01:43:31 +00:00
|
|
|
},
|
2020-10-20 08:58:08 +00:00
|
|
|
});
|