@barqjs/aria 0.0.0-stage → 0.2.0-next.1
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/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
package/src/tree.tsx
ADDED
|
@@ -0,0 +1,2075 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tree: nested rows that expand and collapse.
|
|
3
|
+
*
|
|
4
|
+
* It is a `treegrid` rather than a `tree` because a row may hold its own
|
|
5
|
+
* controls (an expand button, a checkbox, a menu), and a `treeitem` may not
|
|
6
|
+
* contain interactive content. The navigation is a grid list's with one more
|
|
7
|
+
* axis:
|
|
8
|
+
*
|
|
9
|
+
* - **Up and down move between the rows that are showing.** A collapsed
|
|
10
|
+
* row's descendants are still in the collection, so a selection or a route
|
|
11
|
+
* can name them, but the keyboard and typeahead never land on them.
|
|
12
|
+
* - **Right expands, then moves into the row.** On a row that is collapsed and
|
|
13
|
+
* has children the first press opens it; after that Right walks the row's own
|
|
14
|
+
* controls, as in a grid list. Left is the mirror: it collapses an open row,
|
|
15
|
+
* and on a closed one or a leaf it moves to the parent. Both flip in a
|
|
16
|
+
* right-to-left layout.
|
|
17
|
+
*
|
|
18
|
+
* The rows are rendered flat, each a sibling of the next, with `aria-level`,
|
|
19
|
+
* `aria-posinset` and `aria-setsize` saying where each one sits. Nesting the
|
|
20
|
+
* elements would make a row's subtree part of its accessible name.
|
|
21
|
+
*
|
|
22
|
+
* ```tsx
|
|
23
|
+
* <Tree aria-label="Files" items={files()} defaultExpandedKeys={["documents"]}>
|
|
24
|
+
* {(file) => (
|
|
25
|
+
* <TreeItem>
|
|
26
|
+
* <TreeItemContent>
|
|
27
|
+
* {(row) => (
|
|
28
|
+
* <>
|
|
29
|
+
* <Show when={row.hasChildItems()}>
|
|
30
|
+
* <TreeItemExpandButton>{row.isExpanded() ? "v" : ">"}</TreeItemExpandButton>
|
|
31
|
+
* </Show>
|
|
32
|
+
* {file.name}
|
|
33
|
+
* </>
|
|
34
|
+
* )}
|
|
35
|
+
* </TreeItemContent>
|
|
36
|
+
* </TreeItem>
|
|
37
|
+
* )}
|
|
38
|
+
* </Tree>
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* An item's `children` are its child rows, read through `getChildren`. An item
|
|
42
|
+
* whose `getType` answers `"section"` is a group at the root with a header;
|
|
43
|
+
* one that answers `"loader"` is where a `<TreeLoadMoreItem>` goes.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
import {
|
|
47
|
+
cells,
|
|
48
|
+
type Accessor,
|
|
49
|
+
type Child,
|
|
50
|
+
For,
|
|
51
|
+
createContext,
|
|
52
|
+
createEffect,
|
|
53
|
+
createMemo,
|
|
54
|
+
createRenderEffect,
|
|
55
|
+
createSignal,
|
|
56
|
+
getContext,
|
|
57
|
+
getOwner,
|
|
58
|
+
type Incoming,
|
|
59
|
+
install,
|
|
60
|
+
onCleanup,
|
|
61
|
+
provide,
|
|
62
|
+
Show,
|
|
63
|
+
} from "@barqjs/core";
|
|
64
|
+
import { isServer } from "@barqjs/web";
|
|
65
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
66
|
+
import { button, type ButtonOptions } from "./button.tsx";
|
|
67
|
+
import { Checkbox } from "./checkbox.tsx";
|
|
68
|
+
import {
|
|
69
|
+
buildCollection,
|
|
70
|
+
type Collection,
|
|
71
|
+
type DisabledBehavior,
|
|
72
|
+
type ItemAccessors,
|
|
73
|
+
type Key,
|
|
74
|
+
type LayoutDelegate,
|
|
75
|
+
multipleSelectionState,
|
|
76
|
+
type MultipleSelectionOptions,
|
|
77
|
+
type Node,
|
|
78
|
+
type Rect,
|
|
79
|
+
SelectionManager,
|
|
80
|
+
type SelectionBehavior,
|
|
81
|
+
type SelectionMode,
|
|
82
|
+
type Size,
|
|
83
|
+
} from "./collections.ts";
|
|
84
|
+
import {
|
|
85
|
+
activeElement,
|
|
86
|
+
contains,
|
|
87
|
+
ownerDocument,
|
|
88
|
+
PREVENT_FOCUS_ATTRIBUTE,
|
|
89
|
+
scrollIntoViewport,
|
|
90
|
+
scrollParents,
|
|
91
|
+
targetElement,
|
|
92
|
+
} from "./dom.ts";
|
|
93
|
+
import {
|
|
94
|
+
afterDropIndicatorTargets,
|
|
95
|
+
type DroppableCollectionActivateEvent,
|
|
96
|
+
type ItemDropTarget,
|
|
97
|
+
TreeDropTargetDelegate,
|
|
98
|
+
} from "./dnd/index.ts";
|
|
99
|
+
import {
|
|
100
|
+
type CollectionDragAndDrop,
|
|
101
|
+
collectionDragAndDrop,
|
|
102
|
+
type DragAndDropHooks,
|
|
103
|
+
draggableItemSlotProps,
|
|
104
|
+
type DropIndicatorProps,
|
|
105
|
+
provideDragButton,
|
|
106
|
+
provideDropIndicator,
|
|
107
|
+
useDragAndDropContext,
|
|
108
|
+
dropIndicatorStyle,
|
|
109
|
+
} from "./drag-and-drop.tsx";
|
|
110
|
+
import { focusableWalker, focusRing } from "./focus.ts";
|
|
111
|
+
import { rowIdFor, type KeyboardNavigationBehavior } from "./gridlist.tsx";
|
|
112
|
+
import { StringDictionary, stringFormatter, useLocale } from "./i18n.ts";
|
|
113
|
+
import { focused } from "./interactions/focus-events.ts";
|
|
114
|
+
import { focusSafely } from "./interactions/focusable.ts";
|
|
115
|
+
import { hover, type HoverEvent } from "./interactions/hover.ts";
|
|
116
|
+
import { isFocusVisible as focusIsVisible } from "./interactions/modality.ts";
|
|
117
|
+
import { router } from "./interactions/open-link.ts";
|
|
118
|
+
import type { ElementRef, PressEvent } from "./interactions/press.ts";
|
|
119
|
+
import { visuallyHidden } from "./live.ts";
|
|
120
|
+
import {
|
|
121
|
+
ListKeyboardDelegate,
|
|
122
|
+
selectableItem,
|
|
123
|
+
selectableList,
|
|
124
|
+
type KeyboardDelegate,
|
|
125
|
+
} from "./selection.ts";
|
|
126
|
+
import {
|
|
127
|
+
access,
|
|
128
|
+
callback,
|
|
129
|
+
controllable,
|
|
130
|
+
type DOMProps,
|
|
131
|
+
filterDOMProps,
|
|
132
|
+
fromProps,
|
|
133
|
+
id,
|
|
134
|
+
type MaybeAccessor,
|
|
135
|
+
mergeProps,
|
|
136
|
+
type StyleProps,
|
|
137
|
+
styleProps,
|
|
138
|
+
} from "./utils.ts";
|
|
139
|
+
import {
|
|
140
|
+
contentStyle,
|
|
141
|
+
measureRow,
|
|
142
|
+
rowStyle,
|
|
143
|
+
useVirtualizer,
|
|
144
|
+
type LayoutInfo,
|
|
145
|
+
type VirtualizerValue,
|
|
146
|
+
} from "./virtualizer.tsx";
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// The collection
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
interface TreeStructure<T> {
|
|
153
|
+
roots: Node<T>[];
|
|
154
|
+
keyMap: Map<Key, Node<T>>;
|
|
155
|
+
setSizes: Map<Key, number>;
|
|
156
|
+
itemCount: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Re-link the nodes `buildCollection` made into a tree.
|
|
161
|
+
*
|
|
162
|
+
* A list collection links its nodes in document order and numbers its items
|
|
163
|
+
* across the whole list; a tree needs both per sibling group. A section adds
|
|
164
|
+
* no level: its rows sit at the level of the section itself, as React Aria
|
|
165
|
+
* reports them.
|
|
166
|
+
*/
|
|
167
|
+
function structure<T>(roots: Node<T>[]): TreeStructure<T> {
|
|
168
|
+
const keyMap = new Map<Key, Node<T>>();
|
|
169
|
+
const setSizes = new Map<Key, number>();
|
|
170
|
+
let itemCount = 0;
|
|
171
|
+
|
|
172
|
+
const link = (siblings: Node<T>[], level: number, parentKey: Key | undefined): void => {
|
|
173
|
+
const size = siblings.filter((node) => node.type === "item").length;
|
|
174
|
+
let index = 0;
|
|
175
|
+
let previous: Node<T> | null = null;
|
|
176
|
+
for (const node of siblings) {
|
|
177
|
+
keyMap.set(node.key, node);
|
|
178
|
+
node.level = level;
|
|
179
|
+
node.parentKey = parentKey;
|
|
180
|
+
node.prevKey = previous?.key;
|
|
181
|
+
node.nextKey = undefined;
|
|
182
|
+
if (previous !== null) previous.nextKey = node.key;
|
|
183
|
+
node.index = index;
|
|
184
|
+
if (node.type === "item") {
|
|
185
|
+
index++;
|
|
186
|
+
itemCount++;
|
|
187
|
+
setSizes.set(node.key, size);
|
|
188
|
+
}
|
|
189
|
+
link(node.childNodes, node.type === "section" ? level : level + 1, node.key);
|
|
190
|
+
previous = node;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
link(roots, 0, undefined);
|
|
194
|
+
|
|
195
|
+
return { roots, keyMap, setSizes, itemCount };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* A tree, read through which of its rows are expanded.
|
|
200
|
+
*
|
|
201
|
+
* Every node is in it, so `getItem` answers for a row inside a collapsed
|
|
202
|
+
* parent. The navigation (`getKeyAfter`, `getKeyBefore`, the ends, the
|
|
203
|
+
* iterator) only walks the rows that are showing, which is what keeps the
|
|
204
|
+
* keyboard, typeahead and "select all" off the hidden ones.
|
|
205
|
+
*/
|
|
206
|
+
export class TreeCollection<T> implements Collection<T> {
|
|
207
|
+
#tree: TreeStructure<T>;
|
|
208
|
+
#expandedKeys: Set<Key>;
|
|
209
|
+
#visible: Key[] | null = null;
|
|
210
|
+
|
|
211
|
+
constructor(shape: TreeStructure<T>, expandedKeys: Set<Key>) {
|
|
212
|
+
this.#tree = shape;
|
|
213
|
+
this.#expandedKeys = expandedKeys;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The same nodes, opened differently. */
|
|
217
|
+
withExpandedKeys(expandedKeys: Set<Key>): TreeCollection<T> {
|
|
218
|
+
return new TreeCollection(this.#tree, expandedKeys);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
get expandedKeys(): Set<Key> {
|
|
222
|
+
return this.#expandedKeys;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** The top-level nodes: rows, sections and loaders. */
|
|
226
|
+
get rootNodes(): Node<T>[] {
|
|
227
|
+
return this.#tree.roots;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** How many items, expanded or not. Sections and loaders do not count. */
|
|
231
|
+
get size(): number {
|
|
232
|
+
return this.#tree.itemCount;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The showing nodes in order; a section is followed by its next sibling, not its rows. */
|
|
236
|
+
*[Symbol.iterator](): IterableIterator<Node<T>> {
|
|
237
|
+
let key = this.getFirstKey();
|
|
238
|
+
while (key !== null) {
|
|
239
|
+
const node = this.getItem(key);
|
|
240
|
+
if (node === null) return;
|
|
241
|
+
yield node;
|
|
242
|
+
key = node.type === "section" ? (node.nextKey ?? null) : this.getKeyAfter(key);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
getKeys(): IterableIterator<Key> {
|
|
247
|
+
return this.#tree.keyMap.keys();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
getItem(key: Key): Node<T> | null {
|
|
251
|
+
return this.#tree.keyMap.get(key) ?? null;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#opens(node: Node<T>): boolean {
|
|
255
|
+
return node.type !== "item" || this.#expandedKeys.has(node.key);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
getKeyAfter(key: Key): Key | null {
|
|
259
|
+
let node = this.getItem(key);
|
|
260
|
+
if (node === null) return null;
|
|
261
|
+
const first = node.childNodes[0];
|
|
262
|
+
if (this.#opens(node) && first !== undefined) return first.key;
|
|
263
|
+
|
|
264
|
+
while (node !== null) {
|
|
265
|
+
if (node.nextKey !== undefined) return node.nextKey;
|
|
266
|
+
node = node.parentKey === undefined ? null : this.getItem(node.parentKey);
|
|
267
|
+
}
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
getKeyBefore(key: Key): Key | null {
|
|
272
|
+
const node = this.getItem(key);
|
|
273
|
+
if (node === null) return null;
|
|
274
|
+
if (node.prevKey === undefined) return node.parentKey ?? null;
|
|
275
|
+
return this.#deepestShowing(this.getItem(node.prevKey));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#deepestShowing(start: Node<T> | null): Key | null {
|
|
279
|
+
let node = start;
|
|
280
|
+
while (node !== null && this.#opens(node)) {
|
|
281
|
+
const last = node.childNodes.at(-1);
|
|
282
|
+
if (last === undefined) break;
|
|
283
|
+
node = last;
|
|
284
|
+
}
|
|
285
|
+
return node?.key ?? null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
getFirstKey(): Key | null {
|
|
289
|
+
return this.#tree.roots[0]?.key ?? null;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
getLastKey(): Key | null {
|
|
293
|
+
return this.#deepestShowing(this.#tree.roots.at(-1) ?? null);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* A section's showing rows, all levels of them; any other node's direct
|
|
298
|
+
* children. That is the shape a layout walks: a section is a heading
|
|
299
|
+
* followed by its rows, and a row is not a container.
|
|
300
|
+
*/
|
|
301
|
+
getChildren(key: Key): Iterable<Node<T>> {
|
|
302
|
+
const parent = this.getItem(key);
|
|
303
|
+
if (parent === null) return [];
|
|
304
|
+
if (parent.type !== "section") return parent.childNodes;
|
|
305
|
+
|
|
306
|
+
const rows: Node<T>[] = [];
|
|
307
|
+
let at = parent.childNodes[0]?.key ?? null;
|
|
308
|
+
while (at !== null && at !== parent.nextKey) {
|
|
309
|
+
const node = this.getItem(at);
|
|
310
|
+
if (node === null) break;
|
|
311
|
+
rows.push(node);
|
|
312
|
+
at = this.getKeyAfter(at);
|
|
313
|
+
}
|
|
314
|
+
return rows;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
at(index: number): Node<T> | null {
|
|
318
|
+
const key = this.#showing()[index];
|
|
319
|
+
return key === undefined ? null : this.getItem(key);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#showing(): Key[] {
|
|
323
|
+
if (this.#visible !== null) return this.#visible;
|
|
324
|
+
const keys: Key[] = [];
|
|
325
|
+
let key = this.getFirstKey();
|
|
326
|
+
while (key !== null) {
|
|
327
|
+
keys.push(key);
|
|
328
|
+
key = this.getKeyAfter(key);
|
|
329
|
+
}
|
|
330
|
+
this.#visible = keys;
|
|
331
|
+
return keys;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Whether every ancestor of the node is expanded. */
|
|
335
|
+
isShowing(key: Key): boolean {
|
|
336
|
+
let node = this.getItem(key);
|
|
337
|
+
if (node === null) return false;
|
|
338
|
+
while (node.parentKey !== undefined) {
|
|
339
|
+
const parent = this.getItem(node.parentKey);
|
|
340
|
+
if (parent === null) return false;
|
|
341
|
+
if (parent.type === "item" && !this.#expandedKeys.has(parent.key)) return false;
|
|
342
|
+
node = parent;
|
|
343
|
+
}
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** The nearest node that is rendered: the key itself, or its highest collapsed ancestor. */
|
|
348
|
+
closestShowingKey(key: Key): Key {
|
|
349
|
+
let target = key;
|
|
350
|
+
let node = this.getItem(key);
|
|
351
|
+
while (node !== null && node.parentKey !== undefined) {
|
|
352
|
+
const parent = this.getItem(node.parentKey);
|
|
353
|
+
if (parent !== null && parent.type === "item" && !this.#expandedKeys.has(parent.key)) {
|
|
354
|
+
target = parent.key;
|
|
355
|
+
}
|
|
356
|
+
node = parent;
|
|
357
|
+
}
|
|
358
|
+
return target;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** How many item siblings a row has, itself included. */
|
|
362
|
+
setSizeOf(key: Key): number {
|
|
363
|
+
return this.#tree.setSizes.get(key) ?? 0;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** The row's position among the rendered rows, counting from 1. */
|
|
367
|
+
rowIndexOf(key: Key): number {
|
|
368
|
+
const rows = this.#showing().filter((at) => this.getItem(at)?.type !== "section");
|
|
369
|
+
return rows.indexOf(key) + 1;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** How many rows are rendered. */
|
|
373
|
+
get rowCount(): number {
|
|
374
|
+
return this.#showing().filter((at) => this.getItem(at)?.type !== "section").length;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The visible rows between two keys, in order, whichever comes first. */
|
|
378
|
+
keyRange(from: Key, to: Key): Key[] {
|
|
379
|
+
const keys = this.#showing();
|
|
380
|
+
const a = keys.indexOf(from);
|
|
381
|
+
const b = keys.indexOf(to);
|
|
382
|
+
if (a === -1 || b === -1) return [];
|
|
383
|
+
return keys
|
|
384
|
+
.slice(Math.min(a, b), Math.max(a, b) + 1)
|
|
385
|
+
.filter((key) => this.getItem(key)?.type === "item");
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// ---------------------------------------------------------------------------
|
|
390
|
+
// State
|
|
391
|
+
// ---------------------------------------------------------------------------
|
|
392
|
+
|
|
393
|
+
export interface TreeStateOptions<T> extends MultipleSelectionOptions, ItemAccessors<T> {
|
|
394
|
+
items?: MaybeAccessor<Iterable<T> | undefined>;
|
|
395
|
+
expandedKeys?: MaybeAccessor<Iterable<Key> | undefined>;
|
|
396
|
+
defaultExpandedKeys?: MaybeAccessor<Iterable<Key> | undefined>;
|
|
397
|
+
onExpandedChange?: (keys: Set<Key>) => void;
|
|
398
|
+
/** Where rows are, for a virtualised tree. */
|
|
399
|
+
layoutDelegate?: LayoutDelegate;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export interface TreeState<T> {
|
|
403
|
+
collection: Accessor<TreeCollection<T>>;
|
|
404
|
+
selectionManager: Accessor<SelectionManager>;
|
|
405
|
+
disabledKeys: Accessor<Set<Key>>;
|
|
406
|
+
expandedKeys: Accessor<Set<Key>>;
|
|
407
|
+
toggleKey(key: Key): void;
|
|
408
|
+
setExpandedKeys(keys: Set<Key>): void;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* The geometry-free delegate the selection manager orders ranges through.
|
|
413
|
+
*
|
|
414
|
+
* `SelectionManager` decides which end of a range comes first by comparing
|
|
415
|
+
* sibling indexes, which is wrong between two rows at different depths. It
|
|
416
|
+
* asks `getKeyRange` when there is one, and this one answers in rendered order
|
|
417
|
+
* either way round. It has no geometry, so it says so.
|
|
418
|
+
*/
|
|
419
|
+
function rangeDelegate<T>(collection: Accessor<TreeCollection<T>>): LayoutDelegate {
|
|
420
|
+
return {
|
|
421
|
+
getItemRect: (): Rect | null => null,
|
|
422
|
+
getContentSize: (): Size => ({ width: 0, height: 0 }),
|
|
423
|
+
getVisibleRect: (): Rect => ({ x: 0, y: 0, width: 0, height: 0 }),
|
|
424
|
+
getKeyRange: (from, to) => collection().keyRange(from, to),
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Expansion, selection and roving focus over a tree of items.
|
|
430
|
+
*
|
|
431
|
+
* The focused key is repaired when the row it names stops being rendered:
|
|
432
|
+
* removed, it moves to the next row that survives, as a list's does; hidden
|
|
433
|
+
* under a collapsed parent, it moves up to that parent.
|
|
434
|
+
*/
|
|
435
|
+
export function treeState<T>(options: TreeStateOptions<T>): TreeState<T> {
|
|
436
|
+
const selection = multipleSelectionState(options);
|
|
437
|
+
|
|
438
|
+
const [expandedKeys, setExpandedKeys] = controllable<Set<Key>>(
|
|
439
|
+
() => {
|
|
440
|
+
const given = access(options.expandedKeys);
|
|
441
|
+
return given === undefined ? undefined : new Set(given);
|
|
442
|
+
},
|
|
443
|
+
() => new Set(access(options.defaultExpandedKeys) ?? []),
|
|
444
|
+
options.onExpandedChange,
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
const accessors: ItemAccessors<T> = {
|
|
448
|
+
getKey: options.getKey,
|
|
449
|
+
getTextValue: options.getTextValue,
|
|
450
|
+
getChildren: options.getChildren,
|
|
451
|
+
getProps: options.getProps,
|
|
452
|
+
getType: (item: T) => callback(options.getType)?.(item) ?? "item",
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
const shape = createMemo(() =>
|
|
456
|
+
structure([...buildCollection(access(options.items) ?? [], accessors)]),
|
|
457
|
+
);
|
|
458
|
+
const collection = createMemo(() => new TreeCollection(shape(), expandedKeys()));
|
|
459
|
+
|
|
460
|
+
const ranges = rangeDelegate(collection);
|
|
461
|
+
const selectionManager = createMemo(
|
|
462
|
+
() =>
|
|
463
|
+
new SelectionManager(collection() as Collection<unknown>, selection, {
|
|
464
|
+
layoutDelegate: options.layoutDelegate ?? ranges,
|
|
465
|
+
}),
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
let previous: TreeCollection<T> | null = null;
|
|
469
|
+
createRenderEffect(
|
|
470
|
+
(): { current: TreeCollection<T>; repair: boolean; next: Key | null } => {
|
|
471
|
+
const current = collection();
|
|
472
|
+
const focusedKey = selection.focusedKey();
|
|
473
|
+
if (focusedKey === null || previous === null) {
|
|
474
|
+
return { current, repair: false, next: null };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (current.getItem(focusedKey) !== null) {
|
|
478
|
+
if (current.isShowing(focusedKey)) return { current, repair: false, next: null };
|
|
479
|
+
return { current, repair: true, next: current.closestShowingKey(focusedKey) };
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const manager = selectionManager();
|
|
483
|
+
const usable = (key: Key): boolean => {
|
|
484
|
+
const node = current.getItem(key);
|
|
485
|
+
return node !== null && node.type === "item" && current.isShowing(key);
|
|
486
|
+
};
|
|
487
|
+
let next: Key | null = null;
|
|
488
|
+
for (let key = previous.getKeyAfter(focusedKey); key !== null && next === null;) {
|
|
489
|
+
if (usable(key) && !manager.isDisabled(key)) next = key;
|
|
490
|
+
key = previous.getKeyAfter(key);
|
|
491
|
+
}
|
|
492
|
+
for (let key = previous.getKeyBefore(focusedKey); key !== null && next === null;) {
|
|
493
|
+
if (usable(key) && !manager.isDisabled(key)) next = key;
|
|
494
|
+
key = previous.getKeyBefore(key);
|
|
495
|
+
}
|
|
496
|
+
return { current, repair: true, next };
|
|
497
|
+
},
|
|
498
|
+
({ current, repair, next }) => {
|
|
499
|
+
if (repair) selection.setFocusedKey(next);
|
|
500
|
+
previous = current;
|
|
501
|
+
},
|
|
502
|
+
);
|
|
503
|
+
|
|
504
|
+
return {
|
|
505
|
+
collection,
|
|
506
|
+
selectionManager,
|
|
507
|
+
disabledKeys: selection.disabledKeys,
|
|
508
|
+
expandedKeys,
|
|
509
|
+
setExpandedKeys,
|
|
510
|
+
toggleKey(key) {
|
|
511
|
+
setExpandedKeys((open) => {
|
|
512
|
+
const next = new Set(open);
|
|
513
|
+
if (next.has(key)) next.delete(key);
|
|
514
|
+
else next.add(key);
|
|
515
|
+
return next;
|
|
516
|
+
});
|
|
517
|
+
},
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// ---------------------------------------------------------------------------
|
|
522
|
+
// The tree
|
|
523
|
+
// ---------------------------------------------------------------------------
|
|
524
|
+
|
|
525
|
+
export interface TreeOptions {
|
|
526
|
+
ref: ElementRef;
|
|
527
|
+
autoFocus?: MaybeAccessor<boolean | "first" | "last" | undefined>;
|
|
528
|
+
disallowTypeAhead?: MaybeAccessor<boolean | undefined>;
|
|
529
|
+
disallowEmptySelection?: MaybeAccessor<boolean | undefined>;
|
|
530
|
+
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
531
|
+
/** @default "clearSelection" */
|
|
532
|
+
escapeKeyBehavior?: MaybeAccessor<"clearSelection" | "none" | undefined>;
|
|
533
|
+
keyboardDelegate?: KeyboardDelegate;
|
|
534
|
+
layoutDelegate?: LayoutDelegate;
|
|
535
|
+
/** Whether the empty state holds something Tab stops at, so the tree itself need not. */
|
|
536
|
+
emptyStateIsTabbable?: MaybeAccessor<boolean | undefined>;
|
|
537
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
538
|
+
"aria-labelledby"?: MaybeAccessor<string | undefined>;
|
|
539
|
+
"aria-describedby"?: MaybeAccessor<string | undefined>;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export interface TreeResult {
|
|
543
|
+
gridProps: DOMProps;
|
|
544
|
+
/** The base every row id is derived from. */
|
|
545
|
+
baseId: Accessor<string>;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export function tree(options: TreeOptions, state: TreeState<unknown>): TreeResult {
|
|
549
|
+
const baseId = id();
|
|
550
|
+
const isEmpty = (): boolean => state.collection().size === 0;
|
|
551
|
+
|
|
552
|
+
const { listProps } = selectableList({
|
|
553
|
+
...options,
|
|
554
|
+
selectionManager: state.selectionManager,
|
|
555
|
+
collection: state.collection,
|
|
556
|
+
disabledKeys: state.disabledKeys,
|
|
557
|
+
selectOnFocus: () => state.selectionManager().selectionBehavior === "replace",
|
|
558
|
+
allowsTabNavigation: isEmpty,
|
|
559
|
+
disallowSelectAll: isEmpty,
|
|
560
|
+
linkBehavior: "action",
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
return {
|
|
564
|
+
baseId,
|
|
565
|
+
gridProps: mergeProps(filterDOMProps(options, { labelable: true }), listProps, {
|
|
566
|
+
role: "treegrid",
|
|
567
|
+
id: baseId,
|
|
568
|
+
"aria-multiselectable": () =>
|
|
569
|
+
state.selectionManager().selectionMode === "multiple" ? true : undefined,
|
|
570
|
+
// Empty, the tree is still a Tab stop so a keyboard user can land on it
|
|
571
|
+
// and hear that it is empty, unless the empty state has its own stop.
|
|
572
|
+
tabIndex: () => {
|
|
573
|
+
if (!isEmpty()) return access(listProps.tabIndex as MaybeAccessor<number>);
|
|
574
|
+
return access(options.emptyStateIsTabbable) === true ? -1 : 0;
|
|
575
|
+
},
|
|
576
|
+
"aria-rowcount": () =>
|
|
577
|
+
access(options.isVirtualized) === true ? state.collection().rowCount : undefined,
|
|
578
|
+
"aria-colcount": () => (access(options.isVirtualized) === true ? 1 : undefined),
|
|
579
|
+
}),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// ---------------------------------------------------------------------------
|
|
584
|
+
// A row
|
|
585
|
+
// ---------------------------------------------------------------------------
|
|
586
|
+
|
|
587
|
+
const strings = new StringDictionary<"expand" | "collapse" | "select">({
|
|
588
|
+
"ar-AE": { expand: "تمديد", collapse: "طي", select: "تحديد" },
|
|
589
|
+
"bg-BG": { expand: "Разширяване", collapse: "Свиване", select: "Изберете" },
|
|
590
|
+
"cs-CZ": { expand: "Roztáhnout", collapse: "Sbalit", select: "Vybrat" },
|
|
591
|
+
"da-DK": { expand: "Udvid", collapse: "Skjul", select: "Vælg" },
|
|
592
|
+
"de-DE": { expand: "Erweitern", collapse: "Reduzieren", select: "Auswählen" },
|
|
593
|
+
"el-GR": { expand: "Ανάπτυξη", collapse: "Σύμπτυξη", select: "Επιλογή" },
|
|
594
|
+
"en-US": { expand: "Expand", collapse: "Collapse", select: "Select" },
|
|
595
|
+
"es-ES": { expand: "Expandir", collapse: "Contraer", select: "Seleccionar" },
|
|
596
|
+
"et-EE": { expand: "Laienda", collapse: "Ahenda", select: "Vali" },
|
|
597
|
+
"fi-FI": { expand: "Laajenna", collapse: "Pienennä", select: "Valitse" },
|
|
598
|
+
"fr-FR": { expand: "Développer", collapse: "Réduire", select: "Sélectionner" },
|
|
599
|
+
"he-IL": { expand: "הרחב", collapse: "כווץ", select: "בחר" },
|
|
600
|
+
"hr-HR": { expand: "Proširi", collapse: "Sažmi", select: "Odaberite" },
|
|
601
|
+
"hu-HU": { expand: "Kibontás", collapse: "Összecsukás", select: "Kijelölés" },
|
|
602
|
+
"it-IT": { expand: "Espandi", collapse: "Comprimi", select: "Seleziona" },
|
|
603
|
+
"ja-JP": { expand: "展開", collapse: "折りたたむ", select: "選択" },
|
|
604
|
+
"ko-KR": { expand: "펼치기", collapse: "접기", select: "선택" },
|
|
605
|
+
"lt-LT": { expand: "Išskleisti", collapse: "Sutraukti", select: "Pasirinkti" },
|
|
606
|
+
"lv-LV": { expand: "Izvērst", collapse: "Sakļaut", select: "Atlasīt" },
|
|
607
|
+
"nb-NO": { expand: "Utvid", collapse: "Skjul", select: "Velg" },
|
|
608
|
+
"nl-NL": { expand: "Uitvouwen", collapse: "Samenvouwen", select: "Selecteren" },
|
|
609
|
+
"pl-PL": { expand: "Rozwiń", collapse: "Zwiń", select: "Zaznacz" },
|
|
610
|
+
"pt-BR": { expand: "Expandir", collapse: "Recolher", select: "Selecionar" },
|
|
611
|
+
"pt-PT": { expand: "Expandir", collapse: "Colapsar", select: "Selecionar" },
|
|
612
|
+
"ro-RO": { expand: "Extindeți", collapse: "Restrângeți", select: "Selectare" },
|
|
613
|
+
"ru-RU": { expand: "Развернуть", collapse: "Свернуть", select: "Выбрать" },
|
|
614
|
+
"sk-SK": { expand: "Rozbaliť", collapse: "Zbaliť", select: "Vybrať" },
|
|
615
|
+
"sl-SI": { expand: "Razširi", collapse: "Strni", select: "Izberite" },
|
|
616
|
+
"sr-SP": { expand: "Proširi", collapse: "Skupi", select: "Izaberite" },
|
|
617
|
+
"sv-SE": { expand: "Expandera", collapse: "Dölj", select: "Markera" },
|
|
618
|
+
"tr-TR": { expand: "Genişlet", collapse: "Daralt", select: "Seç" },
|
|
619
|
+
"uk-UA": { expand: "Розгорнути", collapse: "Згорнути", select: "Вибрати" },
|
|
620
|
+
"zh-CN": { expand: "扩展", collapse: "折叠", select: "选择" },
|
|
621
|
+
"zh-TW": { expand: "展開", collapse: "收合", select: "選取" },
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
export type TreeFocusMode = "row" | "child";
|
|
625
|
+
|
|
626
|
+
export interface TreeItemOptions {
|
|
627
|
+
key: Key;
|
|
628
|
+
ref: ElementRef;
|
|
629
|
+
baseId: MaybeAccessor<string>;
|
|
630
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
631
|
+
isVirtualized?: MaybeAccessor<boolean | undefined>;
|
|
632
|
+
shouldSelectOnPressUp?: MaybeAccessor<boolean | undefined>;
|
|
633
|
+
/** Expandable even before its children have been loaded. */
|
|
634
|
+
hasChildItems?: MaybeAccessor<boolean | undefined>;
|
|
635
|
+
/**
|
|
636
|
+
* Whether focus arriving on the row moves on to its first tabbable child.
|
|
637
|
+
* @default "row"
|
|
638
|
+
*/
|
|
639
|
+
focusMode?: MaybeAccessor<TreeFocusMode | undefined>;
|
|
640
|
+
/**
|
|
641
|
+
* Whether the arrow keys still move between rows, expand and collapse while
|
|
642
|
+
* focus is on a control inside the row, under `"tab"` navigation.
|
|
643
|
+
*/
|
|
644
|
+
allowsArrowNavigation?: MaybeAccessor<boolean | undefined>;
|
|
645
|
+
/** @default "arrow" */
|
|
646
|
+
keyboardNavigationBehavior?: MaybeAccessor<KeyboardNavigationBehavior | undefined>;
|
|
647
|
+
/** Whether the tree has an `onAction`. Read live: the handler is a prop. */
|
|
648
|
+
hasAction?: MaybeAccessor<boolean | undefined>;
|
|
649
|
+
onAction?: (key: Key) => void;
|
|
650
|
+
onPressStart?: (event: PressEvent) => void;
|
|
651
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
652
|
+
onPressUp?: (event: PressEvent) => void;
|
|
653
|
+
onPress?: (event: PressEvent) => void;
|
|
654
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
export interface TreeItemResult {
|
|
658
|
+
rowProps: DOMProps;
|
|
659
|
+
gridCellProps: DOMProps;
|
|
660
|
+
descriptionProps: DOMProps;
|
|
661
|
+
/** For the button that expands and collapses the row; see {@link TreeItemExpandButton}. */
|
|
662
|
+
expandButtonProps: ButtonOptions & DOMProps;
|
|
663
|
+
/** For a checkbox that selects the row; see {@link TreeItemSelectionCheckbox}. */
|
|
664
|
+
checkboxProps: {
|
|
665
|
+
id: Accessor<string>;
|
|
666
|
+
"aria-label": Accessor<string>;
|
|
667
|
+
"aria-labelledby": Accessor<string>;
|
|
668
|
+
isSelected: Accessor<boolean>;
|
|
669
|
+
isDisabled: Accessor<boolean>;
|
|
670
|
+
onChange: () => void;
|
|
671
|
+
};
|
|
672
|
+
/** Report that a description is rendered, for the row's name to include it. Returns the undo. */
|
|
673
|
+
registerDescription(): () => void;
|
|
674
|
+
isSelected: Accessor<boolean>;
|
|
675
|
+
isFocused: Accessor<boolean>;
|
|
676
|
+
isPressed: Accessor<boolean>;
|
|
677
|
+
isDisabled: Accessor<boolean>;
|
|
678
|
+
allowsSelection: Accessor<boolean>;
|
|
679
|
+
hasAction: Accessor<boolean>;
|
|
680
|
+
isExpanded: Accessor<boolean>;
|
|
681
|
+
hasChildItems: Accessor<boolean>;
|
|
682
|
+
/** Counting from 1, as `aria-level` does. */
|
|
683
|
+
level: Accessor<number>;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
export function treeItem(options: TreeItemOptions, state: TreeState<unknown>): TreeItemResult {
|
|
687
|
+
const locale = useLocale();
|
|
688
|
+
const message = stringFormatter(strings);
|
|
689
|
+
const key = options.key;
|
|
690
|
+
const manager = (): SelectionManager => state.selectionManager();
|
|
691
|
+
// The collection is rebuilt by every expansion; the node only when the items change.
|
|
692
|
+
const node = createMemo(() => state.collection().getItem(key));
|
|
693
|
+
const rowId = (): string => rowIdFor(access(options.baseId), key);
|
|
694
|
+
const behavior = (): KeyboardNavigationBehavior =>
|
|
695
|
+
access(options.keyboardNavigationBehavior) ?? "arrow";
|
|
696
|
+
const focusMode = (): TreeFocusMode => access(options.focusMode) ?? "row";
|
|
697
|
+
const allowsArrows = (): boolean => access(options.allowsArrowNavigation) === true;
|
|
698
|
+
const row = (): HTMLElement | null => (access(options.ref) as HTMLElement | null) ?? null;
|
|
699
|
+
|
|
700
|
+
const hasChildItems = createMemo(
|
|
701
|
+
() =>
|
|
702
|
+
access(options.hasChildItems) === true ||
|
|
703
|
+
node()?.props?.hasChildItems === true ||
|
|
704
|
+
(node()?.childNodes.length ?? 0) > 0,
|
|
705
|
+
);
|
|
706
|
+
const isExpanded = createMemo(() => hasChildItems() && state.expandedKeys().has(key));
|
|
707
|
+
const treeHasAction = (): boolean => access(options.hasAction) ?? options.onAction !== undefined;
|
|
708
|
+
const ownAction = (): unknown => node()?.props?.onAction;
|
|
709
|
+
|
|
710
|
+
// With nothing else for a press to do, pressing a parent row opens it.
|
|
711
|
+
const togglesOnAction = (): boolean =>
|
|
712
|
+
!treeHasAction() &&
|
|
713
|
+
!manager().isLink(key) &&
|
|
714
|
+
manager().selectionMode === "none" &&
|
|
715
|
+
hasChildItems();
|
|
716
|
+
|
|
717
|
+
const item = selectableItem({
|
|
718
|
+
key,
|
|
719
|
+
ref: options.ref,
|
|
720
|
+
id: rowId,
|
|
721
|
+
selectionManager: state.selectionManager,
|
|
722
|
+
shouldSelectOnPressUp: options.shouldSelectOnPressUp,
|
|
723
|
+
hasAction: () => treeHasAction() || typeof ownAction() === "function" || togglesOnAction(),
|
|
724
|
+
onAction: () => {
|
|
725
|
+
const own = ownAction();
|
|
726
|
+
if (typeof own === "function") (own as () => void)();
|
|
727
|
+
if (treeHasAction()) options.onAction?.(key);
|
|
728
|
+
else if (togglesOnAction()) state.toggleKey(key);
|
|
729
|
+
},
|
|
730
|
+
onPressStart: options.onPressStart,
|
|
731
|
+
onPressEnd: options.onPressEnd,
|
|
732
|
+
onPressUp: options.onPressUp,
|
|
733
|
+
onPress: options.onPress,
|
|
734
|
+
onPressChange: options.onPressChange,
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
// Written by a description from its own component body, when it mounts.
|
|
738
|
+
const [descriptions, setDescriptions] = createSignal(0, { ownedWrite: true });
|
|
739
|
+
const descriptionId = id();
|
|
740
|
+
|
|
741
|
+
/** Expand, collapse or step to the parent, if the key asks for one of those. */
|
|
742
|
+
const handleExpansionKeys = (
|
|
743
|
+
event: KeyboardEvent,
|
|
744
|
+
active: Element,
|
|
745
|
+
element: Element,
|
|
746
|
+
): boolean => {
|
|
747
|
+
if (!allowsArrows() && active !== element) return false;
|
|
748
|
+
if (manager().focusedKey !== key) return false;
|
|
749
|
+
const rtl = locale().direction === "rtl";
|
|
750
|
+
const expandKey = rtl ? "ArrowLeft" : "ArrowRight";
|
|
751
|
+
const collapseKey = rtl ? "ArrowRight" : "ArrowLeft";
|
|
752
|
+
const expanded = state.expandedKeys().has(key);
|
|
753
|
+
|
|
754
|
+
if (event.key === expandKey && hasChildItems() && !expanded) {
|
|
755
|
+
state.toggleKey(key);
|
|
756
|
+
event.stopPropagation();
|
|
757
|
+
return true;
|
|
758
|
+
}
|
|
759
|
+
if (event.key !== collapseKey) return false;
|
|
760
|
+
if (hasChildItems() && expanded) {
|
|
761
|
+
state.toggleKey(key);
|
|
762
|
+
event.stopPropagation();
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
const parentKey = node()?.parentKey;
|
|
766
|
+
if (
|
|
767
|
+
!expanded &&
|
|
768
|
+
parentKey !== undefined &&
|
|
769
|
+
state.collection().getItem(parentKey)?.type === "item"
|
|
770
|
+
) {
|
|
771
|
+
manager().setFocusedKey(parentKey);
|
|
772
|
+
event.stopPropagation();
|
|
773
|
+
return true;
|
|
774
|
+
}
|
|
775
|
+
return false;
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
const lastFocusable = (element: HTMLElement): HTMLElement | null => {
|
|
779
|
+
const walker = focusableWalker(element);
|
|
780
|
+
walker.currentNode = element;
|
|
781
|
+
return walker.last() as HTMLElement | null;
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Captured, not bubbled: a control inside the row would otherwise see the
|
|
786
|
+
* arrow keys first, a menu button taking Down and a text field taking Left.
|
|
787
|
+
*/
|
|
788
|
+
const onKeyDownCapture = (event: KeyboardEvent): void => {
|
|
789
|
+
if (behavior() !== "arrow" && !allowsArrows()) return;
|
|
790
|
+
const element = row();
|
|
791
|
+
if (element === null || !contains(element, targetElement(event))) return;
|
|
792
|
+
const active = activeElement(ownerDocument(element));
|
|
793
|
+
if (active === null) return;
|
|
794
|
+
|
|
795
|
+
if (handleExpansionKeys(event, active, element)) return;
|
|
796
|
+
|
|
797
|
+
const rtl = locale().direction === "rtl";
|
|
798
|
+
const inwards = rtl ? "ArrowLeft" : "ArrowRight";
|
|
799
|
+
const outwards = rtl ? "ArrowRight" : "ArrowLeft";
|
|
800
|
+
|
|
801
|
+
if (event.key === inwards || event.key === outwards) {
|
|
802
|
+
if (behavior() !== "arrow") return;
|
|
803
|
+
event.preventDefault();
|
|
804
|
+
event.stopPropagation();
|
|
805
|
+
|
|
806
|
+
const walker = focusableWalker(element);
|
|
807
|
+
walker.currentNode = active;
|
|
808
|
+
const next = (
|
|
809
|
+
event.key === inwards ? walker.nextNode() : walker.previousNode()
|
|
810
|
+
) as HTMLElement | null;
|
|
811
|
+
if (next !== null) {
|
|
812
|
+
focusSafely(next);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
// Off the end of the row: forwards lands back on the row itself,
|
|
816
|
+
// backwards wraps to the last control.
|
|
817
|
+
if (event.key === inwards) {
|
|
818
|
+
standOnRow = true;
|
|
819
|
+
focusSafely(element);
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
const last = lastFocusable(element);
|
|
823
|
+
if (last !== null) focusSafely(last);
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
if ((event.key === "ArrowUp" || event.key === "ArrowDown") && !event.altKey) {
|
|
828
|
+
// The collection's handler is on the tree, and this row is about to stop
|
|
829
|
+
// the event before a child sees it, so it is sent again from the parent.
|
|
830
|
+
event.preventDefault();
|
|
831
|
+
event.stopPropagation();
|
|
832
|
+
element.parentElement?.dispatchEvent(
|
|
833
|
+
new KeyboardEvent(event.type, {
|
|
834
|
+
key: event.key,
|
|
835
|
+
code: event.code,
|
|
836
|
+
altKey: event.altKey,
|
|
837
|
+
ctrlKey: event.ctrlKey,
|
|
838
|
+
metaKey: event.metaKey,
|
|
839
|
+
shiftKey: event.shiftKey,
|
|
840
|
+
bubbles: true,
|
|
841
|
+
cancelable: true,
|
|
842
|
+
}),
|
|
843
|
+
);
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
/** Answers whether it stopped the event, so the row's own press does not see it. */
|
|
848
|
+
const onKeyDown = (event: KeyboardEvent): boolean => {
|
|
849
|
+
const element = row();
|
|
850
|
+
if (element === null || !contains(element, targetElement(event))) return false;
|
|
851
|
+
const active = activeElement(ownerDocument(element));
|
|
852
|
+
if (active === null || behavior() !== "tab") return false;
|
|
853
|
+
|
|
854
|
+
// Under Tab navigation a key typed into a control in the row is the
|
|
855
|
+
// control's: it must not move rows, select, or start a typeahead. Tab
|
|
856
|
+
// still goes up, so the tree can let focus out.
|
|
857
|
+
if (targetElement(event) !== element && event.key !== "Tab") {
|
|
858
|
+
event.stopPropagation();
|
|
859
|
+
return true;
|
|
860
|
+
}
|
|
861
|
+
if (handleExpansionKeys(event, active, element)) return true;
|
|
862
|
+
|
|
863
|
+
if (event.key === "Tab") {
|
|
864
|
+
const walker = focusableWalker(element, { tabbable: true });
|
|
865
|
+
walker.currentNode = active;
|
|
866
|
+
const next = event.shiftKey ? walker.previousNode() : walker.nextNode();
|
|
867
|
+
if (next !== null) {
|
|
868
|
+
event.stopPropagation();
|
|
869
|
+
return true;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
return false;
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
const focusFirstChild = (element: HTMLElement): boolean => {
|
|
876
|
+
const walker = focusableWalker(element, { tabbable: true });
|
|
877
|
+
const first = walker.nextNode() as HTMLElement | null;
|
|
878
|
+
if (first === null) return false;
|
|
879
|
+
focusSafely(first);
|
|
880
|
+
scrollIntoViewport(first, { containingElement: scrollParents(element)[0] ?? null });
|
|
881
|
+
return true;
|
|
882
|
+
};
|
|
883
|
+
|
|
884
|
+
// Set when the arrow keys walk off the end of the row back onto it, which
|
|
885
|
+
// is the one way onto a child-focused row that must not move on again.
|
|
886
|
+
let standOnRow = false;
|
|
887
|
+
|
|
888
|
+
const onFocusIn = (event: FocusEvent): void => {
|
|
889
|
+
const element = row();
|
|
890
|
+
if (element === null) return;
|
|
891
|
+
if (targetElement(event) !== element) {
|
|
892
|
+
// A control inside the row was focused by pointer: the row is where the
|
|
893
|
+
// arrow keys now work from. From the keyboard the focused row already is.
|
|
894
|
+
if (!focusIsVisible()) manager().setFocusedKey(key);
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
const stay = standOnRow;
|
|
898
|
+
standOnRow = false;
|
|
899
|
+
if (focusMode() !== "child" || stay) return;
|
|
900
|
+
// Arriving from its own child is `selectableItem` focusing the row it
|
|
901
|
+
// knows about; the child is the row's stand-in, so focus goes back.
|
|
902
|
+
focusFirstChild(element);
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
const navigate = router();
|
|
906
|
+
const linkData = (name: string): (() => unknown) => {
|
|
907
|
+
return () => {
|
|
908
|
+
const props = node()?.props;
|
|
909
|
+
if (props?.href === undefined || !item.hasAction()) return undefined;
|
|
910
|
+
return name === "href" ? navigate.href(props.href as string) : props[name];
|
|
911
|
+
};
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
const parentLevelSize = (): number => state.collection().setSizeOf(key);
|
|
915
|
+
|
|
916
|
+
const rowProps = mergeProps(item.itemProps, {
|
|
917
|
+
role: "row",
|
|
918
|
+
id: rowId,
|
|
919
|
+
onKeyDownCapture,
|
|
920
|
+
onFocusIn,
|
|
921
|
+
"data-href": linkData("href"),
|
|
922
|
+
"data-target": linkData("target"),
|
|
923
|
+
"data-rel": linkData("rel"),
|
|
924
|
+
"data-download": linkData("download"),
|
|
925
|
+
"data-ping": linkData("ping"),
|
|
926
|
+
"data-referrer-policy": linkData("referrerPolicy"),
|
|
927
|
+
"aria-label": () =>
|
|
928
|
+
access(options["aria-label"]) ??
|
|
929
|
+
(node()?.["aria-label"] || undefined) ??
|
|
930
|
+
(node()?.textValue || undefined),
|
|
931
|
+
"aria-labelledby": () => {
|
|
932
|
+
const named =
|
|
933
|
+
access(options["aria-label"]) !== undefined ||
|
|
934
|
+
Boolean(node()?.["aria-label"]) ||
|
|
935
|
+
Boolean(node()?.textValue);
|
|
936
|
+
return descriptions() > 0 && named ? `${rowId()} ${descriptionId()}` : undefined;
|
|
937
|
+
},
|
|
938
|
+
"aria-selected": () => (manager().canSelectItem(key) ? item.isSelected() : undefined),
|
|
939
|
+
"aria-disabled": () => manager().isDisabled(key) || undefined,
|
|
940
|
+
"aria-expanded": () => (hasChildItems() ? isExpanded() : undefined),
|
|
941
|
+
"aria-level": () => (node()?.level ?? 0) + 1,
|
|
942
|
+
"aria-posinset": () => (node()?.index ?? 0) + 1,
|
|
943
|
+
"aria-setsize": parentLevelSize,
|
|
944
|
+
"aria-rowindex": () =>
|
|
945
|
+
access(options.isVirtualized) === true ? state.collection().rowIndexOf(key) : undefined,
|
|
946
|
+
// Focus that the row hands on to a child must not be taken back.
|
|
947
|
+
tabIndex: () => {
|
|
948
|
+
if (focusMode() === "child" && allowsArrows() && behavior() === "tab") return -1;
|
|
949
|
+
return access(item.itemProps.tabIndex as MaybeAccessor<number | undefined>);
|
|
950
|
+
},
|
|
951
|
+
});
|
|
952
|
+
|
|
953
|
+
const pressKeyDown = rowProps.onKeyDown as ((event: KeyboardEvent) => void) | undefined;
|
|
954
|
+
rowProps.onKeyDown = (event: KeyboardEvent): void => {
|
|
955
|
+
if (!onKeyDown(event)) pressKeyDown?.(event);
|
|
956
|
+
};
|
|
957
|
+
|
|
958
|
+
const expandButtonId = id();
|
|
959
|
+
const checkboxId = id();
|
|
960
|
+
|
|
961
|
+
return {
|
|
962
|
+
rowProps,
|
|
963
|
+
gridCellProps: { role: "gridcell", "aria-colindex": 1 },
|
|
964
|
+
descriptionProps: { id: descriptionId },
|
|
965
|
+
expandButtonProps: {
|
|
966
|
+
id: expandButtonId,
|
|
967
|
+
onPress: () => {
|
|
968
|
+
if (item.isDisabled()) return;
|
|
969
|
+
state.toggleKey(key);
|
|
970
|
+
manager().setFocused(true);
|
|
971
|
+
manager().setFocusedKey(key);
|
|
972
|
+
},
|
|
973
|
+
excludeFromTabOrder: true,
|
|
974
|
+
preventFocusOnPress: true,
|
|
975
|
+
[PREVENT_FOCUS_ATTRIBUTE]: true,
|
|
976
|
+
"aria-label": () => message().format(isExpanded() ? "collapse" : "expand"),
|
|
977
|
+
"aria-labelledby": () => `${expandButtonId()} ${rowId()}`,
|
|
978
|
+
},
|
|
979
|
+
checkboxProps: {
|
|
980
|
+
id: checkboxId,
|
|
981
|
+
"aria-label": () => message().format("select"),
|
|
982
|
+
"aria-labelledby": () => `${checkboxId()} ${rowId()}`,
|
|
983
|
+
isSelected: item.isSelected,
|
|
984
|
+
isDisabled: () => !manager().canSelectItem(key),
|
|
985
|
+
onChange: () => manager().toggleSelection(key),
|
|
986
|
+
},
|
|
987
|
+
registerDescription: () => {
|
|
988
|
+
setDescriptions((count) => count + 1);
|
|
989
|
+
return () => setDescriptions((count) => count - 1);
|
|
990
|
+
},
|
|
991
|
+
isSelected: item.isSelected,
|
|
992
|
+
isFocused: item.isFocused,
|
|
993
|
+
isPressed: item.isPressed,
|
|
994
|
+
isDisabled: item.isDisabled,
|
|
995
|
+
allowsSelection: item.allowsSelection,
|
|
996
|
+
hasAction: item.hasAction,
|
|
997
|
+
isExpanded,
|
|
998
|
+
hasChildItems,
|
|
999
|
+
level: () => (node()?.level ?? 0) + 1,
|
|
1000
|
+
};
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// ---------------------------------------------------------------------------
|
|
1004
|
+
// Components
|
|
1005
|
+
// ---------------------------------------------------------------------------
|
|
1006
|
+
|
|
1007
|
+
interface TreeContextValue {
|
|
1008
|
+
state: TreeState<unknown>;
|
|
1009
|
+
baseId: Accessor<string>;
|
|
1010
|
+
render: (item: unknown) => Child;
|
|
1011
|
+
virtual: VirtualizerValue | null;
|
|
1012
|
+
keyboardNavigationBehavior: Accessor<KeyboardNavigationBehavior>;
|
|
1013
|
+
shouldSelectOnPressUp: Accessor<boolean | undefined>;
|
|
1014
|
+
onAction: Accessor<((key: Key) => void) | undefined>;
|
|
1015
|
+
/** The tree's drag and drop, when it was given `dragAndDropHooks`. */
|
|
1016
|
+
dragAndDrop?: CollectionDragAndDrop | undefined;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
const TreeContext = createContext<TreeContextValue | null>(null);
|
|
1020
|
+
const TreeNodeContext = createContext<Node<unknown> | null>(null);
|
|
1021
|
+
|
|
1022
|
+
/** The tree the component being built is inside. */
|
|
1023
|
+
export function useTree(): TreeContextValue {
|
|
1024
|
+
const value = getContext(TreeContext);
|
|
1025
|
+
if (value === null || value === undefined) {
|
|
1026
|
+
throw new Error("A TreeItem must be rendered inside a Tree.");
|
|
1027
|
+
}
|
|
1028
|
+
return value;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** The collection node the row being built is for. */
|
|
1032
|
+
export function useTreeNode(): Node<unknown> {
|
|
1033
|
+
const node = getContext(TreeNodeContext);
|
|
1034
|
+
if (node === null || node === undefined) {
|
|
1035
|
+
throw new Error("A TreeItem must be rendered inside a Tree's item callback.");
|
|
1036
|
+
}
|
|
1037
|
+
return node;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/** What a tree hands to its empty state. Every field follows the tree. */
|
|
1041
|
+
export interface TreeRenderState {
|
|
1042
|
+
isFocused: Accessor<boolean>;
|
|
1043
|
+
isFocusVisible: Accessor<boolean>;
|
|
1044
|
+
selectionMode: Accessor<SelectionMode>;
|
|
1045
|
+
/** Whether a drop would land on the tree itself. */
|
|
1046
|
+
isDropTarget: Accessor<boolean>;
|
|
1047
|
+
/** Whether its rows can be dragged. */
|
|
1048
|
+
allowsDragging: Accessor<boolean>;
|
|
1049
|
+
state: TreeState<unknown>;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
export interface TreeComponentProps<T> extends StyleProps, ItemAccessors<T> {
|
|
1053
|
+
/** The top-level items. Each one's children are read with `getChildren`. */
|
|
1054
|
+
items: Iterable<T>;
|
|
1055
|
+
/** How one item renders: a `<TreeItem>`, a `<TreeSection>` or a `<TreeLoadMoreItem>`. */
|
|
1056
|
+
children: (item: T) => Child;
|
|
1057
|
+
/** @default "none" */
|
|
1058
|
+
selectionMode?: SelectionMode;
|
|
1059
|
+
/** @default "toggle" */
|
|
1060
|
+
selectionBehavior?: SelectionBehavior;
|
|
1061
|
+
selectedKeys?: "all" | Iterable<Key>;
|
|
1062
|
+
defaultSelectedKeys?: "all" | Iterable<Key>;
|
|
1063
|
+
disabledKeys?: Iterable<Key>;
|
|
1064
|
+
/** Whether `disabledKeys` stops everything, or only selection. @default "all" */
|
|
1065
|
+
disabledBehavior?: DisabledBehavior;
|
|
1066
|
+
disallowEmptySelection?: boolean;
|
|
1067
|
+
expandedKeys?: Iterable<Key>;
|
|
1068
|
+
defaultExpandedKeys?: Iterable<Key>;
|
|
1069
|
+
autoFocus?: boolean | "first" | "last";
|
|
1070
|
+
/** @default "arrow" */
|
|
1071
|
+
keyboardNavigationBehavior?: KeyboardNavigationBehavior;
|
|
1072
|
+
/** @default "clearSelection" */
|
|
1073
|
+
escapeKeyBehavior?: "clearSelection" | "none";
|
|
1074
|
+
shouldSelectOnPressUp?: boolean;
|
|
1075
|
+
/** What to show when there are no items. */
|
|
1076
|
+
renderEmptyState?: (tree: TreeRenderState) => Child;
|
|
1077
|
+
"aria-label"?: string;
|
|
1078
|
+
"aria-labelledby"?: string;
|
|
1079
|
+
"aria-describedby"?: string;
|
|
1080
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1081
|
+
onSelectionChange?: (keys: "all" | Set<Key>) => void;
|
|
1082
|
+
onExpandedChange?: (keys: Set<Key>) => void;
|
|
1083
|
+
onAction?: (key: Key) => void;
|
|
1084
|
+
/**
|
|
1085
|
+
* Drag and drop, from `dragAndDrop()`. A draggable row needs a
|
|
1086
|
+
* `<DragButton>` for keyboard and screen reader users to drag it with.
|
|
1087
|
+
*/
|
|
1088
|
+
dragAndDropHooks?: DragAndDropHooks;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
const NO_ROWS: Node<unknown>[] = [];
|
|
1092
|
+
|
|
1093
|
+
/** A row followed, while it is expanded, by its descendants. */
|
|
1094
|
+
function TreeRow(props: { node: Node<unknown> }) {
|
|
1095
|
+
const root = useTree();
|
|
1096
|
+
const node = props.node;
|
|
1097
|
+
const owner = getOwner();
|
|
1098
|
+
if (owner !== null) install(owner, TreeNodeContext, () => node);
|
|
1099
|
+
const drawn = root.render(node.value);
|
|
1100
|
+
if (node.type !== "item" || root.virtual !== null) return <>{drawn}</>;
|
|
1101
|
+
return (
|
|
1102
|
+
<>
|
|
1103
|
+
{drawn}
|
|
1104
|
+
<TreeRows nodes={root.state.expandedKeys().has(node.key) ? node.childNodes : NO_ROWS} />
|
|
1105
|
+
</>
|
|
1106
|
+
);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
function TreeRows(props: { nodes: Node<unknown>[] }) {
|
|
1110
|
+
return <For each={props.nodes}>{(node: Node<unknown>) => <TreeRow node={node} />}</For>;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* ```tsx
|
|
1115
|
+
* <Tree aria-label="Files" items={files()} selectionMode="multiple">
|
|
1116
|
+
* {(file) => (
|
|
1117
|
+
* <TreeItem>
|
|
1118
|
+
* <TreeItemContent>{file.name}</TreeItemContent>
|
|
1119
|
+
* </TreeItem>
|
|
1120
|
+
* )}
|
|
1121
|
+
* </Tree>
|
|
1122
|
+
* ```
|
|
1123
|
+
*
|
|
1124
|
+
* Expansion is `expandedKeys` or `defaultExpandedKeys`, reported through
|
|
1125
|
+
* `onExpandedChange`. A row with no selection, action or link to perform
|
|
1126
|
+
* expands when pressed.
|
|
1127
|
+
*/
|
|
1128
|
+
export function Tree<T>(props: TreeComponentProps<T>) {
|
|
1129
|
+
const domRef = makeRef<HTMLDivElement>();
|
|
1130
|
+
const options = fromProps(cells(props) as unknown as Incoming<Record<string, unknown>>);
|
|
1131
|
+
const virtual = useVirtualizer();
|
|
1132
|
+
|
|
1133
|
+
const state = treeState<T>({
|
|
1134
|
+
...(options as TreeStateOptions<T>),
|
|
1135
|
+
layoutDelegate: virtual?.layout,
|
|
1136
|
+
onSelectionChange: (keys) => props.onSelectionChange?.(keys),
|
|
1137
|
+
onExpandedChange: (keys) => props.onExpandedChange?.(keys),
|
|
1138
|
+
});
|
|
1139
|
+
const anyState = state as unknown as TreeState<unknown>;
|
|
1140
|
+
|
|
1141
|
+
const dropping: { held?: CollectionDragAndDrop } = {};
|
|
1142
|
+
virtual?.attach(
|
|
1143
|
+
state.collection,
|
|
1144
|
+
domRef,
|
|
1145
|
+
() => state.selectionManager().focusedKey,
|
|
1146
|
+
() => dropping.held?.persistedKeys() ?? [],
|
|
1147
|
+
);
|
|
1148
|
+
|
|
1149
|
+
const isEmpty = (): boolean => state.collection().size === 0;
|
|
1150
|
+
const [emptyStateIsTabbable, setEmptyStateIsTabbable] = createSignal(false);
|
|
1151
|
+
if (!isServer) {
|
|
1152
|
+
createEffect(
|
|
1153
|
+
() => [isEmpty(), domRef()] as const,
|
|
1154
|
+
([empty, element]) => {
|
|
1155
|
+
setEmptyStateIsTabbable(
|
|
1156
|
+
empty &&
|
|
1157
|
+
element !== null &&
|
|
1158
|
+
focusableWalker(element, { tabbable: true }).nextNode() !== null,
|
|
1159
|
+
);
|
|
1160
|
+
},
|
|
1161
|
+
);
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
const { gridProps, baseId } = tree(
|
|
1165
|
+
{
|
|
1166
|
+
...(options as unknown as TreeOptions),
|
|
1167
|
+
ref: domRef,
|
|
1168
|
+
isVirtualized: () => virtual !== null,
|
|
1169
|
+
layoutDelegate: virtual?.layout,
|
|
1170
|
+
emptyStateIsTabbable,
|
|
1171
|
+
},
|
|
1172
|
+
anyState,
|
|
1173
|
+
);
|
|
1174
|
+
|
|
1175
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
1176
|
+
const selectionMode = (): SelectionMode => state.selectionManager().selectionMode;
|
|
1177
|
+
const dragAndDrop = treeDragAndDrop(props.dragAndDropHooks, anyState, domRef, virtual);
|
|
1178
|
+
dropping.held = dragAndDrop;
|
|
1179
|
+
const isDropTarget = (): boolean => dragAndDrop?.isRootDropTarget() === true;
|
|
1180
|
+
const allowsDragging = (): boolean =>
|
|
1181
|
+
dragAndDrop?.dragState !== undefined && !dragAndDrop.dragState.isDisabled();
|
|
1182
|
+
|
|
1183
|
+
const value: TreeContextValue = {
|
|
1184
|
+
state: anyState,
|
|
1185
|
+
baseId,
|
|
1186
|
+
render: props.children as unknown as (item: unknown) => Child,
|
|
1187
|
+
virtual,
|
|
1188
|
+
keyboardNavigationBehavior: () => props.keyboardNavigationBehavior ?? "arrow",
|
|
1189
|
+
shouldSelectOnPressUp: () => props.shouldSelectOnPressUp,
|
|
1190
|
+
onAction: () => props.onAction,
|
|
1191
|
+
dragAndDrop,
|
|
1192
|
+
};
|
|
1193
|
+
|
|
1194
|
+
const elementProps = mergeProps(
|
|
1195
|
+
gridProps,
|
|
1196
|
+
dragAndDrop?.collectionProps,
|
|
1197
|
+
focusProps,
|
|
1198
|
+
filterDOMProps(fromProps(cells(props)), { global: true }),
|
|
1199
|
+
styleProps(cells(props)),
|
|
1200
|
+
{
|
|
1201
|
+
"data-empty": isEmpty,
|
|
1202
|
+
"data-focused": isFocused,
|
|
1203
|
+
"data-focus-visible": isFocusVisible,
|
|
1204
|
+
"data-selection-mode": () => (selectionMode() === "none" ? undefined : selectionMode()),
|
|
1205
|
+
"data-virtualized": () => virtual !== null,
|
|
1206
|
+
"data-drop-target": isDropTarget,
|
|
1207
|
+
"data-allows-dragging": allowsDragging,
|
|
1208
|
+
style: () => (virtual === null ? props.style : contentStyle(virtual.contentSize())),
|
|
1209
|
+
},
|
|
1210
|
+
);
|
|
1211
|
+
|
|
1212
|
+
const windowed = (): Node<unknown>[] => {
|
|
1213
|
+
if (virtual === null) return NO_ROWS;
|
|
1214
|
+
const collection = state.collection();
|
|
1215
|
+
return virtual
|
|
1216
|
+
.visible()
|
|
1217
|
+
.map((info: LayoutInfo) => collection.getItem(info.key) as Node<unknown> | null)
|
|
1218
|
+
.filter((node: Node<unknown> | null): node is Node<unknown> => node !== null);
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
const renderState: TreeRenderState = {
|
|
1222
|
+
isFocused,
|
|
1223
|
+
isFocusVisible,
|
|
1224
|
+
selectionMode,
|
|
1225
|
+
isDropTarget,
|
|
1226
|
+
allowsDragging,
|
|
1227
|
+
state: anyState,
|
|
1228
|
+
};
|
|
1229
|
+
const renderEmptyState = callback(cells(props).renderEmptyState);
|
|
1230
|
+
|
|
1231
|
+
return provide(
|
|
1232
|
+
TreeContext,
|
|
1233
|
+
() => value,
|
|
1234
|
+
() => (
|
|
1235
|
+
<>
|
|
1236
|
+
<div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
|
|
1237
|
+
{dragAndDrop?.dropState === undefined ? null : <TreeRootDropIndicator />}
|
|
1238
|
+
{virtual === null ? (
|
|
1239
|
+
<TreeRows nodes={state.collection().rootNodes as Node<unknown>[]} />
|
|
1240
|
+
) : (
|
|
1241
|
+
<TreeRows nodes={windowed()} />
|
|
1242
|
+
)}
|
|
1243
|
+
{() =>
|
|
1244
|
+
isEmpty() && renderEmptyState !== undefined ? (
|
|
1245
|
+
<div role="row" aria-level={1}>
|
|
1246
|
+
<div role="gridcell">{renderEmptyState(renderState)}</div>
|
|
1247
|
+
</div>
|
|
1248
|
+
) : null
|
|
1249
|
+
}
|
|
1250
|
+
</div>
|
|
1251
|
+
{dragAndDrop?.preview}
|
|
1252
|
+
</>
|
|
1253
|
+
),
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
const EXPAND_KEYS = { ltr: "ArrowRight", rtl: "ArrowLeft" } as const;
|
|
1258
|
+
const COLLAPSE_KEYS = { ltr: "ArrowLeft", rtl: "ArrowRight" } as const;
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* The tree's drag and drop: the flat machinery, with a tree's reading of the
|
|
1262
|
+
* pointer, and expansion as the answer to holding a drag over a row.
|
|
1263
|
+
*/
|
|
1264
|
+
function treeDragAndDrop(
|
|
1265
|
+
hooks: DragAndDropHooks | undefined,
|
|
1266
|
+
state: TreeState<unknown>,
|
|
1267
|
+
domRef: Accessor<HTMLDivElement | null>,
|
|
1268
|
+
virtual: VirtualizerValue | null,
|
|
1269
|
+
): CollectionDragAndDrop | undefined {
|
|
1270
|
+
if (hooks === undefined) return undefined;
|
|
1271
|
+
const layoutDelegate = virtual?.layout;
|
|
1272
|
+
const locale = useLocale();
|
|
1273
|
+
const direction = (): "ltr" | "rtl" => locale().direction;
|
|
1274
|
+
const collection = state.collection as Accessor<Collection<unknown>>;
|
|
1275
|
+
const hasChildren = (key: Key): boolean => {
|
|
1276
|
+
const node = state.collection().getItem(key);
|
|
1277
|
+
return node !== null && (node.hasChildNodes || node.props?.hasChildItems === true);
|
|
1278
|
+
};
|
|
1279
|
+
|
|
1280
|
+
const result: CollectionDragAndDrop = collectionDragAndDrop({
|
|
1281
|
+
virtual,
|
|
1282
|
+
hooks,
|
|
1283
|
+
collection,
|
|
1284
|
+
selectionManager: state.selectionManager,
|
|
1285
|
+
ref: domRef,
|
|
1286
|
+
// A position next to a disabled row is still a place to drop.
|
|
1287
|
+
keyboardDelegate: new ListKeyboardDelegate({
|
|
1288
|
+
collection,
|
|
1289
|
+
ref: domRef,
|
|
1290
|
+
disabledBehavior: () => "selection",
|
|
1291
|
+
direction,
|
|
1292
|
+
layoutDelegate,
|
|
1293
|
+
}),
|
|
1294
|
+
direction,
|
|
1295
|
+
dropTargetDelegate: (base) =>
|
|
1296
|
+
new TreeDropTargetDelegate(base, { collection, expandedKeys: state.expandedKeys }, direction),
|
|
1297
|
+
// Held over a collapsed row, the drag opens it. A screen reader user,
|
|
1298
|
+
// who cannot see the rows, may close it again the same way.
|
|
1299
|
+
onDropActivate: (event: DroppableCollectionActivateEvent) => {
|
|
1300
|
+
if (event.target.type !== "item") return;
|
|
1301
|
+
const key = event.target.key;
|
|
1302
|
+
const expanded = state.expandedKeys().has(key);
|
|
1303
|
+
if (hasChildren(key) && (!expanded || hooks.isVirtualDragging?.() === true)) {
|
|
1304
|
+
state.toggleKey(key);
|
|
1305
|
+
}
|
|
1306
|
+
},
|
|
1307
|
+
onKeyDown: (event: KeyboardEvent) => {
|
|
1308
|
+
const target = result.dropState?.target() ?? null;
|
|
1309
|
+
if (target?.type !== "item" || target.dropPosition !== "on") return;
|
|
1310
|
+
if (!hasChildren(target.key)) return;
|
|
1311
|
+
const expanded = state.expandedKeys().has(target.key);
|
|
1312
|
+
if (event.key === EXPAND_KEYS[direction()] && !expanded) state.toggleKey(target.key);
|
|
1313
|
+
else if (event.key === COLLAPSE_KEYS[direction()] && expanded) state.toggleKey(target.key);
|
|
1314
|
+
},
|
|
1315
|
+
});
|
|
1316
|
+
|
|
1317
|
+
if (result.dropState !== undefined) {
|
|
1318
|
+
provideDropIndicator({ render: (props) => <TreeDropIndicator of={props} /> });
|
|
1319
|
+
}
|
|
1320
|
+
return result;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
/** A drop indicator in a tree: a row at the level of the row it is beside. */
|
|
1324
|
+
function TreeDropIndicator(indicator: { of: Incoming<DropIndicatorProps> }) {
|
|
1325
|
+
const props = indicator.of;
|
|
1326
|
+
const { dragAndDropHooks, dropState } = useDragAndDropContext();
|
|
1327
|
+
if (dragAndDropHooks?.dropIndicator === undefined || dropState === undefined) {
|
|
1328
|
+
throw new Error("A DropIndicator must be rendered inside a droppable Tree.");
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
const target = props.target();
|
|
1332
|
+
const buttonRef = makeRef<HTMLDivElement>();
|
|
1333
|
+
const virtual = useVirtualizer();
|
|
1334
|
+
const { visuallyHiddenProps } = visuallyHidden();
|
|
1335
|
+
const { dropIndicatorProps, isDropTarget, isHidden } = dragAndDropHooks.dropIndicator(
|
|
1336
|
+
{ target },
|
|
1337
|
+
dropState,
|
|
1338
|
+
buttonRef,
|
|
1339
|
+
);
|
|
1340
|
+
const level = (): number =>
|
|
1341
|
+
target.type === "item" ? (dropState.collection().getItem(target.key)?.level ?? 0) + 1 : 1;
|
|
1342
|
+
|
|
1343
|
+
return (
|
|
1344
|
+
<Show when={!isHidden()}>
|
|
1345
|
+
<div
|
|
1346
|
+
class={props.class?.() ?? props.className?.()}
|
|
1347
|
+
style={{
|
|
1348
|
+
position: "relative",
|
|
1349
|
+
"--tree-item-level": String(level()),
|
|
1350
|
+
...props.style?.(),
|
|
1351
|
+
...(target.type === "item" ? dropIndicatorStyle(virtual, target) : undefined),
|
|
1352
|
+
}}
|
|
1353
|
+
role="row"
|
|
1354
|
+
aria-level={level()}
|
|
1355
|
+
data-slot={props["data-slot"]?.() ?? "drop-indicator"}
|
|
1356
|
+
data-drop-target={isDropTarget()}
|
|
1357
|
+
>
|
|
1358
|
+
<div role="gridcell">
|
|
1359
|
+
<div {...visuallyHiddenProps} role="button" {...dropIndicatorProps} ref={buttonRef.set} />
|
|
1360
|
+
{props.children?.()}
|
|
1361
|
+
</div>
|
|
1362
|
+
</div>
|
|
1363
|
+
</Show>
|
|
1364
|
+
);
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
/** The target for a drop on the whole tree, there for keyboard and screen reader drags. */
|
|
1368
|
+
function TreeRootDropIndicator() {
|
|
1369
|
+
const { dragAndDropHooks, dropState } = useDragAndDropContext();
|
|
1370
|
+
if (dragAndDropHooks?.dropIndicator === undefined || dropState === undefined) return null;
|
|
1371
|
+
|
|
1372
|
+
const buttonRef = makeRef<HTMLDivElement>();
|
|
1373
|
+
const { visuallyHiddenProps } = visuallyHidden();
|
|
1374
|
+
const { dropIndicatorProps, isDropTarget } = dragAndDropHooks.dropIndicator(
|
|
1375
|
+
{ target: { type: "root" } },
|
|
1376
|
+
dropState,
|
|
1377
|
+
buttonRef,
|
|
1378
|
+
);
|
|
1379
|
+
const ariaHidden = dropIndicatorProps["aria-hidden"] as Accessor<string | undefined>;
|
|
1380
|
+
|
|
1381
|
+
return (
|
|
1382
|
+
<Show when={isDropTarget() || ariaHidden() === undefined}>
|
|
1383
|
+
<div
|
|
1384
|
+
role="row"
|
|
1385
|
+
aria-hidden={ariaHidden() === "true" ? true : undefined}
|
|
1386
|
+
style={{ position: "absolute" }}
|
|
1387
|
+
>
|
|
1388
|
+
<div role="gridcell">
|
|
1389
|
+
<div {...visuallyHiddenProps} role="button" {...dropIndicatorProps} ref={buttonRef.set} />
|
|
1390
|
+
</div>
|
|
1391
|
+
</div>
|
|
1392
|
+
</Show>
|
|
1393
|
+
);
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
// ---------------------------------------------------------------------------
|
|
1397
|
+
// Items
|
|
1398
|
+
// ---------------------------------------------------------------------------
|
|
1399
|
+
|
|
1400
|
+
/** What a row hands to the elements drawn inside it. Every field follows the row. */
|
|
1401
|
+
export interface TreeItemRenderState {
|
|
1402
|
+
id: Key;
|
|
1403
|
+
level: Accessor<number>;
|
|
1404
|
+
isExpanded: Accessor<boolean>;
|
|
1405
|
+
hasChildItems: Accessor<boolean>;
|
|
1406
|
+
isSelected: Accessor<boolean>;
|
|
1407
|
+
isFocused: Accessor<boolean>;
|
|
1408
|
+
isFocusVisible: Accessor<boolean>;
|
|
1409
|
+
isFocusVisibleWithin: Accessor<boolean>;
|
|
1410
|
+
isPressed: Accessor<boolean>;
|
|
1411
|
+
isHovered: Accessor<boolean>;
|
|
1412
|
+
isDisabled: Accessor<boolean>;
|
|
1413
|
+
allowsSelection: Accessor<boolean>;
|
|
1414
|
+
hasAction: Accessor<boolean>;
|
|
1415
|
+
selectionMode: Accessor<SelectionMode>;
|
|
1416
|
+
selectionBehavior: Accessor<SelectionBehavior>;
|
|
1417
|
+
/** Whether the tree's rows can be dragged. */
|
|
1418
|
+
allowsDragging: Accessor<boolean>;
|
|
1419
|
+
isDragging: Accessor<boolean>;
|
|
1420
|
+
/** Whether a drop would land on this row. */
|
|
1421
|
+
isDropTarget: Accessor<boolean>;
|
|
1422
|
+
state: TreeState<unknown>;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
interface TreeItemContextValue {
|
|
1426
|
+
render: TreeItemRenderState;
|
|
1427
|
+
item: TreeItemResult;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
const TreeItemContext = createContext<TreeItemContextValue | null>(null);
|
|
1431
|
+
|
|
1432
|
+
/** The row the part being built is inside. */
|
|
1433
|
+
export function useTreeItem(): TreeItemContextValue {
|
|
1434
|
+
const value = getContext(TreeItemContext);
|
|
1435
|
+
if (value === null || value === undefined) {
|
|
1436
|
+
throw new Error("This must be rendered inside a TreeItem.");
|
|
1437
|
+
}
|
|
1438
|
+
return value;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
export interface TreeItemComponentProps extends StyleProps {
|
|
1442
|
+
children?: Child;
|
|
1443
|
+
"aria-label"?: string;
|
|
1444
|
+
/** Expandable even before its children have loaded. */
|
|
1445
|
+
hasChildItems?: boolean;
|
|
1446
|
+
/** @default "row" */
|
|
1447
|
+
focusMode?: TreeFocusMode;
|
|
1448
|
+
/** Let the arrow keys work from a control inside the row under `"tab"` navigation. */
|
|
1449
|
+
allowsArrowNavigation?: boolean;
|
|
1450
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1451
|
+
/** Performed on this row, as well as the tree's own `onAction`. */
|
|
1452
|
+
onAction?: () => void;
|
|
1453
|
+
onPress?: (event: PressEvent) => void;
|
|
1454
|
+
onPressStart?: (event: PressEvent) => void;
|
|
1455
|
+
onPressEnd?: (event: PressEvent) => void;
|
|
1456
|
+
onPressUp?: (event: PressEvent) => void;
|
|
1457
|
+
onPressChange?: (isPressed: boolean) => void;
|
|
1458
|
+
onHoverStart?: (event: HoverEvent) => void;
|
|
1459
|
+
onHoverEnd?: (event: HoverEvent) => void;
|
|
1460
|
+
onHoverChange?: (isHovered: boolean) => void;
|
|
1461
|
+
onFocusChange?: (isFocused: boolean) => void;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
/**
|
|
1465
|
+
* One row, holding one cell. Its key, value, level and disabled state come
|
|
1466
|
+
* from the collection node the {@link Tree} is rendering.
|
|
1467
|
+
*
|
|
1468
|
+
* `--tree-item-level` is set on it, counting from 1, for indentation.
|
|
1469
|
+
*/
|
|
1470
|
+
export function TreeItem(props: TreeItemComponentProps) {
|
|
1471
|
+
const domRef = makeRef<HTMLDivElement>();
|
|
1472
|
+
const root = useTree();
|
|
1473
|
+
const node = useTreeNode();
|
|
1474
|
+
const options = fromProps(cells(props));
|
|
1475
|
+
const focusMode = (): TreeFocusMode => props.focusMode ?? "row";
|
|
1476
|
+
const dnd = treeRowDragAndDrop(root, node.key);
|
|
1477
|
+
|
|
1478
|
+
measureRow(root.virtual, node.key, domRef);
|
|
1479
|
+
|
|
1480
|
+
const item = treeItem(
|
|
1481
|
+
{
|
|
1482
|
+
key: node.key,
|
|
1483
|
+
ref: domRef,
|
|
1484
|
+
baseId: root.baseId,
|
|
1485
|
+
"aria-label": () => props["aria-label"],
|
|
1486
|
+
isVirtualized: () => root.virtual !== null,
|
|
1487
|
+
// Selecting on release lets a press that becomes a drag leave the
|
|
1488
|
+
// selection alone, unless only the drag button starts a drag.
|
|
1489
|
+
shouldSelectOnPressUp: () =>
|
|
1490
|
+
root.shouldSelectOnPressUp() === true ||
|
|
1491
|
+
(dnd?.collection.dragState !== undefined &&
|
|
1492
|
+
dnd.collection.hooks?.pointerDragSource !== "handle") ||
|
|
1493
|
+
undefined,
|
|
1494
|
+
hasChildItems: () => props.hasChildItems,
|
|
1495
|
+
focusMode,
|
|
1496
|
+
allowsArrowNavigation: () => props.allowsArrowNavigation,
|
|
1497
|
+
keyboardNavigationBehavior: root.keyboardNavigationBehavior,
|
|
1498
|
+
hasAction: () => root.onAction() !== undefined || props.onAction !== undefined,
|
|
1499
|
+
onAction: (key) => {
|
|
1500
|
+
props.onAction?.();
|
|
1501
|
+
root.onAction()?.(key);
|
|
1502
|
+
},
|
|
1503
|
+
onPress: options.onPress,
|
|
1504
|
+
onPressStart: options.onPressStart,
|
|
1505
|
+
onPressEnd: options.onPressEnd,
|
|
1506
|
+
onPressUp: options.onPressUp,
|
|
1507
|
+
onPressChange: options.onPressChange,
|
|
1508
|
+
},
|
|
1509
|
+
root.state,
|
|
1510
|
+
);
|
|
1511
|
+
|
|
1512
|
+
const { hoverProps, isHovered } = hover({
|
|
1513
|
+
isDisabled: () => !item.allowsSelection() && !item.hasAction() && dnd?.isDraggable() !== true,
|
|
1514
|
+
onHoverStart: options.onHoverStart,
|
|
1515
|
+
onHoverEnd: options.onHoverEnd,
|
|
1516
|
+
onHoverChange: options.onHoverChange,
|
|
1517
|
+
});
|
|
1518
|
+
const { focusProps: focusEventProps } = focused({ onFocusChange: options.onFocusChange });
|
|
1519
|
+
const { focusProps, isFocusVisible } = focusRing();
|
|
1520
|
+
const { focusProps: withinProps, isFocusVisible: isFocusVisibleWithin } = focusRing({
|
|
1521
|
+
within: true,
|
|
1522
|
+
});
|
|
1523
|
+
|
|
1524
|
+
// A row that hands its focus to a child shows its ring while that child has
|
|
1525
|
+
// it: the child is standing in for the row, not one of its controls.
|
|
1526
|
+
const [standInFocused, setStandInFocused] = createSignal(false);
|
|
1527
|
+
// Read from the document rather than the event: the row's own focus
|
|
1528
|
+
// handler may already have moved focus on to the child by now.
|
|
1529
|
+
const trackStandIn = (): void => {
|
|
1530
|
+
const element = domRef();
|
|
1531
|
+
if (element === null || focusMode() !== "child") return;
|
|
1532
|
+
const first = focusableWalker(element, { tabbable: true }).nextNode();
|
|
1533
|
+
setStandInFocused(activeElement(ownerDocument(element)) === first);
|
|
1534
|
+
};
|
|
1535
|
+
const rowFocusVisible = (): boolean =>
|
|
1536
|
+
isFocusVisible() || (focusMode() === "child" && isFocusVisibleWithin() && standInFocused());
|
|
1537
|
+
|
|
1538
|
+
// Memos: the selection manager is rebuilt by every expansion, and a part
|
|
1539
|
+
// drawn on one of these would otherwise be rebuilt with it.
|
|
1540
|
+
const selectionMode = createMemo(() => root.state.selectionManager().selectionMode);
|
|
1541
|
+
const selectionBehavior = createMemo(() => root.state.selectionManager().selectionBehavior);
|
|
1542
|
+
|
|
1543
|
+
const render: TreeItemRenderState = {
|
|
1544
|
+
id: node.key,
|
|
1545
|
+
level: item.level,
|
|
1546
|
+
isExpanded: item.isExpanded,
|
|
1547
|
+
hasChildItems: item.hasChildItems,
|
|
1548
|
+
isSelected: item.isSelected,
|
|
1549
|
+
isFocused: item.isFocused,
|
|
1550
|
+
isFocusVisible: rowFocusVisible,
|
|
1551
|
+
isFocusVisibleWithin,
|
|
1552
|
+
isPressed: item.isPressed,
|
|
1553
|
+
isHovered,
|
|
1554
|
+
isDisabled: item.isDisabled,
|
|
1555
|
+
allowsSelection: item.allowsSelection,
|
|
1556
|
+
hasAction: item.hasAction,
|
|
1557
|
+
selectionMode,
|
|
1558
|
+
selectionBehavior,
|
|
1559
|
+
allowsDragging: () => dnd?.collection.dragState !== undefined,
|
|
1560
|
+
isDragging: () => dnd?.collection.dragState?.isDragging(node.key) === true,
|
|
1561
|
+
isDropTarget: () => dnd?.on?.indicator.isDropTarget() === true,
|
|
1562
|
+
state: root.state,
|
|
1563
|
+
};
|
|
1564
|
+
const context: TreeItemContextValue = { render, item };
|
|
1565
|
+
|
|
1566
|
+
const elementProps = mergeProps(
|
|
1567
|
+
item.rowProps,
|
|
1568
|
+
hoverProps,
|
|
1569
|
+
focusProps,
|
|
1570
|
+
focusEventProps,
|
|
1571
|
+
withinProps,
|
|
1572
|
+
{ onFocusIn: trackStandIn },
|
|
1573
|
+
filterDOMProps(options, { global: true, id: false }),
|
|
1574
|
+
styleProps(cells(props)),
|
|
1575
|
+
{
|
|
1576
|
+
"data-expanded": () => item.hasChildItems() && item.isExpanded(),
|
|
1577
|
+
"data-has-child-items": item.hasChildItems,
|
|
1578
|
+
"data-level": () => String(item.level()),
|
|
1579
|
+
"data-selected": item.isSelected,
|
|
1580
|
+
"data-disabled": item.isDisabled,
|
|
1581
|
+
"data-hovered": isHovered,
|
|
1582
|
+
"data-focused": item.isFocused,
|
|
1583
|
+
"data-focus-visible": rowFocusVisible,
|
|
1584
|
+
"data-pressed": item.isPressed,
|
|
1585
|
+
"data-selection-mode": () => (selectionMode() === "none" ? undefined : selectionMode()),
|
|
1586
|
+
"data-allows-dragging": render.allowsDragging,
|
|
1587
|
+
"data-dragging": render.isDragging,
|
|
1588
|
+
"data-drop-target": render.isDropTarget,
|
|
1589
|
+
style: () => {
|
|
1590
|
+
const level = { "--tree-item-level": String(item.level()) };
|
|
1591
|
+
if (root.virtual !== null) {
|
|
1592
|
+
const info = root.virtual.layout.getLayoutInfo(node.key);
|
|
1593
|
+
if (info !== null) return { ...rowStyle(info), ...level };
|
|
1594
|
+
}
|
|
1595
|
+
return { ...level, ...props.style };
|
|
1596
|
+
},
|
|
1597
|
+
},
|
|
1598
|
+
);
|
|
1599
|
+
|
|
1600
|
+
const row = (
|
|
1601
|
+
<div {...mergeProps(elementProps, dnd?.itemProps)} ref={mergeRefs(domRef.set, props.ref)}>
|
|
1602
|
+
<div {...item.gridCellProps}>
|
|
1603
|
+
{provide(
|
|
1604
|
+
TreeItemContext,
|
|
1605
|
+
() => context,
|
|
1606
|
+
() => props.children as unknown,
|
|
1607
|
+
)}
|
|
1608
|
+
</div>
|
|
1609
|
+
</div>
|
|
1610
|
+
);
|
|
1611
|
+
if (dnd === null) return row;
|
|
1612
|
+
|
|
1613
|
+
const { visuallyHiddenProps } = visuallyHidden();
|
|
1614
|
+
const on = dnd.on;
|
|
1615
|
+
const rowId = item.rowProps.id as Accessor<string>;
|
|
1616
|
+
const activateId = id();
|
|
1617
|
+
|
|
1618
|
+
return (
|
|
1619
|
+
<>
|
|
1620
|
+
<Show when={dnd.collection.shouldRenderIndicator(dnd.before)}>
|
|
1621
|
+
{dnd.collection.renderDropIndicator(dnd.before)}
|
|
1622
|
+
</Show>
|
|
1623
|
+
{on === null ? null : (
|
|
1624
|
+
<Show when={!on.indicator.isHidden()}>
|
|
1625
|
+
<div
|
|
1626
|
+
role="row"
|
|
1627
|
+
aria-level={item.level()}
|
|
1628
|
+
aria-expanded={item.hasChildItems() ? item.isExpanded() : undefined}
|
|
1629
|
+
aria-label={(on.indicator.dropIndicatorProps["aria-label"] as Accessor<string>)()}
|
|
1630
|
+
>
|
|
1631
|
+
<div role="gridcell" aria-colindex={1} style={{ display: "contents" }}>
|
|
1632
|
+
<div
|
|
1633
|
+
role="button"
|
|
1634
|
+
{...visuallyHiddenProps}
|
|
1635
|
+
{...on.indicator.dropIndicatorProps}
|
|
1636
|
+
ref={on.ref.set}
|
|
1637
|
+
/>
|
|
1638
|
+
{/* For a touch screen reader, which cannot reach the row's own expand button mid-drag. */}
|
|
1639
|
+
<Show when={item.hasChildItems()}>
|
|
1640
|
+
<div
|
|
1641
|
+
role="button"
|
|
1642
|
+
{...visuallyHiddenProps}
|
|
1643
|
+
id={activateId()}
|
|
1644
|
+
aria-label={(item.expandButtonProps["aria-label"] as Accessor<string>)()}
|
|
1645
|
+
aria-labelledby={`${activateId()} ${rowId()}`}
|
|
1646
|
+
tabIndex={-1}
|
|
1647
|
+
ref={on.activateRef.set}
|
|
1648
|
+
/>
|
|
1649
|
+
</Show>
|
|
1650
|
+
</div>
|
|
1651
|
+
</div>
|
|
1652
|
+
</Show>
|
|
1653
|
+
)}
|
|
1654
|
+
{row}
|
|
1655
|
+
<For each={dnd.after()}>
|
|
1656
|
+
{(target: ItemDropTarget) => (
|
|
1657
|
+
<Show when={dnd.collection.shouldRenderIndicator(target)}>
|
|
1658
|
+
{dnd.collection.renderDropIndicator(target)}
|
|
1659
|
+
</Show>
|
|
1660
|
+
)}
|
|
1661
|
+
</For>
|
|
1662
|
+
</>
|
|
1663
|
+
);
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
interface TreeRowDragAndDrop {
|
|
1667
|
+
collection: CollectionDragAndDrop;
|
|
1668
|
+
itemProps: DOMProps;
|
|
1669
|
+
isDraggable: Accessor<boolean>;
|
|
1670
|
+
before: ItemDropTarget;
|
|
1671
|
+
after: Accessor<ItemDropTarget[]>;
|
|
1672
|
+
/** The target for a drop ON the row, apart from the row's own controls. */
|
|
1673
|
+
on: {
|
|
1674
|
+
indicator: ReturnType<NonNullable<DragAndDropHooks["dropIndicator"]>>;
|
|
1675
|
+
ref: ReturnType<typeof makeRef<HTMLDivElement>>;
|
|
1676
|
+
activateRef: ReturnType<typeof makeRef<HTMLDivElement>>;
|
|
1677
|
+
} | null;
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/**
|
|
1681
|
+
* A row's part in its tree's drag and drop. A row holds controls, so a
|
|
1682
|
+
* `<DragButton>` inside it starts a keyboard drag rather than Enter on the row.
|
|
1683
|
+
*/
|
|
1684
|
+
function treeRowDragAndDrop(root: TreeContextValue, key: Key): TreeRowDragAndDrop | null {
|
|
1685
|
+
const collection = root.dragAndDrop;
|
|
1686
|
+
if (collection === undefined) return null;
|
|
1687
|
+
const { hooks, dragState, dropState } = collection;
|
|
1688
|
+
|
|
1689
|
+
const draggable =
|
|
1690
|
+
dragState === undefined || hooks?.draggableItem === undefined
|
|
1691
|
+
? null
|
|
1692
|
+
: hooks.draggableItem({ key, hasDragButton: true }, dragState);
|
|
1693
|
+
const dragButtonRef = makeRef<Element>();
|
|
1694
|
+
const slot = draggableItemSlotProps(draggable, hooks, dragButtonRef, dragButtonRef.set);
|
|
1695
|
+
if (draggable !== null) provideDragButton(slot.dragButton);
|
|
1696
|
+
|
|
1697
|
+
let on: TreeRowDragAndDrop["on"] = null;
|
|
1698
|
+
if (dropState !== undefined && hooks?.dropIndicator !== undefined) {
|
|
1699
|
+
const ref = makeRef<HTMLDivElement>();
|
|
1700
|
+
const activateRef = makeRef<HTMLDivElement>();
|
|
1701
|
+
on = {
|
|
1702
|
+
ref,
|
|
1703
|
+
activateRef,
|
|
1704
|
+
indicator: hooks.dropIndicator(
|
|
1705
|
+
{ target: { type: "item", key, dropPosition: "on" }, activateButtonRef: activateRef },
|
|
1706
|
+
dropState,
|
|
1707
|
+
ref,
|
|
1708
|
+
),
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
const after = createMemo(() => {
|
|
1713
|
+
const items = root.state.collection() as Collection<unknown>;
|
|
1714
|
+
const node = items.getItem(key);
|
|
1715
|
+
return node === null ? [] : afterDropIndicatorTargets(items, node);
|
|
1716
|
+
});
|
|
1717
|
+
|
|
1718
|
+
return {
|
|
1719
|
+
collection,
|
|
1720
|
+
on,
|
|
1721
|
+
before: { type: "item", key, dropPosition: "before" },
|
|
1722
|
+
after,
|
|
1723
|
+
isDraggable: () =>
|
|
1724
|
+
dragState !== undefined &&
|
|
1725
|
+
!(dragState.isDisabled() || root.state.selectionManager().isDisabled(key)),
|
|
1726
|
+
itemProps: slot.itemProps,
|
|
1727
|
+
};
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
export interface TreeItemContentProps {
|
|
1731
|
+
/** The row's content, or a function of the row's state that returns it. */
|
|
1732
|
+
children?: Child | ((row: TreeItemRenderState) => Child);
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* The content of a row, with the row's state for whatever is drawn in it.
|
|
1737
|
+
*
|
|
1738
|
+
* ```tsx
|
|
1739
|
+
* <TreeItemContent>
|
|
1740
|
+
* {(row) => <span data-level={row.level()}>{file.name}</span>}
|
|
1741
|
+
* </TreeItemContent>
|
|
1742
|
+
* ```
|
|
1743
|
+
*/
|
|
1744
|
+
export function TreeItemContent(props: TreeItemContentProps) {
|
|
1745
|
+
const row = useTreeItem();
|
|
1746
|
+
const children = props.children;
|
|
1747
|
+
if (typeof children === "function" && children.length > 0) {
|
|
1748
|
+
return <>{(children as (state: TreeItemRenderState) => Child)(row.render)}</>;
|
|
1749
|
+
}
|
|
1750
|
+
return <>{children}</>;
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
export interface TreeItemExpandButtonProps extends StyleProps {
|
|
1754
|
+
children?: Child;
|
|
1755
|
+
ref?: RefTarget<HTMLButtonElement>;
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
/**
|
|
1759
|
+
* The button that expands and collapses its row.
|
|
1760
|
+
*
|
|
1761
|
+
* It is not a Tab stop and does not take focus when pressed: the row keeps
|
|
1762
|
+
* focus, and Right and Left do the same from the keyboard. It is there for a
|
|
1763
|
+
* pointer, and for a screen reader's touch exploration, which is why it is
|
|
1764
|
+
* named "Expand" or "Collapse" followed by the row's own name.
|
|
1765
|
+
*/
|
|
1766
|
+
export function TreeItemExpandButton(props: TreeItemExpandButtonProps) {
|
|
1767
|
+
const domRef = makeRef<HTMLButtonElement>();
|
|
1768
|
+
const row = useTreeItem();
|
|
1769
|
+
const { buttonProps, isPressed } = button(
|
|
1770
|
+
{ ...(fromProps(cells(props)) as ButtonOptions), ...row.item.expandButtonProps },
|
|
1771
|
+
domRef,
|
|
1772
|
+
);
|
|
1773
|
+
const { hoverProps, isHovered } = hover({});
|
|
1774
|
+
const { onClick: _, ...forwarded } = filterDOMProps(fromProps(cells(props)), { global: true });
|
|
1775
|
+
|
|
1776
|
+
const elementProps = mergeProps(
|
|
1777
|
+
buttonProps,
|
|
1778
|
+
{
|
|
1779
|
+
id: row.item.expandButtonProps.id,
|
|
1780
|
+
[PREVENT_FOCUS_ATTRIBUTE]: true,
|
|
1781
|
+
"aria-labelledby": row.item.expandButtonProps["aria-labelledby"],
|
|
1782
|
+
},
|
|
1783
|
+
hoverProps,
|
|
1784
|
+
forwarded,
|
|
1785
|
+
styleProps(cells(props)),
|
|
1786
|
+
{
|
|
1787
|
+
"data-expanded": row.render.isExpanded,
|
|
1788
|
+
"data-pressed": isPressed,
|
|
1789
|
+
"data-hovered": isHovered,
|
|
1790
|
+
"data-disabled": row.render.isDisabled,
|
|
1791
|
+
},
|
|
1792
|
+
);
|
|
1793
|
+
|
|
1794
|
+
return (
|
|
1795
|
+
<button {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
|
|
1796
|
+
{props.children}
|
|
1797
|
+
</button>
|
|
1798
|
+
);
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
export interface TreeItemDescriptionProps extends StyleProps {
|
|
1802
|
+
children?: Child;
|
|
1803
|
+
}
|
|
1804
|
+
|
|
1805
|
+
/** Secondary text in a row, read after the row's name. */
|
|
1806
|
+
export function TreeItemDescription(props: TreeItemDescriptionProps) {
|
|
1807
|
+
const row = useTreeItem();
|
|
1808
|
+
onCleanup(row.item.registerDescription());
|
|
1809
|
+
const elementProps = mergeProps(
|
|
1810
|
+
filterDOMProps(fromProps(cells(props)), { global: true, id: false }),
|
|
1811
|
+
styleProps(cells(props)),
|
|
1812
|
+
row.item.descriptionProps,
|
|
1813
|
+
);
|
|
1814
|
+
return <span {...elementProps}>{props.children}</span>;
|
|
1815
|
+
}
|
|
1816
|
+
|
|
1817
|
+
export interface TreeItemSelectionCheckboxProps extends StyleProps {
|
|
1818
|
+
/** What is drawn inside the checkbox. */
|
|
1819
|
+
children?: Child;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
/**
|
|
1823
|
+
* A checkbox that selects its row, named "Select" followed by the row's name.
|
|
1824
|
+
*
|
|
1825
|
+
* A press on it is the checkbox's, not the row's, so under `"replace"`
|
|
1826
|
+
* selection it adds to the selection rather than replacing it.
|
|
1827
|
+
*/
|
|
1828
|
+
export function TreeItemSelectionCheckbox(props: TreeItemSelectionCheckboxProps) {
|
|
1829
|
+
const row = useTreeItem();
|
|
1830
|
+
const box = row.item.checkboxProps;
|
|
1831
|
+
return (
|
|
1832
|
+
<Checkbox
|
|
1833
|
+
id={box.id()}
|
|
1834
|
+
aria-label={box["aria-label"]()}
|
|
1835
|
+
aria-labelledby={box["aria-labelledby"]()}
|
|
1836
|
+
isSelected={box.isSelected()}
|
|
1837
|
+
isDisabled={box.isDisabled()}
|
|
1838
|
+
onChange={() => box.onChange()}
|
|
1839
|
+
class={props.class}
|
|
1840
|
+
style={props.style}
|
|
1841
|
+
>
|
|
1842
|
+
{props.children}
|
|
1843
|
+
</Checkbox>
|
|
1844
|
+
);
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
// ---------------------------------------------------------------------------
|
|
1848
|
+
// Sections
|
|
1849
|
+
// ---------------------------------------------------------------------------
|
|
1850
|
+
|
|
1851
|
+
interface TreeSectionContextValue {
|
|
1852
|
+
headingId: Accessor<string>;
|
|
1853
|
+
hasHeader: Accessor<boolean>;
|
|
1854
|
+
registerHeader(): () => void;
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
const TreeSectionContext = createContext<TreeSectionContextValue | null>(null);
|
|
1858
|
+
|
|
1859
|
+
export interface TreeSectionProps extends StyleProps {
|
|
1860
|
+
/** A `<TreeHeader>`. The section's rows are rendered after it. */
|
|
1861
|
+
children?: Child;
|
|
1862
|
+
"aria-label"?: string;
|
|
1863
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1864
|
+
}
|
|
1865
|
+
|
|
1866
|
+
/**
|
|
1867
|
+
* A group of top-level rows, named by its `<TreeHeader>` or its `aria-label`.
|
|
1868
|
+
*
|
|
1869
|
+
* Its rows count their level and position from the section, not the tree.
|
|
1870
|
+
* Inside a `<Virtualizer>` the rows are a window positioned by the layout, so
|
|
1871
|
+
* they are siblings of the section rather than its children, and the section
|
|
1872
|
+
* holds only its header, placed where the layout put it.
|
|
1873
|
+
*/
|
|
1874
|
+
export function TreeSection(props: TreeSectionProps) {
|
|
1875
|
+
const root = useTree();
|
|
1876
|
+
const node = useTreeNode();
|
|
1877
|
+
const headingId = id();
|
|
1878
|
+
// Written by a header from its own component body, when it mounts.
|
|
1879
|
+
const [headers, setHeaders] = createSignal(0, { ownedWrite: true });
|
|
1880
|
+
const section: TreeSectionContextValue = {
|
|
1881
|
+
headingId,
|
|
1882
|
+
hasHeader: () => headers() > 0,
|
|
1883
|
+
registerHeader: () => {
|
|
1884
|
+
setHeaders((count) => count + 1);
|
|
1885
|
+
return () => setHeaders((count) => count - 1);
|
|
1886
|
+
},
|
|
1887
|
+
};
|
|
1888
|
+
|
|
1889
|
+
const elementProps = mergeProps(
|
|
1890
|
+
filterDOMProps(fromProps(cells(props)), { global: true, id: false }),
|
|
1891
|
+
styleProps(cells(props)),
|
|
1892
|
+
{
|
|
1893
|
+
role: "rowgroup",
|
|
1894
|
+
"aria-label": () => props["aria-label"],
|
|
1895
|
+
"aria-labelledby": () =>
|
|
1896
|
+
props["aria-label"] === undefined && section.hasHeader() ? headingId() : undefined,
|
|
1897
|
+
style: () => {
|
|
1898
|
+
const info = root.virtual?.layout.getLayoutInfo(node.key) ?? null;
|
|
1899
|
+
return info === null ? props.style : rowStyle(info);
|
|
1900
|
+
},
|
|
1901
|
+
},
|
|
1902
|
+
);
|
|
1903
|
+
|
|
1904
|
+
return (
|
|
1905
|
+
<div {...elementProps} ref={mergeRefs(props.ref)}>
|
|
1906
|
+
{provide(
|
|
1907
|
+
TreeSectionContext,
|
|
1908
|
+
() => section,
|
|
1909
|
+
() => (
|
|
1910
|
+
<>
|
|
1911
|
+
{props.children}
|
|
1912
|
+
{root.virtual === null ? <TreeRows nodes={node.childNodes} /> : null}
|
|
1913
|
+
</>
|
|
1914
|
+
),
|
|
1915
|
+
)}
|
|
1916
|
+
</div>
|
|
1917
|
+
);
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
export interface TreeHeaderProps extends StyleProps {
|
|
1921
|
+
children?: Child;
|
|
1922
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
/** The header row of a {@link TreeSection}. Not focusable: it names the group. */
|
|
1926
|
+
export function TreeHeader(props: TreeHeaderProps) {
|
|
1927
|
+
const section = getContext(TreeSectionContext);
|
|
1928
|
+
if (section === null || section === undefined) {
|
|
1929
|
+
throw new Error("A TreeHeader must be rendered inside a TreeSection.");
|
|
1930
|
+
}
|
|
1931
|
+
onCleanup(section.registerHeader());
|
|
1932
|
+
const elementProps = mergeProps(
|
|
1933
|
+
filterDOMProps(fromProps(cells(props)), { global: true, id: false }),
|
|
1934
|
+
styleProps(cells(props)),
|
|
1935
|
+
{ role: "row" },
|
|
1936
|
+
);
|
|
1937
|
+
return (
|
|
1938
|
+
<div {...elementProps} ref={mergeRefs(props.ref)}>
|
|
1939
|
+
<div role="rowheader" id={section.headingId()}>
|
|
1940
|
+
{props.children}
|
|
1941
|
+
</div>
|
|
1942
|
+
</div>
|
|
1943
|
+
);
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// ---------------------------------------------------------------------------
|
|
1947
|
+
// Loading more
|
|
1948
|
+
// ---------------------------------------------------------------------------
|
|
1949
|
+
|
|
1950
|
+
export interface LoadMoreSentinelOptions {
|
|
1951
|
+
/** Changes whenever the rows above the sentinel do, so the observer is set up again. */
|
|
1952
|
+
collection: Accessor<unknown>;
|
|
1953
|
+
onLoadMore?: () => void;
|
|
1954
|
+
/**
|
|
1955
|
+
* How far from the end of the scrolling region, as a fraction of its height,
|
|
1956
|
+
* the sentinel counts as reached. @default 1
|
|
1957
|
+
*/
|
|
1958
|
+
scrollOffset?: MaybeAccessor<number | undefined>;
|
|
1959
|
+
/** `"start"` for a list that grows upwards. @default "end" */
|
|
1960
|
+
direction?: MaybeAccessor<"start" | "end" | undefined>;
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
/**
|
|
1964
|
+
* Call `onLoadMore` when the sentinel comes within reach of the scrolling
|
|
1965
|
+
* region's edge.
|
|
1966
|
+
*
|
|
1967
|
+
* The observer is rebuilt whenever the collection changes. Kept, it would not
|
|
1968
|
+
* fire again after a page loads that left the sentinel inside the margin,
|
|
1969
|
+
* because nothing about its intersection changed, and the list would stop
|
|
1970
|
+
* growing with room still to fill.
|
|
1971
|
+
*/
|
|
1972
|
+
export function loadMoreSentinel(options: LoadMoreSentinelOptions, ref: ElementRef): void {
|
|
1973
|
+
if (isServer) return;
|
|
1974
|
+
createEffect(
|
|
1975
|
+
() => ({
|
|
1976
|
+
collection: options.collection(),
|
|
1977
|
+
element: access(ref) as Element | null,
|
|
1978
|
+
offset: access(options.scrollOffset) ?? 1,
|
|
1979
|
+
direction: access(options.direction) ?? "end",
|
|
1980
|
+
}),
|
|
1981
|
+
({ element, offset, direction }) => {
|
|
1982
|
+
if (element === null || typeof IntersectionObserver === "undefined") return undefined;
|
|
1983
|
+
const margin = 100 * offset;
|
|
1984
|
+
const observer = new IntersectionObserver(
|
|
1985
|
+
(entries) => {
|
|
1986
|
+
for (const entry of entries) {
|
|
1987
|
+
if (entry.isIntersecting) options.onLoadMore?.();
|
|
1988
|
+
}
|
|
1989
|
+
},
|
|
1990
|
+
{
|
|
1991
|
+
root: scrollParents(element)[0] ?? null,
|
|
1992
|
+
rootMargin:
|
|
1993
|
+
direction === "start"
|
|
1994
|
+
? `${margin}% 0px 0px 0px`
|
|
1995
|
+
: `0px ${margin}% ${margin}% ${margin}%`,
|
|
1996
|
+
},
|
|
1997
|
+
);
|
|
1998
|
+
observer.observe(element);
|
|
1999
|
+
return () => observer.disconnect();
|
|
2000
|
+
},
|
|
2001
|
+
);
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
export interface TreeLoadMoreItemProps extends StyleProps {
|
|
2005
|
+
/** What shows while loading: a spinner, a line of text. */
|
|
2006
|
+
children?: Child;
|
|
2007
|
+
/** Whether the loading row is shown. The sentinel is there either way. */
|
|
2008
|
+
isLoading?: boolean;
|
|
2009
|
+
onLoadMore?: () => void;
|
|
2010
|
+
/** @default 1 */
|
|
2011
|
+
scrollOffset?: number;
|
|
2012
|
+
ref?: RefTarget<HTMLDivElement>;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
/**
|
|
2016
|
+
* Where more rows will appear, rendered for an item whose `getType` is
|
|
2017
|
+
* `"loader"`, at the root or as the last child of a row.
|
|
2018
|
+
*
|
|
2019
|
+
* The sentinel that asks for the next page is always rendered; the row is
|
|
2020
|
+
* rendered while `isLoading`. The row is not a stop for the keyboard and does
|
|
2021
|
+
* not count towards its siblings' `aria-setsize`.
|
|
2022
|
+
*/
|
|
2023
|
+
export function TreeLoadMoreItem(props: TreeLoadMoreItemProps) {
|
|
2024
|
+
const root = useTree();
|
|
2025
|
+
const node = useTreeNode();
|
|
2026
|
+
const sentinel = makeRef<HTMLDivElement>();
|
|
2027
|
+
const options = fromProps(cells(props));
|
|
2028
|
+
const level = (): number => node.level + 1;
|
|
2029
|
+
|
|
2030
|
+
loadMoreSentinel(
|
|
2031
|
+
{
|
|
2032
|
+
collection: root.state.collection,
|
|
2033
|
+
onLoadMore: options.onLoadMore,
|
|
2034
|
+
scrollOffset: () => props.scrollOffset,
|
|
2035
|
+
},
|
|
2036
|
+
sentinel,
|
|
2037
|
+
);
|
|
2038
|
+
|
|
2039
|
+
const elementProps = mergeProps(
|
|
2040
|
+
filterDOMProps(options, { global: true, id: false }),
|
|
2041
|
+
styleProps(cells(props)),
|
|
2042
|
+
{
|
|
2043
|
+
role: "row",
|
|
2044
|
+
"aria-level": level,
|
|
2045
|
+
"data-level": () => String(level()),
|
|
2046
|
+
style: () => {
|
|
2047
|
+
if (root.virtual === null) return props.style;
|
|
2048
|
+
const info = root.virtual.layout.getLayoutInfo(node.key);
|
|
2049
|
+
return info === null ? props.style : rowStyle(info);
|
|
2050
|
+
},
|
|
2051
|
+
},
|
|
2052
|
+
);
|
|
2053
|
+
|
|
2054
|
+
return (
|
|
2055
|
+
<>
|
|
2056
|
+
{/* Zero-sized and inert, so it takes no room and nothing can reach it. */}
|
|
2057
|
+
<div style={{ position: "relative", width: "0", height: "0" }} inert>
|
|
2058
|
+
<div
|
|
2059
|
+
data-load-more-sentinel=""
|
|
2060
|
+
style={{ position: "absolute", width: "1px", height: "1px" }}
|
|
2061
|
+
ref={sentinel.set}
|
|
2062
|
+
/>
|
|
2063
|
+
</div>
|
|
2064
|
+
{() =>
|
|
2065
|
+
props.isLoading === true && props.children !== undefined ? (
|
|
2066
|
+
<div {...elementProps} ref={mergeRefs(props.ref)}>
|
|
2067
|
+
<div role="gridcell" aria-colindex={1}>
|
|
2068
|
+
{props.children}
|
|
2069
|
+
</div>
|
|
2070
|
+
</div>
|
|
2071
|
+
) : null
|
|
2072
|
+
}
|
|
2073
|
+
</>
|
|
2074
|
+
);
|
|
2075
|
+
}
|