2021-09-25 16:51:32 +08:00
|
|
|
import type { ExtractPropTypes } from 'vue';
|
2020-10-28 15:31:45 +08:00
|
|
|
import { defineComponent, inject, nextTick } from 'vue';
|
2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-08-31 16:53:19 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2019-01-12 11:33:27 +08:00
|
|
|
import VcCheckbox from '../vc-checkbox';
|
2020-06-22 22:27:40 +08:00
|
|
|
import hasProp, { getOptionProps, getSlot } from '../_util/props-util';
|
2020-09-30 10:47:18 +08:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
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';
|
2020-06-22 22:27:40 +08:00
|
|
|
function noop() {}
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export const checkboxProps = () => {
|
|
|
|
return {
|
2019-03-15 11:20:37 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2020-10-10 18:16:28 +08:00
|
|
|
defaultChecked: PropTypes.looseBool,
|
|
|
|
checked: PropTypes.looseBool,
|
|
|
|
disabled: PropTypes.looseBool,
|
|
|
|
isGroup: PropTypes.looseBool,
|
2018-09-05 21:28:54 +08:00
|
|
|
value: PropTypes.any,
|
2019-03-15 11:20:37 +08:00
|
|
|
name: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
2020-10-10 18:16:28 +08:00
|
|
|
indeterminate: PropTypes.looseBool,
|
2018-05-05 17:39:56 +08:00
|
|
|
type: PropTypes.string.def('checkbox'),
|
2020-10-10 18:16:28 +08:00
|
|
|
autofocus: PropTypes.looseBool,
|
2020-09-07 17:19:28 +08:00
|
|
|
onChange: PropTypes.func,
|
|
|
|
'onUpdate:checked': PropTypes.func,
|
2021-09-25 16:51:32 +08:00
|
|
|
skipGroup: PropTypes.looseBool,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CheckboxProps = Partial<ExtractPropTypes<ReturnType<typeof checkboxProps>>>;
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'ACheckbox',
|
|
|
|
inheritAttrs: false,
|
|
|
|
__ANT_CHECKBOX: true,
|
|
|
|
props: checkboxProps(),
|
2020-12-26 15:43:33 +08:00
|
|
|
emits: ['change', 'update:checked'],
|
2020-06-20 22:22:00 +08:00
|
|
|
setup() {
|
2021-09-25 16:51:32 +08:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2020-06-20 22:22:00 +08:00
|
|
|
return {
|
2021-09-25 16:51:32 +08:00
|
|
|
formItemContext,
|
2020-09-30 10:47:18 +08:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-06-20 22:22:00 +08:00
|
|
|
checkboxGroupContext: inject('checkboxGroupContext', undefined),
|
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
},
|
2020-06-20 22:22:00 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
watch: {
|
|
|
|
value(value, prevValue) {
|
2021-09-25 16:51:32 +08:00
|
|
|
if (this.skipGroup) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-28 15:31:45 +08:00
|
|
|
nextTick(() => {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { checkboxGroupContext: checkboxGroup = {} } = this;
|
|
|
|
if (checkboxGroup.registerValue && checkboxGroup.cancelValue) {
|
|
|
|
checkboxGroup.cancelValue(prevValue);
|
|
|
|
checkboxGroup.registerValue(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2020-06-20 22:22:00 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
mounted() {
|
|
|
|
const { value, checkboxGroupContext: checkboxGroup = {} } = this;
|
|
|
|
if (checkboxGroup.registerValue) {
|
|
|
|
checkboxGroup.registerValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
warning(
|
|
|
|
hasProp(this, 'checked') || this.checkboxGroupContext || !hasProp(this, 'value'),
|
|
|
|
'Checkbox',
|
|
|
|
'`value` is not validate prop, do you mean `checked`?',
|
|
|
|
);
|
|
|
|
},
|
2020-06-11 16:13:09 +08:00
|
|
|
beforeUnmount() {
|
2020-03-07 19:45:13 +08:00
|
|
|
const { value, checkboxGroupContext: checkboxGroup = {} } = this;
|
|
|
|
if (checkboxGroup.cancelValue) {
|
|
|
|
checkboxGroup.cancelValue(value);
|
|
|
|
}
|
2017-11-06 17:46:08 +08:00
|
|
|
},
|
2017-10-26 15:18:08 +08:00
|
|
|
methods: {
|
2020-11-13 10:51:12 +08:00
|
|
|
handleChange(event: RadioChangeEvent) {
|
|
|
|
const targetChecked = event.target.checked;
|
2020-06-22 22:27:40 +08:00
|
|
|
this.$emit('update:checked', targetChecked);
|
|
|
|
// this.$emit('input', targetChecked);
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('change', event);
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
focus() {
|
2020-11-13 10:51:12 +08:00
|
|
|
(this.$refs.vcCheckbox as HTMLInputElement).focus();
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
blur() {
|
2020-11-13 10:51:12 +08:00
|
|
|
(this.$refs.vcCheckbox as HTMLInputElement).blur();
|
2017-11-06 17:46:08 +08:00
|
|
|
},
|
2017-11-02 11:48:38 +08:00
|
|
|
},
|
2018-09-05 21:28:54 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
|
|
|
const props = getOptionProps(this);
|
2020-06-22 22:27:40 +08:00
|
|
|
const { checkboxGroupContext: checkboxGroup, $attrs } = this;
|
|
|
|
const children = getSlot(this);
|
2021-09-25 16:51:32 +08:00
|
|
|
const {
|
|
|
|
indeterminate,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
skipGroup,
|
|
|
|
id = this.formItemContext.id.value,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2019-09-11 22:35:25 +08:00
|
|
|
const getPrefixCls = this.configProvider.getPrefixCls;
|
2019-03-15 11:20:37 +08:00
|
|
|
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
|
2020-06-22 22:27:40 +08:00
|
|
|
const {
|
|
|
|
onMouseenter = noop,
|
|
|
|
onMouseleave = noop,
|
|
|
|
onInput,
|
|
|
|
class: className,
|
|
|
|
style,
|
|
|
|
...restAttrs
|
2020-11-13 10:51:12 +08:00
|
|
|
} = $attrs;
|
2020-10-14 17:57:38 +08:00
|
|
|
const checkboxProps: any = {
|
2020-06-20 22:22:00 +08:00
|
|
|
...restProps,
|
2021-09-25 16:51:32 +08:00
|
|
|
id,
|
2020-06-20 22:22:00 +08:00
|
|
|
prefixCls,
|
|
|
|
...restAttrs,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-09-25 16:51:32 +08:00
|
|
|
if (checkboxGroup && !skipGroup) {
|
2020-06-20 22:22:00 +08:00
|
|
|
checkboxProps.onChange = (...args) => {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.$emit('change', ...args);
|
2021-09-25 16:51:32 +08:00
|
|
|
this.formItemContext.onFieldChange();
|
2019-01-12 11:33:27 +08:00
|
|
|
checkboxGroup.toggleOption({ label: children, value: props.value });
|
|
|
|
};
|
2020-06-20 22:22:00 +08:00
|
|
|
checkboxProps.name = checkboxGroup.name;
|
|
|
|
checkboxProps.checked = checkboxGroup.sValue.indexOf(props.value) !== -1;
|
|
|
|
checkboxProps.disabled = props.disabled || checkboxGroup.disabled;
|
|
|
|
checkboxProps.indeterminate = indeterminate;
|
2018-09-05 21:28:54 +08:00
|
|
|
} else {
|
2020-06-20 22:22:00 +08:00
|
|
|
checkboxProps.onChange = this.handleChange;
|
2018-01-16 15:41:00 +08:00
|
|
|
}
|
2020-06-20 22:22:00 +08:00
|
|
|
const classString = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-wrapper`]: true,
|
|
|
|
[`${prefixCls}-wrapper-checked`]: checkboxProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: checkboxProps.disabled,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2018-09-05 21:28:54 +08:00
|
|
|
const checkboxClass = classNames({
|
|
|
|
[`${prefixCls}-indeterminate`]: indeterminate,
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-01-16 15:41:00 +08:00
|
|
|
return (
|
2020-06-20 22:22:00 +08:00
|
|
|
<label
|
|
|
|
class={classString}
|
|
|
|
style={style}
|
2021-08-29 10:44:24 +08:00
|
|
|
onMouseenter={onMouseenter as EventHandler}
|
|
|
|
onMouseleave={onMouseleave as EventHandler}
|
2020-06-20 22:22:00 +08:00
|
|
|
>
|
2019-01-12 11:33:27 +08:00
|
|
|
<VcCheckbox {...checkboxProps} class={checkboxClass} ref="vcCheckbox" />
|
2020-08-13 17:46:44 +08:00
|
|
|
{children.length ? <span>{children}</span> : null}
|
2018-01-16 15:41:00 +08:00
|
|
|
</label>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-01-16 15:41:00 +08:00
|
|
|
},
|
2020-10-14 17:57:38 +08:00
|
|
|
});
|