2020-06-20 14:22:00 +00:00
|
|
|
import { inject } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import VcCheckbox from '../vc-checkbox';
|
2020-06-22 14:27:40 +00:00
|
|
|
import hasProp, { getOptionProps, getSlot } from '../_util/props-util';
|
2019-03-15 03:20:37 +00:00
|
|
|
import { ConfigConsumerProps } from '../config-provider';
|
2020-03-07 11:45:13 +00:00
|
|
|
import warning from '../_util/warning';
|
2020-06-22 14:27:40 +00:00
|
|
|
function noop() {}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2017-10-26 07:18:08 +00:00
|
|
|
export default {
|
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,
|
2018-06-08 10:37:03 +00:00
|
|
|
defaultChecked: PropTypes.bool,
|
|
|
|
checked: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
2019-03-15 03:20:37 +00:00
|
|
|
isGroup: PropTypes.bool,
|
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,
|
|
|
|
indeterminate: PropTypes.bool,
|
2018-05-05 09:39:56 +00:00
|
|
|
type: PropTypes.string.def('checkbox'),
|
2019-03-15 03:20:37 +00:00
|
|
|
autoFocus: PropTypes.bool,
|
2017-10-26 07:18:08 +00:00
|
|
|
},
|
2020-06-20 14:22:00 +00:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
configProvider: inject('configProvider', ConfigConsumerProps),
|
|
|
|
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) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
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: {
|
2019-01-12 03:33:27 +00:00
|
|
|
handleChange(event) {
|
|
|
|
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() {
|
|
|
|
this.$refs.vcCheckbox.focus();
|
2018-01-16 07:41:00 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
|
|
|
this.$refs.vcCheckbox.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
|
|
|
|
} = $attrs;
|
2019-01-12 03:33:27 +00:00
|
|
|
const checkboxProps = {
|
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}
|
|
|
|
onMouseenter={onMouseenter}
|
|
|
|
onMouseenter={onMouseleave}
|
|
|
|
>
|
2019-01-12 03:33:27 +00:00
|
|
|
<VcCheckbox {...checkboxProps} class={checkboxClass} ref="vcCheckbox" />
|
2018-12-13 13:26:21 +00:00
|
|
|
{children !== undefined && <span>{children}</span>}
|
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
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|