-
Notifications
You must be signed in to change notification settings - Fork 31
/
Checkbox.jsx
99 lines (87 loc) · 2.54 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
/* eslint-disable jsx-a11y/label-has-associated-control */
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import ToggleButton from '../views/ToggleButton';
import CheckboxView from '../views/Checkbox';
export default class Checkbox extends PureComponent {
static propTypes = {
style: PropTypes.object,
className: PropTypes.string,
labelStyle: PropTypes.object,
labelClassName: PropTypes.string,
label: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
onChange: PropTypes.func,
toggleButton: PropTypes.bool,
checked: PropTypes.bool,
defaultChecked: PropTypes.bool,
disabled: PropTypes.bool,
dangerouslySetLabel: PropTypes.object,
stopPropagation: PropTypes.bool,
};
static defaultProps = {
style: null,
className: null,
label: null,
labelClassName: null,
labelStyle: null,
children: null,
onChange: null,
toggleButton: false,
checked: undefined,
defaultChecked: undefined,
disabled: false,
dangerouslySetLabel: null,
stopPropagation: false,
};
constructor() {
super();
this.id = Math.random();
}
onChange = () => {
const { onChange, disabled } = this.props;
if (!disabled && onChange) {
onChange(this._node.checked);
}
};
renderCheckbox(props) {
return (
<CheckboxView
{...props}
id={this.id}
ref={(ref) => { this._node = ref; }}
onChange={this.onChange}
/>
);
}
renderToggleButton(props) {
return (
<ToggleButton
{...props}
id={this.id}
ref={(ref) => { this._node = ref; }}
onChange={this.onChange}
/>
);
}
render() {
const {
toggleButton,
onChange,
...props
} = this.props;
if (props.className) {
console.warn('[chayns components]: Checkbox/Togglebutton: The prop "className" will be set to the root element in next minor version.');
}
if (toggleButton) {
return this.renderToggleButton(props);
}
return this.renderCheckbox(props);
}
}