-
Notifications
You must be signed in to change notification settings - Fork 103
/
Checkbox.jsx
132 lines (124 loc) · 3.36 KB
/
Checkbox.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import React from 'react';
import PureRenderMixin from 'rc-util/lib/PureRenderMixin';
import classNames from 'classnames';
export default class Checkbox extends React.Component {
static propTypes = {
name: React.PropTypes.string,
prefixCls: React.PropTypes.string,
style: React.PropTypes.object,
type: React.PropTypes.string,
className: React.PropTypes.string,
defaultChecked: React.PropTypes.oneOfType([React.PropTypes.number, React.PropTypes.bool]),
disabled: React.PropTypes.bool,
checked: React.PropTypes.oneOfType([React.PropTypes.number, React.PropTypes.bool]),
onFocus: React.PropTypes.func,
onBlur: React.PropTypes.func,
onChange: React.PropTypes.func,
onClick: React.PropTypes.func,
};
static defaultProps = {
prefixCls: 'rc-checkbox',
style: {},
type: 'checkbox',
className: '',
defaultChecked: false,
onFocus() {},
onBlur() {},
onChange() {},
};
constructor(props) {
super(props);
let checked = false;
if ('checked' in props) {
checked = props.checked;
} else {
checked = props.defaultChecked;
}
this.state = {
checked,
focus: false,
};
}
componentWillReceiveProps(nextProps) {
if ('checked' in nextProps) {
this.setState({
checked: nextProps.checked,
});
}
}
shouldComponentUpdate(...args) {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}
handleFocus = (e) => {
this.setState({ focus: true });
this.props.onFocus(e);
};
handleBlur = (e) => {
this.setState({ focus: false });
this.props.onBlur(e);
};
handleChange = (e) => {
if (this.props.disabled) {
return;
}
if (!('checked' in this.props)) {
this.setState({
checked: e.target.checked,
});
}
this.props.onChange({
target: {
...this.props,
checked: e.target.checked,
},
stopPropagation() {
e.stopPropagation();
},
preventDefault() {
e.preventDefault();
},
});
};
render() {
const props = { ...this.props };
// Remove React warning.
// Warning: Input elements must be either controlled or uncontrolled
// (specify either the value prop, or the defaultValue prop, but not both).
delete props.defaultChecked;
const state = this.state;
const prefixCls = props.prefixCls;
let checked = state.checked;
if (typeof checked === 'boolean') {
checked = checked ? 1 : 0;
}
const className = classNames({
[props.className]: !!props.className,
[prefixCls]: 1,
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-checked-${checked}`]: !!checked,
[`${prefixCls}-focused`]: state.focus,
[`${prefixCls}-disabled`]: props.disabled,
});
return (
<span
className={className}
style={props.style}
>
<span className={`${prefixCls}-inner`} />
<input
name={props.name}
type={props.type}
readOnly={props.readOnly}
disabled={props.disabled}
tabIndex={props.tabIndex}
className={`${prefixCls}-input`}
checked={!!checked}
onClick={this.props.onClick}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onChange={this.handleChange}
/>
</span>
);
}
}