-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathTreeView.js
340 lines (302 loc) · 9.1 KB
/
TreeView.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import TreeViewContext from './TreeViewContext';
import { withStyles } from '@material-ui/core/styles';
export const styles = {
/* Styles applied to the root element. */
root: {
padding: 0,
margin: 0,
listStyle: 'none',
},
};
const defaultExpandedDefault = [];
const TreeView = React.forwardRef(function TreeView(props, ref) {
const {
children,
classes,
className,
defaultCollapseIcon,
defaultEndIcon,
defaultExpanded = defaultExpandedDefault,
defaultExpandIcon,
defaultParentIcon,
onNodeToggle,
...other
} = props;
const [expanded, setExpanded] = React.useState(defaultExpanded);
const [tabable, setTabable] = React.useState(null);
const [focused, setFocused] = React.useState(null);
const firstNode = React.useRef(null);
const nodeMap = React.useRef({});
const firstCharMap = React.useRef({});
const isExpanded = React.useCallback(id => expanded.indexOf(id) !== -1, [expanded]);
const isTabable = id => tabable === id;
const isFocused = id => focused === id;
React.useEffect(() => {
nodeMap.current = {};
const childIds = React.Children.map(children, child => child.props.nodeId);
nodeMap.current[-1] = { parent: null, children: childIds };
(childIds || []).forEach((id, index) => {
if (index === 0) {
firstNode.current = id;
setTabable(id);
}
nodeMap.current[id] = { parent: null };
});
}, [children]);
const getLastNode = React.useCallback(
id => {
const map = nodeMap.current[id];
if (isExpanded(id) && map.children && map.children.length > 0) {
return getLastNode(map.children[map.children.length - 1]);
}
return id;
},
[isExpanded],
);
const focus = id => {
if (id) {
setTabable(id);
}
setFocused(id);
};
const getNextNode = (id, end) => {
const map = nodeMap.current[id];
const parent = nodeMap.current[map.parent];
if (!end) {
if (isExpanded(id)) {
return map.children[0];
}
}
if (parent) {
const nodeIndex = parent.children.indexOf(id);
const nextIndex = nodeIndex + 1;
if (parent.children.length > nextIndex) {
return parent.children[nextIndex];
}
return getNextNode(parent.id, true);
}
const topLevelNodes = nodeMap.current[-1].children;
const topLevelNodeIndex = topLevelNodes.indexOf(id);
if (topLevelNodeIndex !== -1 && topLevelNodeIndex !== topLevelNodes.length - 1) {
return topLevelNodes[topLevelNodeIndex + 1];
}
return null;
};
const getPreviousNode = id => {
const map = nodeMap.current[id];
const parent = nodeMap.current[map.parent];
if (parent) {
const nodeIndex = parent.children.indexOf(id);
if (nodeIndex !== 0) {
const nextIndex = nodeIndex - 1;
return getLastNode(parent.children[nextIndex]);
}
return parent.id;
}
const topLevelNodes = nodeMap.current[-1].children;
const topLevelNodeIndex = topLevelNodes.indexOf(id);
if (topLevelNodeIndex > 0) {
return getLastNode(topLevelNodes[topLevelNodeIndex - 1]);
}
return null;
};
const focusNextNode = id => {
const nextNode = getNextNode(id);
if (nextNode) {
focus(nextNode);
}
};
const focusPreviousNode = id => {
const previousNode = getPreviousNode(id);
if (previousNode) {
focus(previousNode);
}
};
const focusFirstNode = () => {
if (firstNode.current) {
focus(firstNode.current);
}
};
const focusLastNode = () => {
const topLevelNodes = nodeMap.current[-1].children;
const lastNode = getLastNode(topLevelNodes[topLevelNodes.length - 1]);
focus(lastNode);
};
const toggle = (value = focused) => {
setExpanded(prevExpanded => {
let newExpanded;
if (prevExpanded.indexOf(value) !== -1) {
newExpanded = prevExpanded.filter(id => id !== value);
setTabable(oldTabable => {
const map = nodeMap.current[oldTabable];
if (oldTabable && (map && map.parent ? map.parent.id : null) === value) {
return value;
}
return oldTabable;
});
} else {
newExpanded = [value, ...prevExpanded];
}
if (onNodeToggle) {
onNodeToggle(value, newExpanded.indexOf(value) !== -1);
}
return newExpanded;
});
};
const expandAllSiblings = id => {
const map = nodeMap.current[id];
const parent = nodeMap.current[map.parent];
let diff;
if (parent) {
diff = parent.children.filter(child => !isExpanded(child));
} else {
const topLevelNodes = nodeMap.current[-1].children;
diff = topLevelNodes.filter(node => !isExpanded(node));
}
setExpanded(oldExpanded => [...oldExpanded, ...diff]);
};
const handleLeftArrow = (id, event) => {
let flag = false;
if (isExpanded(id)) {
toggle(id);
flag = true;
} else {
const parent = nodeMap.current[id].parent;
if (parent) {
focus(parent);
flag = true;
}
}
if (flag && event) {
event.preventDefault();
event.stopPropagation();
}
};
const getIndexFirstChars = (firstChars, startIndex, char) => {
for (let i = startIndex; i < firstChars.length; i += 1) {
if (char === firstChars[i]) {
return i;
}
}
return -1;
};
const setFocusByFirstCharacter = (id, char) => {
let start;
let index;
const lowercaseChar = char.toLowerCase();
const firstCharIds = [];
const firstChars = [];
// This really only works since the ids are strings
Object.entries(firstCharMap.current).forEach(([nodeId, firstChar]) => {
const map = nodeMap.current[nodeId];
const visible = map.parent ? isExpanded(map.parent) : true;
if (visible) {
firstCharIds.push(nodeId);
firstChars.push(firstChar);
}
});
// Get start index for search based on position of currentItem
start = firstCharIds.indexOf(id) + 1;
if (start === nodeMap.current.length) {
start = 0;
}
// Check remaining slots in the menu
index = getIndexFirstChars(firstChars, start, lowercaseChar);
// If not found in remaining slots, check from beginning
if (index === -1) {
index = getIndexFirstChars(firstChars, 0, lowercaseChar);
}
// If match was found...
if (index > -1) {
focus(firstCharIds[index]);
}
};
const handleNodeMap = (id, childrenIds) => {
const currentMap = nodeMap.current[id];
nodeMap.current[id] = { ...currentMap, children: childrenIds, id };
(childrenIds || []).forEach(childId => {
const currentChildMap = nodeMap.current[childId];
nodeMap.current[childId] = { ...currentChildMap, parent: id, id: childId };
});
};
const handleFirstChars = (id, firstChar) => {
firstCharMap.current[id] = firstChar;
};
return (
<TreeViewContext.Provider
value={{
expandAllSiblings,
focus,
focusFirstNode,
focusLastNode,
focusNextNode,
focusPreviousNode,
handleFirstChars,
handleLeftArrow,
handleNodeMap,
icons: { defaultCollapseIcon, defaultExpandIcon, defaultParentIcon, defaultEndIcon },
isExpanded,
isFocused,
isTabable,
setFocusByFirstCharacter,
toggle,
}}
>
<ul role="tree" className={clsx(classes.root, className)} ref={ref} {...other}>
{children}
</ul>
</TreeViewContext.Provider>
);
});
TreeView.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The default icon used to collapse the node.
*/
defaultCollapseIcon: PropTypes.node,
/**
* The default icon displayed next to a end node. This is applied to all
* tree nodes and can be overridden by the TreeItem `icon` prop.
*/
defaultEndIcon: PropTypes.node,
/**
* Expanded node ids.
*/
defaultExpanded: PropTypes.arrayOf(PropTypes.string),
/**
* The default icon used to expand the node.
*/
defaultExpandIcon: PropTypes.node,
/**
* The default icon displayed next to a parent node. This is applied to all
* parent nodes and can be overridden by the TreeItem `icon` prop.
*/
defaultParentIcon: PropTypes.node,
/**
* Callback fired when a `TreeItem` is expanded/collapsed.
*
* @param {string} nodeId The id of the toggled node.
* @param {boolean} expanded The node status - If `true` the node was expanded. If `false` the node was collapsed.
*/
onNodeToggle: PropTypes.func,
};
export default withStyles(styles, { name: 'MuiTreeView' })(TreeView);