2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-08-07 21:07:52 +08:00
|
|
|
import * as PropTypes from 'prop-types';
|
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';
|
2017-04-14 16:20:09 +08:00
|
|
|
import shallowEqual from 'shallowequal';
|
2017-11-21 21:15:36 +08:00
|
|
|
import CheckboxGroup, { CheckboxGroupContext } from './Group';
|
2018-12-05 19:12:18 +08:00
|
|
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
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;
|
2017-03-13 10:20:25 +08:00
|
|
|
onMouseEnter?: React.MouseEventHandler<any>;
|
|
|
|
onMouseLeave?: React.MouseEventHandler<any>;
|
2018-02-16 03:10:35 +08:00
|
|
|
onKeyPress?: React.KeyboardEventHandler<any>;
|
|
|
|
onKeyDown?: React.KeyboardEventHandler<any>;
|
2017-04-14 16:20:09 +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;
|
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
|
|
|
|
2019-01-08 18:40:03 +08:00
|
|
|
export default class Checkbox extends React.Component<CheckboxProps, {}> {
|
2017-03-13 10:20:25 +08:00
|
|
|
static Group: typeof CheckboxGroup;
|
|
|
|
static defaultProps = {
|
|
|
|
indeterminate: false,
|
|
|
|
};
|
2017-04-14 16:20:09 +08:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
checkboxGroup: PropTypes.any,
|
|
|
|
};
|
|
|
|
|
2018-11-09 17:38:19 +08:00
|
|
|
context: any;
|
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
private rcCheckbox: any;
|
|
|
|
|
2018-12-07 20:02:01 +08:00
|
|
|
shouldComponentUpdate(
|
|
|
|
nextProps: CheckboxProps,
|
|
|
|
nextState: {},
|
|
|
|
nextContext: CheckboxGroupContext,
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
!shallowEqual(this.props, nextProps) ||
|
|
|
|
!shallowEqual(this.state, nextState) ||
|
|
|
|
!shallowEqual(this.context.checkboxGroup, nextContext.checkboxGroup)
|
|
|
|
);
|
2017-03-13 10:20:25 +08:00
|
|
|
}
|
2017-04-14 16:20:09 +08:00
|
|
|
|
2017-11-19 01:41:40 +08:00
|
|
|
focus() {
|
|
|
|
this.rcCheckbox.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.rcCheckbox.blur();
|
|
|
|
}
|
|
|
|
|
2017-11-21 21:15:36 +08:00
|
|
|
saveCheckbox = (node: any) => {
|
2017-11-19 01:41:40 +08:00
|
|
|
this.rcCheckbox = node;
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2017-11-19 01:41:40 +08:00
|
|
|
|
2018-12-05 19:12:18 +08:00
|
|
|
renderCheckbox = ({ getPrefixCls }: 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;
|
|
|
|
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 });
|
|
|
|
};
|
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,
|
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 (
|
|
|
|
<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
|
|
|
}
|