2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2017-04-14 16:20:09 +08:00
|
|
|
import classNames from 'classnames';
|
2017-03-13 10:20:25 +08:00
|
|
|
import RcCheckbox from 'rc-checkbox';
|
2020-02-28 11:11:00 +08:00
|
|
|
import CheckboxGroup, { GroupContext } from './Group';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
2020-05-14 15:57:04 +08:00
|
|
|
import devWarning from '../_util/devWarning';
|
2017-03-13 10:20:25 +08:00
|
|
|
|
2018-03-08 20:34:40 +08:00
|
|
|
export interface AbstractCheckboxProps<T> {
|
2017-03-13 10:20:25 +08:00
|
|
|
prefixCls?: string;
|
2017-04-14 16:20:09 +08:00
|
|
|
className?: string;
|
2017-03-13 10:20:25 +08:00
|
|
|
defaultChecked?: boolean;
|
2017-04-14 16:20:09 +08:00
|
|
|
checked?: boolean;
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
disabled?: boolean;
|
2018-03-08 20:34:40 +08:00
|
|
|
onChange?: (e: T) => void;
|
2019-06-16 20:51:47 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
|
|
|
onMouseEnter?: React.MouseEventHandler<HTMLElement>;
|
|
|
|
onMouseLeave?: React.MouseEventHandler<HTMLElement>;
|
|
|
|
onKeyPress?: React.KeyboardEventHandler<HTMLElement>;
|
|
|
|
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
|
2019-06-24 11:29:58 +08:00
|
|
|
value?: any;
|
2018-01-12 00:09:23 +08:00
|
|
|
tabIndex?: number;
|
2017-05-27 09:31:06 +08:00
|
|
|
name?: string;
|
2017-09-18 15:40:25 +08:00
|
|
|
children?: React.ReactNode;
|
2019-05-03 21:55:18 +08:00
|
|
|
id?: string;
|
|
|
|
autoFocus?: boolean;
|
2017-04-14 16:20:09 +08:00
|
|
|
}
|
|
|
|
|
2018-03-08 20:34:40 +08:00
|
|
|
export interface CheckboxProps extends AbstractCheckboxProps<CheckboxChangeEvent> {
|
2017-04-14 16:20:09 +08:00
|
|
|
indeterminate?: boolean;
|
2017-03-13 10:20:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-08 20:34:40 +08:00
|
|
|
export interface CheckboxChangeEventTarget extends CheckboxProps {
|
|
|
|
checked: boolean;
|
|
|
|
}
|
|
|
|
|
2018-03-08 21:42:22 +08:00
|
|
|
export interface CheckboxChangeEvent {
|
|
|
|
target: CheckboxChangeEventTarget;
|
|
|
|
stopPropagation: () => void;
|
|
|
|
preventDefault: () => void;
|
2018-07-24 14:49:23 +08:00
|
|
|
nativeEvent: MouseEvent;
|
2018-03-08 21:42:22 +08:00
|
|
|
}
|
2018-03-08 20:34:40 +08:00
|
|
|
|
2020-02-28 11:11:00 +08:00
|
|
|
class Checkbox extends React.PureComponent<CheckboxProps, {}> {
|
2017-03-13 10:20:25 +08:00
|
|
|
static Group: typeof CheckboxGroup;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-08-07 14:33:01 +08:00
|
|
|
static __ANT_CHECKBOX = true;
|
|
|
|
|
2017-03-13 10:20:25 +08:00
|
|
|
static defaultProps = {
|
|
|
|
indeterminate: false,
|
|
|
|
};
|
2017-04-14 16:20:09 +08:00
|
|
|
|
2020-01-31 21:25:34 +08:00
|
|
|
static contextType = GroupContext;
|
2017-04-14 16:20:09 +08:00
|
|
|
|
2018-11-09 17:38:19 +08:00
|
|
|
context: any;
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
private rcCheckbox: any;
|
|
|
|
|
2019-04-30 23:39:01 +08:00
|
|
|
componentDidMount() {
|
|
|
|
const { value } = this.props;
|
2020-02-28 11:11:00 +08:00
|
|
|
this.context?.registerValue(value);
|
2019-08-27 18:29:20 +08:00
|
|
|
|
2020-05-14 15:57:04 +08:00
|
|
|
devWarning(
|
2020-02-28 11:11:00 +08:00
|
|
|
'checked' in this.props || this.context || !('value' in this.props),
|
2019-08-27 18:29:20 +08:00
|
|
|
'Checkbox',
|
2020-04-14 16:48:23 +08:00
|
|
|
'`value` is not a valid prop, do you mean `checked`?',
|
2019-08-27 18:29:20 +08:00
|
|
|
);
|
2019-04-30 23:39:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate({ value: prevValue }: CheckboxProps) {
|
|
|
|
const { value } = this.props;
|
2020-02-28 11:11:00 +08:00
|
|
|
if (value !== prevValue) {
|
|
|
|
this.context?.cancelValue(prevValue);
|
|
|
|
this.context?.registerValue(value);
|
2019-04-30 23:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const { value } = this.props;
|
2020-02-28 11:11:00 +08:00
|
|
|
this.context?.cancelValue(value);
|
2019-04-30 23:39:01 +08:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
saveCheckbox = (node: any) => {
|
|
|
|
this.rcCheckbox = node;
|
|
|
|
};
|
2017-04-14 16:20:09 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
focus() {
|
|
|
|
this.rcCheckbox.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.rcCheckbox.blur();
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:39:10 +08:00
|
|
|
renderCheckbox = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
|
2017-04-14 16:20:09 +08:00
|
|
|
const { props, context } = this;
|
2017-03-13 10:20:25 +08:00
|
|
|
const {
|
2018-12-05 19:12:18 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
2017-04-14 16:20:09 +08:00
|
|
|
className,
|
|
|
|
children,
|
|
|
|
indeterminate,
|
|
|
|
style,
|
|
|
|
onMouseEnter,
|
|
|
|
onMouseLeave,
|
2018-06-02 20:12:09 +08:00
|
|
|
...restProps
|
2017-04-14 16:20:09 +08:00
|
|
|
} = props;
|
2020-02-28 11:11:00 +08:00
|
|
|
const checkboxGroup = context;
|
2018-12-05 19:12:18 +08:00
|
|
|
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
|
2018-11-10 21:40:21 +08:00
|
|
|
const checkboxProps: CheckboxProps = { ...restProps };
|
2017-04-14 16:20:09 +08:00
|
|
|
if (checkboxGroup) {
|
2018-10-23 23:40:40 +08:00
|
|
|
checkboxProps.onChange = (...args) => {
|
|
|
|
if (restProps.onChange) {
|
|
|
|
restProps.onChange(...args);
|
|
|
|
}
|
|
|
|
checkboxGroup.toggleOption({ label: children, value: props.value });
|
|
|
|
};
|
2019-04-01 10:16:18 +08:00
|
|
|
checkboxProps.name = checkboxGroup.name;
|
2017-04-14 16:20:09 +08:00
|
|
|
checkboxProps.checked = checkboxGroup.value.indexOf(props.value) !== -1;
|
2018-04-11 16:59:22 +08:00
|
|
|
checkboxProps.disabled = props.disabled || checkboxGroup.disabled;
|
2017-04-14 16:20:09 +08:00
|
|
|
}
|
2017-03-13 10:20:25 +08:00
|
|
|
const classString = classNames(className, {
|
|
|
|
[`${prefixCls}-wrapper`]: true,
|
2020-04-09 12:39:10 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2018-11-20 15:10:50 +08:00
|
|
|
[`${prefixCls}-wrapper-checked`]: checkboxProps.checked,
|
|
|
|
[`${prefixCls}-wrapper-disabled`]: checkboxProps.disabled,
|
2017-03-13 10:20:25 +08:00
|
|
|
});
|
|
|
|
const checkboxClass = classNames({
|
|
|
|
[`${prefixCls}-indeterminate`]: indeterminate,
|
|
|
|
});
|
|
|
|
return (
|
2019-08-06 15:02:05 +08:00
|
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
2017-03-13 10:20:25 +08:00
|
|
|
<label
|
|
|
|
className={classString}
|
|
|
|
style={style}
|
|
|
|
onMouseEnter={onMouseEnter}
|
|
|
|
onMouseLeave={onMouseLeave}
|
|
|
|
>
|
|
|
|
<RcCheckbox
|
2017-04-14 16:20:09 +08:00
|
|
|
{...checkboxProps}
|
2017-03-13 10:20:25 +08:00
|
|
|
prefixCls={prefixCls}
|
|
|
|
className={checkboxClass}
|
2017-11-19 01:41:40 +08:00
|
|
|
ref={this.saveCheckbox}
|
2017-03-13 10:20:25 +08:00
|
|
|
/>
|
2018-11-23 13:42:37 +08:00
|
|
|
{children !== undefined && <span>{children}</span>}
|
2017-03-13 10:20:25 +08:00
|
|
|
</label>
|
|
|
|
);
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderCheckbox}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2017-03-13 10:20:25 +08:00
|
|
|
}
|
2019-04-30 23:39:01 +08:00
|
|
|
|
|
|
|
export default Checkbox;
|