-
Notifications
You must be signed in to change notification settings - Fork 8
/
CheckBoxGroup.js
109 lines (90 loc) · 3.41 KB
/
CheckBoxGroup.js
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
import React, { Children } from 'React';
import PropTypes from 'prop-types';
import warning from 'warning';
import Stylable from 'b:Stylable';
import { decl } from 'bem-react-core';
export default decl([Stylable], {
block : 'CheckBoxGroup',
willInit({ focused, disabled }) {
warning(!(focused && disabled), `${this.block}: Can't have both "focused" and "disabled" props.`);
this.state = { focused };
this._options = [];
this._registerOption = this._registerOption.bind(this);
this._unregisterOption = this._unregisterOption.bind(this);
this._onOptionChange = this._onOptionChange.bind(this);
this._onOptionFocusChange = this._onOptionFocusChange.bind(this);
},
willReceiveProps({ focused, disabled }) {
warning(!(focused && disabled), `${this.block}: Can't have both "focused" and "disabled" props.`);
typeof focused !== 'undefined' && this.setState({ focused });
},
getChildContext() {
const { name, type, value, disabled, theme, size } = this.props;
return {
_checkBoxGroupName : name,
_checkBoxGroupType : type,
_checkBoxGroupValue : value,
_checkBoxGroupDisabled : disabled,
_checkBoxGroupTheme : theme,
_checkBoxGroupSize : size,
_checkBoxGroupRegisterOption : this._registerOption,
_checkBoxGroupUnregisterOption : this._unregisterOption,
_checkBoxGroupOnOptionChange : this._onOptionChange,
_checkBoxGroupOnOptionFocusChange : this._onOptionFocusChange
};
},
mods({ type, disabled }) {
const { focused } = this.state;
return { ...this.__base(...arguments), type, disabled, focused };
},
content({ children, type }) {
const res = [];
Children.forEach(children, (option, i) => {
i && !type && res.push(<br key={i}/>);
res.push(option);
});
return res;
},
_registerOption(option) {
this._options.push(option);
},
_unregisterOption(option) {
this._options.splice(this._options.indexOf(option), 1);
},
_onOptionChange(checked, value) {
this.props.onChange(this._options.reduce((res, option) => {
(option.props.value === value? checked : option._checked) &&
res.push(option.props.value);
return res;
}, []));
},
_onOptionFocusChange(focused) {
this.setState({ focused },
() => this.props.onFocusChange(focused));
}
}, {
propTypes : {
name : PropTypes.string,
type : PropTypes.oneOf([undefined, 'button', 'line']),
value : PropTypes.array,
disabled : PropTypes.bool,
onChange : PropTypes.func.isRequired,
onFocusChange : PropTypes.func
},
defaultProps : {
value : [],
onFocusChange() {}
},
childContextTypes : {
_checkBoxGroupName : PropTypes.string,
_checkBoxGroupType : PropTypes.string,
_checkBoxGroupValue : PropTypes.array,
_checkBoxGroupDisabled : PropTypes.bool,
_checkBoxGroupTheme : PropTypes.string,
_checkBoxGroupSize : PropTypes.string,
_checkBoxGroupRegisterOption : PropTypes.func,
_checkBoxGroupUnregisterOption : PropTypes.func,
_checkBoxGroupOnOptionChange : PropTypes.func,
_checkBoxGroupOnOptionFocusChange : PropTypes.func
}
});