@djangocfg/ui-tools 2.1.314 → 2.1.315
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/dist/TreeRoot-DO33TIS5.mjs +4 -0
- package/dist/TreeRoot-DO33TIS5.mjs.map +1 -0
- package/dist/TreeRoot-NJOZ2DMV.cjs +19 -0
- package/dist/TreeRoot-NJOZ2DMV.cjs.map +1 -0
- package/dist/chunk-E5BP4IXF.mjs +1231 -0
- package/dist/chunk-E5BP4IXF.mjs.map +1 -0
- package/dist/chunk-MA552EWC.cjs +1282 -0
- package/dist/chunk-MA552EWC.cjs.map +1 -0
- package/dist/index.cjs +186 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +435 -2
- package/dist/index.d.ts +435 -2
- package/dist/index.mjs +60 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -6
- package/src/index.ts +4 -0
- package/src/tools/Tree/README.md +220 -0
- package/src/tools/Tree/Tree.story.tsx +536 -0
- package/src/tools/Tree/TreeRoot.tsx +157 -0
- package/src/tools/Tree/components/TreeChevron.tsx +39 -0
- package/src/tools/Tree/components/TreeContent.tsx +48 -0
- package/src/tools/Tree/components/TreeEmpty.tsx +21 -0
- package/src/tools/Tree/components/TreeError.tsx +24 -0
- package/src/tools/Tree/components/TreeIcon.tsx +29 -0
- package/src/tools/Tree/components/TreeIndentGuides.tsx +33 -0
- package/src/tools/Tree/components/TreeLabel.tsx +24 -0
- package/src/tools/Tree/components/TreeRow.tsx +173 -0
- package/src/tools/Tree/components/TreeSearchInput.tsx +50 -0
- package/src/tools/Tree/components/TreeSkeleton.tsx +22 -0
- package/src/tools/Tree/components/index.ts +22 -0
- package/src/tools/Tree/context/TreeContext.tsx +538 -0
- package/src/tools/Tree/context/hooks.ts +110 -0
- package/src/tools/Tree/context/index.ts +13 -0
- package/src/tools/Tree/data/appearance.ts +175 -0
- package/src/tools/Tree/data/childCache.ts +43 -0
- package/src/tools/Tree/data/createDemoTree.ts +42 -0
- package/src/tools/Tree/data/flatten.ts +51 -0
- package/src/tools/Tree/data/index.ts +24 -0
- package/src/tools/Tree/data/persist.ts +62 -0
- package/src/tools/Tree/hooks/index.ts +6 -0
- package/src/tools/Tree/hooks/useTreeKeyboard.ts +137 -0
- package/src/tools/Tree/hooks/useTreeTypeAhead.ts +100 -0
- package/src/tools/Tree/index.tsx +99 -0
- package/src/tools/Tree/lazy.tsx +14 -0
- package/src/tools/Tree/types.ts +136 -0
- package/src/tools/index.ts +75 -0
|
@@ -0,0 +1,1231 @@
|
|
|
1
|
+
import { __name } from './chunk-CGILA3WO.mjs';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { createContext, useMemo, useReducer, useRef, useCallback, useEffect, Fragment as Fragment$1 } from 'react';
|
|
4
|
+
import { cn } from '@djangocfg/ui-core/lib';
|
|
5
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
6
|
+
import { ChevronDown, ChevronRight, FolderOpen, Folder, File, Loader2, Search, X } from 'lucide-react';
|
|
7
|
+
|
|
8
|
+
// src/tools/Tree/types.ts
|
|
9
|
+
var DEFAULT_TREE_LABELS = {
|
|
10
|
+
loading: "Loading\u2026",
|
|
11
|
+
empty: "Nothing to show",
|
|
12
|
+
error: "Failed to load",
|
|
13
|
+
searchPlaceholder: "Search\u2026",
|
|
14
|
+
searchMatches: /* @__PURE__ */ __name((n) => `${n} match${n === 1 ? "" : "es"}`, "searchMatches"),
|
|
15
|
+
ariaLabel: "Tree"
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/tools/Tree/data/childCache.ts
|
|
19
|
+
var createChildCache = /* @__PURE__ */ __name(() => /* @__PURE__ */ new Map(), "createChildCache");
|
|
20
|
+
var resolveChildren = /* @__PURE__ */ __name((cache, node) => {
|
|
21
|
+
if (Array.isArray(node.children)) {
|
|
22
|
+
return { children: node.children, status: "loaded" };
|
|
23
|
+
}
|
|
24
|
+
const entry = cache.get(node.id);
|
|
25
|
+
if (!entry) return { children: null, status: "idle" };
|
|
26
|
+
if (entry.status === "loaded") {
|
|
27
|
+
return { children: entry.children, status: "loaded" };
|
|
28
|
+
}
|
|
29
|
+
return { children: null, status: entry.status, error: entry.error };
|
|
30
|
+
}, "resolveChildren");
|
|
31
|
+
|
|
32
|
+
// src/tools/Tree/data/flatten.ts
|
|
33
|
+
var isNodeFolder = /* @__PURE__ */ __name((node) => {
|
|
34
|
+
if (typeof node.isFolder === "boolean") return node.isFolder;
|
|
35
|
+
return Array.isArray(node.children);
|
|
36
|
+
}, "isNodeFolder");
|
|
37
|
+
function flattenTree({ roots, expandedIds, cache }) {
|
|
38
|
+
const out = [];
|
|
39
|
+
const walk = /* @__PURE__ */ __name((nodes, level, parentId) => {
|
|
40
|
+
for (const node of nodes) {
|
|
41
|
+
const isFolder = isNodeFolder(node);
|
|
42
|
+
const isExpanded = expandedIds.has(node.id);
|
|
43
|
+
const resolved = isFolder ? resolveChildren(cache, node) : { children: [], status: "loaded" };
|
|
44
|
+
out.push({
|
|
45
|
+
node,
|
|
46
|
+
level,
|
|
47
|
+
parentId,
|
|
48
|
+
isFolder,
|
|
49
|
+
isExpanded,
|
|
50
|
+
isLoading: resolved.status === "loading",
|
|
51
|
+
hasError: resolved.status === "error"
|
|
52
|
+
});
|
|
53
|
+
if (isFolder && isExpanded && resolved.children) {
|
|
54
|
+
walk(resolved.children, level + 1, node.id);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}, "walk");
|
|
58
|
+
walk(roots, 0, null);
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
__name(flattenTree, "flattenTree");
|
|
62
|
+
|
|
63
|
+
// src/tools/Tree/data/persist.ts
|
|
64
|
+
var KEY_PREFIX = "@djangocfg/tree:";
|
|
65
|
+
var VERSION = 1;
|
|
66
|
+
function safeStorage() {
|
|
67
|
+
if (typeof window === "undefined") return null;
|
|
68
|
+
try {
|
|
69
|
+
return window.localStorage;
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
__name(safeStorage, "safeStorage");
|
|
75
|
+
function loadTreeState(key) {
|
|
76
|
+
const storage = safeStorage();
|
|
77
|
+
if (!storage) return null;
|
|
78
|
+
try {
|
|
79
|
+
const raw = storage.getItem(KEY_PREFIX + key);
|
|
80
|
+
if (!raw) return null;
|
|
81
|
+
const parsed = JSON.parse(raw);
|
|
82
|
+
if (parsed.version !== VERSION) return null;
|
|
83
|
+
return {
|
|
84
|
+
expandedItems: Array.isArray(parsed.expandedItems) ? parsed.expandedItems : [],
|
|
85
|
+
selectedItems: Array.isArray(parsed.selectedItems) ? parsed.selectedItems : []
|
|
86
|
+
};
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
__name(loadTreeState, "loadTreeState");
|
|
92
|
+
function saveTreeState(key, state) {
|
|
93
|
+
const storage = safeStorage();
|
|
94
|
+
if (!storage) return;
|
|
95
|
+
try {
|
|
96
|
+
const payload = { ...state, version: VERSION };
|
|
97
|
+
storage.setItem(KEY_PREFIX + key, JSON.stringify(payload));
|
|
98
|
+
} catch {
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
__name(saveTreeState, "saveTreeState");
|
|
102
|
+
function clearTreeState(key) {
|
|
103
|
+
const storage = safeStorage();
|
|
104
|
+
if (!storage) return;
|
|
105
|
+
try {
|
|
106
|
+
storage.removeItem(KEY_PREFIX + key);
|
|
107
|
+
} catch {
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
__name(clearTreeState, "clearTreeState");
|
|
111
|
+
|
|
112
|
+
// src/tools/Tree/data/appearance.ts
|
|
113
|
+
var DENSITY_PRESETS = {
|
|
114
|
+
compact: { rowHeight: 24, iconSize: 14, fontSize: 13, gap: 6 },
|
|
115
|
+
cozy: { rowHeight: 28, iconSize: 16, fontSize: 13, gap: 8 },
|
|
116
|
+
comfortable: { rowHeight: 32, iconSize: 16, fontSize: 14, gap: 8 }
|
|
117
|
+
};
|
|
118
|
+
var DEFAULT_TREE_APPEARANCE = {
|
|
119
|
+
density: "cozy",
|
|
120
|
+
...DENSITY_PRESETS.cozy,
|
|
121
|
+
iconStrokeWidth: 1.5,
|
|
122
|
+
indent: 16,
|
|
123
|
+
accent: "default",
|
|
124
|
+
radius: "sm",
|
|
125
|
+
indentGuideOpacity: 0.4,
|
|
126
|
+
showActiveIndicator: true
|
|
127
|
+
};
|
|
128
|
+
function resolveAppearance(input, outerIndent) {
|
|
129
|
+
if (!input && outerIndent === void 0) return DEFAULT_TREE_APPEARANCE;
|
|
130
|
+
const density = input?.density ?? "cozy";
|
|
131
|
+
const preset = DENSITY_PRESETS[density];
|
|
132
|
+
return {
|
|
133
|
+
density,
|
|
134
|
+
rowHeight: input?.rowHeight ?? preset.rowHeight,
|
|
135
|
+
iconSize: input?.iconSize ?? preset.iconSize,
|
|
136
|
+
iconStrokeWidth: input?.iconStrokeWidth ?? DEFAULT_TREE_APPEARANCE.iconStrokeWidth,
|
|
137
|
+
fontSize: input?.fontSize ?? preset.fontSize,
|
|
138
|
+
gap: input?.gap ?? preset.gap,
|
|
139
|
+
indent: input?.indent ?? outerIndent ?? DEFAULT_TREE_APPEARANCE.indent,
|
|
140
|
+
accent: input?.accent ?? DEFAULT_TREE_APPEARANCE.accent,
|
|
141
|
+
radius: input?.radius ?? DEFAULT_TREE_APPEARANCE.radius,
|
|
142
|
+
indentGuideOpacity: input?.indentGuideOpacity ?? DEFAULT_TREE_APPEARANCE.indentGuideOpacity,
|
|
143
|
+
showActiveIndicator: input?.showActiveIndicator ?? DEFAULT_TREE_APPEARANCE.showActiveIndicator
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
__name(resolveAppearance, "resolveAppearance");
|
|
147
|
+
function appearanceToStyle(a) {
|
|
148
|
+
return {
|
|
149
|
+
["--tree-row-height"]: `${a.rowHeight}px`,
|
|
150
|
+
["--tree-icon-size"]: `${a.iconSize}px`,
|
|
151
|
+
["--tree-icon-stroke"]: a.iconStrokeWidth,
|
|
152
|
+
["--tree-font-size"]: `${a.fontSize}px`,
|
|
153
|
+
["--tree-gap"]: `${a.gap}px`,
|
|
154
|
+
["--tree-indent"]: `${a.indent}px`,
|
|
155
|
+
["--tree-guide-opacity"]: a.indentGuideOpacity
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
__name(appearanceToStyle, "appearanceToStyle");
|
|
159
|
+
var RADIUS_CLASS = {
|
|
160
|
+
none: "rounded-none",
|
|
161
|
+
sm: "rounded-sm",
|
|
162
|
+
md: "rounded-md"
|
|
163
|
+
};
|
|
164
|
+
function radiusClass(a) {
|
|
165
|
+
return RADIUS_CLASS[a.radius];
|
|
166
|
+
}
|
|
167
|
+
__name(radiusClass, "radiusClass");
|
|
168
|
+
var HOVER = {
|
|
169
|
+
subtle: "hover:bg-foreground/[.03]",
|
|
170
|
+
default: "hover:bg-foreground/[.06]",
|
|
171
|
+
strong: "hover:bg-foreground/[.09]"
|
|
172
|
+
};
|
|
173
|
+
var FOCUSED_NOT_SELECTED = {
|
|
174
|
+
subtle: "data-[focused=true]:bg-foreground/[.05]",
|
|
175
|
+
default: "data-[focused=true]:bg-foreground/[.08]",
|
|
176
|
+
strong: "data-[focused=true]:bg-foreground/[.12]"
|
|
177
|
+
};
|
|
178
|
+
var SELECTED_INACTIVE = {
|
|
179
|
+
subtle: "data-[selected=true]:bg-foreground/[.06]",
|
|
180
|
+
default: "data-[selected=true]:bg-foreground/[.10]",
|
|
181
|
+
strong: "data-[selected=true]:bg-foreground/[.14]"
|
|
182
|
+
};
|
|
183
|
+
var SELECTED_ACTIVE = {
|
|
184
|
+
subtle: "data-[selected=true]:group-focus-within/tree:bg-primary/10 data-[selected=true]:group-focus-within/tree:text-primary",
|
|
185
|
+
default: "data-[selected=true]:group-focus-within/tree:bg-primary/15 data-[selected=true]:group-focus-within/tree:text-primary",
|
|
186
|
+
strong: "data-[selected=true]:group-focus-within/tree:bg-primary/25 data-[selected=true]:group-focus-within/tree:text-primary"
|
|
187
|
+
};
|
|
188
|
+
function rowStateClasses(a) {
|
|
189
|
+
return [
|
|
190
|
+
HOVER[a.accent],
|
|
191
|
+
FOCUSED_NOT_SELECTED[a.accent],
|
|
192
|
+
SELECTED_INACTIVE[a.accent],
|
|
193
|
+
SELECTED_ACTIVE[a.accent]
|
|
194
|
+
].join(" ");
|
|
195
|
+
}
|
|
196
|
+
__name(rowStateClasses, "rowStateClasses");
|
|
197
|
+
var reducer = /* @__PURE__ */ __name((state, action) => {
|
|
198
|
+
switch (action.type) {
|
|
199
|
+
case "expand": {
|
|
200
|
+
if (state.expanded.has(action.id)) return state;
|
|
201
|
+
const next = new Set(state.expanded);
|
|
202
|
+
next.add(action.id);
|
|
203
|
+
return { ...state, expanded: next };
|
|
204
|
+
}
|
|
205
|
+
case "collapse": {
|
|
206
|
+
if (!state.expanded.has(action.id)) return state;
|
|
207
|
+
const next = new Set(state.expanded);
|
|
208
|
+
next.delete(action.id);
|
|
209
|
+
return { ...state, expanded: next };
|
|
210
|
+
}
|
|
211
|
+
case "toggle": {
|
|
212
|
+
const next = new Set(state.expanded);
|
|
213
|
+
if (next.has(action.id)) next.delete(action.id);
|
|
214
|
+
else next.add(action.id);
|
|
215
|
+
return { ...state, expanded: next };
|
|
216
|
+
}
|
|
217
|
+
case "set-expanded":
|
|
218
|
+
return { ...state, expanded: new Set(action.ids) };
|
|
219
|
+
case "select": {
|
|
220
|
+
if (action.mode === "none") return state;
|
|
221
|
+
if (action.mode === "single") {
|
|
222
|
+
return { ...state, selected: /* @__PURE__ */ new Set([action.id]), focused: action.id };
|
|
223
|
+
}
|
|
224
|
+
const next = new Set(state.selected);
|
|
225
|
+
if (next.has(action.id)) next.delete(action.id);
|
|
226
|
+
else next.add(action.id);
|
|
227
|
+
return { ...state, selected: next, focused: action.id };
|
|
228
|
+
}
|
|
229
|
+
case "select-many":
|
|
230
|
+
return { ...state, selected: new Set(action.ids) };
|
|
231
|
+
case "clear-selection":
|
|
232
|
+
return { ...state, selected: /* @__PURE__ */ new Set() };
|
|
233
|
+
case "focus":
|
|
234
|
+
return { ...state, focused: action.id };
|
|
235
|
+
case "set-query":
|
|
236
|
+
return { ...state, query: action.q };
|
|
237
|
+
case "cache-tick":
|
|
238
|
+
return { ...state, cacheTick: state.cacheTick + 1 };
|
|
239
|
+
default:
|
|
240
|
+
return state;
|
|
241
|
+
}
|
|
242
|
+
}, "reducer");
|
|
243
|
+
var TreeContext = createContext(null);
|
|
244
|
+
function useTreeContext() {
|
|
245
|
+
const ctx = React.useContext(TreeContext);
|
|
246
|
+
if (!ctx) {
|
|
247
|
+
throw new Error("useTreeContext must be used inside <TreeProvider>");
|
|
248
|
+
}
|
|
249
|
+
return ctx;
|
|
250
|
+
}
|
|
251
|
+
__name(useTreeContext, "useTreeContext");
|
|
252
|
+
var setEqualsArr = /* @__PURE__ */ __name((set, arr) => {
|
|
253
|
+
if (set.size !== arr.length) return false;
|
|
254
|
+
for (const id of arr) if (!set.has(id)) return false;
|
|
255
|
+
return true;
|
|
256
|
+
}, "setEqualsArr");
|
|
257
|
+
var collectAllIds = /* @__PURE__ */ __name((roots, cache, out) => {
|
|
258
|
+
for (const node of roots) {
|
|
259
|
+
if (Array.isArray(node.children)) {
|
|
260
|
+
out.push(node.id);
|
|
261
|
+
collectAllIds(node.children, cache, out);
|
|
262
|
+
} else if (node.isFolder) {
|
|
263
|
+
out.push(node.id);
|
|
264
|
+
const entry = cache.get(node.id);
|
|
265
|
+
if (entry?.children) collectAllIds(entry.children, cache, out);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}, "collectAllIds");
|
|
269
|
+
function TreeProvider(props) {
|
|
270
|
+
const {
|
|
271
|
+
data,
|
|
272
|
+
getItemName,
|
|
273
|
+
loadChildren,
|
|
274
|
+
selectionMode = "single",
|
|
275
|
+
initialExpandedIds,
|
|
276
|
+
initialSelectedIds,
|
|
277
|
+
indent,
|
|
278
|
+
appearance,
|
|
279
|
+
onSelectionChange,
|
|
280
|
+
onExpansionChange,
|
|
281
|
+
onActivate,
|
|
282
|
+
enableSearch = false,
|
|
283
|
+
showIndentGuides = false,
|
|
284
|
+
renderIcon,
|
|
285
|
+
renderLabel,
|
|
286
|
+
renderActions,
|
|
287
|
+
renderContextMenu,
|
|
288
|
+
labels: labelsOverride,
|
|
289
|
+
persistKey,
|
|
290
|
+
persistSelection = false,
|
|
291
|
+
children
|
|
292
|
+
} = props;
|
|
293
|
+
const labels = useMemo(
|
|
294
|
+
() => ({ ...DEFAULT_TREE_LABELS, ...labelsOverride }),
|
|
295
|
+
[labelsOverride]
|
|
296
|
+
);
|
|
297
|
+
const resolvedAppearance = useMemo(
|
|
298
|
+
() => resolveAppearance(appearance, indent),
|
|
299
|
+
[appearance, indent]
|
|
300
|
+
);
|
|
301
|
+
const persisted = useMemo(
|
|
302
|
+
() => persistKey ? loadTreeState(persistKey) : null,
|
|
303
|
+
[persistKey]
|
|
304
|
+
);
|
|
305
|
+
const [state, dispatch] = useReducer(reducer, void 0, () => ({
|
|
306
|
+
expanded: new Set(persisted?.expandedItems ?? initialExpandedIds ?? []),
|
|
307
|
+
selected: new Set(
|
|
308
|
+
(persistSelection ? persisted?.selectedItems : void 0) ?? initialSelectedIds ?? []
|
|
309
|
+
),
|
|
310
|
+
focused: null,
|
|
311
|
+
query: "",
|
|
312
|
+
cacheTick: 0
|
|
313
|
+
}));
|
|
314
|
+
const cacheRef = useRef(createChildCache());
|
|
315
|
+
const inflightRef = useRef(/* @__PURE__ */ new Map());
|
|
316
|
+
const fetchChildren = useCallback(
|
|
317
|
+
async (node) => {
|
|
318
|
+
if (!loadChildren) return;
|
|
319
|
+
if (Array.isArray(node.children)) return;
|
|
320
|
+
const existing = cacheRef.current.get(node.id);
|
|
321
|
+
if (existing?.status === "loaded" || existing?.status === "loading") return;
|
|
322
|
+
const inflight = inflightRef.current.get(node.id);
|
|
323
|
+
if (inflight) return inflight;
|
|
324
|
+
cacheRef.current.set(node.id, { status: "loading", children: [] });
|
|
325
|
+
dispatch({ type: "cache-tick" });
|
|
326
|
+
const promise = (async () => {
|
|
327
|
+
try {
|
|
328
|
+
const children2 = await loadChildren(node);
|
|
329
|
+
cacheRef.current.set(node.id, { status: "loaded", children: children2 });
|
|
330
|
+
} catch (err) {
|
|
331
|
+
cacheRef.current.set(node.id, {
|
|
332
|
+
status: "error",
|
|
333
|
+
children: [],
|
|
334
|
+
error: err instanceof Error ? err.message : String(err)
|
|
335
|
+
});
|
|
336
|
+
} finally {
|
|
337
|
+
inflightRef.current.delete(node.id);
|
|
338
|
+
dispatch({ type: "cache-tick" });
|
|
339
|
+
}
|
|
340
|
+
})();
|
|
341
|
+
inflightRef.current.set(node.id, promise);
|
|
342
|
+
return promise;
|
|
343
|
+
},
|
|
344
|
+
[loadChildren]
|
|
345
|
+
);
|
|
346
|
+
const nodeById = useMemo(() => {
|
|
347
|
+
const map = /* @__PURE__ */ new Map();
|
|
348
|
+
const walk = /* @__PURE__ */ __name((nodes) => {
|
|
349
|
+
for (const n of nodes) {
|
|
350
|
+
map.set(n.id, n);
|
|
351
|
+
if (Array.isArray(n.children)) walk(n.children);
|
|
352
|
+
else {
|
|
353
|
+
const entry = cacheRef.current.get(n.id);
|
|
354
|
+
if (entry?.children) walk(entry.children);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}, "walk");
|
|
358
|
+
walk(data);
|
|
359
|
+
return map;
|
|
360
|
+
}, [data, state.cacheTick]);
|
|
361
|
+
useEffect(() => {
|
|
362
|
+
if (!loadChildren) return;
|
|
363
|
+
for (const id of state.expanded) {
|
|
364
|
+
const node = nodeById.get(id);
|
|
365
|
+
if (!node) continue;
|
|
366
|
+
void fetchChildren(node);
|
|
367
|
+
}
|
|
368
|
+
}, [loadChildren, state.expanded, state.cacheTick, nodeById, fetchChildren]);
|
|
369
|
+
const flatRows = useMemo(
|
|
370
|
+
() => flattenTree({
|
|
371
|
+
roots: data,
|
|
372
|
+
expandedIds: state.expanded,
|
|
373
|
+
cache: cacheRef.current
|
|
374
|
+
}),
|
|
375
|
+
[data, state.expanded, state.cacheTick]
|
|
376
|
+
);
|
|
377
|
+
const matchingIds = useMemo(() => {
|
|
378
|
+
const set = /* @__PURE__ */ new Set();
|
|
379
|
+
if (!enableSearch || state.query.trim() === "") return set;
|
|
380
|
+
const q = state.query.trim().toLowerCase();
|
|
381
|
+
for (const row of flatRows) {
|
|
382
|
+
if (getItemName(row.node).toLowerCase().includes(q)) {
|
|
383
|
+
set.add(row.node.id);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return set;
|
|
387
|
+
}, [enableSearch, state.query, flatRows, getItemName]);
|
|
388
|
+
const onSelectionChangeRef = useRef(onSelectionChange);
|
|
389
|
+
const onExpansionChangeRef = useRef(onExpansionChange);
|
|
390
|
+
const onActivateRef = useRef(onActivate);
|
|
391
|
+
onSelectionChangeRef.current = onSelectionChange;
|
|
392
|
+
onExpansionChangeRef.current = onExpansionChange;
|
|
393
|
+
onActivateRef.current = onActivate;
|
|
394
|
+
const lastSelectedArrRef = useRef([...state.selected]);
|
|
395
|
+
const lastExpandedArrRef = useRef([...state.expanded]);
|
|
396
|
+
useEffect(() => {
|
|
397
|
+
const arr = [...state.expanded];
|
|
398
|
+
if (!setEqualsArr(state.expanded, lastExpandedArrRef.current)) {
|
|
399
|
+
lastExpandedArrRef.current = arr;
|
|
400
|
+
onExpansionChangeRef.current?.(arr);
|
|
401
|
+
if (persistKey) {
|
|
402
|
+
saveTreeState(persistKey, {
|
|
403
|
+
expandedItems: arr,
|
|
404
|
+
selectedItems: persistSelection ? [...state.selected] : []
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}, [state.expanded, persistKey, persistSelection, state.selected]);
|
|
409
|
+
useEffect(() => {
|
|
410
|
+
const arr = [...state.selected];
|
|
411
|
+
if (!setEqualsArr(state.selected, lastSelectedArrRef.current)) {
|
|
412
|
+
lastSelectedArrRef.current = arr;
|
|
413
|
+
onSelectionChangeRef.current?.(arr);
|
|
414
|
+
if (persistKey && persistSelection) {
|
|
415
|
+
saveTreeState(persistKey, {
|
|
416
|
+
expandedItems: [...state.expanded],
|
|
417
|
+
selectedItems: arr
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}, [state.selected, persistKey, persistSelection, state.expanded]);
|
|
422
|
+
const expand = useCallback((id) => dispatch({ type: "expand", id }), []);
|
|
423
|
+
const collapse = useCallback((id) => dispatch({ type: "collapse", id }), []);
|
|
424
|
+
const toggle = useCallback((id) => dispatch({ type: "toggle", id }), []);
|
|
425
|
+
const expandAll = useCallback(() => {
|
|
426
|
+
const ids = [];
|
|
427
|
+
collectAllIds(data, cacheRef.current, ids);
|
|
428
|
+
dispatch({ type: "set-expanded", ids });
|
|
429
|
+
}, [data]);
|
|
430
|
+
const collapseAll = useCallback(
|
|
431
|
+
() => dispatch({ type: "set-expanded", ids: [] }),
|
|
432
|
+
[]
|
|
433
|
+
);
|
|
434
|
+
const select = useCallback(
|
|
435
|
+
(id) => dispatch({ type: "select", id, mode: selectionMode }),
|
|
436
|
+
[selectionMode]
|
|
437
|
+
);
|
|
438
|
+
const setSelectedIds = useCallback(
|
|
439
|
+
(ids) => dispatch({ type: "select-many", ids }),
|
|
440
|
+
[]
|
|
441
|
+
);
|
|
442
|
+
const clearSelection = useCallback(() => dispatch({ type: "clear-selection" }), []);
|
|
443
|
+
const setFocus = useCallback(
|
|
444
|
+
(id) => dispatch({ type: "focus", id }),
|
|
445
|
+
[]
|
|
446
|
+
);
|
|
447
|
+
const setQuery = useCallback((q) => dispatch({ type: "set-query", q }), []);
|
|
448
|
+
const refresh = useCallback(
|
|
449
|
+
async (id) => {
|
|
450
|
+
const node = nodeById.get(id);
|
|
451
|
+
if (!node || !loadChildren) return;
|
|
452
|
+
cacheRef.current.delete(id);
|
|
453
|
+
dispatch({ type: "cache-tick" });
|
|
454
|
+
await fetchChildren(node);
|
|
455
|
+
},
|
|
456
|
+
[nodeById, loadChildren, fetchChildren]
|
|
457
|
+
);
|
|
458
|
+
const refreshAll = useCallback(async () => {
|
|
459
|
+
cacheRef.current.clear();
|
|
460
|
+
dispatch({ type: "cache-tick" });
|
|
461
|
+
if (!loadChildren) return;
|
|
462
|
+
await Promise.all(
|
|
463
|
+
[...state.expanded].map((id) => {
|
|
464
|
+
const node = nodeById.get(id);
|
|
465
|
+
return node ? fetchChildren(node) : void 0;
|
|
466
|
+
})
|
|
467
|
+
);
|
|
468
|
+
}, [loadChildren, state.expanded, nodeById, fetchChildren]);
|
|
469
|
+
const activate = useCallback(
|
|
470
|
+
(node) => onActivateRef.current?.(node),
|
|
471
|
+
[]
|
|
472
|
+
);
|
|
473
|
+
const value = useMemo(
|
|
474
|
+
() => ({
|
|
475
|
+
expanded: state.expanded,
|
|
476
|
+
selected: state.selected,
|
|
477
|
+
focused: state.focused,
|
|
478
|
+
query: state.query,
|
|
479
|
+
flatRows,
|
|
480
|
+
matchingIds,
|
|
481
|
+
expand,
|
|
482
|
+
collapse,
|
|
483
|
+
toggle,
|
|
484
|
+
expandAll,
|
|
485
|
+
collapseAll,
|
|
486
|
+
select,
|
|
487
|
+
setSelectedIds,
|
|
488
|
+
clearSelection,
|
|
489
|
+
setFocus,
|
|
490
|
+
setQuery,
|
|
491
|
+
refresh,
|
|
492
|
+
refreshAll,
|
|
493
|
+
activate,
|
|
494
|
+
labels,
|
|
495
|
+
appearance: resolvedAppearance,
|
|
496
|
+
indent: resolvedAppearance.indent,
|
|
497
|
+
selectionMode,
|
|
498
|
+
enableSearch,
|
|
499
|
+
showIndentGuides,
|
|
500
|
+
getItemName,
|
|
501
|
+
renderIcon,
|
|
502
|
+
renderLabel,
|
|
503
|
+
renderActions,
|
|
504
|
+
renderContextMenu
|
|
505
|
+
}),
|
|
506
|
+
[
|
|
507
|
+
state.expanded,
|
|
508
|
+
state.selected,
|
|
509
|
+
state.focused,
|
|
510
|
+
state.query,
|
|
511
|
+
flatRows,
|
|
512
|
+
matchingIds,
|
|
513
|
+
expand,
|
|
514
|
+
collapse,
|
|
515
|
+
toggle,
|
|
516
|
+
expandAll,
|
|
517
|
+
collapseAll,
|
|
518
|
+
select,
|
|
519
|
+
setSelectedIds,
|
|
520
|
+
clearSelection,
|
|
521
|
+
setFocus,
|
|
522
|
+
setQuery,
|
|
523
|
+
refresh,
|
|
524
|
+
refreshAll,
|
|
525
|
+
activate,
|
|
526
|
+
labels,
|
|
527
|
+
resolvedAppearance,
|
|
528
|
+
selectionMode,
|
|
529
|
+
enableSearch,
|
|
530
|
+
showIndentGuides,
|
|
531
|
+
getItemName,
|
|
532
|
+
renderIcon,
|
|
533
|
+
renderLabel,
|
|
534
|
+
renderActions,
|
|
535
|
+
renderContextMenu
|
|
536
|
+
]
|
|
537
|
+
);
|
|
538
|
+
return /* @__PURE__ */ jsx(TreeContext.Provider, { value, children });
|
|
539
|
+
}
|
|
540
|
+
__name(TreeProvider, "TreeProvider");
|
|
541
|
+
function TreeChevron({ isExpanded, isFolder, className }) {
|
|
542
|
+
const { appearance } = useTreeContext();
|
|
543
|
+
const size = { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" };
|
|
544
|
+
if (!isFolder) {
|
|
545
|
+
return /* @__PURE__ */ jsx(
|
|
546
|
+
"span",
|
|
547
|
+
{
|
|
548
|
+
"aria-hidden": true,
|
|
549
|
+
style: size,
|
|
550
|
+
className: cn("inline-block shrink-0", className)
|
|
551
|
+
}
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
const Icon = isExpanded ? ChevronDown : ChevronRight;
|
|
555
|
+
return /* @__PURE__ */ jsx(
|
|
556
|
+
Icon,
|
|
557
|
+
{
|
|
558
|
+
"aria-hidden": true,
|
|
559
|
+
strokeWidth: appearance.iconStrokeWidth,
|
|
560
|
+
style: size,
|
|
561
|
+
className: cn(
|
|
562
|
+
"shrink-0 text-muted-foreground/70 transition-transform",
|
|
563
|
+
className
|
|
564
|
+
)
|
|
565
|
+
}
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
__name(TreeChevron, "TreeChevron");
|
|
569
|
+
function TreeIcon({ isFolder, isExpanded, className }) {
|
|
570
|
+
const { appearance } = useTreeContext();
|
|
571
|
+
const Icon = isFolder ? isExpanded ? FolderOpen : Folder : File;
|
|
572
|
+
return /* @__PURE__ */ jsx(
|
|
573
|
+
Icon,
|
|
574
|
+
{
|
|
575
|
+
"aria-hidden": true,
|
|
576
|
+
strokeWidth: appearance.iconStrokeWidth,
|
|
577
|
+
style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
|
|
578
|
+
className: cn(
|
|
579
|
+
"shrink-0",
|
|
580
|
+
isFolder ? "text-foreground/70" : "text-muted-foreground/80",
|
|
581
|
+
className
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
__name(TreeIcon, "TreeIcon");
|
|
587
|
+
function TreeIndentGuides({ level, indent }) {
|
|
588
|
+
const { appearance } = useTreeContext();
|
|
589
|
+
if (level <= 0) return null;
|
|
590
|
+
return /* @__PURE__ */ jsx(
|
|
591
|
+
"span",
|
|
592
|
+
{
|
|
593
|
+
"aria-hidden": true,
|
|
594
|
+
className: "pointer-events-none absolute inset-y-0 left-0",
|
|
595
|
+
style: { width: 8 + level * indent, opacity: appearance.indentGuideOpacity },
|
|
596
|
+
children: Array.from({ length: level }).map((_, i) => /* @__PURE__ */ jsx(
|
|
597
|
+
"span",
|
|
598
|
+
{
|
|
599
|
+
className: "absolute inset-y-0 w-px bg-border",
|
|
600
|
+
style: { left: 8 + i * indent + indent / 2 - 0.5 }
|
|
601
|
+
},
|
|
602
|
+
i
|
|
603
|
+
))
|
|
604
|
+
}
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
__name(TreeIndentGuides, "TreeIndentGuides");
|
|
608
|
+
function TreeLabel({ children, isMatchingSearch, className }) {
|
|
609
|
+
return /* @__PURE__ */ jsx(
|
|
610
|
+
"span",
|
|
611
|
+
{
|
|
612
|
+
style: { fontSize: "var(--tree-font-size)" },
|
|
613
|
+
className: cn(
|
|
614
|
+
"truncate leading-tight tracking-[-0.005em]",
|
|
615
|
+
isMatchingSearch && "font-medium text-foreground",
|
|
616
|
+
className
|
|
617
|
+
),
|
|
618
|
+
children
|
|
619
|
+
}
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
__name(TreeLabel, "TreeLabel");
|
|
623
|
+
function TreeRow({ row, className }) {
|
|
624
|
+
const ctx = useTreeContext();
|
|
625
|
+
const {
|
|
626
|
+
appearance,
|
|
627
|
+
showIndentGuides,
|
|
628
|
+
selected,
|
|
629
|
+
focused,
|
|
630
|
+
matchingIds,
|
|
631
|
+
select,
|
|
632
|
+
toggle,
|
|
633
|
+
setFocus,
|
|
634
|
+
activate,
|
|
635
|
+
getItemName,
|
|
636
|
+
renderIcon,
|
|
637
|
+
renderLabel,
|
|
638
|
+
renderActions,
|
|
639
|
+
renderContextMenu
|
|
640
|
+
} = ctx;
|
|
641
|
+
const { node, level, isFolder, isExpanded, isLoading } = row;
|
|
642
|
+
const isSelected = selected.has(node.id);
|
|
643
|
+
const isFocused = focused === node.id;
|
|
644
|
+
const isMatchingSearch = matchingIds.has(node.id);
|
|
645
|
+
const slot = {
|
|
646
|
+
node,
|
|
647
|
+
level,
|
|
648
|
+
isSelected,
|
|
649
|
+
isExpanded,
|
|
650
|
+
isFocused,
|
|
651
|
+
isFolder,
|
|
652
|
+
isLoading,
|
|
653
|
+
isMatchingSearch
|
|
654
|
+
};
|
|
655
|
+
const handleActivate = /* @__PURE__ */ __name(() => {
|
|
656
|
+
if (node.disabled) return;
|
|
657
|
+
setFocus(node.id);
|
|
658
|
+
select(node.id);
|
|
659
|
+
if (isFolder) toggle(node.id);
|
|
660
|
+
else activate(node);
|
|
661
|
+
}, "handleActivate");
|
|
662
|
+
const handleClick = /* @__PURE__ */ __name((e) => {
|
|
663
|
+
handleActivate();
|
|
664
|
+
e.currentTarget.scrollIntoView?.({ block: "nearest" });
|
|
665
|
+
}, "handleClick");
|
|
666
|
+
const handleDoubleClick = /* @__PURE__ */ __name(() => {
|
|
667
|
+
if (node.disabled) return;
|
|
668
|
+
if (!isFolder) activate(node);
|
|
669
|
+
}, "handleDoubleClick");
|
|
670
|
+
const handleKeyDown = /* @__PURE__ */ __name((e) => {
|
|
671
|
+
if (node.disabled) return;
|
|
672
|
+
if (e.key === "Enter" || e.key === " ") {
|
|
673
|
+
e.preventDefault();
|
|
674
|
+
handleActivate();
|
|
675
|
+
}
|
|
676
|
+
}, "handleKeyDown");
|
|
677
|
+
const trigger = /* @__PURE__ */ jsxs(
|
|
678
|
+
"div",
|
|
679
|
+
{
|
|
680
|
+
role: "treeitem",
|
|
681
|
+
"aria-level": level + 1,
|
|
682
|
+
"aria-expanded": isFolder ? isExpanded : void 0,
|
|
683
|
+
"aria-selected": isSelected || void 0,
|
|
684
|
+
"aria-current": isSelected ? "true" : void 0,
|
|
685
|
+
"aria-disabled": node.disabled || void 0,
|
|
686
|
+
"data-tree-row": "",
|
|
687
|
+
"data-id": node.id,
|
|
688
|
+
"data-selected": isSelected ? "true" : void 0,
|
|
689
|
+
"data-focused": isFocused && !isSelected ? "true" : void 0,
|
|
690
|
+
"data-folder": isFolder || void 0,
|
|
691
|
+
"data-expanded": isExpanded || void 0,
|
|
692
|
+
tabIndex: isFocused ? 0 : -1,
|
|
693
|
+
style: {
|
|
694
|
+
paddingLeft: 6 + level * appearance.indent,
|
|
695
|
+
height: "var(--tree-row-height)",
|
|
696
|
+
gap: "var(--tree-gap)"
|
|
697
|
+
},
|
|
698
|
+
onClick: handleClick,
|
|
699
|
+
onDoubleClick: handleDoubleClick,
|
|
700
|
+
onKeyDown: handleKeyDown,
|
|
701
|
+
onFocus: () => setFocus(node.id),
|
|
702
|
+
className: cn(
|
|
703
|
+
"group/row relative flex w-full select-none items-center pr-2 text-left",
|
|
704
|
+
"transition-colors outline-none",
|
|
705
|
+
node.disabled ? "cursor-not-allowed" : "cursor-pointer",
|
|
706
|
+
radiusClass(appearance),
|
|
707
|
+
rowStateClasses(appearance),
|
|
708
|
+
"focus-visible:ring-1 focus-visible:ring-ring/50",
|
|
709
|
+
isMatchingSearch && "ring-1 ring-primary/30",
|
|
710
|
+
node.disabled && "opacity-50",
|
|
711
|
+
className
|
|
712
|
+
),
|
|
713
|
+
children: [
|
|
714
|
+
appearance.showActiveIndicator && isSelected ? /* @__PURE__ */ jsx(
|
|
715
|
+
"span",
|
|
716
|
+
{
|
|
717
|
+
"aria-hidden": true,
|
|
718
|
+
className: cn(
|
|
719
|
+
"absolute left-0 top-1 bottom-1 w-0.5 rounded-r-full",
|
|
720
|
+
"bg-foreground/30 group-focus-within/tree:bg-primary"
|
|
721
|
+
)
|
|
722
|
+
}
|
|
723
|
+
) : null,
|
|
724
|
+
showIndentGuides && level > 0 ? /* @__PURE__ */ jsx(TreeIndentGuides, { level, indent: appearance.indent }) : null,
|
|
725
|
+
/* @__PURE__ */ jsx(TreeChevron, { isExpanded, isFolder }),
|
|
726
|
+
isLoading ? /* @__PURE__ */ jsx(
|
|
727
|
+
Loader2,
|
|
728
|
+
{
|
|
729
|
+
"aria-hidden": true,
|
|
730
|
+
strokeWidth: appearance.iconStrokeWidth,
|
|
731
|
+
style: { width: "var(--tree-icon-size)", height: "var(--tree-icon-size)" },
|
|
732
|
+
className: "shrink-0 animate-spin text-muted-foreground/70"
|
|
733
|
+
}
|
|
734
|
+
) : renderIcon ? renderIcon(slot) : /* @__PURE__ */ jsx(TreeIcon, { isFolder, isExpanded }),
|
|
735
|
+
/* @__PURE__ */ jsx(
|
|
736
|
+
"span",
|
|
737
|
+
{
|
|
738
|
+
className: "flex min-w-0 flex-1 items-center",
|
|
739
|
+
style: { gap: "var(--tree-gap)" },
|
|
740
|
+
children: renderLabel ? renderLabel(slot) : /* @__PURE__ */ jsx(TreeLabel, { isMatchingSearch, children: getItemName(node) })
|
|
741
|
+
}
|
|
742
|
+
),
|
|
743
|
+
renderActions ? /* @__PURE__ */ jsx(
|
|
744
|
+
"span",
|
|
745
|
+
{
|
|
746
|
+
className: "ml-auto flex shrink-0 items-center gap-1 opacity-0 transition-opacity group-hover/row:opacity-100 group-focus-within/row:opacity-100",
|
|
747
|
+
onClick: (e) => e.stopPropagation(),
|
|
748
|
+
children: renderActions(slot)
|
|
749
|
+
}
|
|
750
|
+
) : null
|
|
751
|
+
]
|
|
752
|
+
}
|
|
753
|
+
);
|
|
754
|
+
if (renderContextMenu) {
|
|
755
|
+
return /* @__PURE__ */ jsx(Fragment, { children: renderContextMenu(slot, trigger) });
|
|
756
|
+
}
|
|
757
|
+
return trigger;
|
|
758
|
+
}
|
|
759
|
+
__name(TreeRow, "TreeRow");
|
|
760
|
+
function TreeEmpty({ children, className }) {
|
|
761
|
+
return /* @__PURE__ */ jsx(
|
|
762
|
+
"div",
|
|
763
|
+
{
|
|
764
|
+
className: cn(
|
|
765
|
+
"flex h-full min-h-32 items-center justify-center px-4 py-6 text-sm text-muted-foreground",
|
|
766
|
+
className
|
|
767
|
+
),
|
|
768
|
+
children
|
|
769
|
+
}
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
__name(TreeEmpty, "TreeEmpty");
|
|
773
|
+
function TreeContent({ children, className, ariaLabel }) {
|
|
774
|
+
const { flatRows, labels, selected, focused, matchingIds } = useTreeContext();
|
|
775
|
+
if (flatRows.length === 0) {
|
|
776
|
+
return /* @__PURE__ */ jsx(TreeEmpty, { children: labels.empty });
|
|
777
|
+
}
|
|
778
|
+
return /* @__PURE__ */ jsx(
|
|
779
|
+
"div",
|
|
780
|
+
{
|
|
781
|
+
role: "tree",
|
|
782
|
+
"aria-label": ariaLabel ?? labels.ariaLabel,
|
|
783
|
+
className: cn("relative flex flex-col py-1", className),
|
|
784
|
+
children: flatRows.map((row) => {
|
|
785
|
+
const slot = {
|
|
786
|
+
node: row.node,
|
|
787
|
+
level: row.level,
|
|
788
|
+
isSelected: selected.has(row.node.id),
|
|
789
|
+
isExpanded: row.isExpanded,
|
|
790
|
+
isFocused: focused === row.node.id,
|
|
791
|
+
isFolder: row.isFolder,
|
|
792
|
+
isLoading: row.isLoading,
|
|
793
|
+
isMatchingSearch: matchingIds.has(row.node.id)
|
|
794
|
+
};
|
|
795
|
+
const node = children ? children(slot) : /* @__PURE__ */ jsx(TreeRow, { row });
|
|
796
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: node }, row.node.id);
|
|
797
|
+
})
|
|
798
|
+
}
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
__name(TreeContent, "TreeContent");
|
|
802
|
+
function useTreeLabels() {
|
|
803
|
+
return useTreeContext().labels;
|
|
804
|
+
}
|
|
805
|
+
__name(useTreeLabels, "useTreeLabels");
|
|
806
|
+
function useTreeRows() {
|
|
807
|
+
return useTreeContext().flatRows;
|
|
808
|
+
}
|
|
809
|
+
__name(useTreeRows, "useTreeRows");
|
|
810
|
+
function useTreeSelection() {
|
|
811
|
+
const ctx = useTreeContext();
|
|
812
|
+
const selectedIds = useMemo(() => [...ctx.selected], [ctx.selected]);
|
|
813
|
+
const isSelected = useCallback(
|
|
814
|
+
(id) => ctx.selected.has(id),
|
|
815
|
+
[ctx.selected]
|
|
816
|
+
);
|
|
817
|
+
return useMemo(
|
|
818
|
+
() => ({
|
|
819
|
+
selectedIds,
|
|
820
|
+
select: ctx.select,
|
|
821
|
+
setSelectedIds: ctx.setSelectedIds,
|
|
822
|
+
clear: ctx.clearSelection,
|
|
823
|
+
isSelected
|
|
824
|
+
}),
|
|
825
|
+
[selectedIds, ctx.select, ctx.setSelectedIds, ctx.clearSelection, isSelected]
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
__name(useTreeSelection, "useTreeSelection");
|
|
829
|
+
function useTreeExpansion() {
|
|
830
|
+
const ctx = useTreeContext();
|
|
831
|
+
const expandedIds = useMemo(() => [...ctx.expanded], [ctx.expanded]);
|
|
832
|
+
const isExpanded = useCallback(
|
|
833
|
+
(id) => ctx.expanded.has(id),
|
|
834
|
+
[ctx.expanded]
|
|
835
|
+
);
|
|
836
|
+
return useMemo(
|
|
837
|
+
() => ({
|
|
838
|
+
expandedIds,
|
|
839
|
+
expand: ctx.expand,
|
|
840
|
+
collapse: ctx.collapse,
|
|
841
|
+
toggle: ctx.toggle,
|
|
842
|
+
expandAll: ctx.expandAll,
|
|
843
|
+
collapseAll: ctx.collapseAll,
|
|
844
|
+
isExpanded
|
|
845
|
+
}),
|
|
846
|
+
[
|
|
847
|
+
expandedIds,
|
|
848
|
+
ctx.expand,
|
|
849
|
+
ctx.collapse,
|
|
850
|
+
ctx.toggle,
|
|
851
|
+
ctx.expandAll,
|
|
852
|
+
ctx.collapseAll,
|
|
853
|
+
isExpanded
|
|
854
|
+
]
|
|
855
|
+
);
|
|
856
|
+
}
|
|
857
|
+
__name(useTreeExpansion, "useTreeExpansion");
|
|
858
|
+
function useTreeFocus() {
|
|
859
|
+
const ctx = useTreeContext();
|
|
860
|
+
return useMemo(
|
|
861
|
+
() => ({ focusedId: ctx.focused, setFocus: ctx.setFocus }),
|
|
862
|
+
[ctx.focused, ctx.setFocus]
|
|
863
|
+
);
|
|
864
|
+
}
|
|
865
|
+
__name(useTreeFocus, "useTreeFocus");
|
|
866
|
+
function useTreeSearch() {
|
|
867
|
+
const ctx = useTreeContext();
|
|
868
|
+
return useMemo(
|
|
869
|
+
() => ({
|
|
870
|
+
isOpen: ctx.enableSearch,
|
|
871
|
+
query: ctx.query,
|
|
872
|
+
setQuery: ctx.setQuery,
|
|
873
|
+
matchingIds: ctx.matchingIds,
|
|
874
|
+
matchCount: ctx.matchingIds.size
|
|
875
|
+
}),
|
|
876
|
+
[ctx.enableSearch, ctx.query, ctx.setQuery, ctx.matchingIds]
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
__name(useTreeSearch, "useTreeSearch");
|
|
880
|
+
function useTreeActions() {
|
|
881
|
+
const ctx = useTreeContext();
|
|
882
|
+
return useMemo(
|
|
883
|
+
() => ({
|
|
884
|
+
expand: ctx.expand,
|
|
885
|
+
collapse: ctx.collapse,
|
|
886
|
+
toggle: ctx.toggle,
|
|
887
|
+
expandAll: ctx.expandAll,
|
|
888
|
+
collapseAll: ctx.collapseAll,
|
|
889
|
+
refresh: ctx.refresh,
|
|
890
|
+
refreshAll: ctx.refreshAll,
|
|
891
|
+
activate: ctx.activate
|
|
892
|
+
}),
|
|
893
|
+
[
|
|
894
|
+
ctx.expand,
|
|
895
|
+
ctx.collapse,
|
|
896
|
+
ctx.toggle,
|
|
897
|
+
ctx.expandAll,
|
|
898
|
+
ctx.collapseAll,
|
|
899
|
+
ctx.refresh,
|
|
900
|
+
ctx.refreshAll,
|
|
901
|
+
ctx.activate
|
|
902
|
+
]
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
__name(useTreeActions, "useTreeActions");
|
|
906
|
+
function TreeSearchInput({ className, showMatches = true }) {
|
|
907
|
+
const { labels } = useTreeContext();
|
|
908
|
+
const { query, setQuery, matchCount } = useTreeSearch();
|
|
909
|
+
return /* @__PURE__ */ jsxs(
|
|
910
|
+
"div",
|
|
911
|
+
{
|
|
912
|
+
className: cn(
|
|
913
|
+
"flex items-center gap-2 rounded-md border border-border bg-background px-2",
|
|
914
|
+
className
|
|
915
|
+
),
|
|
916
|
+
children: [
|
|
917
|
+
/* @__PURE__ */ jsx(Search, { "aria-hidden": true, className: "size-3.5 shrink-0 text-muted-foreground" }),
|
|
918
|
+
/* @__PURE__ */ jsx(
|
|
919
|
+
"input",
|
|
920
|
+
{
|
|
921
|
+
type: "search",
|
|
922
|
+
value: query,
|
|
923
|
+
onChange: (e) => setQuery(e.target.value),
|
|
924
|
+
placeholder: labels.searchPlaceholder,
|
|
925
|
+
className: "h-7 w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground"
|
|
926
|
+
}
|
|
927
|
+
),
|
|
928
|
+
showMatches && query.trim().length > 0 ? /* @__PURE__ */ jsx("span", { className: "shrink-0 text-xs text-muted-foreground", children: labels.searchMatches(matchCount) }) : null,
|
|
929
|
+
query.length > 0 ? /* @__PURE__ */ jsx(
|
|
930
|
+
"button",
|
|
931
|
+
{
|
|
932
|
+
type: "button",
|
|
933
|
+
onClick: () => setQuery(""),
|
|
934
|
+
"aria-label": "Clear search",
|
|
935
|
+
className: "shrink-0 rounded p-1 text-muted-foreground hover:bg-accent hover:text-foreground",
|
|
936
|
+
children: /* @__PURE__ */ jsx(X, { "aria-hidden": true, className: "size-3.5" })
|
|
937
|
+
}
|
|
938
|
+
) : null
|
|
939
|
+
]
|
|
940
|
+
}
|
|
941
|
+
);
|
|
942
|
+
}
|
|
943
|
+
__name(TreeSearchInput, "TreeSearchInput");
|
|
944
|
+
function useTreeKeyboard({
|
|
945
|
+
containerRef,
|
|
946
|
+
rows,
|
|
947
|
+
focusedId,
|
|
948
|
+
enabled = true,
|
|
949
|
+
onFocus,
|
|
950
|
+
onSelect,
|
|
951
|
+
onActivate,
|
|
952
|
+
onExpand,
|
|
953
|
+
onCollapse,
|
|
954
|
+
onClearSelection
|
|
955
|
+
}) {
|
|
956
|
+
useEffect(() => {
|
|
957
|
+
if (!enabled) return;
|
|
958
|
+
const target = containerRef.current;
|
|
959
|
+
if (!target) return;
|
|
960
|
+
const handler = /* @__PURE__ */ __name((e) => {
|
|
961
|
+
const tag = e.target?.tagName;
|
|
962
|
+
if (tag === "INPUT" || tag === "TEXTAREA") return;
|
|
963
|
+
if (e.target?.isContentEditable) return;
|
|
964
|
+
if (rows.length === 0) return;
|
|
965
|
+
const currentIndex = focusedId ? rows.findIndex((r) => r.node.id === focusedId) : -1;
|
|
966
|
+
const current = currentIndex >= 0 ? rows[currentIndex] : null;
|
|
967
|
+
switch (e.key) {
|
|
968
|
+
case "ArrowDown": {
|
|
969
|
+
e.preventDefault();
|
|
970
|
+
const next = rows[Math.min(currentIndex + 1, rows.length - 1)] ?? rows[0];
|
|
971
|
+
onFocus(next.node.id);
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
case "ArrowUp": {
|
|
975
|
+
e.preventDefault();
|
|
976
|
+
const prev = rows[Math.max(currentIndex - 1, 0)] ?? rows[0];
|
|
977
|
+
onFocus(prev.node.id);
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
case "Home": {
|
|
981
|
+
e.preventDefault();
|
|
982
|
+
onFocus(rows[0].node.id);
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
case "End": {
|
|
986
|
+
e.preventDefault();
|
|
987
|
+
onFocus(rows[rows.length - 1].node.id);
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
case "ArrowRight": {
|
|
991
|
+
if (!current) return;
|
|
992
|
+
e.preventDefault();
|
|
993
|
+
if (current.isFolder && !current.isExpanded) {
|
|
994
|
+
onExpand(current.node.id);
|
|
995
|
+
} else if (current.isFolder && current.isExpanded) {
|
|
996
|
+
const next = rows[currentIndex + 1];
|
|
997
|
+
if (next) onFocus(next.node.id);
|
|
998
|
+
}
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
case "ArrowLeft": {
|
|
1002
|
+
if (!current) return;
|
|
1003
|
+
e.preventDefault();
|
|
1004
|
+
if (current.isFolder && current.isExpanded) {
|
|
1005
|
+
onCollapse(current.node.id);
|
|
1006
|
+
} else if (current.parentId) {
|
|
1007
|
+
onFocus(current.parentId);
|
|
1008
|
+
}
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
case "Enter":
|
|
1012
|
+
case " ": {
|
|
1013
|
+
if (!current) return;
|
|
1014
|
+
e.preventDefault();
|
|
1015
|
+
onSelect(current.node.id);
|
|
1016
|
+
if (current.isFolder) {
|
|
1017
|
+
if (current.isExpanded) onCollapse(current.node.id);
|
|
1018
|
+
else onExpand(current.node.id);
|
|
1019
|
+
} else {
|
|
1020
|
+
onActivate(current.node.id);
|
|
1021
|
+
}
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
case "Escape": {
|
|
1025
|
+
e.preventDefault();
|
|
1026
|
+
onClearSelection();
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}, "handler");
|
|
1031
|
+
target.addEventListener("keydown", handler);
|
|
1032
|
+
return () => target.removeEventListener("keydown", handler);
|
|
1033
|
+
}, [
|
|
1034
|
+
containerRef,
|
|
1035
|
+
rows,
|
|
1036
|
+
focusedId,
|
|
1037
|
+
enabled,
|
|
1038
|
+
onFocus,
|
|
1039
|
+
onSelect,
|
|
1040
|
+
onActivate,
|
|
1041
|
+
onExpand,
|
|
1042
|
+
onCollapse,
|
|
1043
|
+
onClearSelection
|
|
1044
|
+
]);
|
|
1045
|
+
}
|
|
1046
|
+
__name(useTreeKeyboard, "useTreeKeyboard");
|
|
1047
|
+
var FLUSH_MS = 600;
|
|
1048
|
+
function useTreeTypeAhead({
|
|
1049
|
+
rows,
|
|
1050
|
+
getItemName,
|
|
1051
|
+
containerRef,
|
|
1052
|
+
onMatch,
|
|
1053
|
+
enabled = true
|
|
1054
|
+
}) {
|
|
1055
|
+
const bufferRef = useRef("");
|
|
1056
|
+
const timerRef = useRef(null);
|
|
1057
|
+
const rowsRef = useRef(rows);
|
|
1058
|
+
const getNameRef = useRef(getItemName);
|
|
1059
|
+
const onMatchRef = useRef(onMatch);
|
|
1060
|
+
rowsRef.current = rows;
|
|
1061
|
+
getNameRef.current = getItemName;
|
|
1062
|
+
onMatchRef.current = onMatch;
|
|
1063
|
+
useEffect(() => {
|
|
1064
|
+
if (!enabled) return;
|
|
1065
|
+
const target = containerRef.current;
|
|
1066
|
+
if (!target) return;
|
|
1067
|
+
const reset = /* @__PURE__ */ __name(() => {
|
|
1068
|
+
bufferRef.current = "";
|
|
1069
|
+
if (timerRef.current) {
|
|
1070
|
+
clearTimeout(timerRef.current);
|
|
1071
|
+
timerRef.current = null;
|
|
1072
|
+
}
|
|
1073
|
+
}, "reset");
|
|
1074
|
+
const handler = /* @__PURE__ */ __name((e) => {
|
|
1075
|
+
const tag = e.target?.tagName;
|
|
1076
|
+
if (tag === "INPUT" || tag === "TEXTAREA") return;
|
|
1077
|
+
if (e.target?.isContentEditable) return;
|
|
1078
|
+
if (e.metaKey || e.ctrlKey || e.altKey) return;
|
|
1079
|
+
if (e.key === "Escape" || e.key === "Enter" || e.key === "Tab" || e.key.startsWith("Arrow") || e.key === "Home" || e.key === "End" || e.key === "PageUp" || e.key === "PageDown") {
|
|
1080
|
+
reset();
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
if (e.key.length !== 1) return;
|
|
1084
|
+
bufferRef.current += e.key.toLowerCase();
|
|
1085
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
1086
|
+
timerRef.current = setTimeout(reset, FLUSH_MS);
|
|
1087
|
+
const prefix = bufferRef.current;
|
|
1088
|
+
const hit = rowsRef.current.find(
|
|
1089
|
+
(row) => getNameRef.current(row.node).toLowerCase().startsWith(prefix)
|
|
1090
|
+
);
|
|
1091
|
+
if (hit) {
|
|
1092
|
+
e.preventDefault();
|
|
1093
|
+
onMatchRef.current(hit.node.id);
|
|
1094
|
+
}
|
|
1095
|
+
}, "handler");
|
|
1096
|
+
target.addEventListener("keydown", handler);
|
|
1097
|
+
return () => {
|
|
1098
|
+
target.removeEventListener("keydown", handler);
|
|
1099
|
+
reset();
|
|
1100
|
+
};
|
|
1101
|
+
}, [containerRef, enabled]);
|
|
1102
|
+
}
|
|
1103
|
+
__name(useTreeTypeAhead, "useTreeTypeAhead");
|
|
1104
|
+
function TreeRoot(props) {
|
|
1105
|
+
const {
|
|
1106
|
+
data,
|
|
1107
|
+
getItemName,
|
|
1108
|
+
loadChildren,
|
|
1109
|
+
selectionMode,
|
|
1110
|
+
initialExpandedIds,
|
|
1111
|
+
initialSelectedIds,
|
|
1112
|
+
indent,
|
|
1113
|
+
appearance,
|
|
1114
|
+
onSelectionChange,
|
|
1115
|
+
onExpansionChange,
|
|
1116
|
+
onActivate,
|
|
1117
|
+
enableSearch = false,
|
|
1118
|
+
enableTypeAhead = true,
|
|
1119
|
+
showIndentGuides = false,
|
|
1120
|
+
renderRow,
|
|
1121
|
+
renderIcon,
|
|
1122
|
+
renderLabel,
|
|
1123
|
+
renderActions,
|
|
1124
|
+
renderContextMenu,
|
|
1125
|
+
labels,
|
|
1126
|
+
persistKey,
|
|
1127
|
+
persistSelection = false,
|
|
1128
|
+
className,
|
|
1129
|
+
style
|
|
1130
|
+
} = props;
|
|
1131
|
+
return /* @__PURE__ */ jsx(
|
|
1132
|
+
TreeProvider,
|
|
1133
|
+
{
|
|
1134
|
+
data,
|
|
1135
|
+
getItemName,
|
|
1136
|
+
loadChildren,
|
|
1137
|
+
selectionMode,
|
|
1138
|
+
initialExpandedIds,
|
|
1139
|
+
initialSelectedIds,
|
|
1140
|
+
indent,
|
|
1141
|
+
appearance,
|
|
1142
|
+
onSelectionChange,
|
|
1143
|
+
onExpansionChange,
|
|
1144
|
+
onActivate,
|
|
1145
|
+
enableSearch,
|
|
1146
|
+
showIndentGuides,
|
|
1147
|
+
renderIcon,
|
|
1148
|
+
renderLabel,
|
|
1149
|
+
renderActions,
|
|
1150
|
+
renderContextMenu,
|
|
1151
|
+
labels,
|
|
1152
|
+
persistKey,
|
|
1153
|
+
persistSelection,
|
|
1154
|
+
children: /* @__PURE__ */ jsx(
|
|
1155
|
+
TreeRootShell,
|
|
1156
|
+
{
|
|
1157
|
+
className,
|
|
1158
|
+
style,
|
|
1159
|
+
enableSearch,
|
|
1160
|
+
enableTypeAhead,
|
|
1161
|
+
renderRow
|
|
1162
|
+
}
|
|
1163
|
+
)
|
|
1164
|
+
}
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
__name(TreeRoot, "TreeRoot");
|
|
1168
|
+
function TreeRootShell({
|
|
1169
|
+
className,
|
|
1170
|
+
style,
|
|
1171
|
+
enableSearch,
|
|
1172
|
+
enableTypeAhead,
|
|
1173
|
+
renderRow
|
|
1174
|
+
}) {
|
|
1175
|
+
const containerRef = useRef(null);
|
|
1176
|
+
const ctx = useTreeContext();
|
|
1177
|
+
useTreeKeyboard({
|
|
1178
|
+
containerRef,
|
|
1179
|
+
rows: ctx.flatRows,
|
|
1180
|
+
focusedId: ctx.focused,
|
|
1181
|
+
onFocus: ctx.setFocus,
|
|
1182
|
+
onSelect: ctx.select,
|
|
1183
|
+
onActivate: /* @__PURE__ */ __name((id) => {
|
|
1184
|
+
const row = ctx.flatRows.find((r) => r.node.id === id);
|
|
1185
|
+
if (row) ctx.activate(row.node);
|
|
1186
|
+
}, "onActivate"),
|
|
1187
|
+
onExpand: ctx.expand,
|
|
1188
|
+
onCollapse: ctx.collapse,
|
|
1189
|
+
onClearSelection: ctx.clearSelection
|
|
1190
|
+
});
|
|
1191
|
+
const onTypeAheadMatch = useCallback(
|
|
1192
|
+
(id) => {
|
|
1193
|
+
ctx.setFocus(id);
|
|
1194
|
+
const el = containerRef.current?.querySelector(
|
|
1195
|
+
`[data-tree-row][data-id="${CSS.escape(id)}"]`
|
|
1196
|
+
);
|
|
1197
|
+
el?.scrollIntoView({ block: "nearest" });
|
|
1198
|
+
},
|
|
1199
|
+
[ctx]
|
|
1200
|
+
);
|
|
1201
|
+
useTreeTypeAhead({
|
|
1202
|
+
rows: ctx.flatRows,
|
|
1203
|
+
getItemName: ctx.getItemName,
|
|
1204
|
+
containerRef,
|
|
1205
|
+
onMatch: onTypeAheadMatch,
|
|
1206
|
+
enabled: enableTypeAhead
|
|
1207
|
+
});
|
|
1208
|
+
return /* @__PURE__ */ jsxs(
|
|
1209
|
+
"div",
|
|
1210
|
+
{
|
|
1211
|
+
ref: containerRef,
|
|
1212
|
+
tabIndex: 0,
|
|
1213
|
+
className: cn(
|
|
1214
|
+
"group/tree flex h-full w-full flex-col gap-2 outline-none",
|
|
1215
|
+
className
|
|
1216
|
+
),
|
|
1217
|
+
style: { ...appearanceToStyle(ctx.appearance), ...style },
|
|
1218
|
+
"data-tree-root": "",
|
|
1219
|
+
children: [
|
|
1220
|
+
enableSearch ? /* @__PURE__ */ jsx(TreeSearchInput, { className: "mx-2 mt-2" }) : null,
|
|
1221
|
+
/* @__PURE__ */ jsx("div", { className: "min-h-0 flex-1 overflow-auto px-1", children: /* @__PURE__ */ jsx(TreeContent, { children: renderRow }) })
|
|
1222
|
+
]
|
|
1223
|
+
}
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
__name(TreeRootShell, "TreeRootShell");
|
|
1227
|
+
var TreeRoot_default = TreeRoot;
|
|
1228
|
+
|
|
1229
|
+
export { DEFAULT_TREE_APPEARANCE, DEFAULT_TREE_LABELS, TreeChevron, TreeContent, TreeEmpty, TreeIcon, TreeIndentGuides, TreeLabel, TreeProvider, TreeRoot, TreeRoot_default, TreeRow, TreeSearchInput, appearanceToStyle, clearTreeState, createChildCache, flattenTree, loadTreeState, resolveAppearance, resolveChildren, saveTreeState, useTreeActions, useTreeContext, useTreeExpansion, useTreeFocus, useTreeKeyboard, useTreeLabels, useTreeRows, useTreeSearch, useTreeSelection, useTreeTypeAhead };
|
|
1230
|
+
//# sourceMappingURL=chunk-E5BP4IXF.mjs.map
|
|
1231
|
+
//# sourceMappingURL=chunk-E5BP4IXF.mjs.map
|