ant-design/components/checkbox/Checkbox.tsx

135 lines
3.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
import classNames from 'classnames';
import RcCheckbox from 'rc-checkbox';
import shallowEqual from 'shallowequal';
2017-11-21 21:15:36 +08:00
import CheckboxGroup, { CheckboxGroupContext } from './Group';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
export interface AbstractCheckboxProps<T> {
prefixCls?: string;
className?: string;
defaultChecked?: boolean;
checked?: boolean;
style?: React.CSSProperties;
disabled?: boolean;
onChange?: (e: T) => void;
onMouseEnter?: React.MouseEventHandler<any>;
onMouseLeave?: React.MouseEventHandler<any>;
onKeyPress?: React.KeyboardEventHandler<any>;
onKeyDown?: React.KeyboardEventHandler<any>;
value?: any;
tabIndex?: number;
name?: string;
children?: React.ReactNode;
}
export interface CheckboxProps extends AbstractCheckboxProps<CheckboxChangeEvent> {
indeterminate?: boolean;
}
export interface CheckboxChangeEventTarget extends CheckboxProps {
checked: boolean;
}
export interface CheckboxChangeEvent {
target: CheckboxChangeEventTarget;
stopPropagation: () => void;
preventDefault: () => void;
nativeEvent: MouseEvent;
}
2019-01-08 18:40:03 +08:00
export default class Checkbox extends React.Component<CheckboxProps, {}> {
static Group: typeof CheckboxGroup;
static defaultProps = {
indeterminate: false,
};
static contextTypes = {
checkboxGroup: PropTypes.any,
};
2018-11-09 17:38:19 +08:00
context: any;
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)
);
}
focus() {
this.rcCheckbox.focus();
}
blur() {
this.rcCheckbox.blur();
}
2017-11-21 21:15:36 +08:00
saveCheckbox = (node: any) => {
this.rcCheckbox = node;
2018-12-07 20:02:01 +08:00
};
renderCheckbox = ({ getPrefixCls }: ConfigConsumerProps) => {
const { props, context } = this;
const {
prefixCls: customizePrefixCls,
className,
children,
indeterminate,
style,
onMouseEnter,
onMouseLeave,
...restProps
} = props;
const { checkboxGroup } = context;
const prefixCls = getPrefixCls('checkbox', customizePrefixCls);
2018-11-10 21:40:21 +08:00
const checkboxProps: CheckboxProps = { ...restProps };
if (checkboxGroup) {
checkboxProps.onChange = (...args) => {
if (restProps.onChange) {
restProps.onChange(...args);
}
checkboxGroup.toggleOption({ label: children, value: props.value });
};
checkboxProps.checked = checkboxGroup.value.indexOf(props.value) !== -1;
checkboxProps.disabled = props.disabled || checkboxGroup.disabled;
}
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,
});
const checkboxClass = classNames({
[`${prefixCls}-indeterminate`]: indeterminate,
});
return (
<label
className={classString}
style={style}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<RcCheckbox
{...checkboxProps}
prefixCls={prefixCls}
className={checkboxClass}
ref={this.saveCheckbox}
/>
{children !== undefined && <span>{children}</span>}
</label>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderCheckbox}</ConfigConsumer>;
}
}