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