nish-ui 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -8
- package/dist/chart.js +1 -1
- package/dist/elements-all.js +13 -13
- package/dist/elements-charts.js +1 -1
- package/dist/elements-data.js +1 -1
- package/dist/elements-forms.js +1 -1
- package/dist/elements.js +3 -3
- package/dist/esm/components/app-shell.js +145 -45
- package/dist/esm/components/breadcrumbs.js +1 -1
- package/dist/esm/components/calendar-classes.js +325 -0
- package/dist/esm/components/calendar.js +263 -0
- package/dist/esm/components/canvas/route.js +56 -20
- package/dist/esm/components/carousel-classes.js +27 -0
- package/dist/esm/components/carousel.js +317 -0
- package/dist/esm/components/command.js +2 -27
- package/dist/esm/components/context-menu.js +199 -0
- package/dist/esm/components/date-picker.js +208 -0
- package/dist/esm/components/display-classes.js +10 -13
- package/dist/esm/components/display.js +22 -19
- package/dist/esm/components/drawer.js +139 -0
- package/dist/esm/components/filters.js +1 -1
- package/dist/esm/components/flow.js +1 -1
- package/dist/esm/components/hover-card.js +221 -0
- package/dist/esm/components/icons.js +17 -0
- package/dist/esm/components/inbox.js +2 -1
- package/dist/esm/components/lazy-module.js +53 -0
- package/dist/esm/components/menu.js +344 -0
- package/dist/esm/components/modal.js +173 -0
- package/dist/esm/components/more-sheet.js +52 -0
- package/dist/esm/components/multi-select.js +420 -0
- package/dist/esm/components/nav-items.js +138 -0
- package/dist/esm/components/nav.js +13 -174
- package/dist/esm/components/navigate.js +33 -0
- package/dist/esm/components/overlay-core.js +221 -0
- package/dist/esm/components/overlay-dialog.js +255 -0
- package/dist/esm/components/overlay-position.js +163 -0
- package/dist/esm/components/overlay.js +7 -1441
- package/dist/esm/components/popover.js +92 -0
- package/dist/esm/components/segmented.js +2 -1
- package/dist/esm/components/split.js +1 -1
- package/dist/esm/components/strings.js +25 -0
- package/dist/esm/components/tooltip.js +177 -0
- package/dist/esm/components/tree-view-classes.js +57 -0
- package/dist/esm/components/tree-view.js +319 -0
- package/dist/esm/components/virtual-table.js +245 -0
- package/dist/esm/components/virtual.js +291 -0
- package/dist/esm/inbox.js +12 -0
- package/dist/esm/index.js +61 -2
- package/dist/esm/runtime.js +1 -1
- package/dist/esm/service-tokens.js +6 -0
- package/dist/esm/theme.js +2 -2
- package/dist/esm/virtual-range.js +73 -0
- package/dist/format.js +1 -1
- package/dist/inbox.d.ts +238 -0
- package/dist/index.d.ts +1030 -777
- package/dist/nish-components.css +2 -0
- package/dist/nish-components.min.css +1 -1
- package/dist/service-tokens.d.ts +60 -0
- package/dist/theme.d.ts +50 -0
- package/dist/theme.global.js +28 -3
- package/dist/theme.js +28 -3
- package/package.json +17 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/tree-view.tsx
|
|
4
|
+
import {
|
|
5
|
+
memo,
|
|
6
|
+
useCallback,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState
|
|
12
|
+
} from "react";
|
|
13
|
+
import { cn } from "../cn.js";
|
|
14
|
+
import { isRtl, useIsoLayoutEffect } from "./client.js";
|
|
15
|
+
import { warnOnce } from "./data-classes.js";
|
|
16
|
+
import { useLatest } from "./latest.js";
|
|
17
|
+
import { duplicateIds, hasChildren, parentMap, treeRowClass, visibleNodes } from "./tree-view-classes.js";
|
|
18
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
19
|
+
function Chevron({ open }) {
|
|
20
|
+
return /* @__PURE__ */ jsx(
|
|
21
|
+
"svg",
|
|
22
|
+
{
|
|
23
|
+
width: "14",
|
|
24
|
+
height: "14",
|
|
25
|
+
viewBox: "0 0 24 24",
|
|
26
|
+
fill: "none",
|
|
27
|
+
stroke: "currentColor",
|
|
28
|
+
strokeWidth: "2",
|
|
29
|
+
strokeLinecap: "round",
|
|
30
|
+
strokeLinejoin: "round",
|
|
31
|
+
"aria-hidden": "true",
|
|
32
|
+
className: cn("transition-transform motion-reduce:transition-none rtl:-scale-x-100", open && "rotate-90 rtl:-rotate-90"),
|
|
33
|
+
children: /* @__PURE__ */ jsx("path", { d: "m9 18 6-6-6-6" })
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
var TreeItem = memo(function TreeItem2({
|
|
38
|
+
node,
|
|
39
|
+
level,
|
|
40
|
+
posinset,
|
|
41
|
+
setsize,
|
|
42
|
+
open,
|
|
43
|
+
selectable,
|
|
44
|
+
selected,
|
|
45
|
+
isTarget,
|
|
46
|
+
labelId,
|
|
47
|
+
itemRef,
|
|
48
|
+
onRowClick,
|
|
49
|
+
onToggle,
|
|
50
|
+
children
|
|
51
|
+
}) {
|
|
52
|
+
const parent = hasChildren(node);
|
|
53
|
+
return /* @__PURE__ */ jsxs(
|
|
54
|
+
"li",
|
|
55
|
+
{
|
|
56
|
+
ref: itemRef,
|
|
57
|
+
role: "treeitem",
|
|
58
|
+
"data-id": node.id,
|
|
59
|
+
"aria-labelledby": labelId,
|
|
60
|
+
"aria-level": level,
|
|
61
|
+
"aria-setsize": setsize,
|
|
62
|
+
"aria-posinset": posinset,
|
|
63
|
+
"aria-expanded": parent ? open : void 0,
|
|
64
|
+
"aria-selected": selectable ? selected : void 0,
|
|
65
|
+
"aria-disabled": node.disabled || void 0,
|
|
66
|
+
"data-state": parent ? open ? "open" : "closed" : void 0,
|
|
67
|
+
"data-disabled": node.disabled ? "" : void 0,
|
|
68
|
+
tabIndex: isTarget ? 0 : -1,
|
|
69
|
+
className: "outline-none [&:focus-visible>div]:outline-2 [&:focus-visible>div]:-outline-offset-2 [&:focus-visible>div]:outline-th-accent",
|
|
70
|
+
children: [
|
|
71
|
+
/* @__PURE__ */ jsxs(
|
|
72
|
+
"div",
|
|
73
|
+
{
|
|
74
|
+
className: treeRowClass({ selected: selectable && selected, disabled: node.disabled }),
|
|
75
|
+
style: { paddingInlineStart: `calc(${level - 1} * 1.25rem + 0.25rem)` },
|
|
76
|
+
onClick: () => onRowClick(node),
|
|
77
|
+
children: [
|
|
78
|
+
/* @__PURE__ */ jsx(
|
|
79
|
+
"span",
|
|
80
|
+
{
|
|
81
|
+
"aria-hidden": "true",
|
|
82
|
+
"data-tree-toggle": "",
|
|
83
|
+
onClick: parent ? (e) => {
|
|
84
|
+
e.stopPropagation();
|
|
85
|
+
onToggle(node);
|
|
86
|
+
} : void 0,
|
|
87
|
+
className: "inline-flex w-6 shrink-0 self-stretch items-center justify-center text-th-hi3 pointer-coarse:w-11",
|
|
88
|
+
children: parent ? /* @__PURE__ */ jsx(Chevron, { open }) : null
|
|
89
|
+
}
|
|
90
|
+
),
|
|
91
|
+
node.icon ? /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "inline-flex shrink-0 text-th-hi3", children: node.icon }) : null,
|
|
92
|
+
/* @__PURE__ */ jsx("span", { id: labelId, className: "min-w-0 flex-1 truncate", children: node.label }),
|
|
93
|
+
node.trailing != null ? /* @__PURE__ */ jsx("span", { className: "ms-auto shrink-0 text-xs text-th-hi3", children: node.trailing }) : null
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
),
|
|
97
|
+
open ? /* @__PURE__ */ jsx("ul", { role: "group", className: "grid gap-px", children }) : null
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
function TreeView(props) {
|
|
103
|
+
const {
|
|
104
|
+
items,
|
|
105
|
+
expanded: expandedProp,
|
|
106
|
+
defaultExpanded,
|
|
107
|
+
onExpandedChange,
|
|
108
|
+
selected: selectedProp,
|
|
109
|
+
defaultSelected,
|
|
110
|
+
onSelectedChange,
|
|
111
|
+
onActivate,
|
|
112
|
+
selectable = true,
|
|
113
|
+
toggleOnSelect = true,
|
|
114
|
+
className,
|
|
115
|
+
ref,
|
|
116
|
+
onKeyDown: onKeyDownProp,
|
|
117
|
+
onFocus: onFocusProp,
|
|
118
|
+
onBlur: onBlurProp,
|
|
119
|
+
...rest
|
|
120
|
+
} = props;
|
|
121
|
+
const uid = useId();
|
|
122
|
+
const [internalExpanded, setInternalExpanded] = useState(() => defaultExpanded ?? []);
|
|
123
|
+
const expandedControlled = expandedProp !== void 0;
|
|
124
|
+
const expandedList = expandedControlled ? expandedProp : internalExpanded;
|
|
125
|
+
const expandedSet = useMemo(() => new Set(expandedList), [expandedList]);
|
|
126
|
+
const [internalSelected, setInternalSelected] = useState(() => defaultSelected ?? null);
|
|
127
|
+
const selectedId = selectedProp !== void 0 ? selectedProp : internalSelected;
|
|
128
|
+
const [focusedId, setFocusedId] = useState(null);
|
|
129
|
+
const flat = useMemo(() => visibleNodes(items, expandedSet), [items, expandedSet]);
|
|
130
|
+
const parents = useMemo(() => parentMap(items), [items]);
|
|
131
|
+
const visible = useMemo(() => new Set(flat.map((f) => f.node.id)), [flat]);
|
|
132
|
+
let targetId = null;
|
|
133
|
+
if (flat.length > 0) {
|
|
134
|
+
let cursor = focusedId;
|
|
135
|
+
while (cursor != null && !visible.has(cursor)) cursor = parents.get(cursor);
|
|
136
|
+
if (cursor != null) targetId = cursor;
|
|
137
|
+
else if (selectedId != null && visible.has(selectedId)) targetId = selectedId;
|
|
138
|
+
else targetId = flat[0].node.id;
|
|
139
|
+
}
|
|
140
|
+
const elements = useRef(/* @__PURE__ */ new Map());
|
|
141
|
+
const pendingFocus = useRef(null);
|
|
142
|
+
const focusWithin = useRef(false);
|
|
143
|
+
const lastFocused = useRef(null);
|
|
144
|
+
const setExpanded = (next) => {
|
|
145
|
+
const list = [...next];
|
|
146
|
+
if (!expandedControlled) setInternalExpanded(list);
|
|
147
|
+
onExpandedChange?.(list);
|
|
148
|
+
};
|
|
149
|
+
const expand = (id) => {
|
|
150
|
+
if (!expandedSet.has(id)) setExpanded(new Set(expandedSet).add(id));
|
|
151
|
+
};
|
|
152
|
+
const collapse = (id) => {
|
|
153
|
+
if (!expandedSet.has(id)) return;
|
|
154
|
+
const next = new Set(expandedSet);
|
|
155
|
+
next.delete(id);
|
|
156
|
+
setExpanded(next);
|
|
157
|
+
};
|
|
158
|
+
const toggle = (id) => expandedSet.has(id) ? collapse(id) : expand(id);
|
|
159
|
+
const select = (node) => {
|
|
160
|
+
if (!selectable || node.disabled) return;
|
|
161
|
+
if (selectedProp === void 0) setInternalSelected(node.id);
|
|
162
|
+
onSelectedChange?.(node.id, node);
|
|
163
|
+
};
|
|
164
|
+
const activate = (node) => {
|
|
165
|
+
if (node.disabled) return;
|
|
166
|
+
onActivate?.(node);
|
|
167
|
+
};
|
|
168
|
+
const focusNode = (id) => {
|
|
169
|
+
setFocusedId(id);
|
|
170
|
+
pendingFocus.current = id;
|
|
171
|
+
const el = elements.current.get(id);
|
|
172
|
+
if (el) {
|
|
173
|
+
pendingFocus.current = null;
|
|
174
|
+
el.focus();
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
useIsoLayoutEffect(() => {
|
|
178
|
+
const pending = pendingFocus.current;
|
|
179
|
+
if (pending !== null) {
|
|
180
|
+
const el = elements.current.get(pending);
|
|
181
|
+
if (el) {
|
|
182
|
+
pendingFocus.current = null;
|
|
183
|
+
el.focus();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const last = lastFocused.current;
|
|
188
|
+
if (focusWithin.current && last && !last.isConnected && typeof document !== "undefined" && document.activeElement === document.body && targetId !== null) {
|
|
189
|
+
lastFocused.current = null;
|
|
190
|
+
elements.current.get(targetId)?.focus();
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
useEffect(() => {
|
|
194
|
+
const dupes = duplicateIds(items);
|
|
195
|
+
if (dupes.length > 0) warnOnce("tree-dup", `TreeView: node ids must be unique; "${dupes[0]}" appears more than once.`);
|
|
196
|
+
}, [items]);
|
|
197
|
+
const labelled = !!(rest["aria-label"] || rest["aria-labelledby"]);
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
if (!labelled) warnOnce("tree-label", "TreeView: give the tree an aria-label or aria-labelledby.");
|
|
200
|
+
}, [labelled]);
|
|
201
|
+
const onFocus = (e) => {
|
|
202
|
+
onFocusProp?.(e);
|
|
203
|
+
focusWithin.current = true;
|
|
204
|
+
const t = e.target;
|
|
205
|
+
if (t.getAttribute("role") === "treeitem") {
|
|
206
|
+
lastFocused.current = t;
|
|
207
|
+
const id = t.getAttribute("data-id");
|
|
208
|
+
if (id !== null) setFocusedId(id);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const onBlur = (e) => {
|
|
212
|
+
onBlurProp?.(e);
|
|
213
|
+
const next = e.relatedTarget;
|
|
214
|
+
if (next ? !e.currentTarget.contains(next) : e.target.isConnected) focusWithin.current = false;
|
|
215
|
+
};
|
|
216
|
+
const onKeyDown = (e) => {
|
|
217
|
+
onKeyDownProp?.(e);
|
|
218
|
+
if (e.defaultPrevented) return;
|
|
219
|
+
const target = e.target;
|
|
220
|
+
if (target.getAttribute("role") !== "treeitem") return;
|
|
221
|
+
const id = target.getAttribute("data-id");
|
|
222
|
+
const index = flat.findIndex((f) => f.node.id === id);
|
|
223
|
+
if (index < 0) return;
|
|
224
|
+
const current = flat[index];
|
|
225
|
+
const node = current.node;
|
|
226
|
+
const parent = hasChildren(node);
|
|
227
|
+
const open = parent && expandedSet.has(node.id);
|
|
228
|
+
const rtl = isRtl(e.currentTarget);
|
|
229
|
+
const forward = rtl ? "ArrowLeft" : "ArrowRight";
|
|
230
|
+
const back = rtl ? "ArrowRight" : "ArrowLeft";
|
|
231
|
+
const go = (i) => {
|
|
232
|
+
const f = flat[i];
|
|
233
|
+
if (f) focusNode(f.node.id);
|
|
234
|
+
};
|
|
235
|
+
if (e.key === "ArrowDown") go(Math.min(index + 1, flat.length - 1));
|
|
236
|
+
else if (e.key === "ArrowUp") go(Math.max(index - 1, 0));
|
|
237
|
+
else if (e.key === forward) {
|
|
238
|
+
if (parent && !open) expand(node.id);
|
|
239
|
+
else if (open) go(index + 1);
|
|
240
|
+
} else if (e.key === back) {
|
|
241
|
+
if (open) collapse(node.id);
|
|
242
|
+
else if (current.parentId !== null) focusNode(current.parentId);
|
|
243
|
+
} else if (e.key === "Home") go(0);
|
|
244
|
+
else if (e.key === "End") go(flat.length - 1);
|
|
245
|
+
else if (e.key === "Enter") {
|
|
246
|
+
select(node);
|
|
247
|
+
activate(node);
|
|
248
|
+
if (parent && toggleOnSelect) toggle(node.id);
|
|
249
|
+
} else if (e.key === " ") select(node);
|
|
250
|
+
else if (e.key === "*") {
|
|
251
|
+
const next = new Set(expandedSet);
|
|
252
|
+
for (const f of flat) if (f.parentId === current.parentId && hasChildren(f.node)) next.add(f.node.id);
|
|
253
|
+
if (next.size !== expandedSet.size) setExpanded(next);
|
|
254
|
+
} else if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
|
|
255
|
+
const ch = e.key.toLowerCase();
|
|
256
|
+
for (let step = 1; step < flat.length; step++) {
|
|
257
|
+
const f = flat[(index + step) % flat.length];
|
|
258
|
+
if (f.node.label.toLowerCase().startsWith(ch)) {
|
|
259
|
+
focusNode(f.node.id);
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
} else return;
|
|
264
|
+
e.preventDefault();
|
|
265
|
+
};
|
|
266
|
+
const itemRef = useCallback((el) => {
|
|
267
|
+
const id = el?.getAttribute("data-id");
|
|
268
|
+
if (!el || id == null) return void 0;
|
|
269
|
+
elements.current.set(id, el);
|
|
270
|
+
return () => {
|
|
271
|
+
if (elements.current.get(id) === el) elements.current.delete(id);
|
|
272
|
+
};
|
|
273
|
+
}, []);
|
|
274
|
+
const latest = useLatest({ focusNode, select, activate, toggle, toggleOnSelect });
|
|
275
|
+
const onRowClick = useCallback(
|
|
276
|
+
(node) => {
|
|
277
|
+
const l = latest.current;
|
|
278
|
+
l.focusNode(node.id);
|
|
279
|
+
l.select(node);
|
|
280
|
+
l.activate(node);
|
|
281
|
+
if (hasChildren(node) && l.toggleOnSelect) l.toggle(node.id);
|
|
282
|
+
},
|
|
283
|
+
[latest]
|
|
284
|
+
);
|
|
285
|
+
const onToggle = useCallback(
|
|
286
|
+
(node) => {
|
|
287
|
+
const l = latest.current;
|
|
288
|
+
l.focusNode(node.id);
|
|
289
|
+
l.toggle(node.id);
|
|
290
|
+
},
|
|
291
|
+
[latest]
|
|
292
|
+
);
|
|
293
|
+
const renderNodes = (nodes, level) => nodes.map((node, i) => {
|
|
294
|
+
const open = hasChildren(node) && expandedSet.has(node.id);
|
|
295
|
+
return /* @__PURE__ */ jsx(
|
|
296
|
+
TreeItem,
|
|
297
|
+
{
|
|
298
|
+
node,
|
|
299
|
+
level,
|
|
300
|
+
posinset: i + 1,
|
|
301
|
+
setsize: nodes.length,
|
|
302
|
+
open,
|
|
303
|
+
selectable,
|
|
304
|
+
selected: selectedId === node.id,
|
|
305
|
+
isTarget: node.id === targetId,
|
|
306
|
+
labelId: `${uid}-${encodeURIComponent(node.id)}`,
|
|
307
|
+
itemRef,
|
|
308
|
+
onRowClick,
|
|
309
|
+
onToggle,
|
|
310
|
+
children: open ? renderNodes(node.children, level + 1) : null
|
|
311
|
+
},
|
|
312
|
+
node.id
|
|
313
|
+
);
|
|
314
|
+
});
|
|
315
|
+
return /* @__PURE__ */ jsx("ul", { ...rest, ref, role: "tree", className: cn("grid gap-px text-sm", className), onKeyDown, onFocus, onBlur, children: renderNodes(items, 1) });
|
|
316
|
+
}
|
|
317
|
+
export {
|
|
318
|
+
TreeView
|
|
319
|
+
};
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/components/virtual-table.tsx
|
|
4
|
+
import {
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useImperativeHandle,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState
|
|
11
|
+
} from "react";
|
|
12
|
+
import { cn } from "../cn.js";
|
|
13
|
+
import { useIsoLayoutEffect } from "./client.js";
|
|
14
|
+
import { sortRows } from "./data-classes.js";
|
|
15
|
+
import { useNishStrings } from "./provider.js";
|
|
16
|
+
import { useLatest } from "./latest.js";
|
|
17
|
+
import { Table, TableWrap, Td, Th, Tr } from "./table.js";
|
|
18
|
+
import { useVirtualRows } from "./virtual.js";
|
|
19
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
20
|
+
var HIDE_BELOW = { sm: "hidden sm:table-cell", md: "hidden md:table-cell", lg: "hidden lg:table-cell" };
|
|
21
|
+
var ROW_FOCUS = "focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-th-accent";
|
|
22
|
+
var INTERACTIVE_CHILD = "a,button,input,select,textarea,[role=button]";
|
|
23
|
+
function VirtualTable({
|
|
24
|
+
rows,
|
|
25
|
+
columns,
|
|
26
|
+
rowKey,
|
|
27
|
+
label,
|
|
28
|
+
height = 480,
|
|
29
|
+
rowHeight,
|
|
30
|
+
measure = false,
|
|
31
|
+
dense = false,
|
|
32
|
+
zebra = false,
|
|
33
|
+
minWidth = 640,
|
|
34
|
+
stickyFirstColumn,
|
|
35
|
+
sort,
|
|
36
|
+
onSortChange,
|
|
37
|
+
onRowClick,
|
|
38
|
+
rowTone,
|
|
39
|
+
loading = false,
|
|
40
|
+
empty,
|
|
41
|
+
error,
|
|
42
|
+
overscan = 8,
|
|
43
|
+
onRangeChange,
|
|
44
|
+
apiRef,
|
|
45
|
+
className,
|
|
46
|
+
ref,
|
|
47
|
+
...rest
|
|
48
|
+
}) {
|
|
49
|
+
const t = useNishStrings();
|
|
50
|
+
const size = rowHeight ?? (onRowClick ? 44 : dense ? 33 : 41);
|
|
51
|
+
const [innerSort, setInnerSort] = useState(null);
|
|
52
|
+
const [announcement, setAnnouncement] = useState("");
|
|
53
|
+
const external = sort !== void 0 || onSortChange !== void 0;
|
|
54
|
+
const current = sort !== void 0 ? sort : innerSort;
|
|
55
|
+
const sortKey = current?.key;
|
|
56
|
+
const sortDir = current?.dir;
|
|
57
|
+
const changeSort = (next) => {
|
|
58
|
+
setInnerSort(next);
|
|
59
|
+
onSortChange?.(next);
|
|
60
|
+
const header = columns.find((c) => c.key === next?.key)?.header ?? "";
|
|
61
|
+
setAnnouncement(t.sortAnnounce(header, next ? next.dir : null));
|
|
62
|
+
};
|
|
63
|
+
const toggle = (key) => changeSort(current?.key === key ? { key, dir: current.dir === "asc" ? "desc" : "asc" } : { key, dir: "asc" });
|
|
64
|
+
const sorted = useMemo(() => {
|
|
65
|
+
if (external || !sortKey || !sortDir) return rows;
|
|
66
|
+
const get = columns.find((c) => c.key === sortKey)?.sortValue;
|
|
67
|
+
return get ? sortRows(rows, get, sortDir) : rows;
|
|
68
|
+
}, [rows, sortKey, sortDir, external]);
|
|
69
|
+
const showTable = !error && !loading && sorted.length > 0;
|
|
70
|
+
const count = showTable ? sorted.length : 0;
|
|
71
|
+
const scrollRef = useRef(null);
|
|
72
|
+
const headRef = useRef(null);
|
|
73
|
+
const bodyRef = useRef(null);
|
|
74
|
+
const pendingFocus = useRef(null);
|
|
75
|
+
const hadFocus = useRef(false);
|
|
76
|
+
const [focusIndex, setFocusIndex] = useState(0);
|
|
77
|
+
const keyRef = useLatest(rowKey);
|
|
78
|
+
const getKey = useCallback((i) => keyRef.current(sorted[i]), [sorted, keyRef]);
|
|
79
|
+
const coverTop = useCallback(() => headRef.current?.offsetHeight ?? 0, []);
|
|
80
|
+
const initialCount = Math.min(40, Math.ceil((typeof height === "number" ? height : 480) / size));
|
|
81
|
+
const virtual = useVirtualRows({
|
|
82
|
+
count,
|
|
83
|
+
itemSize: size,
|
|
84
|
+
getKey,
|
|
85
|
+
measure,
|
|
86
|
+
gap: 0,
|
|
87
|
+
overscan,
|
|
88
|
+
initialCount,
|
|
89
|
+
scrollRef,
|
|
90
|
+
listRef: bodyRef,
|
|
91
|
+
coverTop,
|
|
92
|
+
pinMode: "contiguous",
|
|
93
|
+
onRangeChange
|
|
94
|
+
});
|
|
95
|
+
const { indices, total, offsetOf, sizeOf, measureRef, pin, scrollToIndex } = virtual;
|
|
96
|
+
useImperativeHandle(apiRef, () => ({ scrollToIndex }), [scrollToIndex]);
|
|
97
|
+
const first = indices[0] ?? 0;
|
|
98
|
+
const last = indices[indices.length - 1] ?? -1;
|
|
99
|
+
const topSpace = indices.length > 0 ? offsetOf(first) : 0;
|
|
100
|
+
const bottomSpace = Math.max(0, total - (last >= 0 ? offsetOf(last) + sizeOf(last) : 0));
|
|
101
|
+
const interactive = !!onRowClick;
|
|
102
|
+
const tabbable = indices.includes(focusIndex) ? focusIndex : first;
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (count > 0 && focusIndex > count - 1) setFocusIndex(count - 1);
|
|
105
|
+
}, [count, focusIndex]);
|
|
106
|
+
useIsoLayoutEffect(() => {
|
|
107
|
+
const body = bodyRef.current;
|
|
108
|
+
if (pendingFocus.current !== null) {
|
|
109
|
+
const row = body?.querySelector(`tr[data-index="${pendingFocus.current}"]`);
|
|
110
|
+
if (row) {
|
|
111
|
+
pendingFocus.current = null;
|
|
112
|
+
row.focus({ preventScroll: true });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (hadFocus.current && document.activeElement === document.body && pendingFocus.current === null) {
|
|
117
|
+
hadFocus.current = false;
|
|
118
|
+
pin(null);
|
|
119
|
+
scrollRef.current?.focus({ preventScroll: true });
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const onBodyFocus = (e) => {
|
|
123
|
+
hadFocus.current = true;
|
|
124
|
+
const row = e.target.closest("tr[data-index]");
|
|
125
|
+
if (row && bodyRef.current?.contains(row)) pin(Number(row.dataset.index));
|
|
126
|
+
};
|
|
127
|
+
const onBodyBlur = (e) => {
|
|
128
|
+
const next = e.relatedTarget;
|
|
129
|
+
if (next ? !bodyRef.current?.contains(next) : e.target.isConnected) {
|
|
130
|
+
hadFocus.current = false;
|
|
131
|
+
pin(null);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const pageSize = () => {
|
|
135
|
+
const scroller = scrollRef.current;
|
|
136
|
+
const head = headRef.current;
|
|
137
|
+
const room = scroller ? scroller.clientHeight - (head?.offsetHeight ?? 0) : 0;
|
|
138
|
+
return Math.max(1, Math.floor((room || size) / size));
|
|
139
|
+
};
|
|
140
|
+
const goTo = (next) => {
|
|
141
|
+
const n = Math.min(count - 1, Math.max(0, next));
|
|
142
|
+
pendingFocus.current = n;
|
|
143
|
+
setFocusIndex(n);
|
|
144
|
+
pin(n);
|
|
145
|
+
scrollToIndex(n);
|
|
146
|
+
};
|
|
147
|
+
const onRowKeyDown = (e, row, i) => {
|
|
148
|
+
if (e.target !== e.currentTarget) return;
|
|
149
|
+
const key = e.key;
|
|
150
|
+
if (key === "Enter" || key === " ") {
|
|
151
|
+
e.preventDefault();
|
|
152
|
+
onRowClick?.(row, i);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const page = key === "PageDown" || key === "PageUp" ? pageSize() : 1;
|
|
156
|
+
const target = key === "ArrowDown" || key === "PageDown" ? i + page : key === "ArrowUp" || key === "PageUp" ? i - page : key === "Home" ? 0 : key === "End" ? count - 1 : null;
|
|
157
|
+
if (target === null) return;
|
|
158
|
+
e.preventDefault();
|
|
159
|
+
goTo(target);
|
|
160
|
+
};
|
|
161
|
+
const onRowClickEvent = (e, row, i) => {
|
|
162
|
+
const inner = e.target.closest(INTERACTIVE_CHILD);
|
|
163
|
+
if (inner && inner !== e.currentTarget && e.currentTarget.contains(inner)) return;
|
|
164
|
+
setFocusIndex(i);
|
|
165
|
+
onRowClick?.(row, i);
|
|
166
|
+
};
|
|
167
|
+
let content;
|
|
168
|
+
if (error) {
|
|
169
|
+
content = /* @__PURE__ */ jsxs("div", { role: "alert", className: "grid justify-items-center gap-2 rounded-xl bg-th-loss-soft p-8", children: [
|
|
170
|
+
/* @__PURE__ */ jsx("p", { className: "text-sm", children: error.message }),
|
|
171
|
+
error.onRetry ? /* @__PURE__ */ jsx(
|
|
172
|
+
"button",
|
|
173
|
+
{
|
|
174
|
+
type: "button",
|
|
175
|
+
onClick: error.onRetry,
|
|
176
|
+
className: "rounded-full border border-th-line-strong px-3 py-1 pointer-coarse:min-h-11",
|
|
177
|
+
children: t.retry
|
|
178
|
+
}
|
|
179
|
+
) : null
|
|
180
|
+
] });
|
|
181
|
+
} else if (loading) {
|
|
182
|
+
content = /* @__PURE__ */ jsxs("div", { role: "status", children: [
|
|
183
|
+
/* @__PURE__ */ jsx("span", { className: "sr-only", children: t.loadingNamed(label) }),
|
|
184
|
+
/* @__PURE__ */ jsx("div", { className: "nish-skeleton h-56 rounded-xl" })
|
|
185
|
+
] });
|
|
186
|
+
} else if (sorted.length === 0) {
|
|
187
|
+
content = empty ?? /* @__PURE__ */ jsx("p", { className: "rounded-xl border border-dashed border-th-line p-8 text-center text-sm", children: t.nothingHere });
|
|
188
|
+
} else {
|
|
189
|
+
const spacer = (px) => px > 0 ? /* @__PURE__ */ jsx("tr", { "aria-hidden": "true", "data-virtual-spacer": "", children: /* @__PURE__ */ jsx("td", { colSpan: columns.length, style: { height: px, padding: 0, border: 0 } }) }) : null;
|
|
190
|
+
content = /* @__PURE__ */ jsx(TableWrap, { ref: scrollRef, style: { overflowAnchor: "none" }, maxHeight: height, stickyHeader: true, minWidth, stickyFirstColumn, label, children: /* @__PURE__ */ jsxs(Table, { dense, "aria-label": label, "aria-rowcount": sorted.length + 1, className: "table-fixed", children: [
|
|
191
|
+
/* @__PURE__ */ jsx("thead", { ref: headRef, children: /* @__PURE__ */ jsx("tr", { "aria-rowindex": 1, children: columns.map((c) => /* @__PURE__ */ jsx(
|
|
192
|
+
Th,
|
|
193
|
+
{
|
|
194
|
+
numeric: c.numeric,
|
|
195
|
+
sort: c.sortValue ? current?.key === c.key ? current.dir : false : void 0,
|
|
196
|
+
onSort: c.sortValue ? () => toggle(c.key) : void 0,
|
|
197
|
+
className: cn(c.hideBelow && HIDE_BELOW[c.hideBelow]),
|
|
198
|
+
style: c.width ? { width: c.width } : void 0,
|
|
199
|
+
children: c.header
|
|
200
|
+
},
|
|
201
|
+
c.key
|
|
202
|
+
)) }) }),
|
|
203
|
+
/* @__PURE__ */ jsxs("tbody", { ref: bodyRef, onFocus: interactive ? onBodyFocus : void 0, onBlur: interactive ? onBodyBlur : void 0, children: [
|
|
204
|
+
spacer(topSpace),
|
|
205
|
+
indices.map((i) => {
|
|
206
|
+
const row = sorted[i];
|
|
207
|
+
if (row === void 0) return null;
|
|
208
|
+
return /* @__PURE__ */ jsx(
|
|
209
|
+
Tr,
|
|
210
|
+
{
|
|
211
|
+
ref: measure ? measureRef : void 0,
|
|
212
|
+
tone: rowTone?.(row),
|
|
213
|
+
interactive,
|
|
214
|
+
"data-index": i,
|
|
215
|
+
style: measure ? void 0 : { height: size },
|
|
216
|
+
"aria-rowindex": i + 2,
|
|
217
|
+
tabIndex: interactive ? i === tabbable ? 0 : -1 : void 0,
|
|
218
|
+
className: cn(zebra && i % 2 === 1 && "bg-th-elevated/50", interactive && ROW_FOCUS),
|
|
219
|
+
onClick: interactive ? (e) => onRowClickEvent(e, row, i) : void 0,
|
|
220
|
+
onKeyDown: interactive ? (e) => onRowKeyDown(e, row, i) : void 0,
|
|
221
|
+
children: columns.map((c) => /* @__PURE__ */ jsx(
|
|
222
|
+
Td,
|
|
223
|
+
{
|
|
224
|
+
numeric: c.numeric,
|
|
225
|
+
className: cn(c.hideBelow && HIDE_BELOW[c.hideBelow], !measure && "truncate"),
|
|
226
|
+
children: c.cell(row)
|
|
227
|
+
},
|
|
228
|
+
c.key
|
|
229
|
+
))
|
|
230
|
+
},
|
|
231
|
+
rowKey(row)
|
|
232
|
+
);
|
|
233
|
+
}),
|
|
234
|
+
spacer(bottomSpace)
|
|
235
|
+
] })
|
|
236
|
+
] }) });
|
|
237
|
+
}
|
|
238
|
+
return /* @__PURE__ */ jsxs("div", { ...rest, ref, className, children: [
|
|
239
|
+
content,
|
|
240
|
+
/* @__PURE__ */ jsx("div", { role: "status", className: "sr-only", children: announcement })
|
|
241
|
+
] });
|
|
242
|
+
}
|
|
243
|
+
export {
|
|
244
|
+
VirtualTable
|
|
245
|
+
};
|