2021-12-28 16:47:52 +08:00
|
|
|
import { watchEffect, onMounted, defineComponent, inject, onBeforeUnmount, ref } from 'vue';
|
2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2021-12-28 16:47:52 +08:00
|
|
|
import VcCheckbox from '../vc-checkbox/Checkbox';
|
|
|
|
import { flattenChildren } from '../_util/props-util';
|
2020-03-07 19:45:13 +08:00
|
|
|
import warning from '../_util/warning';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { RadioChangeEvent } from '../radio/interface';
|
2021-08-29 10:44:24 +08:00
|
|
|
import type { EventHandler } from '../_util/EventInterface';
|
2021-09-25 16:51:32 +08:00
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
2021-12-28 16:47:52 +08:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2021-12-28 16:47:52 +08:00
|
|
|
import type { CheckboxProps } from './interface';
|
|
|
|
import { CheckboxGroupContextKey, checkboxProps } from './interface';
|
2021-09-25 16:51:32 +08:00
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'ACheckbox',
|
|
|
|
inheritAttrs: false,
|
|
|
|
__ANT_CHECKBOX: true,
|
|
|
|
props: checkboxProps(),
|
2020-12-26 15:43:33 +08:00
|
|
|
emits: ['change', 'update:checked'],
|
2021-12-28 16:47:52 +08:00
|
|
|
setup(props, { emit, attrs, slots, expose }) {
|
2021-09-25 16:51:32 +08:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2021-12-28 16:47:52 +08:00
|
|
|
const { prefixCls, direction } = useConfigInject('checkbox', props);
|
|
|
|
const checkboxGroup = inject(CheckboxGroupContextKey, undefined);
|
|
|
|
const uniId = Symbol('checkboxUniId');
|
2020-06-20 22:22:00 +08:00
|
|
|
|
2021-12-28 16:47:52 +08:00
|
|
|
watchEffect(() => {
|
|
|
|
if (!props.skipGroup && checkboxGroup) {
|
|
|
|
checkboxGroup.registerValue(uniId, props.value);
|
2021-09-25 16:51:32 +08:00
|
|
|
}
|
2021-12-28 16:47:52 +08:00
|
|
|
});
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
if (checkboxGroup) {
|
|
|
|
checkboxGroup.cancelValue(uniId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
onMounted(() => {
|
|
|
|
warning(
|
|
|
|
props.checked !== undefined || checkboxGroup || props.value === undefined,
|
|
|
|
'Checkbox',
|
|
|
|
'`value` is not validate prop, do you mean `checked`?',
|
|
|
|
);
|
|
|
|
});
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2021-12-28 16:47:52 +08:00
|
|
|
const handleChange = (event: RadioChangeEvent) => {
|
2020-11-13 10:51:12 +08:00
|
|
|
const targetChecked = event.target.checked;
|
2021-12-28 16:47:52 +08:00
|
|
|
emit('update:checked', targetChecked);
|
|
|
|
emit('change', event);
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-12-28 16:47:52 +08:00
|
|
|
const checkboxRef = ref();
|
|
|
|
const focus = () => {
|
|
|
|
checkboxRef.value?.focus();
|
|
|
|
};
|
|
|
|
const blur = () => {
|
|
|
|
checkboxRef.value?.blur();
|
|
|
|
};
|
|
|
|
expose({
|
|
|
|
focus,
|
|
|
|
blur,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2021-12-28 16:47:52 +08:00
|
|
|
return () => {
|
|
|
|
const children = flattenChildren(slots.default?.());
|
2022-03-20 10:05:44 +08:00
|
|
|
const { indeterminate, skipGroup, id = formItemContext.id.value, ...restProps } = props;
|
2021-12-28 16:47:52 +08:00
|
|
|
const { onMouseenter, onMouseleave, onInput, class: className, style, ...restAttrs } = attrs;
|
|
|
|
const checkboxProps: CheckboxProps = {
|
|
|
|
...restProps,
|
|
|
|
id,
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
...restAttrs,
|
|
|
|
};
|
|
|
|
if (checkboxGroup && !skipGroup) {
|
|
|
|
checkboxProps.onChange = (...args) => {
|
|
|
|
emit('change', ...args);
|
|
|
|
checkboxGroup.toggleOption({ label: children, value: props.value });
|
|
|
|
};
|
|
|
|
checkboxProps.name = checkboxGroup.name.value;
|
|
|
|
checkboxProps.checked = checkboxGroup.mergedValue.value.indexOf(props.value) !== -1;
|
|
|
|
checkboxProps.disabled = props.disabled || checkboxGroup.disabled.value;
|
|
|
|
checkboxProps.indeterminate = indeterminate;
|
|
|
|
} else {
|
|
|
|
checkboxProps.onChange = handleChange;
|
|
|
|
}
|
|
|
|
const classString = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls.value}-wrapper`]: true,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[`${prefixCls.value}-wrapper-checked`]: checkboxProps.checked,
|
|
|
|
[`${prefixCls.value}-wrapper-disabled`]: checkboxProps.disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
|
|
|
const checkboxClass = classNames({
|
|
|
|
[`${prefixCls.value}-indeterminate`]: indeterminate,
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<label
|
|
|
|
class={classString}
|
|
|
|
style={style}
|
|
|
|
onMouseenter={onMouseenter as EventHandler}
|
|
|
|
onMouseleave={onMouseleave as EventHandler}
|
|
|
|
>
|
|
|
|
<VcCheckbox {...checkboxProps} class={checkboxClass} ref={checkboxRef} />
|
|
|
|
{children.length ? <span>{children}</span> : null}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
};
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
2020-10-14 17:57:38 +08:00
|
|
|
});
|