react-arborist 3.7.0 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/main/components/cursor.js +1 -2
- package/dist/main/components/default-container.js +31 -2
- package/dist/main/components/default-cursor.js +1 -1
- package/dist/main/components/default-drag-preview.d.ts +1 -1
- package/dist/main/components/default-drag-preview.js +1 -1
- package/dist/main/components/default-row.d.ts +1 -1
- package/dist/main/components/default-row.js +1 -1
- package/dist/main/components/list-outer-element.js +1 -1
- package/dist/main/components/provider.d.ts +1 -1
- package/dist/main/components/provider.js +2 -2
- package/dist/main/components/provider.test.js +70 -0
- package/dist/main/components/row-container.d.ts +1 -1
- package/dist/main/components/row-container.js +2 -3
- package/dist/main/dnd/drag-hook.d.ts +2 -0
- package/dist/main/dnd/drag-hook.js +13 -4
- package/dist/main/dnd/drag-hook.test.d.ts +1 -0
- package/dist/main/dnd/drag-hook.test.js +19 -0
- package/dist/main/hooks/use-validated-props.js +1 -2
- package/dist/main/interfaces/node-api.js +4 -1
- package/dist/main/interfaces/tree-api.d.ts +27 -5
- package/dist/main/interfaces/tree-api.js +98 -14
- package/dist/main/interfaces/tree-api.test.js +31 -0
- package/dist/main/state/drag-slice.js +1 -2
- package/dist/main/types/dnd.d.ts +3 -1
- package/dist/main/types/state.d.ts +1 -1
- package/dist/main/types/tree-props.d.ts +4 -1
- package/dist/module/components/cursor.js +1 -2
- package/dist/module/components/default-container.js +32 -3
- package/dist/module/components/default-cursor.js +1 -1
- package/dist/module/components/default-drag-preview.d.ts +1 -1
- package/dist/module/components/default-drag-preview.js +1 -1
- package/dist/module/components/default-row.d.ts +1 -1
- package/dist/module/components/default-row.js +1 -1
- package/dist/module/components/list-outer-element.js +1 -1
- package/dist/module/components/provider.d.ts +1 -1
- package/dist/module/components/provider.js +4 -4
- package/dist/module/components/provider.test.js +71 -1
- package/dist/module/components/row-container.d.ts +1 -1
- package/dist/module/components/row-container.js +2 -3
- package/dist/module/dnd/compute-drop.js +1 -1
- package/dist/module/dnd/drag-hook.d.ts +2 -0
- package/dist/module/dnd/drag-hook.js +12 -4
- package/dist/module/dnd/drag-hook.test.d.ts +1 -0
- package/dist/module/dnd/drag-hook.test.js +17 -0
- package/dist/module/hooks/use-validated-props.js +1 -2
- package/dist/module/interfaces/node-api.js +4 -1
- package/dist/module/interfaces/tree-api.d.ts +27 -5
- package/dist/module/interfaces/tree-api.js +98 -14
- package/dist/module/interfaces/tree-api.test.js +31 -0
- package/dist/module/state/drag-slice.js +1 -2
- package/dist/module/types/dnd.d.ts +3 -1
- package/dist/module/types/state.d.ts +1 -1
- package/dist/module/types/tree-props.d.ts +4 -1
- package/package.json +27 -27
- package/src/components/cursor.tsx +1 -2
- package/src/components/default-container.tsx +40 -19
- package/src/components/default-cursor.tsx +1 -5
- package/src/components/default-drag-preview.tsx +3 -16
- package/src/components/default-node.tsx +0 -1
- package/src/components/default-row.tsx +2 -13
- package/src/components/drag-preview-container.tsx +1 -1
- package/src/components/list-inner-element.tsx +1 -1
- package/src/components/list-outer-element.tsx +2 -3
- package/src/components/provider.test.tsx +85 -9
- package/src/components/provider.tsx +8 -23
- package/src/components/row-container.tsx +4 -9
- package/src/components/tree.tsx +2 -6
- package/src/context.ts +2 -3
- package/src/data/create-index.ts +0 -1
- package/src/data/create-list.ts +1 -2
- package/src/data/create-root.ts +2 -9
- package/src/data/simple-tree.ts +5 -3
- package/src/dnd/compute-drop.ts +6 -15
- package/src/dnd/drag-hook.test.ts +22 -0
- package/src/dnd/drag-hook.ts +15 -6
- package/src/dnd/measure-hover.ts +2 -6
- package/src/dnd/outer-drop-hook.ts +1 -1
- package/src/hooks/use-fresh-node.ts +0 -1
- package/src/hooks/use-simple-tree.ts +2 -8
- package/src/hooks/use-validated-props.ts +4 -8
- package/src/interfaces/node-api.ts +2 -2
- package/src/interfaces/tree-api.test.ts +35 -0
- package/src/interfaces/tree-api.ts +103 -36
- package/src/state/dnd-slice.ts +1 -1
- package/src/state/drag-slice.ts +2 -5
- package/src/state/edit-slice.ts +1 -4
- package/src/state/focus-slice.ts +1 -1
- package/src/state/open-slice.ts +2 -5
- package/src/state/selection-slice.ts +2 -6
- package/src/types/dnd.ts +6 -1
- package/src/types/handlers.ts +1 -3
- package/src/types/renderers.ts +0 -1
- package/src/types/state.ts +1 -1
- package/src/types/tree-props.ts +15 -10
- package/src/types/utils.ts +2 -3
- package/src/utils.ts +5 -14
|
@@ -56,6 +56,46 @@ class TreeApi {
|
|
|
56
56
|
this.listEl = listEl;
|
|
57
57
|
this.visibleStartIndex = 0;
|
|
58
58
|
this.visibleStopIndex = 0;
|
|
59
|
+
/* Memoized prefix-sum of row heights; only used for variable heights. */
|
|
60
|
+
this.rowOffsets = null;
|
|
61
|
+
/**
|
|
62
|
+
* The height of the row at `index`, evaluating the `rowHeight` function if
|
|
63
|
+
* given. Falls back to the default height for an out-of-range index so this
|
|
64
|
+
* never feeds an invalid `0` to react-window's `itemSize`.
|
|
65
|
+
*/
|
|
66
|
+
this.rowHeightAt = (index) => {
|
|
67
|
+
const rowHeight = this.props.rowHeight;
|
|
68
|
+
if (typeof rowHeight === "function") {
|
|
69
|
+
const node = this.at(index);
|
|
70
|
+
return node ? rowHeight(node) : this.rowHeight;
|
|
71
|
+
}
|
|
72
|
+
return rowHeight !== null && rowHeight !== void 0 ? rowHeight : 24;
|
|
73
|
+
};
|
|
74
|
+
/** The pixel offset of the top of the row at `index` from the top of the list. */
|
|
75
|
+
this.rowTopPosition = (index) => {
|
|
76
|
+
/* Fixed heights: O(1). */
|
|
77
|
+
if (typeof this.props.rowHeight !== "function") {
|
|
78
|
+
return index * this.rowHeight;
|
|
79
|
+
}
|
|
80
|
+
/* Variable heights: O(1) amortized via a memoized prefix sum. */
|
|
81
|
+
const offsets = this.getRowOffsets();
|
|
82
|
+
const clamped = Math.max(0, Math.min(index, offsets.length - 1));
|
|
83
|
+
return offsets[clamped];
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Tell the underlying virtualized list to recompute row heights at and after
|
|
87
|
+
* `index`. Call this if a `rowHeight` function's output changes for reasons
|
|
88
|
+
* the tree can't observe (e.g. external state).
|
|
89
|
+
*/
|
|
90
|
+
this.redrawList = (afterIndex = 0) => {
|
|
91
|
+
this.rowOffsets = null;
|
|
92
|
+
/* Only the VariableSizeList (function rowHeight) caches measurements; a
|
|
93
|
+
FixedSizeList has constant heights and nothing to recompute. */
|
|
94
|
+
const list = this.list.current;
|
|
95
|
+
if (list && "resetAfterIndex" in list) {
|
|
96
|
+
list.resetAfterIndex(Math.max(0, afterIndex));
|
|
97
|
+
}
|
|
98
|
+
};
|
|
59
99
|
/* Changes here must also be made in update() */
|
|
60
100
|
this.root = (0, create_root_1.createRoot)(this);
|
|
61
101
|
this.visibleNodes = (0, create_list_1.createList)(this);
|
|
@@ -67,6 +107,18 @@ class TreeApi {
|
|
|
67
107
|
this.root = (0, create_root_1.createRoot)(this);
|
|
68
108
|
this.visibleNodes = (0, create_list_1.createList)(this);
|
|
69
109
|
this.idToIndex = (0, create_index_1.createIndex)(this.visibleNodes);
|
|
110
|
+
this.rowOffsets = null;
|
|
111
|
+
/* Variable-height mode renders a VariableSizeList, which caches item
|
|
112
|
+
measurements by index and never invalidates them on its own. When the
|
|
113
|
+
visible nodes change (insert/remove/reorder), those cached sizes belong
|
|
114
|
+
to the wrong rows, so drop them. Fixed-height mode renders a
|
|
115
|
+
FixedSizeList (no cache, nothing to reset). update() runs during render,
|
|
116
|
+
so pass shouldForceUpdate=false: the in-progress render repaints the list
|
|
117
|
+
and a forceUpdate here would warn about setting state mid-render. */
|
|
118
|
+
const list = this.list.current;
|
|
119
|
+
if (list && "resetAfterIndex" in list) {
|
|
120
|
+
list.resetAfterIndex(0, false);
|
|
121
|
+
}
|
|
70
122
|
}
|
|
71
123
|
/* Store helpers */
|
|
72
124
|
dispatch(action) {
|
|
@@ -91,9 +143,24 @@ class TreeApi {
|
|
|
91
143
|
var _a;
|
|
92
144
|
return (_a = this.props.indent) !== null && _a !== void 0 ? _a : 24;
|
|
93
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* The fixed row height. When a `rowHeight` function is supplied for variable
|
|
148
|
+
* heights, this returns the default (24); use `rowHeightAt(index)` to get the
|
|
149
|
+
* height of a specific row.
|
|
150
|
+
*/
|
|
94
151
|
get rowHeight() {
|
|
95
|
-
|
|
96
|
-
|
|
152
|
+
return typeof this.props.rowHeight === "number" ? this.props.rowHeight : 24;
|
|
153
|
+
}
|
|
154
|
+
/** Lazily-built prefix sum where offsets[i] is the top of row i. */
|
|
155
|
+
getRowOffsets() {
|
|
156
|
+
if (this.rowOffsets)
|
|
157
|
+
return this.rowOffsets;
|
|
158
|
+
const offsets = [0];
|
|
159
|
+
for (let i = 0; i < this.visibleNodes.length; i++) {
|
|
160
|
+
offsets.push(offsets[i] + this.rowHeightAt(i));
|
|
161
|
+
}
|
|
162
|
+
this.rowOffsets = offsets;
|
|
163
|
+
return offsets;
|
|
97
164
|
}
|
|
98
165
|
get overscanCount() {
|
|
99
166
|
var _a;
|
|
@@ -195,9 +262,7 @@ class TreeApi {
|
|
|
195
262
|
create() {
|
|
196
263
|
return __awaiter(this, arguments, void 0, function* (opts = {}) {
|
|
197
264
|
var _a, _b;
|
|
198
|
-
const parentId = opts.parentId === undefined
|
|
199
|
-
? utils.getInsertParentId(this)
|
|
200
|
-
: opts.parentId;
|
|
265
|
+
const parentId = opts.parentId === undefined ? utils.getInsertParentId(this) : opts.parentId;
|
|
201
266
|
const index = (_a = opts.index) !== null && _a !== void 0 ? _a : utils.getInsertIndex(this);
|
|
202
267
|
const type = (_b = opts.type) !== null && _b !== void 0 ? _b : "leaf";
|
|
203
268
|
const data = yield safeRun(this.props.onCreate, {
|
|
@@ -224,20 +289,26 @@ class TreeApi {
|
|
|
224
289
|
const idents = Array.isArray(node) ? node : [node];
|
|
225
290
|
const ids = idents.map(identify);
|
|
226
291
|
const nodes = ids.map((id) => this.get(id)).filter((n) => !!n);
|
|
292
|
+
/* Guard against Math.min(...[]) === Infinity when no ids resolve to nodes. */
|
|
293
|
+
const fromIndex = nodes.length ? Math.min(...nodes.map((n) => { var _a; return (_a = n.rowIndex) !== null && _a !== void 0 ? _a : 0; })) : 0;
|
|
227
294
|
yield safeRun(this.props.onDelete, { nodes, ids });
|
|
295
|
+
this.redrawList(fromIndex);
|
|
228
296
|
});
|
|
229
297
|
}
|
|
230
298
|
edit(node) {
|
|
299
|
+
var _a, _b;
|
|
231
300
|
const id = identify(node);
|
|
232
301
|
this.resolveEdit({ cancelled: true });
|
|
233
302
|
this.scrollTo(id);
|
|
234
303
|
this.dispatch((0, edit_slice_1.edit)(id));
|
|
304
|
+
this.redrawList((_b = (_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0);
|
|
235
305
|
return new Promise((resolve) => {
|
|
236
306
|
TreeApi.editPromise = resolve;
|
|
237
307
|
});
|
|
238
308
|
}
|
|
239
309
|
submit(identity, value) {
|
|
240
310
|
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
+
var _a, _b;
|
|
241
312
|
if (!identity)
|
|
242
313
|
return;
|
|
243
314
|
const id = identify(identity);
|
|
@@ -248,12 +319,14 @@ class TreeApi {
|
|
|
248
319
|
});
|
|
249
320
|
this.dispatch((0, edit_slice_1.edit)(null));
|
|
250
321
|
this.resolveEdit({ cancelled: false, value });
|
|
322
|
+
this.redrawList((_b = (_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0);
|
|
251
323
|
setTimeout(() => this.onFocus()); // Return focus to element;
|
|
252
324
|
});
|
|
253
325
|
}
|
|
254
326
|
reset() {
|
|
255
327
|
this.dispatch((0, edit_slice_1.edit)(null));
|
|
256
328
|
this.resolveEdit({ cancelled: true });
|
|
329
|
+
this.redrawList();
|
|
257
330
|
setTimeout(() => this.onFocus()); // Return focus to element;
|
|
258
331
|
}
|
|
259
332
|
activate(id) {
|
|
@@ -433,9 +506,7 @@ class TreeApi {
|
|
|
433
506
|
return this.state.dnd.cursor.type === "highlight";
|
|
434
507
|
}
|
|
435
508
|
get dragNodes() {
|
|
436
|
-
return this.state.dnd.dragIds
|
|
437
|
-
.map((id) => this.get(id))
|
|
438
|
-
.filter((n) => !!n);
|
|
509
|
+
return this.state.dnd.dragIds.map((id) => this.get(id)).filter((n) => !!n);
|
|
439
510
|
}
|
|
440
511
|
get dragNode() {
|
|
441
512
|
return this.get(this.state.nodes.drag.id);
|
|
@@ -487,22 +558,28 @@ class TreeApi {
|
|
|
487
558
|
this.dispatch(dnd_slice_1.actions.cursor(cursor));
|
|
488
559
|
}
|
|
489
560
|
/* Visibility */
|
|
490
|
-
open(identity) {
|
|
561
|
+
open(identity, redraw = true) {
|
|
562
|
+
var _a, _b;
|
|
491
563
|
const id = identifyNull(identity);
|
|
492
564
|
if (!id)
|
|
493
565
|
return;
|
|
494
566
|
if (this.isOpen(id))
|
|
495
567
|
return;
|
|
496
568
|
this.dispatch(open_slice_1.actions.open(id, this.isFiltered));
|
|
569
|
+
if (redraw)
|
|
570
|
+
this.redrawList((_b = (_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0);
|
|
497
571
|
safeRun(this.props.onToggle, id);
|
|
498
572
|
}
|
|
499
|
-
close(identity) {
|
|
573
|
+
close(identity, redraw = true) {
|
|
574
|
+
var _a, _b;
|
|
500
575
|
const id = identifyNull(identity);
|
|
501
576
|
if (!id)
|
|
502
577
|
return;
|
|
503
578
|
if (!this.isOpen(id))
|
|
504
579
|
return;
|
|
505
580
|
this.dispatch(open_slice_1.actions.close(id, this.isFiltered));
|
|
581
|
+
if (redraw)
|
|
582
|
+
this.redrawList((_b = (_a = this.get(id)) === null || _a === void 0 ? void 0 : _a.rowIndex) !== null && _b !== void 0 ? _b : 0);
|
|
506
583
|
safeRun(this.props.onToggle, id);
|
|
507
584
|
}
|
|
508
585
|
toggle(identity) {
|
|
@@ -518,9 +595,10 @@ class TreeApi {
|
|
|
518
595
|
const node = utils.dfs(this.root, id);
|
|
519
596
|
let parent = node === null || node === void 0 ? void 0 : node.parent;
|
|
520
597
|
while (parent) {
|
|
521
|
-
this.open(parent.id);
|
|
598
|
+
this.open(parent.id, false);
|
|
522
599
|
parent = parent.parent;
|
|
523
600
|
}
|
|
601
|
+
this.redrawList();
|
|
524
602
|
}
|
|
525
603
|
openSiblings(node) {
|
|
526
604
|
const parent = node.parent;
|
|
@@ -531,23 +609,29 @@ class TreeApi {
|
|
|
531
609
|
const isOpen = node.isOpen;
|
|
532
610
|
for (let sibling of parent.children) {
|
|
533
611
|
if (sibling.isInternal) {
|
|
534
|
-
|
|
612
|
+
if (isOpen)
|
|
613
|
+
this.close(sibling.id, false);
|
|
614
|
+
else
|
|
615
|
+
this.open(sibling.id, false);
|
|
535
616
|
}
|
|
536
617
|
}
|
|
618
|
+
this.redrawList();
|
|
537
619
|
this.scrollTo(this.focusedNode);
|
|
538
620
|
}
|
|
539
621
|
}
|
|
540
622
|
openAll() {
|
|
541
623
|
utils.walk(this.root, (node) => {
|
|
542
624
|
if (node.isInternal)
|
|
543
|
-
|
|
625
|
+
this.open(node.id, false);
|
|
544
626
|
});
|
|
627
|
+
this.redrawList();
|
|
545
628
|
}
|
|
546
629
|
closeAll() {
|
|
547
630
|
utils.walk(this.root, (node) => {
|
|
548
631
|
if (node.isInternal)
|
|
549
|
-
|
|
632
|
+
this.close(node.id, false);
|
|
550
633
|
});
|
|
634
|
+
this.redrawList();
|
|
551
635
|
}
|
|
552
636
|
/* Scrolling */
|
|
553
637
|
scrollTo(identity, align = "smart") {
|
|
@@ -12,3 +12,34 @@ test("tree.canDrop()", () => {
|
|
|
12
12
|
expect(setupApi({ disableDrop: () => false }).canDrop()).toBe(true);
|
|
13
13
|
expect(setupApi({ disableDrop: false }).canDrop()).toBe(true);
|
|
14
14
|
});
|
|
15
|
+
const rowData = [{ id: "a" }, { id: "b" }, { id: "c" }];
|
|
16
|
+
test("rowHeight defaults to 24", () => {
|
|
17
|
+
const api = setupApi({});
|
|
18
|
+
expect(api.rowHeight).toBe(24);
|
|
19
|
+
expect(api.rowHeightAt(0)).toBe(24);
|
|
20
|
+
});
|
|
21
|
+
test("fixed numeric rowHeight", () => {
|
|
22
|
+
const api = setupApi({ data: rowData, rowHeight: 30 });
|
|
23
|
+
expect(api.rowHeight).toBe(30);
|
|
24
|
+
expect(api.rowHeightAt(0)).toBe(30);
|
|
25
|
+
expect(api.rowTopPosition(0)).toBe(0);
|
|
26
|
+
expect(api.rowTopPosition(2)).toBe(60);
|
|
27
|
+
expect(api.rowTopPosition(3)).toBe(90); // total list height
|
|
28
|
+
});
|
|
29
|
+
test("variable rowHeight function", () => {
|
|
30
|
+
const heights = { a: 10, b: 20, c: 40 };
|
|
31
|
+
const api = setupApi({
|
|
32
|
+
data: rowData,
|
|
33
|
+
rowHeight: (node) => heights[node.id],
|
|
34
|
+
});
|
|
35
|
+
// The back-compat getter falls back to the default for variable heights.
|
|
36
|
+
expect(api.rowHeight).toBe(24);
|
|
37
|
+
expect(api.rowHeightAt(0)).toBe(10);
|
|
38
|
+
expect(api.rowHeightAt(1)).toBe(20);
|
|
39
|
+
expect(api.rowTopPosition(0)).toBe(0);
|
|
40
|
+
expect(api.rowTopPosition(1)).toBe(10);
|
|
41
|
+
expect(api.rowTopPosition(2)).toBe(30);
|
|
42
|
+
expect(api.rowTopPosition(3)).toBe(70); // total list height
|
|
43
|
+
// Out-of-range index falls back to the default height, never an invalid 0.
|
|
44
|
+
expect(api.rowHeightAt(99)).toBe(24);
|
|
45
|
+
});
|
|
@@ -10,8 +10,7 @@ function reducer(state = (0, initial_1.initialState)().nodes.drag, action) {
|
|
|
10
10
|
case "DND_DRAG_END":
|
|
11
11
|
return Object.assign(Object.assign({}, state), { id: null, destinationParentId: null, destinationIndex: null, selectedIds: [] });
|
|
12
12
|
case "DND_HOVERING":
|
|
13
|
-
if (action.parentId !== state.destinationParentId ||
|
|
14
|
-
action.index != state.destinationIndex) {
|
|
13
|
+
if (action.parentId !== state.destinationParentId || action.index != state.destinationIndex) {
|
|
15
14
|
return Object.assign(Object.assign({}, state), { destinationParentId: action.parentId, destinationIndex: action.index });
|
|
16
15
|
}
|
|
17
16
|
else {
|
package/dist/main/types/dnd.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { NodeApi } from "../interfaces/node-api";
|
|
2
|
-
export type NodeState = typeof NodeApi.prototype["state"];
|
|
2
|
+
export type NodeState = (typeof NodeApi.prototype)["state"];
|
|
@@ -6,6 +6,8 @@ import { ListOnScrollProps, CommonProps as ReactWindowCommonProps } from "react-
|
|
|
6
6
|
import { NodeApi } from "../interfaces/node-api";
|
|
7
7
|
import { OpenMap } from "../state/open-slice";
|
|
8
8
|
import { useDragDropManager, DndProviderProps } from "react-dnd";
|
|
9
|
+
/** Returns the height in pixels for a given node's row. */
|
|
10
|
+
export type RowHeightAccessor<T> = (node: NodeApi<T>) => number;
|
|
9
11
|
export interface TreeProps<T> {
|
|
10
12
|
data?: readonly T[];
|
|
11
13
|
initialData?: readonly T[];
|
|
@@ -18,7 +20,7 @@ export interface TreeProps<T> {
|
|
|
18
20
|
renderDragPreview?: ElementType<renderers.DragPreviewProps>;
|
|
19
21
|
renderCursor?: ElementType<renderers.CursorProps>;
|
|
20
22
|
renderContainer?: ElementType<{}>;
|
|
21
|
-
rowHeight?: number
|
|
23
|
+
rowHeight?: number | RowHeightAccessor<T>;
|
|
22
24
|
overscanCount?: number;
|
|
23
25
|
width?: number | string;
|
|
24
26
|
height?: number;
|
|
@@ -57,6 +59,7 @@ export interface TreeProps<T> {
|
|
|
57
59
|
backend: unknown;
|
|
58
60
|
}>["backend"];
|
|
59
61
|
dndManager?: ReturnType<typeof useDragDropManager>;
|
|
62
|
+
dragType?: string | ((node: NodeApi<T>) => string);
|
|
60
63
|
outerElementType?: ReactWindowCommonProps["outerElementType"];
|
|
61
64
|
innerElementType?: ReactWindowCommonProps["innerElementType"];
|
|
62
65
|
}
|
|
@@ -8,8 +8,7 @@ export function Cursor() {
|
|
|
8
8
|
if (!cursor || cursor.type !== "line")
|
|
9
9
|
return null;
|
|
10
10
|
const indent = tree.indent;
|
|
11
|
-
const top = tree.
|
|
12
|
-
((_b = (_a = tree.props.padding) !== null && _a !== void 0 ? _a : tree.props.paddingTop) !== null && _b !== void 0 ? _b : 0);
|
|
11
|
+
const top = tree.rowTopPosition(cursor.index) + ((_b = (_a = tree.props.padding) !== null && _a !== void 0 ? _a : tree.props.paddingTop) !== null && _b !== void 0 ? _b : 0);
|
|
13
12
|
const left = indent * cursor.level;
|
|
14
13
|
const Cursor = tree.renderCursor;
|
|
15
14
|
return _jsx(Cursor, { top, left, indent });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { FixedSizeList } from "react-window";
|
|
2
|
+
import { FixedSizeList, VariableSizeList } from "react-window";
|
|
3
3
|
import { useDataUpdates, useTreeApi } from "../context";
|
|
4
4
|
import { focusNextElement, focusPrevElement } from "../utils";
|
|
5
5
|
import { ListOuterElement } from "./list-outer-element";
|
|
@@ -13,7 +13,6 @@ let timeoutId = null;
|
|
|
13
13
|
* the event handler. Future clean up welcome.
|
|
14
14
|
*/
|
|
15
15
|
export function DefaultContainer() {
|
|
16
|
-
var _a, _b;
|
|
17
16
|
useDataUpdates();
|
|
18
17
|
const tree = useTreeApi();
|
|
19
18
|
return (_jsx("div", { role: "tree", style: {
|
|
@@ -231,5 +230,35 @@ export function DefaultContainer() {
|
|
|
231
230
|
});
|
|
232
231
|
if (node)
|
|
233
232
|
tree.focus(node.id);
|
|
234
|
-
}, children: _jsx(
|
|
233
|
+
}, children: _jsx(List, {}) }));
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Fixed-height trees (numeric rowHeight) render a FixedSizeList, preserving the
|
|
237
|
+
* original O(1) layout and avoiding VariableSizeList's measurement cache. Only
|
|
238
|
+
* the function form, which needs per-row heights, uses VariableSizeList.
|
|
239
|
+
*/
|
|
240
|
+
function List() {
|
|
241
|
+
var _a, _b;
|
|
242
|
+
const tree = useTreeApi();
|
|
243
|
+
const commonProps = {
|
|
244
|
+
className: tree.props.className,
|
|
245
|
+
outerRef: tree.listEl,
|
|
246
|
+
itemCount: tree.visibleNodes.length,
|
|
247
|
+
height: tree.height,
|
|
248
|
+
width: tree.width,
|
|
249
|
+
overscanCount: tree.overscanCount,
|
|
250
|
+
itemKey: (index) => { var _a; return ((_a = tree.visibleNodes[index]) === null || _a === void 0 ? void 0 : _a.id) || index; },
|
|
251
|
+
outerElementType: (_a = tree.props.outerElementType) !== null && _a !== void 0 ? _a : ListOuterElement,
|
|
252
|
+
innerElementType: (_b = tree.props.innerElementType) !== null && _b !== void 0 ? _b : ListInnerElement,
|
|
253
|
+
onScroll: tree.props.onScroll,
|
|
254
|
+
onItemsRendered: tree.onItemsRendered.bind(tree),
|
|
255
|
+
};
|
|
256
|
+
if (typeof tree.props.rowHeight === "function") {
|
|
257
|
+
return (
|
|
258
|
+
// @ts-ignore
|
|
259
|
+
_jsx(VariableSizeList, Object.assign({}, commonProps, { itemSize: tree.rowHeightAt, ref: tree.list, children: RowContainer })));
|
|
260
|
+
}
|
|
261
|
+
return (
|
|
262
|
+
// @ts-ignore
|
|
263
|
+
_jsx(FixedSizeList, Object.assign({}, commonProps, { itemSize: tree.rowHeight, ref: tree.list, children: RowContainer })));
|
|
235
264
|
}
|
|
@@ -17,7 +17,7 @@ const circleStyle = {
|
|
|
17
17
|
boxShadow: "0 0 0 3px #4B91E2",
|
|
18
18
|
borderRadius: "50%",
|
|
19
19
|
};
|
|
20
|
-
export const DefaultCursor = React.memo(function DefaultCursor({ top, left, indent
|
|
20
|
+
export const DefaultCursor = React.memo(function DefaultCursor({ top, left, indent }) {
|
|
21
21
|
const style = {
|
|
22
22
|
position: "absolute",
|
|
23
23
|
pointerEvents: "none",
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { DragPreviewProps } from "../types/renderers";
|
|
2
|
-
export declare function DefaultDragPreview({ offset, mouse, id, dragIds, isDragging
|
|
2
|
+
export declare function DefaultDragPreview({ offset, mouse, id, dragIds, isDragging }: DragPreviewProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -22,7 +22,7 @@ const getCountStyle = (offset) => {
|
|
|
22
22
|
const { x, y } = offset;
|
|
23
23
|
return { transform: `translate(${x + 10}px, ${y + 10}px)` };
|
|
24
24
|
};
|
|
25
|
-
export function DefaultDragPreview({ offset, mouse, id, dragIds, isDragging
|
|
25
|
+
export function DefaultDragPreview({ offset, mouse, id, dragIds, isDragging }) {
|
|
26
26
|
return (_jsxs(Overlay, { isDragging: isDragging, children: [_jsx(Position, { offset: offset, children: _jsx(PreviewNode, { id: id, dragIds: dragIds }) }), _jsx(Count, { mouse: mouse, count: dragIds.length })] }));
|
|
27
27
|
}
|
|
28
28
|
const Overlay = memo(function Overlay(props) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { RowRendererProps } from "../types/renderers";
|
|
2
|
-
export declare function DefaultRow<T>({ node, attrs, innerRef, children
|
|
2
|
+
export declare function DefaultRow<T>({ node, attrs, innerRef, children }: RowRendererProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
export function DefaultRow({ node, attrs, innerRef, children
|
|
2
|
+
export function DefaultRow({ node, attrs, innerRef, children }) {
|
|
3
3
|
return (_jsx("div", Object.assign({}, attrs, { ref: innerRef, onFocus: (e) => e.stopPropagation(), onClick: node.handleClick, children: children })));
|
|
4
4
|
}
|
|
@@ -26,7 +26,7 @@ export const ListOuterElement = forwardRef(function Outer(props, ref) {
|
|
|
26
26
|
export const DropContainer = () => {
|
|
27
27
|
const tree = useTreeApi();
|
|
28
28
|
return (_jsx("div", { style: {
|
|
29
|
-
height: tree.visibleNodes.length
|
|
29
|
+
height: tree.rowTopPosition(tree.visibleNodes.length),
|
|
30
30
|
width: "100%",
|
|
31
31
|
position: "absolute",
|
|
32
32
|
left: "0",
|
|
@@ -6,5 +6,5 @@ type Props<T> = {
|
|
|
6
6
|
imperativeHandle: React.Ref<TreeApi<T> | undefined>;
|
|
7
7
|
children: ReactNode;
|
|
8
8
|
};
|
|
9
|
-
export declare function TreeProvider<T>({ treeProps, imperativeHandle, children
|
|
9
|
+
export declare function TreeProvider<T>({ treeProps, imperativeHandle, children }: Props<T>): import("react/jsx-runtime").JSX.Element;
|
|
10
10
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect, useImperativeHandle, useMemo, useRef
|
|
2
|
+
import { useEffect, useImperativeHandle, useMemo, useRef } from "react";
|
|
3
3
|
import { useSyncExternalStore } from "use-sync-external-store/shim";
|
|
4
|
-
import { DataUpdatesContext, DndContext, NodesContext, TreeApiContext
|
|
4
|
+
import { DataUpdatesContext, DndContext, NodesContext, TreeApiContext } from "../context";
|
|
5
5
|
import { TreeApi } from "../interfaces/tree-api";
|
|
6
6
|
import { initialState } from "../state/initial";
|
|
7
7
|
import { rootReducer } from "../state/root-reducer";
|
|
@@ -10,7 +10,7 @@ import { DndProvider } from "react-dnd";
|
|
|
10
10
|
import { createStore } from "redux";
|
|
11
11
|
import { actions as visibility } from "../state/open-slice";
|
|
12
12
|
const SERVER_STATE = initialState();
|
|
13
|
-
export function TreeProvider({ treeProps, imperativeHandle, children
|
|
13
|
+
export function TreeProvider({ treeProps, imperativeHandle, children }) {
|
|
14
14
|
const list = useRef(null);
|
|
15
15
|
const listEl = useRef(null);
|
|
16
16
|
const store = useRef(
|
|
@@ -26,7 +26,7 @@ export function TreeProvider({ treeProps, imperativeHandle, children, }) {
|
|
|
26
26
|
useMemo(() => {
|
|
27
27
|
updateCount.current += 1;
|
|
28
28
|
api.update(treeProps);
|
|
29
|
-
},
|
|
29
|
+
}, Object.values(treeProps));
|
|
30
30
|
/* Rebuild visible nodes when open state changes, without clobbering
|
|
31
31
|
props set imperatively via api.update(). Bumping updateCount keeps
|
|
32
32
|
DataUpdates consumers (e.g. DefaultContainer) in sync. */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { createRef } from "react";
|
|
3
|
-
import { act, render } from "@testing-library/react";
|
|
3
|
+
import { act, render, screen } from "@testing-library/react";
|
|
4
|
+
import { FixedSizeList, VariableSizeList } from "react-window";
|
|
4
5
|
import { Tree } from "./tree";
|
|
5
6
|
const data = [
|
|
6
7
|
{
|
|
@@ -29,3 +30,72 @@ test("imperative tree.update() props survive node toggles (#228)", () => {
|
|
|
29
30
|
});
|
|
30
31
|
expect(api.rowHeight).toBe(48);
|
|
31
32
|
});
|
|
33
|
+
/* Backwards compatibility: switching FixedSizeList -> VariableSizeList must not
|
|
34
|
+
change layout for a numeric rowHeight. With openByDefault, all four nodes
|
|
35
|
+
(1 > 2, 3 > 4) are visible in DFS order. */
|
|
36
|
+
test("numeric rowHeight positions rows at index * height (#238 back-compat)", () => {
|
|
37
|
+
render(_jsx(Tree, { data: data, rowHeight: 24, openByDefault: true }));
|
|
38
|
+
const rows = screen.getAllByRole("treeitem");
|
|
39
|
+
expect(rows).toHaveLength(4);
|
|
40
|
+
rows.forEach((row, i) => {
|
|
41
|
+
expect(row.style.height).toBe("24px");
|
|
42
|
+
expect(row.style.top).toBe(`${i * 24}px`);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
test("function rowHeight gives each row its own height and cumulative top (#238)", () => {
|
|
46
|
+
const heights = { "1": 40, "2": 20, "3": 30, "4": 10 };
|
|
47
|
+
render(_jsx(Tree, { data: data, rowHeight: (node) => heights[node.id], openByDefault: true }));
|
|
48
|
+
const rows = screen.getAllByRole("treeitem");
|
|
49
|
+
expect(rows).toHaveLength(4);
|
|
50
|
+
const expected = [40, 20, 30, 10];
|
|
51
|
+
let top = 0;
|
|
52
|
+
rows.forEach((row, i) => {
|
|
53
|
+
expect(row.style.height).toBe(`${expected[i]}px`);
|
|
54
|
+
expect(row.style.top).toBe(`${top}px`);
|
|
55
|
+
top += expected[i];
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
test("mutations tell the list to recompute heights (#238)", () => {
|
|
59
|
+
const ref = createRef();
|
|
60
|
+
/* Only variable-height mode renders a VariableSizeList with a measurement
|
|
61
|
+
cache to recompute, so use a function rowHeight here. */
|
|
62
|
+
render(_jsx(Tree, { data: data, ref: ref, rowHeight: () => 24, openByDefault: true }));
|
|
63
|
+
const api = ref.current;
|
|
64
|
+
const reset = jest.spyOn(api.list.current, "resetAfterIndex");
|
|
65
|
+
act(() => api.close("1"));
|
|
66
|
+
expect(reset).toHaveBeenCalled();
|
|
67
|
+
reset.mockClear();
|
|
68
|
+
act(() => api.open("1"));
|
|
69
|
+
expect(reset).toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
/* react-window caches measurements by index and never invalidates them itself.
|
|
72
|
+
When data changes via props in variable-height mode, those cached sizes belong
|
|
73
|
+
to the wrong rows, so update() must drop the cache. It runs during render, so
|
|
74
|
+
it uses the shouldForceUpdate=false variant. */
|
|
75
|
+
test("changing data in variable-height mode resets the list cache (#238)", () => {
|
|
76
|
+
const ref = createRef();
|
|
77
|
+
const rowHeight = (node) => (node.isInternal ? 40 : 20);
|
|
78
|
+
const { rerender } = render(_jsx(Tree, { data: data, ref: ref, rowHeight: rowHeight, openByDefault: true }));
|
|
79
|
+
const reset = jest.spyOn(ref.current.list.current, "resetAfterIndex");
|
|
80
|
+
const nextData = [{ id: "9", name: "fresh" }, ...data];
|
|
81
|
+
act(() => {
|
|
82
|
+
rerender(_jsx(Tree, { data: nextData, ref: ref, rowHeight: rowHeight, openByDefault: true }));
|
|
83
|
+
});
|
|
84
|
+
expect(reset).toHaveBeenCalledWith(0, false);
|
|
85
|
+
});
|
|
86
|
+
/* The numeric path must stay on FixedSizeList: it has constant item sizes, so
|
|
87
|
+
there is no measurement cache to go stale and none of VariableSizeList's
|
|
88
|
+
overhead. A FixedSizeList has no resetAfterIndex method at all. */
|
|
89
|
+
test("numeric rowHeight renders a cache-free FixedSizeList (#238)", () => {
|
|
90
|
+
const ref = createRef();
|
|
91
|
+
render(_jsx(Tree, { data: data, ref: ref, rowHeight: 24, openByDefault: true }));
|
|
92
|
+
const list = ref.current.list.current;
|
|
93
|
+
expect(list).toBeInstanceOf(FixedSizeList);
|
|
94
|
+
expect("resetAfterIndex" in list).toBe(false);
|
|
95
|
+
});
|
|
96
|
+
/* The function path uses VariableSizeList so per-row heights are possible. */
|
|
97
|
+
test("function rowHeight renders a VariableSizeList (#238)", () => {
|
|
98
|
+
const ref = createRef();
|
|
99
|
+
render(_jsx(Tree, { data: data, ref: ref, rowHeight: () => 24, openByDefault: true }));
|
|
100
|
+
expect(ref.current.list.current).toBeInstanceOf(VariableSizeList);
|
|
101
|
+
});
|
|
@@ -3,5 +3,5 @@ type Props = {
|
|
|
3
3
|
style: React.CSSProperties;
|
|
4
4
|
index: number;
|
|
5
5
|
};
|
|
6
|
-
export declare const RowContainer: React.MemoExoticComponent<(<T>({ index, style
|
|
6
|
+
export declare const RowContainer: React.MemoExoticComponent<(<T>({ index, style }: Props) => import("react/jsx-runtime").JSX.Element)>;
|
|
7
7
|
export {};
|
|
@@ -4,7 +4,7 @@ import { useDataUpdates, useNodesContext, useTreeApi } from "../context";
|
|
|
4
4
|
import { useDragHook } from "../dnd/drag-hook";
|
|
5
5
|
import { useDropHook } from "../dnd/drop-hook";
|
|
6
6
|
import { useFreshNode } from "../hooks/use-fresh-node";
|
|
7
|
-
export const RowContainer = React.memo(function RowContainer({ index, style
|
|
7
|
+
export const RowContainer = React.memo(function RowContainer({ index, style }) {
|
|
8
8
|
/* When will the <Row> will re-render.
|
|
9
9
|
*
|
|
10
10
|
* The row component is memo'd so it will only render
|
|
@@ -35,8 +35,7 @@ export const RowContainer = React.memo(function RowContainer({ index, style, })
|
|
|
35
35
|
const nodeStyle = useMemo(() => ({ paddingLeft: indent }), [indent]);
|
|
36
36
|
const rowStyle = useMemo(() => {
|
|
37
37
|
var _a, _b;
|
|
38
|
-
return (Object.assign(Object.assign({}, style), { top: parseFloat(style.top) +
|
|
39
|
-
((_b = (_a = tree.props.padding) !== null && _a !== void 0 ? _a : tree.props.paddingTop) !== null && _b !== void 0 ? _b : 0) }));
|
|
38
|
+
return (Object.assign(Object.assign({}, style), { top: parseFloat(style.top) + ((_b = (_a = tree.props.padding) !== null && _a !== void 0 ? _a : tree.props.paddingTop) !== null && _b !== void 0 ? _b : 0) }));
|
|
40
39
|
}, [style, tree.props.padding, tree.props.paddingTop]);
|
|
41
40
|
const rowAttrs = {
|
|
42
41
|
role: "treeitem",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { bound, indexOf, isClosed, isItem, isOpenWithEmptyChildren
|
|
1
|
+
import { bound, indexOf, isClosed, isItem, isOpenWithEmptyChildren } from "../utils";
|
|
2
2
|
function measureHover(el, offset) {
|
|
3
3
|
const rect = el.getBoundingClientRect();
|
|
4
4
|
const x = offset.x - Math.round(rect.x);
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import { ConnectDragSource } from "react-dnd";
|
|
2
2
|
import { NodeApi } from "../interfaces/node-api";
|
|
3
|
+
import { TreeProps } from "../types/tree-props";
|
|
4
|
+
export declare function dragTypeForNode<T>(dragType: TreeProps<T>["dragType"], node: NodeApi<T>): string;
|
|
3
5
|
export declare function useDragHook<T>(node: NodeApi<T>): ConnectDragSource;
|
|
@@ -3,23 +3,31 @@ import { useDrag } from "react-dnd";
|
|
|
3
3
|
import { getEmptyImage } from "react-dnd-html5-backend";
|
|
4
4
|
import { useTreeApi } from "../context";
|
|
5
5
|
import { actions as dnd } from "../state/dnd-slice";
|
|
6
|
+
/* The react-dnd item type a row's drag source broadcasts. The dragType prop
|
|
7
|
+
can be a fixed string or a per-node function; it defaults to "NODE". */
|
|
8
|
+
export function dragTypeForNode(dragType, node) {
|
|
9
|
+
if (typeof dragType === "function")
|
|
10
|
+
return dragType(node);
|
|
11
|
+
return dragType !== null && dragType !== void 0 ? dragType : "NODE";
|
|
12
|
+
}
|
|
6
13
|
export function useDragHook(node) {
|
|
7
14
|
const tree = useTreeApi();
|
|
8
15
|
const ids = tree.selectedIds;
|
|
9
16
|
const [_, ref, preview] = useDrag(() => ({
|
|
10
17
|
canDrag: () => node.isDraggable,
|
|
11
|
-
type:
|
|
18
|
+
type: dragTypeForNode(tree.props.dragType, node),
|
|
12
19
|
item: () => {
|
|
13
|
-
// This is fired once at the
|
|
20
|
+
// This is fired once at the beginning of a drag operation
|
|
14
21
|
const dragIds = tree.isSelected(node.id) ? Array.from(ids) : [node.id];
|
|
15
22
|
tree.dispatch(dnd.dragStart(node.id, dragIds));
|
|
16
|
-
return { id: node.id, dragIds };
|
|
23
|
+
return { id: node.id, dragIds, data: node.data };
|
|
17
24
|
},
|
|
18
25
|
end: () => {
|
|
19
26
|
tree.hideCursor();
|
|
27
|
+
tree.redrawList();
|
|
20
28
|
tree.dispatch(dnd.dragEnd());
|
|
21
29
|
},
|
|
22
|
-
}), [ids, node]);
|
|
30
|
+
}), [ids, node, tree.props.dragType]);
|
|
23
31
|
useEffect(() => {
|
|
24
32
|
preview(getEmptyImage());
|
|
25
33
|
}, [preview]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { dragTypeForNode } from "./drag-hook";
|
|
2
|
+
/* dragTypeForNode only reads node.data when dragType is a function, so a
|
|
3
|
+
minimal stub stands in for a real NodeApi. */
|
|
4
|
+
function nodeWith(data) {
|
|
5
|
+
return { data };
|
|
6
|
+
}
|
|
7
|
+
test("defaults to the internal NODE type when dragType is undefined", () => {
|
|
8
|
+
expect(dragTypeForNode(undefined, nodeWith({ id: "a" }))).toBe("NODE");
|
|
9
|
+
});
|
|
10
|
+
test("uses a fixed string dragType for every node", () => {
|
|
11
|
+
expect(dragTypeForNode("FILE", nodeWith({ id: "a" }))).toBe("FILE");
|
|
12
|
+
});
|
|
13
|
+
test("resolves a per-node dragType function against the node", () => {
|
|
14
|
+
const dragType = (node) => node.data.kind.toUpperCase();
|
|
15
|
+
expect(dragTypeForNode(dragType, nodeWith({ kind: "folder" }))).toBe("FOLDER");
|
|
16
|
+
expect(dragTypeForNode(dragType, nodeWith({ kind: "file" }))).toBe("FILE");
|
|
17
|
+
});
|