You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.7 KiB
103 lines
2.7 KiB
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?: React.ChangeEventHandler<HTMLInputElement>;
|
|
onMouseEnter?: React.MouseEventHandler<any>;
|
|
onMouseLeave?: React.MouseEventHandler<any>;
|
|
onKeyPress?: React.KeyboardEvent<any>;
|
|
onKeyDown?: React.KeyboardEvent<any>;
|
|
value?: any;
|
|
tabIndex?: number;
|
|
name?: string;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
export interface CheckboxProps extends AbstractCheckboxProps {
|
|
indeterminate?: boolean;
|
|
}
|
|
|
|
export default class Checkbox extends React.Component<CheckboxProps, {}> {
|
|
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 (
|
|
<label
|
|
className={classString}
|
|
style={style}
|
|
onMouseEnter={onMouseEnter}
|
|
onMouseLeave={onMouseLeave}
|
|
>
|
|
<RcCheckbox
|
|
{...checkboxProps}
|
|
prefixCls={prefixCls}
|
|
className={checkboxClass}
|
|
ref={this.saveCheckbox}
|
|
/>
|
|
{children !== undefined ? <span>{children}</span> : null}
|
|
</label>
|
|
);
|
|
}
|
|
}
|
|
|