| 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 |
1x
17x
27x
17x
17x
17x
18x
36x
18x
18x
16x
16x
1x
1x
18x
18x
| import React, {PropTypes} from 'react';
import cx from 'classnames';
import Checkbox from 'boundless-checkbox';
import omit from 'boundless-utils-omit-keys';
import uuid from 'boundless-utils-uuid';
const noop = () => {};
/**
__A controller view for managing the aggregate state of multiple, related checkboxes.__
The most common use case for `CheckboxGroup` is a "select all" / children scenario. This particular
configuration is built-in and is activated by passing the `selectAll` prop.
*/
export default class CheckboxGroup extends React.PureComponent {
static selectAll = {
BEFORE: uuid(),
AFTER: uuid(),
NONE: uuid(),
}
static propTypes = {
/**
* any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
*/
'*': PropTypes.any,
/**
* override the wrapper HTML element if desired
*/
component: PropTypes.string,
/**
* the data wished to be rendered, each item must conform to the [Checkbox prop spec](./Checkbox#props)
*/
items: PropTypes.arrayOf(Checkbox.propTypes.inputProps).isRequired,
/**
* called when all children become checked (not fired on first render), no return
*/
onAllChecked: PropTypes.func,
/**
* called when all children become unchecked (not fired on first render), no return
*/
onAllUnchecked: PropTypes.func,
/**
* called when a specific child has become checked, returns the child definition
*/
onChildChecked: PropTypes.func,
/**
* called when a specific child has become checked, returns the child definition
*/
onChildUnchecked: PropTypes.func,
/**
* renders a master checkbox that can manipulate the values of all children simultaneously
*/
selectAll: PropTypes.oneOf([
CheckboxGroup.selectAll.BEFORE,
CheckboxGroup.selectAll.AFTER,
CheckboxGroup.selectAll.NONE,
]),
/**
* must conform to the [Checkbox prop spec](./Checkbox#props)
*/
selectAllProps: PropTypes.shape({
/**
* any [React-supported attribute](https://facebook.github.io/react/docs/tags-and-attributes.html#html-attributes)
*/
'*': PropTypes.any,
/**
* the text or renderable node to display next to the checkbox
*/
label: PropTypes.string,
inputProps: PropTypes.object,
}),
}
static defaultProps = {
component: 'div',
items: [],
onAllChecked: noop,
onAllUnchecked: noop,
onChildChecked: noop,
onChildUnchecked: noop,
selectAll: CheckboxGroup.selectAll.BEFORE,
selectAllProps: {},
}
static internalKeys = Object.keys(CheckboxGroup.defaultProps)
selectAllUUID = uuid()
allItemsChecked() {
return this.props.items.every((item) => item.inputProps.checked === true);
}
anyItemsChecked() {
return this.props.items.some((item) => item.inputProps.checked === true);
}
renderSelectAllCheckbox() {
const allChecked = this.allItemsChecked();
const {inputProps} = this.props.selectAllProps;
return (
<Checkbox
{...this.props.selectAllProps}
key={this.selectAllUUID}
className={cx('b-checkbox-group-all', this.props.selectAllProps.className)}
inputProps={{
...inputProps,
checked: allChecked,
indeterminate: !allChecked && this.anyItemsChecked(),
name: inputProps && inputProps.name ? inputProps.name : null,
}}
label={this.props.selectAllProps.label || 'Select All'}
onChecked={this.props.onAllChecked}
onUnchecked={this.props.onAllUnchecked} />
);
}
renderCheckboxes() {
return this.props.items.map((item) => {
return (
<Checkbox
{...item}
key={item.inputProps.name}
onChecked={this.props.onChildChecked}
onUnchecked={this.props.onChildUnchecked} />
);
});
}
renderChildren() {
const children = [this.renderCheckboxes()];
switch (this.props.selectAll) {
case CheckboxGroup.selectAll.BEFORE:
children.unshift(this.renderSelectAllCheckbox());
break;
case CheckboxGroup.selectAll.AFTER:
children.push(this.renderSelectAllCheckbox());
break;
}
return children;
}
render() {
return (
<this.props.component
{...omit(this.props, CheckboxGroup.internalKeys)}
className={cx('b-checkbox-group', this.props.className)}>
{this.renderChildren()}
</this.props.component>
);
}
}
|