-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.tsx
453 lines (414 loc) · 11.4 KB
/
Tree.tsx
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* eslint-disable react/prop-types */
import React, {
useState,
ReactNode,
createContext,
useContext,
useCallback,
CSSProperties,
forwardRef,
memo
} from "react";
import {
TreeNode,
FlatNode,
NodeId,
Node,
TreeNodeSort,
sortTree,
createAlphaNumericSort
} from "./Node";
import { TreeElement } from "./TreeElement";
import { Schema } from "./Schema";
import { useSelectionState, SelectionState } from "./SelectionState";
import { getSelectedNodeIds, toTreeNodes, findTreeNodeById } from "./NodeUtils";
import { useKeyboard } from "./Keyboard";
import { isMoveValid } from "./TreeUtils";
export interface TreeProps {
/**
* The nodes to create a Tree from. This property is not converted into
* a state variable so the <Tree/> should always reflect this value.
*/
nodes: FlatNode[];
/**
* The property on your `FlatNode` which serves as the name. This is text
* the user sees for each node and is typically be called `name` or `title`. This
* is required to support searching in ARIA and is also used to creating a
* sorting function if prop `sortFunction` is not set.
*
* See:
*
* https://www.w3.org/TR/wai-aria-practices/examples/treeview/treeview-1/treeview-1b.html#kbd_label
*/
nameProperty: keyof FlatNode;
/**
* Library users need to implement this and update the nodes with the new value
* and also update their own state with the new nodes so the changes are reflected
* in the <Tree/>.
*
* @param node The node that changed.
* @param property The property that changed
* @param value The new value to be applied to the changed nodes
* @param selection The `SelectionState` at the time of the change
*/
onChange?(
nodes: FlatNode[],
property: keyof FlatNode,
value: any,
selection: SelectionState
): void;
/**
* Library users need to implement this unless `disableCopy` is set to true. This
* is called after copy only. Cut and pasted nodes are handled using `onChange`.
*
* The pasted nodes in a tree structure which should be incroporated to the `nodes`
* prop value after a new `id` property has been assigned to each node and
* the `parentId` has been updated to reference the new parent
* node `id`.
*
* See the example code for a possible implementation of the
* `handlePaste` function.
*
* @param nodes the TreeNode that were copied.
* @param newParentId the `id` if the node they were copied to.
*/
onPaste?(nodes: TreeNode[], newParentId: NodeId): void;
/**
* Listen for selection events.
*
* see `SelectionState`
*
* @param selected the nodes that are selected
*/
onSelectionChange?(selected: SelectionState): void;
/**
* Render a single node however you like. The output of this call will be wrapped
* in an internal `TreeElement` wrapper which is setup for drag and drop.
*
* @param node The `TreeNode` to be rendered. This is created
* form the `FlatNode` provided to the `nodes` prop
*
* @param depth The node's depth in the tree. Useful for apply
* horizontal padding. Zero is root.
*/
renderElement(node: TreeNode, depth: number): JSX.Element;
/**
* Set constraints. See `Schema`.
*/
schema?: Schema;
/**
* Pass a sort function to make the Tree sorted. You can use the exported
* function `createAlphaNumericSort` for most cases. If you don't set this
* prop then an alphanumeric sort is is used based on the `nameProperty` prop.
*/
sortFunction?: TreeNodeSort;
/**
* Disable drag and drop. Default false.
*/
disableDrag?: boolean;
/**
* Disable all selection. Default false.
*/
disableSelection?: boolean;
/**
* Disable multiple selection. Default false.
*/
disableMultiSelection?: boolean;
/**
* Disable support for cutting and pasting nodes. Default false.
*/
disableCut?: boolean;
/**
* Disable support for copying (adding) and pasting nodes. Default false.
*/
disableCopy?: boolean;
/**
* The `id` of the node which should be focused, if any. The tree does
* not ensure this node is visible in an expanded parent or perform any
* scrolling. Default behaviour is not to focus any nodes.
*/
initialFocusedNodeId?: NodeId;
/**
* Override the browser's default drag image.
*
* @param nodes the ids of the Nodes that are being dragged
*/
renderDragImage?(nodes: NodeId[]): HTMLImageElement | HTMLCanvasElement;
/**
* The ARIA label Id. This should be the Id of heading element which explains
* what the tree is used for. This element can be visually hidden if necessary
* but needs to present in accessible applications.
*
* See:
*
* https://www.w3.org/TR/wai-aria-practices/examples/treeview/treeview-1/treeview-1b.html#kbd_label
* https://a11yproject.com/posts/how-to-hide-content/
*
*/
labelledbyId?: string;
}
export interface TreeContextValue {
overId?: NodeId;
dragId?: NodeId;
handleDrag(id?: NodeId): void;
handleOver(id?: NodeId): void;
handleDrop(dropped: NodeId, target?: NodeId): void;
handleToggleCollapse(nodes: Node[]): void;
disableDrag: boolean;
selection: SelectionState;
handleSelectionChange(selection: SelectionState): void;
handleClick(event: React.MouseEvent, node: NodeId): void;
renderDragImage?(nodes: NodeId[]): HTMLImageElement | HTMLCanvasElement;
}
export const TreeContext = createContext<TreeContextValue | undefined>(
undefined
);
export const useTree = () => useContext(TreeContext);
const ListStyle: CSSProperties = {
padding: 0
};
const ListItemStyle: CSSProperties = {
listStyle: "none"
};
/**
* See docs on `TreeProps`.
*/
export const Tree = forwardRef<HTMLUListElement, TreeProps>((props, ref) => {
const {
nodes,
nameProperty,
renderElement,
onChange,
onPaste,
onSelectionChange,
sortFunction,
disableDrag,
schema,
disableSelection,
disableMultiSelection,
disableCut,
disableCopy,
renderDragImage,
initialFocusedNodeId,
labelledbyId
} = props;
const treeNodes = toTreeNodes(nodes);
const [dragId, setDragId] = useState<NodeId>();
const [overId, setOverId] = useState<NodeId>();
if (sortFunction) {
sortTree(treeNodes, sortFunction);
} else {
sortTree(treeNodes, createAlphaNumericSort(nameProperty));
}
const initialSelection =
initialFocusedNodeId !== undefined ? [initialFocusedNodeId!] : [];
const {
selection,
handleClick,
handleSelectionChange,
handleBlur
} = useSelectionState(
treeNodes,
onSelectionChange,
disableSelection,
disableMultiSelection,
initialSelection
);
const selected = [...selection.selected, dragId as NodeId];
const handlePasteNodes = () => {
const target = selection.selected[0];
if (target === undefined) {
return;
}
if (selection.cut.length) {
if (!onChange) {
return;
}
const changed = nodes.filter((node) => selection.cut.includes(node.id));
if (
isMoveValid(
nodes,
changed.map((node) => node.id),
true,
target,
schema
)
) {
onChange(changed, "parentId", target, selection);
}
} else {
if (!onPaste) {
return;
}
const changed = nodes.filter((node) =>
selection.copied.includes(node.id)
);
if (
isMoveValid(
nodes,
changed.map((node) => node.id),
false,
target,
schema
)
) {
const rootNodes = getSelectedNodeIds(treeNodes, selection.copied);
const changedTree = rootNodes.map((id) => {
const result = findTreeNodeById(id, treeNodes);
return result?.node as TreeNode;
});
onPaste(changedTree, target);
}
}
};
const handleToggleCollapse = (nodes: Node[]) => {
if (onChange) {
onChange(nodes, "expanded", !nodes[0].expanded, selection);
}
};
useKeyboard(
treeNodes,
selection,
nameProperty,
handleSelectionChange,
handlePasteNodes,
handleToggleCollapse,
!!disableCut,
!!disableCopy
);
const handleDrop = (dropped: NodeId, target?: NodeId) => {
if (target !== undefined) {
const node = nodes.find((node) => String(node.id) === String(dropped));
if (node && String(node.parentId) !== String(target)) {
if (onChange) {
const changedIds = getSelectedNodeIds(treeNodes, [
...selection.selected,
dragId as NodeId
]);
const changed = changedIds.map((id) =>
nodes.find((node) => node.id === id)
) as FlatNode[];
onChange(changed, "parentId", target, selection);
}
}
}
setOverId(undefined);
setDragId(undefined);
};
const handleOver = (overId?: NodeId) => {
if (isMoveValid(nodes, selected, true, overId, schema)) {
setOverId(overId);
} else {
setOverId(undefined);
}
};
const renderTree = useCallback(
(nodes: TreeNode[], depth = 0) => {
const result: ReactNode[] = [];
nodes.forEach((node, index) => {
const nodeItem = renderElement(node, depth);
let children: ReactNode[] = [];
const expanded = node.expanded === undefined || node.expanded;
if (expanded) {
children = renderTree(node.children, depth + 1);
}
const attrs: any = {};
if (node.id === overId && overId !== node.parentId) {
attrs["data-rt-drop-valid"] = true;
}
if (node.children.length) {
attrs["aria-expanded"] = expanded;
}
const focused = selection.focused && node.id === selection.selected[0];
result.push(
<ListItem
node={node}
index={index}
depth={depth}
setsize={nodes.length}
focused={focused}
nodeItem={nodeItem}
key={node.id}
{...attrs}
>
{children}
</ListItem>
);
});
return result;
},
[overId, selection, renderElement]
);
const tree = renderTree(treeNodes);
const value: TreeContextValue = {
overId,
handleDrag: setDragId,
handleOver,
handleDrop,
handleToggleCollapse,
disableDrag: !!disableDrag,
selection,
handleSelectionChange,
handleClick,
renderDragImage,
dragId
};
return (
<TreeContext.Provider value={value}>
<ul
ref={ref}
data-rt-tree
role="tree"
style={ListStyle}
onBlur={handleBlur}
aria-labelledby={labelledbyId}
>
{tree}
</ul>
</TreeContext.Provider>
);
});
interface ListItemProps {
node: TreeNode;
index: number;
depth: number;
setsize: number;
focused: boolean;
nodeItem: JSX.Element;
children?: ReactNode;
[key: string]: any;
}
const ListItem = memo((props: ListItemProps) => {
const {
node,
index,
depth,
setsize,
focused,
nodeItem,
children,
...rest
} = props;
return (
<li
data-rt-element-wrapper={node.id}
role="treeitem"
aria-level={depth + 1}
aria-setsize={setsize}
aria-posinset={index + 1}
tabIndex={focused ? 0 : -1}
{...rest}
style={ListItemStyle}
key={node.id}
>
<TreeElement node={node} depth={depth}>
{nodeItem}
</TreeElement>
{children && (
<ul role="group" style={ListStyle}>
{children}
</ul>
)}
</li>
);
});