-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathHierarchicalCheckboxMenu.tsx
More file actions
273 lines (251 loc) · 7.5 KB
/
HierarchicalCheckboxMenu.tsx
File metadata and controls
273 lines (251 loc) · 7.5 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React, { Component } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { type IconDefinition } from '@deephaven/icons';
import classNames from 'classnames';
import Checkbox from './Checkbox';
import Popper from './popper/Popper';
import './HierarchicalCheckboxMenu.scss';
import Button from './Button';
export type HierarchicalCheckboxValueMap = Map<
string,
boolean | HierarchicalCheckboxValueMap
>;
type HierarchicalCheckboxMenuProps = {
className: string;
menuText: string;
valueMap: HierarchicalCheckboxValueMap;
onUpdateValueMap: (map: HierarchicalCheckboxValueMap) => void;
icon: IconDefinition | null;
id: string;
'data-testid'?: string;
};
type HierarchicalCheckboxMenuState = {
menuIsOpen: boolean;
};
/**
* A pull down menu that displays a hierarchy of checkboxes.
*
* Currently supports only two levels of check boxes. The input should be a Map
* where the keys are the parent names. The values can a boolean if the parent
* has no children or another Map where keys are child names and values are booleans.
*
* Here is an example:
* const INITIAL_TYPE_MAP = new Map([
* ['Queries', new Map([['Live', true], ['Batch', true]])],
* [
* 'DBA Queries',
* new Map([
* ['Data Merge', true],
* ['Data Validation', true],
* ['Imports', true],
* ['Data Services', true],
* ]),
* ],
* ['Helper Queries', true],
* ]);
*
* When a checkbox is changed, this component will make a deep copy of the Map
* with the appropriate booleans changed. It will then call onUpdateValueMap
* with the new Map.
*/
class HierarchicalCheckboxMenu extends Component<
HierarchicalCheckboxMenuProps,
HierarchicalCheckboxMenuState
> {
static defaultProps: Partial<HierarchicalCheckboxMenuProps> = {
className: '',
icon: null,
id: '',
'data-testid': undefined,
};
static isParentSelected(
parent: string,
valueMap: HierarchicalCheckboxValueMap
): boolean | null {
const children = valueMap.get(parent);
if (children === undefined) {
return false;
}
if (typeof children === 'boolean') {
// This parent has no children
return children;
}
const includesTrue = Array.from(children.values()).includes(true);
const includesFalse = Array.from(children.values()).includes(false);
if (includesTrue && includesFalse) {
// Indeterminate
return null;
}
return includesTrue;
}
constructor(props: HierarchicalCheckboxMenuProps) {
super(props);
this.toggleMenu = this.toggleMenu.bind(this);
this.toggleValueFor = this.toggleValueFor.bind(this);
this.selectAll = this.selectAll.bind(this);
this.clear = this.clear.bind(this);
this.state = {
menuIsOpen: false,
};
}
toggleMenu(event: React.MouseEvent<HTMLButtonElement>): void {
event.stopPropagation();
event.preventDefault();
this.setState(state => {
const { menuIsOpen } = state;
return { menuIsOpen: !menuIsOpen };
});
}
toggleValueFor(parent: string, child?: string): void {
const { valueMap, onUpdateValueMap } = this.props;
// Make a deep copy of the Map and toggle the boolean for parent / child
const map = new Map(valueMap);
const children = map.get(parent);
if (children instanceof Map) {
const newChildren = new Map(children);
if (child != null) {
newChildren.set(child, children.get(child) !== true);
} else {
const parentSelected = HierarchicalCheckboxMenu.isParentSelected(
parent,
map
);
const newChildValue = parentSelected == null || !parentSelected;
children.forEach((_, key) => newChildren.set(key, newChildValue));
}
map.set(parent, newChildren);
} else {
map.set(parent, children == null || !children);
}
// The parent was clicked so all children must be toggled
const currentChildren = map.get(parent);
if (
child === undefined &&
currentChildren !== undefined &&
typeof currentChildren !== 'boolean'
) {
const parentSelected = HierarchicalCheckboxMenu.isParentSelected(
parent,
valueMap
);
if (parentSelected != null && parentSelected) {
currentChildren.forEach((_, key) => currentChildren.set(key, false));
} else {
// for parent selection of false or null (indeterminate), select everything
currentChildren.forEach((_, key) => currentChildren.set(key, true));
}
}
onUpdateValueMap(map);
}
setAllValues(value: boolean): void {
const { valueMap, onUpdateValueMap } = this.props;
// Make a deep copy of the Map and set everything
const copy = new Map();
valueMap.forEach((child, parent) => {
if (typeof child === 'boolean') {
copy.set(parent, value);
} else {
const children = new Map();
child.forEach((_, key) => children.set(key, value));
copy.set(parent, children);
}
});
onUpdateValueMap(copy);
}
selectAll(): void {
this.setAllValues(true);
}
clear(): void {
this.setAllValues(false);
}
renderMenuElement(): JSX.Element {
const { valueMap, 'data-testid': dataTestId } = this.props;
return (
<div className="hcm-menu-container">
{Array.from(valueMap.entries()).map(([parent, children]) => (
<div key={parent}>
<Checkbox
className="hcm-parent"
checked={HierarchicalCheckboxMenu.isParentSelected(
parent,
valueMap
)}
onChange={() => this.toggleValueFor(parent)}
>
{parent}
</Checkbox>
{children !== undefined &&
typeof children !== 'boolean' &&
Array.from(children.entries()).map(([child, value]) => (
<Checkbox
className="hcm-child"
key={child}
checked={typeof value === 'boolean' ? value : null}
onChange={() => this.toggleValueFor(parent, child)}
>
{child}
</Checkbox>
))}
</div>
))}
<Button
kind="ghost"
onClick={this.selectAll}
data-testid={
dataTestId !== undefined
? `${dataTestId}-btn-select-all`
: undefined
}
>
Select All
</Button>
<Button
kind="ghost"
onClick={this.clear}
data-testid={
dataTestId !== undefined ? `${dataTestId}-btn-clear` : undefined
}
>
Clear
</Button>
</div>
);
}
render(): JSX.Element {
const {
menuText,
className,
icon,
id,
'data-testid': dataTestId,
} = this.props;
const { menuIsOpen } = this.state;
return (
<button
type="button"
className={classNames('btn hcm-btn', className)}
onClick={this.toggleMenu}
id={id}
data-testid={dataTestId}
>
<span>
{icon && <FontAwesomeIcon icon={icon} className="hcm-icon mr-1" />}
{menuText}
</span>
<span className="cs-caret" />
<Popper
options={{ placement: 'bottom' }}
isShown={menuIsOpen}
onExited={() => {
this.setState({ menuIsOpen: false });
}}
closeOnBlur
interactive
>
{this.renderMenuElement()}
</Popper>
</button>
);
}
}
export default HierarchicalCheckboxMenu;