import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import RcCheckbox from 'rc-checkbox'; import shallowEqual from 'shallowequal'; import CheckboxGroup from './Group'; export interface AbstractCheckboxProps { prefixCls?: string; className?: string; defaultChecked?: boolean; checked?: boolean; style?: React.CSSProperties; disabled?: boolean; onChange?: React.FormEventHandler; onMouseEnter?: React.MouseEventHandler; onMouseLeave?: React.MouseEventHandler; value?: any; name?: string; children?: React.ReactChild; } export interface CheckboxProps extends AbstractCheckboxProps { indeterminate?: boolean; } export default class Checkbox extends React.Component { static Group: typeof CheckboxGroup; static defaultProps = { prefixCls: 'ant-checkbox', indeterminate: false, }; static contextTypes = { checkboxGroup: PropTypes.any, }; shouldComponentUpdate(nextProps, nextState, nextContext) { return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState) || !shallowEqual(this.context.checkboxGroup, nextContext.checkboxGroup); } 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 = 'disabled' in props ? props.disabled : checkboxGroup.disabled; } const classString = classNames(className, { [`${prefixCls}-wrapper`]: true, }); const checkboxClass = classNames({ [`${prefixCls}-indeterminate`]: indeterminate, }); return ( ); } }