ant-design/components/checkbox/index.tsx

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2015-12-24 15:03:06 +08:00
import RcCheckbox from 'rc-checkbox';
2016-09-21 11:54:53 +08:00
import React from 'react';
import CheckboxGroup from './Group';
2016-02-17 18:02:33 +08:00
import classNames from 'classnames';
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
2016-06-22 13:18:43 +08:00
import splitObject from '../_util/splitObject';
2016-07-07 20:25:03 +08:00
export interface CheckboxProps {
2016-07-13 11:14:24 +08:00
/** 指定当前是否选中 */
checked?: boolean;
/** 初始是否选中 */
defaultChecked?: boolean;
/** indeterminate 状态,只负责样式控制 */
indeterminate?: boolean;
2016-07-13 11:14:24 +08:00
/** 变化时回调函数 */
2016-10-19 17:51:33 +08:00
onChange?: React.FormEventHandler<any>;
2016-07-13 11:14:24 +08:00
style?: React.CSSProperties;
disabled?: boolean;
className?: string;
}
export default class Checkbox extends React.Component<CheckboxProps, any> {
static Group = CheckboxGroup;
static defaultProps = {
2016-05-11 09:32:33 +08:00
prefixCls: 'ant-checkbox',
indeterminate: false,
2016-07-13 11:14:24 +08:00
};
shouldComponentUpdate(...args) {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
2015-08-18 14:03:44 +08:00
render() {
const [{ prefixCls, style, children, className, indeterminate, onMouseEnter,
onMouseLeave }, restProps] = splitObject(
this.props, ['prefixCls', 'style', 'children', 'className', 'indeterminate',
'onMouseEnter', 'onMouseLeave']
2016-07-13 17:22:23 +08:00
);
const classString = classNames(className, {
2016-02-17 18:02:33 +08:00
[`${prefixCls}-wrapper`]: true,
});
const checkboxClass = classNames({
[`${prefixCls}-indeterminate`]: indeterminate,
});
2016-02-17 18:02:33 +08:00
return (
<label
className={classString}
style={style}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
>
<RcCheckbox
{...restProps}
prefixCls={prefixCls}
className={checkboxClass}
children={null}
/>
{children !== undefined ? <span>{children}</span> : null}
2016-02-17 18:02:33 +08:00
</label>
);
2015-07-15 16:01:24 +08:00
}
}