import * as React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import RcCheckbox from 'rc-checkbox'; import shallowEqual from 'shallowequal'; import CheckboxGroup, { CheckboxGroupContext } from './Group'; export interface AbstractCheckboxProps { prefixCls?: string; className?: string; defaultChecked?: boolean; checked?: boolean; style?: React.CSSProperties; disabled?: boolean; onChange?: (e: T) => void; onMouseEnter?: React.MouseEventHandler; onMouseLeave?: React.MouseEventHandler; onKeyPress?: React.KeyboardEventHandler; onKeyDown?: React.KeyboardEventHandler; value?: any; tabIndex?: number; name?: string; children?: React.ReactNode; } export interface CheckboxProps extends AbstractCheckboxProps { indeterminate?: boolean; } export interface CheckboxChangeEventTarget extends CheckboxProps { checked: boolean; } export interface CheckboxChangeEvent { target: CheckboxChangeEventTarget; stopPropagation: () => void; preventDefault: () => void; nativeEvent: MouseEvent; } export default class Checkbox extends React.Component { static Group: typeof CheckboxGroup; static defaultProps = { prefixCls: 'ant-checkbox', indeterminate: false, }; static contextTypes = { checkboxGroup: PropTypes.any, }; private rcCheckbox: any; 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(); } saveCheckbox = (node: any) => { this.rcCheckbox = node; } render() { const { props, context } = this; const { prefixCls, className, children, indeterminate, style, onMouseEnter, onMouseLeave, ...restProps } = props; const { checkboxGroup } = context; let checkboxProps: CheckboxProps = { ...restProps }; if (checkboxGroup) { checkboxProps.onChange = () => 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, }); const checkboxClass = classNames({ [`${prefixCls}-indeterminate`]: indeterminate, }); return ( ); } }