@hexclave/dashboard-ui-components 1.0.3 → 1.0.6
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/components/alert.js +4 -4
- package/dist/components/alert.js.map +1 -1
- package/dist/components/badge.js +1 -1
- package/dist/components/badge.js.map +1 -1
- package/dist/components/button.d.ts +4 -4
- package/dist/components/button.js +3 -3
- package/dist/components/button.js.map +1 -1
- package/dist/components/data-grid/data-grid-toolbar.js +5 -5
- package/dist/components/data-grid/data-grid-toolbar.js.map +1 -1
- package/dist/components/data-grid/data-grid.d.ts.map +1 -1
- package/dist/components/data-grid/data-grid.js +11 -5
- package/dist/components/data-grid/data-grid.js.map +1 -1
- package/dist/components/data-grid/data-grid.test.js +34 -1
- package/dist/components/data-grid/data-grid.test.js.map +1 -1
- package/dist/components/dialog.d.ts +6 -2
- package/dist/components/dialog.d.ts.map +1 -1
- package/dist/components/dialog.js +14 -10
- package/dist/components/dialog.js.map +1 -1
- package/dist/components/input.js +12 -9
- package/dist/components/input.js.map +1 -1
- package/dist/components/pill-toggle.js +3 -3
- package/dist/components/pill-toggle.js.map +1 -1
- package/dist/components/tabs.js +2 -2
- package/dist/components/tabs.js.map +1 -1
- package/dist/dashboard-ui-components.global.js +15639 -17400
- package/dist/dashboard-ui-components.global.js.map +4 -4
- package/dist/esm/components/alert.js +4 -4
- package/dist/esm/components/alert.js.map +1 -1
- package/dist/esm/components/badge.js +1 -1
- package/dist/esm/components/badge.js.map +1 -1
- package/dist/esm/components/button.d.ts +4 -4
- package/dist/esm/components/button.js +3 -3
- package/dist/esm/components/button.js.map +1 -1
- package/dist/esm/components/data-grid/data-grid-toolbar.js +5 -5
- package/dist/esm/components/data-grid/data-grid-toolbar.js.map +1 -1
- package/dist/esm/components/data-grid/data-grid.d.ts.map +1 -1
- package/dist/esm/components/data-grid/data-grid.js +11 -5
- package/dist/esm/components/data-grid/data-grid.js.map +1 -1
- package/dist/esm/components/data-grid/data-grid.test.js +34 -1
- package/dist/esm/components/data-grid/data-grid.test.js.map +1 -1
- package/dist/esm/components/dialog.d.ts +6 -2
- package/dist/esm/components/dialog.d.ts.map +1 -1
- package/dist/esm/components/dialog.js +13 -11
- package/dist/esm/components/dialog.js.map +1 -1
- package/dist/esm/components/input.js +12 -9
- package/dist/esm/components/input.js.map +1 -1
- package/dist/esm/components/pill-toggle.js +4 -4
- package/dist/esm/components/pill-toggle.js.map +1 -1
- package/dist/esm/components/tabs.js +2 -2
- package/dist/esm/components/tabs.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -0
- package/package.json +4 -3
- package/src/components/alert.tsx +120 -0
- package/src/components/analytics-chart/analytics-chart-pie.tsx +369 -0
- package/src/components/analytics-chart/analytics-chart.tsx +1585 -0
- package/src/components/analytics-chart/default-analytics-chart-tooltip.tsx +265 -0
- package/src/components/analytics-chart/format.ts +101 -0
- package/src/components/analytics-chart/index.ts +75 -0
- package/src/components/analytics-chart/palette.ts +68 -0
- package/src/components/analytics-chart/render-data-series.tsx +169 -0
- package/src/components/analytics-chart/state.ts +165 -0
- package/src/components/analytics-chart/strings.ts +72 -0
- package/src/components/analytics-chart/types.ts +220 -0
- package/src/components/badge.tsx +108 -0
- package/src/components/button.tsx +104 -0
- package/src/components/card.tsx +263 -0
- package/src/components/chart-card.tsx +101 -0
- package/src/components/chart-container.tsx +155 -0
- package/src/components/chart-legend.tsx +65 -0
- package/src/components/chart-theme.tsx +52 -0
- package/src/components/chart-tooltip.tsx +165 -0
- package/src/components/cursor-blast-effect.tsx +334 -0
- package/src/components/data-grid/data-grid-sizing.ts +51 -0
- package/src/components/data-grid/data-grid-toolbar.tsx +373 -0
- package/src/components/data-grid/data-grid.test.tsx +407 -0
- package/src/components/data-grid/data-grid.tsx +1231 -0
- package/src/components/data-grid/index.ts +64 -0
- package/src/components/data-grid/state.ts +235 -0
- package/src/components/data-grid/strings.ts +45 -0
- package/src/components/data-grid/types.ts +401 -0
- package/src/components/data-grid/use-data-source.ts +413 -0
- package/src/components/data-grid/use-url-state.test.tsx +88 -0
- package/src/components/data-grid/use-url-state.ts +298 -0
- package/src/components/dialog.tsx +218 -0
- package/src/components/edit-mode.tsx +17 -0
- package/src/components/empty-state.tsx +64 -0
- package/src/components/input.tsx +97 -0
- package/src/components/metric-card.tsx +142 -0
- package/src/components/pill-toggle.tsx +159 -0
- package/src/components/progress-bar.tsx +82 -0
- package/src/components/separator.tsx +36 -0
- package/src/components/skeleton.tsx +30 -0
- package/src/components/table.tsx +113 -0
- package/src/components/tabs.tsx +217 -0
- package/src/index.ts +71 -0
|
@@ -0,0 +1,1231 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ArrowDown,
|
|
5
|
+
ArrowUp,
|
|
6
|
+
CaretDown,
|
|
7
|
+
CaretUp,
|
|
8
|
+
CheckSquare,
|
|
9
|
+
MinusSquare,
|
|
10
|
+
Square,
|
|
11
|
+
} from "@phosphor-icons/react";
|
|
12
|
+
import { throwErr } from "@hexclave/shared/dist/utils/errors";
|
|
13
|
+
import { cn } from "@hexclave/ui";
|
|
14
|
+
import {
|
|
15
|
+
type ColumnDef,
|
|
16
|
+
type ColumnOrderState,
|
|
17
|
+
type ColumnPinningState,
|
|
18
|
+
type ColumnSizingState,
|
|
19
|
+
type RowSelectionState,
|
|
20
|
+
type SortingState,
|
|
21
|
+
type VisibilityState,
|
|
22
|
+
type Header,
|
|
23
|
+
type Updater,
|
|
24
|
+
getCoreRowModel,
|
|
25
|
+
useReactTable,
|
|
26
|
+
} from "@tanstack/react-table";
|
|
27
|
+
import { useVirtualizer, type VirtualItem } from "@tanstack/react-virtual";
|
|
28
|
+
import React, {
|
|
29
|
+
type CSSProperties,
|
|
30
|
+
useCallback,
|
|
31
|
+
useEffect,
|
|
32
|
+
useLayoutEffect,
|
|
33
|
+
useMemo,
|
|
34
|
+
useRef,
|
|
35
|
+
useState,
|
|
36
|
+
} from "react";
|
|
37
|
+
|
|
38
|
+
import { DesignSkeleton } from "../skeleton";
|
|
39
|
+
import { DEFAULT_COL_WIDTH, clampColumnWidth, getEffectiveMaxWidth, getEffectiveMinWidth } from "./data-grid-sizing";
|
|
40
|
+
import { DataGridToolbar } from "./data-grid-toolbar";
|
|
41
|
+
import { exportToCsv, formatGridDate, resolveColumnValue } from "./state";
|
|
42
|
+
import { resolveDataGridStrings } from "./strings";
|
|
43
|
+
import type {
|
|
44
|
+
DataGridCellContext,
|
|
45
|
+
DataGridColumnDef,
|
|
46
|
+
DataGridDateDisplay,
|
|
47
|
+
DataGridFooterContext,
|
|
48
|
+
DataGridHeaderContext,
|
|
49
|
+
DataGridPaginationMode,
|
|
50
|
+
DataGridProps,
|
|
51
|
+
DataGridSelectionModel,
|
|
52
|
+
DataGridSortItem,
|
|
53
|
+
DataGridSortModel,
|
|
54
|
+
DataGridState,
|
|
55
|
+
DataGridStrings,
|
|
56
|
+
DataGridToolbarContext,
|
|
57
|
+
RowId,
|
|
58
|
+
} from "./types";
|
|
59
|
+
|
|
60
|
+
// ─── Row click target ────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
function getEventTargetElement(target: EventTarget | null): Element | null {
|
|
63
|
+
if (target instanceof Element) return target;
|
|
64
|
+
if (target instanceof Node) return target.parentElement;
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function isDataGridInteractiveRowClickTarget(target: EventTarget | null): boolean {
|
|
69
|
+
const el = getEventTargetElement(target);
|
|
70
|
+
return el?.closest([
|
|
71
|
+
"a",
|
|
72
|
+
"button",
|
|
73
|
+
"input",
|
|
74
|
+
"select",
|
|
75
|
+
"textarea",
|
|
76
|
+
"[role=\"button\"]",
|
|
77
|
+
"[role=\"menuitem\"]",
|
|
78
|
+
"[contenteditable]:not([contenteditable=\"false\"])",
|
|
79
|
+
"[data-no-row-click]",
|
|
80
|
+
].join(",")) != null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function shouldIgnoreRowClick(event: React.MouseEvent): boolean {
|
|
84
|
+
return event.defaultPrevented || isDataGridInteractiveRowClickTarget(event.target);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ─── State translators (DataGridState ⇄ TanStack) ────────────────────
|
|
88
|
+
|
|
89
|
+
function toTanstackSorting(sorting: DataGridSortModel): SortingState {
|
|
90
|
+
return sorting.map((s) => ({ id: s.columnId, desc: s.direction === "desc" }));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function fromTanstackSorting(sorting: SortingState): DataGridSortModel {
|
|
94
|
+
return sorting.map((s) => ({ columnId: s.id, direction: s.desc ? "desc" : "asc" }));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function toTanstackRowSelection(ids: ReadonlySet<RowId>): RowSelectionState {
|
|
98
|
+
const out: RowSelectionState = {};
|
|
99
|
+
for (const id of ids) out[id] = true;
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function resolveUpdater<T>(updater: Updater<T>, current: T): T {
|
|
104
|
+
return typeof updater === "function" ? (updater as (old: T) => T)(current) : updater;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ─── Flex column width distribution ──────────────────────────────────
|
|
108
|
+
|
|
109
|
+
function distributeFlexWidths<TRow>(
|
|
110
|
+
sizes: Record<string, number>,
|
|
111
|
+
visibleColumns: readonly DataGridColumnDef<TRow>[],
|
|
112
|
+
available: number,
|
|
113
|
+
): void {
|
|
114
|
+
const flexCols = visibleColumns.filter((c) => c.flex != null && c.flex > 0);
|
|
115
|
+
if (flexCols.length === 0 || available <= 0) return;
|
|
116
|
+
const totalFlex = flexCols.reduce((acc, c) => acc + (c.flex ?? 0), 0);
|
|
117
|
+
let remaining = available;
|
|
118
|
+
flexCols.forEach((col, i) => {
|
|
119
|
+
const isLast = i === flexCols.length - 1;
|
|
120
|
+
const share = isLast
|
|
121
|
+
? remaining
|
|
122
|
+
: Math.floor(available * ((col.flex ?? 0) / totalFlex));
|
|
123
|
+
const max = col.maxWidth ?? Infinity;
|
|
124
|
+
const add = Math.max(0, Math.min(share, max - sizes[col.id]));
|
|
125
|
+
sizes[col.id] += add;
|
|
126
|
+
remaining -= add;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ─── Selection logic (with shift-range anchor) ───────────────────────
|
|
131
|
+
|
|
132
|
+
type SelectionInput = {
|
|
133
|
+
current: DataGridSelectionModel;
|
|
134
|
+
rowId: RowId;
|
|
135
|
+
mode: "single" | "multiple";
|
|
136
|
+
modifiers: { shift: boolean; ctrl: boolean };
|
|
137
|
+
allRowIds: readonly RowId[];
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
function selectSingle(current: DataGridSelectionModel, rowId: RowId): DataGridSelectionModel {
|
|
141
|
+
const isSelected = current.selectedIds.has(rowId);
|
|
142
|
+
return {
|
|
143
|
+
selectedIds: isSelected ? new Set() : new Set([rowId]),
|
|
144
|
+
anchorId: isSelected ? null : rowId,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function selectRange(
|
|
149
|
+
current: DataGridSelectionModel,
|
|
150
|
+
rowId: RowId,
|
|
151
|
+
allRowIds: readonly RowId[],
|
|
152
|
+
additive: boolean,
|
|
153
|
+
): DataGridSelectionModel | null {
|
|
154
|
+
if (current.anchorId == null) return null;
|
|
155
|
+
const anchorIdx = allRowIds.indexOf(current.anchorId);
|
|
156
|
+
const currentIdx = allRowIds.indexOf(rowId);
|
|
157
|
+
if (anchorIdx < 0 || currentIdx < 0) return null;
|
|
158
|
+
const start = Math.min(anchorIdx, currentIdx);
|
|
159
|
+
const end = Math.max(anchorIdx, currentIdx);
|
|
160
|
+
const next = additive ? new Set(current.selectedIds) : new Set<RowId>();
|
|
161
|
+
for (let i = start; i <= end; i++) next.add(allRowIds[i]!);
|
|
162
|
+
return { selectedIds: next, anchorId: current.anchorId };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function selectToggle(current: DataGridSelectionModel, rowId: RowId): DataGridSelectionModel {
|
|
166
|
+
const next = new Set(current.selectedIds);
|
|
167
|
+
if (next.has(rowId)) next.delete(rowId);
|
|
168
|
+
else next.add(rowId);
|
|
169
|
+
return { selectedIds: next, anchorId: rowId };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function nextSelection(input: SelectionInput): DataGridSelectionModel {
|
|
173
|
+
const { current, rowId, mode, modifiers, allRowIds } = input;
|
|
174
|
+
if (mode === "single") return selectSingle(current, rowId);
|
|
175
|
+
if (modifiers.shift) {
|
|
176
|
+
const range = selectRange(current, rowId, allRowIds, modifiers.ctrl);
|
|
177
|
+
if (range != null) return range;
|
|
178
|
+
}
|
|
179
|
+
if (modifiers.ctrl) return selectToggle(current, rowId);
|
|
180
|
+
return { selectedIds: new Set([rowId]), anchorId: rowId };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ─── Header cell ─────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
function HeaderCell<TRow>({
|
|
186
|
+
header,
|
|
187
|
+
col,
|
|
188
|
+
resizable,
|
|
189
|
+
}: {
|
|
190
|
+
header: Header<TRow, unknown>;
|
|
191
|
+
col: DataGridColumnDef<TRow>;
|
|
192
|
+
resizable: boolean;
|
|
193
|
+
}) {
|
|
194
|
+
const sorted = header.column.getIsSorted(); // false | "asc" | "desc"
|
|
195
|
+
const sortIndex = header.column.getSortIndex();
|
|
196
|
+
const totalSorts = header.column.getCanMultiSort()
|
|
197
|
+
? header.getContext().table.getState().sorting.length
|
|
198
|
+
: 0;
|
|
199
|
+
const ctx: DataGridHeaderContext<TRow> = {
|
|
200
|
+
columnId: col.id,
|
|
201
|
+
columnDef: col,
|
|
202
|
+
isSorted: sorted === false ? false : sorted,
|
|
203
|
+
sortIndex: totalSorts > 1 && sortIndex >= 0 ? sortIndex + 1 : null,
|
|
204
|
+
};
|
|
205
|
+
const label = typeof col.header === "function" ? col.header(ctx) : col.header;
|
|
206
|
+
const sortable = header.column.getCanSort();
|
|
207
|
+
const canResize = resizable && header.column.getCanResize();
|
|
208
|
+
const isResizing = header.column.getIsResizing();
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<div
|
|
212
|
+
className={cn(
|
|
213
|
+
"group/header relative flex items-center gap-1.5 px-3 select-none bg-transparent overflow-hidden",
|
|
214
|
+
"border-r border-black/[0.04] dark:border-white/[0.04] last:border-r-0",
|
|
215
|
+
sortable && "cursor-pointer",
|
|
216
|
+
)}
|
|
217
|
+
style={{ width: `calc(var(--col-${col.id}-size) * 1px)` }}
|
|
218
|
+
data-col-id={col.id}
|
|
219
|
+
onClick={sortable ? header.column.getToggleSortingHandler() : undefined}
|
|
220
|
+
role="columnheader"
|
|
221
|
+
aria-sort={sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : "none"}
|
|
222
|
+
>
|
|
223
|
+
<span
|
|
224
|
+
className={cn(
|
|
225
|
+
"flex-1 min-w-0 truncate text-xs font-semibold uppercase tracking-wider text-muted-foreground",
|
|
226
|
+
col.align === "center" && "text-center",
|
|
227
|
+
col.align === "right" && "text-right",
|
|
228
|
+
)}
|
|
229
|
+
>
|
|
230
|
+
{label}
|
|
231
|
+
</span>
|
|
232
|
+
|
|
233
|
+
{sorted && (
|
|
234
|
+
<span className="flex items-center gap-0.5 text-foreground/60">
|
|
235
|
+
{sorted === "asc"
|
|
236
|
+
? <ArrowUp className="h-3 w-3" weight="bold" />
|
|
237
|
+
: <ArrowDown className="h-3 w-3" weight="bold" />}
|
|
238
|
+
{ctx.sortIndex != null && (
|
|
239
|
+
<span className="text-[10px] font-medium tabular-nums">{ctx.sortIndex}</span>
|
|
240
|
+
)}
|
|
241
|
+
</span>
|
|
242
|
+
)}
|
|
243
|
+
|
|
244
|
+
{!sorted && sortable && (
|
|
245
|
+
<span className="hidden group-hover/header:flex items-center text-foreground/20">
|
|
246
|
+
<CaretUp className="h-2.5 w-2.5 -mb-[1px]" weight="bold" />
|
|
247
|
+
<CaretDown className="h-2.5 w-2.5 -mt-[1px]" weight="bold" />
|
|
248
|
+
</span>
|
|
249
|
+
)}
|
|
250
|
+
|
|
251
|
+
{canResize && (
|
|
252
|
+
<div
|
|
253
|
+
onMouseDown={header.getResizeHandler()}
|
|
254
|
+
onTouchStart={header.getResizeHandler()}
|
|
255
|
+
onClick={(e) => {
|
|
256
|
+
e.preventDefault();
|
|
257
|
+
e.stopPropagation();
|
|
258
|
+
}}
|
|
259
|
+
className={cn(
|
|
260
|
+
"absolute right-0 top-0 bottom-0 z-10 w-[5px] cursor-col-resize touch-none",
|
|
261
|
+
"group-hover/header:bg-foreground/[0.06] hover:!bg-blue-500/30",
|
|
262
|
+
"transition-colors duration-100",
|
|
263
|
+
isResizing && "bg-blue-500/40",
|
|
264
|
+
)}
|
|
265
|
+
/>
|
|
266
|
+
)}
|
|
267
|
+
</div>
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ─── Data cell ───────────────────────────────────────────────────────
|
|
272
|
+
|
|
273
|
+
function DataCell<TRow>({
|
|
274
|
+
col,
|
|
275
|
+
row,
|
|
276
|
+
rowId,
|
|
277
|
+
rowIndex,
|
|
278
|
+
isSelected,
|
|
279
|
+
dateDisplay,
|
|
280
|
+
}: {
|
|
281
|
+
col: DataGridColumnDef<TRow>;
|
|
282
|
+
row: TRow;
|
|
283
|
+
rowId: RowId;
|
|
284
|
+
rowIndex: number;
|
|
285
|
+
isSelected: boolean;
|
|
286
|
+
dateDisplay: DataGridDateDisplay;
|
|
287
|
+
}) {
|
|
288
|
+
const value = resolveColumnValue(col, row);
|
|
289
|
+
const ctx: DataGridCellContext<TRow> = { row, rowId, rowIndex, value, columnId: col.id, isSelected, dateDisplay };
|
|
290
|
+
|
|
291
|
+
const isDateCol = col.type === "date" || col.type === "dateTime";
|
|
292
|
+
let content: React.ReactNode;
|
|
293
|
+
if (col.renderCell) content = col.renderCell(ctx);
|
|
294
|
+
else if (isDateCol) content = renderDateCell(value, dateDisplay, col);
|
|
295
|
+
else content = formatCellValue(value);
|
|
296
|
+
|
|
297
|
+
const hasCellClick = col.onCellClick || col.onCellDoubleClick;
|
|
298
|
+
const isWrap = col.cellOverflow === "wrap";
|
|
299
|
+
|
|
300
|
+
return (
|
|
301
|
+
<div
|
|
302
|
+
className={cn(
|
|
303
|
+
"flex px-3 bg-transparent overflow-hidden",
|
|
304
|
+
"border-r border-black/[0.04] dark:border-white/[0.04] last:border-r-0",
|
|
305
|
+
"text-sm text-foreground",
|
|
306
|
+
isWrap ? "items-start py-2" : "items-center",
|
|
307
|
+
col.align === "center" && "justify-center",
|
|
308
|
+
col.align === "right" && "justify-end",
|
|
309
|
+
hasCellClick && "cursor-pointer",
|
|
310
|
+
)}
|
|
311
|
+
style={{ width: `calc(var(--col-${col.id}-size) * 1px)` }}
|
|
312
|
+
data-col-id={col.id}
|
|
313
|
+
role="gridcell"
|
|
314
|
+
onClick={col.onCellClick ? (e) => {
|
|
315
|
+
e.stopPropagation();
|
|
316
|
+
col.onCellClick!(ctx, e);
|
|
317
|
+
} : undefined}
|
|
318
|
+
onDoubleClick={col.onCellDoubleClick ? (e) => {
|
|
319
|
+
e.stopPropagation();
|
|
320
|
+
col.onCellDoubleClick!(ctx, e);
|
|
321
|
+
} : undefined}
|
|
322
|
+
>
|
|
323
|
+
<div className={cn("min-w-0", isWrap ? "flex-1" : "truncate")}>{content}</div>
|
|
324
|
+
</div>
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function formatCellValue(value: unknown): React.ReactNode {
|
|
329
|
+
if (value == null) return <span className="text-muted-foreground/40">-</span>;
|
|
330
|
+
if (typeof value === "boolean") {
|
|
331
|
+
return (
|
|
332
|
+
<span className={cn(
|
|
333
|
+
"inline-flex items-center px-1.5 py-0.5 rounded-md text-xs font-medium",
|
|
334
|
+
value
|
|
335
|
+
? "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400"
|
|
336
|
+
: "bg-foreground/[0.04] text-muted-foreground",
|
|
337
|
+
)}>
|
|
338
|
+
{value ? "Yes" : "No"}
|
|
339
|
+
</span>
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
if (value instanceof Date) {
|
|
343
|
+
return <span className="tabular-nums text-muted-foreground">{value.toLocaleDateString()}</span>;
|
|
344
|
+
}
|
|
345
|
+
return <span className="truncate">{String(value)}</span>;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function renderDateCell<TRow>(
|
|
349
|
+
value: unknown,
|
|
350
|
+
dateDisplay: DataGridDateDisplay,
|
|
351
|
+
col: DataGridColumnDef<TRow>,
|
|
352
|
+
): React.ReactNode {
|
|
353
|
+
const { display, tooltip } = formatGridDate(value, dateDisplay, {
|
|
354
|
+
parseValue: col.parseValue,
|
|
355
|
+
dateFormat: col.dateFormat,
|
|
356
|
+
});
|
|
357
|
+
if (display == null) return <span className="text-muted-foreground/40">-</span>;
|
|
358
|
+
return (
|
|
359
|
+
<span className="tabular-nums text-muted-foreground truncate cursor-help" title={tooltip ?? undefined}>
|
|
360
|
+
{display}
|
|
361
|
+
</span>
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// ─── Skeleton row ────────────────────────────────────────────────────
|
|
366
|
+
|
|
367
|
+
function hashStringToInt(value: string): number {
|
|
368
|
+
let hash = 0;
|
|
369
|
+
for (let i = 0; i < value.length; i++) hash = ((hash << 5) - hash + value.charCodeAt(i)) | 0;
|
|
370
|
+
return Math.abs(hash);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function SkeletonRow({
|
|
374
|
+
columns,
|
|
375
|
+
height,
|
|
376
|
+
showCheckbox,
|
|
377
|
+
}: {
|
|
378
|
+
columns: readonly DataGridColumnDef<any>[];
|
|
379
|
+
height: number;
|
|
380
|
+
showCheckbox?: boolean;
|
|
381
|
+
}) {
|
|
382
|
+
return (
|
|
383
|
+
<div className="flex" style={{ height }} role="row">
|
|
384
|
+
{showCheckbox && (
|
|
385
|
+
<div className="flex items-center justify-center border-r border-black/[0.04] dark:border-white/[0.04]" style={{ width: 44 }}>
|
|
386
|
+
<DesignSkeleton className="h-4 w-4 rounded" />
|
|
387
|
+
</div>
|
|
388
|
+
)}
|
|
389
|
+
{columns.map((col) => (
|
|
390
|
+
<div
|
|
391
|
+
key={col.id}
|
|
392
|
+
className="flex items-center px-3 border-r border-black/[0.04] dark:border-white/[0.04] last:border-r-0"
|
|
393
|
+
style={{ width: `calc(var(--col-${col.id}-size) * 1px)` }}
|
|
394
|
+
>
|
|
395
|
+
<DesignSkeleton className="h-3.5 rounded-md" style={{ width: `${40 + (hashStringToInt(col.id) % 40)}%` }} />
|
|
396
|
+
</div>
|
|
397
|
+
))}
|
|
398
|
+
</div>
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// ─── Selection checkbox ──────────────────────────────────────────────
|
|
403
|
+
|
|
404
|
+
function SelectionCheckbox({
|
|
405
|
+
checked,
|
|
406
|
+
indeterminate,
|
|
407
|
+
onChange,
|
|
408
|
+
ariaLabel,
|
|
409
|
+
title,
|
|
410
|
+
}: {
|
|
411
|
+
checked: boolean;
|
|
412
|
+
indeterminate?: boolean;
|
|
413
|
+
onChange: (event: React.MouseEvent<HTMLButtonElement>) => void;
|
|
414
|
+
ariaLabel: string;
|
|
415
|
+
title?: string;
|
|
416
|
+
}) {
|
|
417
|
+
const Icon = indeterminate ? MinusSquare : checked ? CheckSquare : Square;
|
|
418
|
+
return (
|
|
419
|
+
<button
|
|
420
|
+
className={cn(
|
|
421
|
+
"flex items-center justify-center w-full h-full",
|
|
422
|
+
"hover:bg-foreground/[0.04] transition-colors duration-75",
|
|
423
|
+
checked || indeterminate
|
|
424
|
+
? "text-blue-600 dark:text-blue-400"
|
|
425
|
+
: "text-muted-foreground/40 hover:text-muted-foreground/60",
|
|
426
|
+
)}
|
|
427
|
+
onClick={(e) => {
|
|
428
|
+
e.stopPropagation();
|
|
429
|
+
onChange(e);
|
|
430
|
+
}}
|
|
431
|
+
aria-label={ariaLabel}
|
|
432
|
+
title={title ?? ariaLabel}
|
|
433
|
+
role="checkbox"
|
|
434
|
+
aria-checked={indeterminate ? "mixed" : checked}
|
|
435
|
+
>
|
|
436
|
+
<Icon className="h-4 w-4" weight={checked || indeterminate ? "fill" : "regular"} />
|
|
437
|
+
</button>
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// ─── Infinite scroll sentinel ────────────────────────────────────────
|
|
442
|
+
|
|
443
|
+
const NOOP = () => {};
|
|
444
|
+
|
|
445
|
+
function InfiniteScrollSentinel({
|
|
446
|
+
onIntersect,
|
|
447
|
+
isLoading,
|
|
448
|
+
rootRef,
|
|
449
|
+
strings,
|
|
450
|
+
}: {
|
|
451
|
+
onIntersect: () => void;
|
|
452
|
+
isLoading: boolean;
|
|
453
|
+
rootRef?: React.RefObject<Element | null>;
|
|
454
|
+
strings: DataGridStrings;
|
|
455
|
+
}) {
|
|
456
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
const el = ref.current;
|
|
459
|
+
if (!el) return;
|
|
460
|
+
const observer = new IntersectionObserver(
|
|
461
|
+
(entries) => { if (entries[0]?.isIntersecting) onIntersect(); },
|
|
462
|
+
{ root: rootRef?.current ?? null, rootMargin: "200px" },
|
|
463
|
+
);
|
|
464
|
+
observer.observe(el);
|
|
465
|
+
return () => observer.disconnect();
|
|
466
|
+
}, [onIntersect, rootRef]);
|
|
467
|
+
return (
|
|
468
|
+
<div ref={ref} className="flex items-center justify-center py-4">
|
|
469
|
+
{isLoading && (
|
|
470
|
+
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
471
|
+
<div className="h-3 w-3 rounded-full border-2 border-current border-t-transparent animate-spin" />
|
|
472
|
+
{strings.loadingMore}
|
|
473
|
+
</div>
|
|
474
|
+
)}
|
|
475
|
+
</div>
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// ─── Footer ──────────────────────────────────────────────────────────
|
|
480
|
+
|
|
481
|
+
function DefaultFooter<TRow>({
|
|
482
|
+
ctx,
|
|
483
|
+
pagination,
|
|
484
|
+
onChange,
|
|
485
|
+
}: {
|
|
486
|
+
ctx: DataGridFooterContext<TRow>;
|
|
487
|
+
pagination: DataGridPaginationMode;
|
|
488
|
+
onChange: React.Dispatch<React.SetStateAction<DataGridState>>;
|
|
489
|
+
}) {
|
|
490
|
+
const { state, totalRowCount, visibleRowCount, selectedRowCount, strings } = ctx;
|
|
491
|
+
const totalPages = totalRowCount != null
|
|
492
|
+
? Math.max(1, Math.ceil(totalRowCount / state.pagination.pageSize))
|
|
493
|
+
: undefined;
|
|
494
|
+
|
|
495
|
+
const setPage = (pageIndex: number) =>
|
|
496
|
+
onChange((s) => ({ ...s, pagination: { ...s.pagination, pageIndex } }));
|
|
497
|
+
const setPageSize = (pageSize: number) =>
|
|
498
|
+
onChange((s) => ({ ...s, pagination: { ...s.pagination, pageSize, pageIndex: 0 } }));
|
|
499
|
+
|
|
500
|
+
return (
|
|
501
|
+
<div className="flex items-center justify-between px-4 py-2.5 border-t border-foreground/[0.06] text-xs text-muted-foreground">
|
|
502
|
+
<div className="flex items-center gap-3">
|
|
503
|
+
{selectedRowCount > 0 && (
|
|
504
|
+
<span className="font-medium text-foreground">{strings.rowsSelected(selectedRowCount)}</span>
|
|
505
|
+
)}
|
|
506
|
+
{totalRowCount != null && <span>{visibleRowCount} of {totalRowCount} rows</span>}
|
|
507
|
+
</div>
|
|
508
|
+
|
|
509
|
+
{pagination !== "infinite" && totalPages != null && (
|
|
510
|
+
<div className="flex items-center gap-3">
|
|
511
|
+
<div className="flex items-center gap-1.5">
|
|
512
|
+
<span>{strings.rowsPerPage}</span>
|
|
513
|
+
<select
|
|
514
|
+
className={cn(
|
|
515
|
+
"h-7 rounded-lg border border-black/[0.08] dark:border-white/[0.06] px-1.5",
|
|
516
|
+
"bg-white dark:bg-background shadow-sm ring-1 ring-black/[0.08] dark:ring-white/[0.06]",
|
|
517
|
+
"text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-foreground/[0.1]",
|
|
518
|
+
"cursor-pointer transition-all duration-150 hover:transition-none hover:ring-black/[0.12] dark:hover:ring-white/[0.1]",
|
|
519
|
+
)}
|
|
520
|
+
value={state.pagination.pageSize}
|
|
521
|
+
onChange={(e) => setPageSize(Number(e.target.value))}
|
|
522
|
+
>
|
|
523
|
+
{[10, 25, 50, 100].map((size) => <option key={size} value={size}>{size}</option>)}
|
|
524
|
+
</select>
|
|
525
|
+
</div>
|
|
526
|
+
<div className="flex items-center gap-1">
|
|
527
|
+
<button
|
|
528
|
+
className={cn(
|
|
529
|
+
"h-7 w-7 flex items-center justify-center rounded-lg",
|
|
530
|
+
"hover:bg-foreground/[0.04] disabled:opacity-30 disabled:cursor-not-allowed",
|
|
531
|
+
"transition-colors duration-75",
|
|
532
|
+
)}
|
|
533
|
+
onClick={() => setPage(state.pagination.pageIndex - 1)}
|
|
534
|
+
disabled={state.pagination.pageIndex === 0}
|
|
535
|
+
aria-label="Previous page"
|
|
536
|
+
>
|
|
537
|
+
<CaretUp className="h-3.5 w-3.5 -rotate-90" weight="bold" />
|
|
538
|
+
</button>
|
|
539
|
+
<span className="px-2 tabular-nums font-medium">
|
|
540
|
+
{strings.pageOf(state.pagination.pageIndex + 1, totalPages)}
|
|
541
|
+
</span>
|
|
542
|
+
<button
|
|
543
|
+
className={cn(
|
|
544
|
+
"h-7 w-7 flex items-center justify-center rounded-lg",
|
|
545
|
+
"hover:bg-foreground/[0.04] disabled:opacity-30 disabled:cursor-not-allowed",
|
|
546
|
+
"transition-colors duration-75",
|
|
547
|
+
)}
|
|
548
|
+
onClick={() => setPage(state.pagination.pageIndex + 1)}
|
|
549
|
+
disabled={state.pagination.pageIndex >= totalPages - 1}
|
|
550
|
+
aria-label="Next page"
|
|
551
|
+
>
|
|
552
|
+
<CaretDown className="h-3.5 w-3.5 -rotate-90" weight="bold" />
|
|
553
|
+
</button>
|
|
554
|
+
</div>
|
|
555
|
+
</div>
|
|
556
|
+
)}
|
|
557
|
+
</div>
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
// ─── Main DataGrid ───────────────────────────────────────────────────
|
|
562
|
+
|
|
563
|
+
/**
|
|
564
|
+
* Interactive table built on TanStack Table v8. Sorting, column sizing,
|
|
565
|
+
* visibility, ordering, and pinning are owned by the table instance; we
|
|
566
|
+
* layer virtualization, sticky toolbar/header/footer, infinite scroll,
|
|
567
|
+
* quick search, CSV export, and date-format toggling on top.
|
|
568
|
+
*
|
|
569
|
+
* The grid is display-only — it does not fetch or page data itself. Pair
|
|
570
|
+
* with `useDataSource` for client- or server-side data and pass the
|
|
571
|
+
* already-processed slice through `rows`.
|
|
572
|
+
*
|
|
573
|
+
* ```tsx
|
|
574
|
+
* const columns = useMemo(() => [...], []);
|
|
575
|
+
* const [gridState, setGridState] = useState(() => createDefaultDataGridState(columns));
|
|
576
|
+
* const gridData = useDataSource({
|
|
577
|
+
* data: users, columns, getRowId: (r) => r.id,
|
|
578
|
+
* sorting: gridState.sorting,
|
|
579
|
+
* quickSearch: gridState.quickSearch,
|
|
580
|
+
* pagination: gridState.pagination,
|
|
581
|
+
* paginationMode: "client",
|
|
582
|
+
* });
|
|
583
|
+
*
|
|
584
|
+
* <DataGrid
|
|
585
|
+
* columns={columns}
|
|
586
|
+
* rows={gridData.rows}
|
|
587
|
+
* getRowId={(r) => r.id}
|
|
588
|
+
* totalRowCount={gridData.totalRowCount}
|
|
589
|
+
* isLoading={gridData.isLoading}
|
|
590
|
+
* state={gridState}
|
|
591
|
+
* onChange={setGridState}
|
|
592
|
+
* />
|
|
593
|
+
* ```
|
|
594
|
+
*
|
|
595
|
+
* Iron rules:
|
|
596
|
+
* - `rows` is always `gridData.rows`, never your raw array.
|
|
597
|
+
* - Columns must be stable (define outside the component or wrap in `useMemo`).
|
|
598
|
+
* - Initialize state with `createDefaultDataGridState(columns)`.
|
|
599
|
+
* - `renderCell` must be a pure function — no React hooks inside.
|
|
600
|
+
*/
|
|
601
|
+
export function DataGrid<TRow>(props: DataGridProps<TRow>) {
|
|
602
|
+
const {
|
|
603
|
+
columns: allColumns,
|
|
604
|
+
rows,
|
|
605
|
+
getRowId,
|
|
606
|
+
totalRowCount,
|
|
607
|
+
isLoading = false,
|
|
608
|
+
isRefetching = false,
|
|
609
|
+
hasMore = false,
|
|
610
|
+
isLoadingMore = false,
|
|
611
|
+
onLoadMore,
|
|
612
|
+
state,
|
|
613
|
+
onChange,
|
|
614
|
+
paginationMode = "paginated",
|
|
615
|
+
selectionMode = "none",
|
|
616
|
+
resizable = true,
|
|
617
|
+
rowHeight: rowHeightProp = 44,
|
|
618
|
+
estimatedRowHeight: estimatedRowHeightProp,
|
|
619
|
+
headerHeight = 44,
|
|
620
|
+
overscan = 5,
|
|
621
|
+
maxHeight,
|
|
622
|
+
fillHeight = true,
|
|
623
|
+
stickyTop,
|
|
624
|
+
toolbar,
|
|
625
|
+
toolbarExtra,
|
|
626
|
+
emptyState,
|
|
627
|
+
loadingState,
|
|
628
|
+
footer,
|
|
629
|
+
footerExtra,
|
|
630
|
+
exportFilename = "export",
|
|
631
|
+
strings: stringsOverride,
|
|
632
|
+
className,
|
|
633
|
+
onRowClick,
|
|
634
|
+
onRowDoubleClick,
|
|
635
|
+
onSelectionChange,
|
|
636
|
+
onSortChange,
|
|
637
|
+
onColumnResize,
|
|
638
|
+
onColumnVisibilityChange,
|
|
639
|
+
} = props;
|
|
640
|
+
|
|
641
|
+
const isDynamicRowHeight = rowHeightProp === "auto";
|
|
642
|
+
const fixedRowHeight = isDynamicRowHeight ? undefined : rowHeightProp;
|
|
643
|
+
const estimatedRowHeight = estimatedRowHeightProp ?? (fixedRowHeight ?? 44);
|
|
644
|
+
|
|
645
|
+
const strings = useMemo(() => resolveDataGridStrings(stringsOverride), [stringsOverride]);
|
|
646
|
+
|
|
647
|
+
// ── Build TanStack column defs from our column defs ──────────
|
|
648
|
+
const tableColumns = useMemo<ColumnDef<TRow>[]>(
|
|
649
|
+
() =>
|
|
650
|
+
allColumns.map((col) => ({
|
|
651
|
+
id: col.id,
|
|
652
|
+
accessorFn: (row) => resolveColumnValue(col, row),
|
|
653
|
+
header: typeof col.header === "string" ? col.header : col.id,
|
|
654
|
+
size: col.width ?? DEFAULT_COL_WIDTH,
|
|
655
|
+
minSize: getEffectiveMinWidth(col),
|
|
656
|
+
maxSize: getEffectiveMaxWidth(col),
|
|
657
|
+
enableSorting: col.sortable !== false,
|
|
658
|
+
enableHiding: col.hideable !== false,
|
|
659
|
+
enableResizing: col.resizable !== false,
|
|
660
|
+
enableMultiSort: true,
|
|
661
|
+
})),
|
|
662
|
+
[allColumns],
|
|
663
|
+
);
|
|
664
|
+
|
|
665
|
+
// ── Translate our state ⇄ TanStack state via change handlers ───
|
|
666
|
+
const tanstackSorting = useMemo(() => toTanstackSorting(state.sorting), [state.sorting]);
|
|
667
|
+
const tanstackRowSelection = useMemo(
|
|
668
|
+
() => toTanstackRowSelection(state.selection.selectedIds),
|
|
669
|
+
[state.selection.selectedIds],
|
|
670
|
+
);
|
|
671
|
+
// ColumnSizing/Visibility/Order/Pinning share the same shape with TanStack.
|
|
672
|
+
const tanstackColumnPinning = useMemo<ColumnPinningState>(
|
|
673
|
+
() => ({ left: [...state.columnPinning.left], right: [...state.columnPinning.right] }),
|
|
674
|
+
[state.columnPinning],
|
|
675
|
+
);
|
|
676
|
+
const tanstackColumnOrder = useMemo<ColumnOrderState>(
|
|
677
|
+
() => [...state.columnOrder],
|
|
678
|
+
[state.columnOrder],
|
|
679
|
+
);
|
|
680
|
+
|
|
681
|
+
const allColumnsRef = useRef(allColumns);
|
|
682
|
+
allColumnsRef.current = allColumns;
|
|
683
|
+
|
|
684
|
+
const handleSortingChange = useCallback(
|
|
685
|
+
(updater: Updater<SortingState>) => {
|
|
686
|
+
const next = resolveUpdater(updater, toTanstackSorting(state.sorting));
|
|
687
|
+
const ours: DataGridSortItem[] = fromTanstackSorting(next).map((s) => ({ ...s }));
|
|
688
|
+
// Reset to page 0 — page N of the new sort order is meaningless when
|
|
689
|
+
// the order itself changed, and would silently scroll past relevant
|
|
690
|
+
// rows.
|
|
691
|
+
onChange((s) => ({
|
|
692
|
+
...s,
|
|
693
|
+
sorting: ours,
|
|
694
|
+
pagination: { ...s.pagination, pageIndex: 0 },
|
|
695
|
+
}));
|
|
696
|
+
onSortChange?.(ours);
|
|
697
|
+
},
|
|
698
|
+
[onChange, onSortChange, state.sorting],
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const handleColumnSizingChange = useCallback(
|
|
702
|
+
(updater: Updater<ColumnSizingState>) => {
|
|
703
|
+
const next = resolveUpdater(updater, state.columnWidths);
|
|
704
|
+
// Clamp each new width to our canvas-measured min and explicit max.
|
|
705
|
+
const clamped: Record<string, number> = {};
|
|
706
|
+
for (const [id, w] of Object.entries(next)) {
|
|
707
|
+
const col = allColumnsRef.current.find((c) => c.id === id);
|
|
708
|
+
clamped[id] = col ? clampColumnWidth(col, w) : w;
|
|
709
|
+
}
|
|
710
|
+
onChange((s) => ({ ...s, columnWidths: clamped }));
|
|
711
|
+
// Fire onColumnResize for any column whose width changed.
|
|
712
|
+
if (onColumnResize) {
|
|
713
|
+
for (const [id, w] of Object.entries(clamped)) {
|
|
714
|
+
if (state.columnWidths[id] !== w) onColumnResize(id, w);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
},
|
|
718
|
+
[onChange, onColumnResize, state.columnWidths],
|
|
719
|
+
);
|
|
720
|
+
|
|
721
|
+
const handleVisibilityChange = useCallback(
|
|
722
|
+
(updater: Updater<VisibilityState>) => {
|
|
723
|
+
const next = resolveUpdater(updater, state.columnVisibility);
|
|
724
|
+
onChange((s) => ({ ...s, columnVisibility: next }));
|
|
725
|
+
onColumnVisibilityChange?.(next);
|
|
726
|
+
},
|
|
727
|
+
[onChange, onColumnVisibilityChange, state.columnVisibility],
|
|
728
|
+
);
|
|
729
|
+
|
|
730
|
+
const handleColumnOrderChange = useCallback(
|
|
731
|
+
(updater: Updater<ColumnOrderState>) => {
|
|
732
|
+
const next = resolveUpdater(updater, [...state.columnOrder]);
|
|
733
|
+
onChange((s) => ({ ...s, columnOrder: next }));
|
|
734
|
+
},
|
|
735
|
+
[onChange, state.columnOrder],
|
|
736
|
+
);
|
|
737
|
+
|
|
738
|
+
const handleColumnPinningChange = useCallback(
|
|
739
|
+
(updater: Updater<ColumnPinningState>) => {
|
|
740
|
+
const current: ColumnPinningState = {
|
|
741
|
+
left: [...state.columnPinning.left],
|
|
742
|
+
right: [...state.columnPinning.right],
|
|
743
|
+
};
|
|
744
|
+
const next = resolveUpdater(updater, current);
|
|
745
|
+
onChange((s) => ({
|
|
746
|
+
...s,
|
|
747
|
+
columnPinning: { left: next.left ?? [], right: next.right ?? [] },
|
|
748
|
+
}));
|
|
749
|
+
},
|
|
750
|
+
[onChange, state.columnPinning],
|
|
751
|
+
);
|
|
752
|
+
|
|
753
|
+
// ── TanStack Table instance ──────────────────────────────────
|
|
754
|
+
const table = useReactTable<TRow>({
|
|
755
|
+
data: rows as TRow[],
|
|
756
|
+
columns: tableColumns,
|
|
757
|
+
getRowId: (row) => getRowId(row),
|
|
758
|
+
getCoreRowModel: getCoreRowModel(),
|
|
759
|
+
state: {
|
|
760
|
+
sorting: tanstackSorting,
|
|
761
|
+
columnVisibility: state.columnVisibility,
|
|
762
|
+
columnSizing: state.columnWidths,
|
|
763
|
+
columnOrder: tanstackColumnOrder,
|
|
764
|
+
columnPinning: tanstackColumnPinning,
|
|
765
|
+
rowSelection: tanstackRowSelection,
|
|
766
|
+
},
|
|
767
|
+
onSortingChange: handleSortingChange,
|
|
768
|
+
onColumnSizingChange: handleColumnSizingChange,
|
|
769
|
+
onColumnVisibilityChange: handleVisibilityChange,
|
|
770
|
+
onColumnOrderChange: handleColumnOrderChange,
|
|
771
|
+
onColumnPinningChange: handleColumnPinningChange,
|
|
772
|
+
columnResizeMode: "onEnd",
|
|
773
|
+
enableRowSelection: selectionMode !== "none",
|
|
774
|
+
enableMultiRowSelection: selectionMode === "multiple",
|
|
775
|
+
enableColumnResizing: resizable,
|
|
776
|
+
manualSorting: true,
|
|
777
|
+
manualPagination: true,
|
|
778
|
+
manualFiltering: true,
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
// ── Visible columns (in TanStack-resolved order, after visibility) ──
|
|
782
|
+
const visibleColumns = useMemo(() => {
|
|
783
|
+
const colMap = new Map(allColumns.map((c) => [c.id, c]));
|
|
784
|
+
return table
|
|
785
|
+
.getVisibleLeafColumns()
|
|
786
|
+
.map((c) => colMap.get(c.id))
|
|
787
|
+
.filter(Boolean) as DataGridColumnDef<TRow>[];
|
|
788
|
+
}, [allColumns, table, state.columnOrder, state.columnVisibility]);
|
|
789
|
+
|
|
790
|
+
// ── Row IDs (stable across this render) ──────────────────────
|
|
791
|
+
const rowIds = useMemo(() => rows.map(getRowId), [rows, getRowId]);
|
|
792
|
+
|
|
793
|
+
// ── Container width tracking (for `flex` column distribution) ─
|
|
794
|
+
// Measure the scroll container's clientWidth, which excludes the vertical
|
|
795
|
+
// scrollbar — using the outer grid would leave a few pixels of phantom
|
|
796
|
+
// horizontal scroll when rows overflow.
|
|
797
|
+
const [containerWidth, setContainerWidth] = useState(0);
|
|
798
|
+
useLayoutEffect(() => {
|
|
799
|
+
const grid = gridRef.current;
|
|
800
|
+
const scroller = scrollContainerRef.current;
|
|
801
|
+
if (!grid) return;
|
|
802
|
+
const update = (entries?: ResizeObserverEntry[]) => {
|
|
803
|
+
let measured = 0;
|
|
804
|
+
if (entries != null) {
|
|
805
|
+
for (const entry of entries) {
|
|
806
|
+
measured = Math.max(measured, entry.contentRect.width);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
if (measured <= 0) {
|
|
810
|
+
const scrollerWidth = scroller?.clientWidth ?? 0;
|
|
811
|
+
const gridWidth = grid.clientWidth;
|
|
812
|
+
measured = scrollerWidth > 0 ? scrollerWidth : gridWidth;
|
|
813
|
+
}
|
|
814
|
+
if (measured > 0) setContainerWidth(measured);
|
|
815
|
+
};
|
|
816
|
+
update();
|
|
817
|
+
const observer = new ResizeObserver((entries) => update(entries));
|
|
818
|
+
observer.observe(grid);
|
|
819
|
+
if (scroller) observer.observe(scroller);
|
|
820
|
+
return () => observer.disconnect();
|
|
821
|
+
}, []);
|
|
822
|
+
|
|
823
|
+
// ── Column width CSS variables (TanStack pattern) ────────────
|
|
824
|
+
// With `columnResizeMode: "onEnd"`, live drag width comes from deltaOffset; committed sizes update on pointer-up.
|
|
825
|
+
const columnSizingInfo = table.getState().columnSizingInfo;
|
|
826
|
+
const columnSizes = useMemo<Record<string, number>>(() => {
|
|
827
|
+
const sizes: Record<string, number> = {};
|
|
828
|
+
let baseTotal = selectionMode !== "none" ? 44 : 0;
|
|
829
|
+
const resizingId = columnSizingInfo.isResizingColumn || null;
|
|
830
|
+
const deltaOffset = columnSizingInfo.deltaOffset ?? 0;
|
|
831
|
+
for (const col of visibleColumns) {
|
|
832
|
+
const tsCol = table.getColumn(col.id);
|
|
833
|
+
const baseSize = tsCol?.getSize() ?? col.width ?? DEFAULT_COL_WIDTH;
|
|
834
|
+
const liveSize = resizingId === col.id
|
|
835
|
+
? clampColumnWidth(col, baseSize + deltaOffset)
|
|
836
|
+
: baseSize;
|
|
837
|
+
sizes[col.id] = liveSize;
|
|
838
|
+
baseTotal += liveSize;
|
|
839
|
+
}
|
|
840
|
+
distributeFlexWidths(sizes, visibleColumns, containerWidth - baseTotal);
|
|
841
|
+
return sizes;
|
|
842
|
+
}, [visibleColumns, table, columnSizingInfo, state.columnWidths, containerWidth, selectionMode]);
|
|
843
|
+
|
|
844
|
+
const totalContentWidth = useMemo(() => {
|
|
845
|
+
let total = selectionMode !== "none" ? 44 : 0;
|
|
846
|
+
for (const col of visibleColumns) total += columnSizes[col.id] ?? 0;
|
|
847
|
+
return total;
|
|
848
|
+
}, [visibleColumns, columnSizes, selectionMode]);
|
|
849
|
+
|
|
850
|
+
const cssVars = useMemo<CSSProperties>(() => {
|
|
851
|
+
const vars: Record<string, string | number> = { "--grid-total-w": `${totalContentWidth}px` };
|
|
852
|
+
for (const col of visibleColumns) {
|
|
853
|
+
vars[`--col-${col.id}-size`] = columnSizes[col.id];
|
|
854
|
+
}
|
|
855
|
+
return vars as CSSProperties;
|
|
856
|
+
}, [visibleColumns, columnSizes, totalContentWidth]);
|
|
857
|
+
|
|
858
|
+
// ── Selection handlers ───────────────────────────────────────
|
|
859
|
+
const fireSelection = useCallback(
|
|
860
|
+
(next: DataGridSelectionModel) => {
|
|
861
|
+
onChange((s) => ({ ...s, selection: next }));
|
|
862
|
+
if (onSelectionChange) {
|
|
863
|
+
const idSet = next.selectedIds;
|
|
864
|
+
const selectedRows = rows.filter((r) => idSet.has(getRowId(r)));
|
|
865
|
+
onSelectionChange(idSet, selectedRows);
|
|
866
|
+
}
|
|
867
|
+
},
|
|
868
|
+
[onChange, onSelectionChange, rows, getRowId],
|
|
869
|
+
);
|
|
870
|
+
|
|
871
|
+
const handleRowClick = useCallback(
|
|
872
|
+
(row: TRow, rowId: RowId, event: React.MouseEvent) => {
|
|
873
|
+
if (selectionMode !== "none") {
|
|
874
|
+
const next = nextSelection({
|
|
875
|
+
current: state.selection,
|
|
876
|
+
rowId,
|
|
877
|
+
mode: selectionMode,
|
|
878
|
+
modifiers: { shift: event.shiftKey, ctrl: event.metaKey || event.ctrlKey },
|
|
879
|
+
allRowIds: rowIds,
|
|
880
|
+
});
|
|
881
|
+
fireSelection(next);
|
|
882
|
+
}
|
|
883
|
+
onRowClick?.(row, rowId, event);
|
|
884
|
+
},
|
|
885
|
+
[selectionMode, state.selection, rowIds, fireSelection, onRowClick],
|
|
886
|
+
);
|
|
887
|
+
|
|
888
|
+
const handleSelectAll = useCallback(() => {
|
|
889
|
+
const allSelectedNow = rowIds.length > 0 && rowIds.every((id) => state.selection.selectedIds.has(id));
|
|
890
|
+
fireSelection(
|
|
891
|
+
allSelectedNow
|
|
892
|
+
? { selectedIds: new Set(), anchorId: null }
|
|
893
|
+
: { selectedIds: new Set(rowIds), anchorId: null },
|
|
894
|
+
);
|
|
895
|
+
}, [rowIds, state.selection.selectedIds, fireSelection]);
|
|
896
|
+
|
|
897
|
+
// ── CSV export ───────────────────────────────────────────────
|
|
898
|
+
// The grid only knows about rows currently in memory (the visible page in
|
|
899
|
+
// paginated mode, or the loaded prefix in infinite mode). To avoid users
|
|
900
|
+
// assuming "Export CSV" means "everything that exists on the server", we
|
|
901
|
+
// confirm with the loaded-row count before downloading. Consumers that
|
|
902
|
+
// want true full-dataset export can override this via a parent toolbar.
|
|
903
|
+
const handleExportCsv = useCallback(() => {
|
|
904
|
+
if (typeof window !== "undefined" && rows.length > 0) {
|
|
905
|
+
const totalSuffix = totalRowCount != null && totalRowCount > rows.length
|
|
906
|
+
? ` of ${totalRowCount} total — load more rows first to include them`
|
|
907
|
+
: "";
|
|
908
|
+
const confirmed = window.confirm(
|
|
909
|
+
`Export ${rows.length.toLocaleString()} loaded row${rows.length === 1 ? "" : "s"}${totalSuffix}?`,
|
|
910
|
+
);
|
|
911
|
+
if (!confirmed) return;
|
|
912
|
+
}
|
|
913
|
+
exportToCsv(rows, visibleColumns, exportFilename);
|
|
914
|
+
}, [rows, visibleColumns, exportFilename, totalRowCount]);
|
|
915
|
+
|
|
916
|
+
// ── Virtualizer ──────────────────────────────────────────────
|
|
917
|
+
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
|
918
|
+
const headerScrollRef = useRef<HTMLDivElement>(null);
|
|
919
|
+
const stickyChromeRef = useRef<HTMLDivElement>(null);
|
|
920
|
+
const rowsClipRef = useRef<HTMLDivElement>(null);
|
|
921
|
+
const gridRef = useRef<HTMLDivElement>(null);
|
|
922
|
+
const measureElementFn = useCallback((el: Element) => el.getBoundingClientRect().height, []);
|
|
923
|
+
|
|
924
|
+
const rowVirtualizer = useVirtualizer({
|
|
925
|
+
count: rows.length,
|
|
926
|
+
getScrollElement: () => scrollContainerRef.current,
|
|
927
|
+
estimateSize: () => estimatedRowHeight,
|
|
928
|
+
overscan,
|
|
929
|
+
getItemKey: (index) => {
|
|
930
|
+
const row = rows[index];
|
|
931
|
+
return row != null ? String(getRowId(row)) : index;
|
|
932
|
+
},
|
|
933
|
+
...(isDynamicRowHeight ? { measureElement: measureElementFn } : {}),
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
// ── Sticky chrome clipping ───────────────────────────────────
|
|
937
|
+
useLayoutEffect(() => {
|
|
938
|
+
const gridEl = gridRef.current;
|
|
939
|
+
const stickyEl = stickyChromeRef.current;
|
|
940
|
+
const bodyEl = scrollContainerRef.current;
|
|
941
|
+
const clipEl = rowsClipRef.current;
|
|
942
|
+
if (!gridEl || !stickyEl || !bodyEl || !clipEl) return;
|
|
943
|
+
|
|
944
|
+
const updateClip = () => {
|
|
945
|
+
const stickyRect = stickyEl.getBoundingClientRect();
|
|
946
|
+
const clipRect = clipEl.getBoundingClientRect();
|
|
947
|
+
const overlap = Math.max(0, stickyRect.bottom - clipRect.top);
|
|
948
|
+
clipEl.style.setProperty("--data-grid-sticky-overlap", `${overlap}px`);
|
|
949
|
+
};
|
|
950
|
+
updateClip();
|
|
951
|
+
bodyEl.addEventListener("scroll", updateClip);
|
|
952
|
+
window.addEventListener("scroll", updateClip, true);
|
|
953
|
+
window.addEventListener("resize", updateClip);
|
|
954
|
+
const observer = new ResizeObserver(updateClip);
|
|
955
|
+
observer.observe(gridEl);
|
|
956
|
+
observer.observe(stickyEl);
|
|
957
|
+
observer.observe(bodyEl);
|
|
958
|
+
return () => {
|
|
959
|
+
bodyEl.removeEventListener("scroll", updateClip);
|
|
960
|
+
window.removeEventListener("scroll", updateClip, true);
|
|
961
|
+
window.removeEventListener("resize", updateClip);
|
|
962
|
+
observer.disconnect();
|
|
963
|
+
};
|
|
964
|
+
}, []);
|
|
965
|
+
|
|
966
|
+
const handleBodyScroll = useCallback(() => {
|
|
967
|
+
const body = scrollContainerRef.current;
|
|
968
|
+
const header = headerScrollRef.current;
|
|
969
|
+
if (body && header) header.scrollLeft = body.scrollLeft;
|
|
970
|
+
}, []);
|
|
971
|
+
|
|
972
|
+
// ── Toolbar / Footer context ─────────────────────────────────
|
|
973
|
+
const toolbarCtx: DataGridToolbarContext<TRow> = useMemo(
|
|
974
|
+
() => ({
|
|
975
|
+
state,
|
|
976
|
+
onChange,
|
|
977
|
+
columns: allColumns,
|
|
978
|
+
visibleColumns,
|
|
979
|
+
totalRowCount,
|
|
980
|
+
selectedRowCount: state.selection.selectedIds.size,
|
|
981
|
+
strings,
|
|
982
|
+
exportCsv: handleExportCsv,
|
|
983
|
+
}),
|
|
984
|
+
[state, onChange, allColumns, visibleColumns, totalRowCount, strings, handleExportCsv],
|
|
985
|
+
);
|
|
986
|
+
const footerCtx: DataGridFooterContext<TRow> = useMemo(
|
|
987
|
+
() => ({
|
|
988
|
+
state,
|
|
989
|
+
totalRowCount,
|
|
990
|
+
visibleRowCount: rows.length,
|
|
991
|
+
selectedRowCount: state.selection.selectedIds.size,
|
|
992
|
+
paginationMode,
|
|
993
|
+
strings,
|
|
994
|
+
}),
|
|
995
|
+
[state, totalRowCount, rows.length, paginationMode, strings],
|
|
996
|
+
);
|
|
997
|
+
|
|
998
|
+
const allSelected = rowIds.length > 0 && rowIds.every((id) => state.selection.selectedIds.has(id));
|
|
999
|
+
const someSelected = !allSelected && rowIds.some((id) => state.selection.selectedIds.has(id));
|
|
1000
|
+
const infiniteScrollRootRef =
|
|
1001
|
+
paginationMode === "infinite" && (fillHeight || maxHeight != null)
|
|
1002
|
+
? scrollContainerRef
|
|
1003
|
+
: undefined;
|
|
1004
|
+
|
|
1005
|
+
// ── Header rendering helper (TanStack header objects, in order) ──
|
|
1006
|
+
const headers: Header<TRow, unknown>[] = useMemo(
|
|
1007
|
+
() =>
|
|
1008
|
+
table
|
|
1009
|
+
.getHeaderGroups()[0]?.headers
|
|
1010
|
+
.filter((h) => visibleColumns.some((c) => c.id === h.column.id)) ?? [],
|
|
1011
|
+
[table, visibleColumns],
|
|
1012
|
+
);
|
|
1013
|
+
const headerByColId = useMemo(() => {
|
|
1014
|
+
const m = new Map<string, Header<TRow, unknown>>();
|
|
1015
|
+
for (const h of headers) m.set(h.column.id, h);
|
|
1016
|
+
return m;
|
|
1017
|
+
}, [headers]);
|
|
1018
|
+
|
|
1019
|
+
const isBounded = fillHeight || maxHeight != null;
|
|
1020
|
+
|
|
1021
|
+
return (
|
|
1022
|
+
<div
|
|
1023
|
+
ref={gridRef}
|
|
1024
|
+
className={cn(
|
|
1025
|
+
"isolate flex w-full min-w-0 max-w-full flex-col bg-transparent rounded-[calc(var(--radius)*2)]",
|
|
1026
|
+
fillHeight ? "min-h-0 h-full" : "min-h-0 h-auto",
|
|
1027
|
+
isBounded && "overflow-hidden",
|
|
1028
|
+
className,
|
|
1029
|
+
)}
|
|
1030
|
+
style={maxHeight != null ? { ...cssVars, maxHeight } : cssVars}
|
|
1031
|
+
role="grid"
|
|
1032
|
+
aria-rowcount={totalRowCount ?? rows.length}
|
|
1033
|
+
aria-colcount={visibleColumns.length}
|
|
1034
|
+
>
|
|
1035
|
+
<div
|
|
1036
|
+
ref={stickyChromeRef}
|
|
1037
|
+
className="sticky z-30 w-full min-w-0 shrink-0 overflow-visible rounded-t-[calc(var(--radius)*2)] bg-white/90 dark:bg-background/60 backdrop-blur-xl"
|
|
1038
|
+
style={{ top: stickyTop ?? (maxHeight != null ? 0 : "var(--data-grid-sticky-top, 0px)") }}
|
|
1039
|
+
>
|
|
1040
|
+
{toolbar !== false && (
|
|
1041
|
+
<div className="relative bg-transparent">
|
|
1042
|
+
{toolbar
|
|
1043
|
+
? toolbar(toolbarCtx)
|
|
1044
|
+
: (
|
|
1045
|
+
<DataGridToolbar
|
|
1046
|
+
ctx={toolbarCtx}
|
|
1047
|
+
extra={typeof toolbarExtra === "function" ? toolbarExtra(toolbarCtx) : toolbarExtra}
|
|
1048
|
+
/>
|
|
1049
|
+
)}
|
|
1050
|
+
</div>
|
|
1051
|
+
)}
|
|
1052
|
+
|
|
1053
|
+
<div className="relative">
|
|
1054
|
+
{isRefetching && (
|
|
1055
|
+
<div className="absolute top-0 left-0 right-0 h-0.5 z-30 bg-foreground/[0.04] overflow-hidden">
|
|
1056
|
+
<div className="h-full w-1/3 bg-blue-500/60 rounded-full animate-pulse" />
|
|
1057
|
+
</div>
|
|
1058
|
+
)}
|
|
1059
|
+
<div
|
|
1060
|
+
ref={headerScrollRef}
|
|
1061
|
+
className="w-full min-w-0 shrink-0 overflow-hidden border-b border-foreground/[0.06]"
|
|
1062
|
+
>
|
|
1063
|
+
<div
|
|
1064
|
+
className="flex"
|
|
1065
|
+
style={{ height: headerHeight, minWidth: totalContentWidth }}
|
|
1066
|
+
role="row"
|
|
1067
|
+
>
|
|
1068
|
+
{selectionMode !== "none" && (
|
|
1069
|
+
<div
|
|
1070
|
+
className="flex items-center justify-center border-r border-foreground/[0.04]"
|
|
1071
|
+
style={{ width: 44 }}
|
|
1072
|
+
>
|
|
1073
|
+
{selectionMode === "multiple" && (
|
|
1074
|
+
<SelectionCheckbox
|
|
1075
|
+
checked={allSelected}
|
|
1076
|
+
indeterminate={someSelected}
|
|
1077
|
+
onChange={handleSelectAll}
|
|
1078
|
+
ariaLabel="Select all rows on this page"
|
|
1079
|
+
title="Select all rows on this page"
|
|
1080
|
+
/>
|
|
1081
|
+
)}
|
|
1082
|
+
</div>
|
|
1083
|
+
)}
|
|
1084
|
+
{visibleColumns.map((col) => {
|
|
1085
|
+
const header = headerByColId.get(col.id);
|
|
1086
|
+
if (!header) return null;
|
|
1087
|
+
return <HeaderCell key={col.id} header={header} col={col} resizable={resizable} />;
|
|
1088
|
+
})}
|
|
1089
|
+
</div>
|
|
1090
|
+
</div>
|
|
1091
|
+
</div>
|
|
1092
|
+
</div>
|
|
1093
|
+
|
|
1094
|
+
<div
|
|
1095
|
+
ref={scrollContainerRef}
|
|
1096
|
+
className={cn(
|
|
1097
|
+
"relative z-0 w-full min-w-0 overflow-auto bg-transparent",
|
|
1098
|
+
isBounded ? "min-h-0 flex-1" : "flex-none",
|
|
1099
|
+
"[&::-webkit-scrollbar]:w-1.5 [&::-webkit-scrollbar]:h-1.5",
|
|
1100
|
+
"[&::-webkit-scrollbar-track]:bg-transparent",
|
|
1101
|
+
"[&::-webkit-scrollbar-thumb]:bg-foreground/[0.08] [&::-webkit-scrollbar-thumb]:rounded-full",
|
|
1102
|
+
"[&::-webkit-scrollbar-thumb]:hover:bg-foreground/[0.15]",
|
|
1103
|
+
)}
|
|
1104
|
+
onScroll={handleBodyScroll}
|
|
1105
|
+
>
|
|
1106
|
+
<div
|
|
1107
|
+
ref={rowsClipRef}
|
|
1108
|
+
data-data-grid-rows-clip=""
|
|
1109
|
+
className="relative z-0"
|
|
1110
|
+
style={{
|
|
1111
|
+
minWidth: totalContentWidth,
|
|
1112
|
+
clipPath: "inset(var(--data-grid-sticky-overlap, 0px) 0 0 0)",
|
|
1113
|
+
}}
|
|
1114
|
+
>
|
|
1115
|
+
{isLoading && (
|
|
1116
|
+
<div style={{ minWidth: totalContentWidth }}>
|
|
1117
|
+
{loadingState ?? Array.from({ length: 8 }).map((_, i) => (
|
|
1118
|
+
<SkeletonRow
|
|
1119
|
+
key={i}
|
|
1120
|
+
columns={visibleColumns}
|
|
1121
|
+
height={estimatedRowHeight}
|
|
1122
|
+
showCheckbox={selectionMode !== "none"}
|
|
1123
|
+
/>
|
|
1124
|
+
))}
|
|
1125
|
+
</div>
|
|
1126
|
+
)}
|
|
1127
|
+
|
|
1128
|
+
{!isLoading && rows.length === 0 && (
|
|
1129
|
+
<div
|
|
1130
|
+
className="flex items-center justify-center py-16 text-sm text-muted-foreground"
|
|
1131
|
+
style={{ minWidth: totalContentWidth }}
|
|
1132
|
+
>
|
|
1133
|
+
{emptyState ?? strings.noData}
|
|
1134
|
+
</div>
|
|
1135
|
+
)}
|
|
1136
|
+
|
|
1137
|
+
{!isLoading && rows.length > 0 && (
|
|
1138
|
+
<div
|
|
1139
|
+
style={{
|
|
1140
|
+
height: rowVirtualizer.getTotalSize(),
|
|
1141
|
+
width: "100%",
|
|
1142
|
+
minWidth: totalContentWidth,
|
|
1143
|
+
position: "relative",
|
|
1144
|
+
}}
|
|
1145
|
+
>
|
|
1146
|
+
{rowVirtualizer.getVirtualItems().map((virtualRow: VirtualItem) => {
|
|
1147
|
+
const row = rows[virtualRow.index] ?? throwErr(
|
|
1148
|
+
`DataGrid: virtualized row index ${virtualRow.index} out of range (rows.length=${rows.length})`,
|
|
1149
|
+
);
|
|
1150
|
+
const rowId = getRowId(row);
|
|
1151
|
+
const isSelected = state.selection.selectedIds.has(rowId);
|
|
1152
|
+
const isOddRow = virtualRow.index % 2 === 1;
|
|
1153
|
+
return (
|
|
1154
|
+
<div
|
|
1155
|
+
key={rowId}
|
|
1156
|
+
ref={isDynamicRowHeight ? rowVirtualizer.measureElement : undefined}
|
|
1157
|
+
data-index={virtualRow.index}
|
|
1158
|
+
className={cn(
|
|
1159
|
+
"absolute left-0 w-full flex",
|
|
1160
|
+
"border-b border-black/[0.03] dark:border-white/[0.03]",
|
|
1161
|
+
"transition-colors duration-75",
|
|
1162
|
+
isSelected
|
|
1163
|
+
? "bg-blue-500/[0.06] dark:bg-blue-400/[0.08] hover:bg-blue-500/[0.08] dark:hover:bg-blue-400/[0.1]"
|
|
1164
|
+
: isOddRow
|
|
1165
|
+
? "bg-foreground/[0.02] dark:bg-foreground/[0.03] hover:bg-foreground/[0.04] dark:hover:bg-foreground/[0.06]"
|
|
1166
|
+
: "hover:bg-foreground/[0.025] dark:hover:bg-foreground/[0.04]",
|
|
1167
|
+
(selectionMode !== "none" || onRowClick) && "cursor-pointer",
|
|
1168
|
+
)}
|
|
1169
|
+
style={{
|
|
1170
|
+
...(isDynamicRowHeight
|
|
1171
|
+
? { minHeight: estimatedRowHeight }
|
|
1172
|
+
: { height: fixedRowHeight }),
|
|
1173
|
+
transform: `translateY(${virtualRow.start}px)`,
|
|
1174
|
+
}}
|
|
1175
|
+
onClick={(e) => { if (!shouldIgnoreRowClick(e)) handleRowClick(row, rowId, e); }}
|
|
1176
|
+
onDoubleClick={(e) => { if (!shouldIgnoreRowClick(e)) onRowDoubleClick?.(row, rowId, e); }}
|
|
1177
|
+
role="row"
|
|
1178
|
+
aria-rowindex={virtualRow.index + 2}
|
|
1179
|
+
aria-selected={isSelected}
|
|
1180
|
+
data-row-id={rowId}
|
|
1181
|
+
data-state={isSelected ? "selected" : undefined}
|
|
1182
|
+
>
|
|
1183
|
+
{selectionMode !== "none" && (
|
|
1184
|
+
<div
|
|
1185
|
+
className="flex items-center justify-center border-r border-black/[0.04] dark:border-white/[0.04]"
|
|
1186
|
+
style={{ width: 44 }}
|
|
1187
|
+
>
|
|
1188
|
+
<SelectionCheckbox
|
|
1189
|
+
checked={isSelected}
|
|
1190
|
+
onChange={(event) => handleRowClick(row, rowId, event)}
|
|
1191
|
+
ariaLabel={`Select row ${rowId}`}
|
|
1192
|
+
/>
|
|
1193
|
+
</div>
|
|
1194
|
+
)}
|
|
1195
|
+
{visibleColumns.map((col) => (
|
|
1196
|
+
<DataCell
|
|
1197
|
+
key={col.id}
|
|
1198
|
+
col={col}
|
|
1199
|
+
row={row}
|
|
1200
|
+
rowId={rowId}
|
|
1201
|
+
rowIndex={virtualRow.index}
|
|
1202
|
+
isSelected={isSelected}
|
|
1203
|
+
dateDisplay={state.dateDisplay}
|
|
1204
|
+
/>
|
|
1205
|
+
))}
|
|
1206
|
+
</div>
|
|
1207
|
+
);
|
|
1208
|
+
})}
|
|
1209
|
+
</div>
|
|
1210
|
+
)}
|
|
1211
|
+
|
|
1212
|
+
{paginationMode === "infinite" && hasMore && !isLoading && (
|
|
1213
|
+
<InfiniteScrollSentinel
|
|
1214
|
+
onIntersect={onLoadMore ?? NOOP}
|
|
1215
|
+
isLoading={isLoadingMore}
|
|
1216
|
+
rootRef={infiniteScrollRootRef}
|
|
1217
|
+
strings={strings}
|
|
1218
|
+
/>
|
|
1219
|
+
)}
|
|
1220
|
+
</div>
|
|
1221
|
+
</div>
|
|
1222
|
+
|
|
1223
|
+
{footer !== false && (
|
|
1224
|
+
<div className="sticky bottom-0 z-30 shrink-0 overflow-hidden rounded-b-[calc(var(--radius)*2)] bg-white/90 dark:bg-background/60 backdrop-blur-xl">
|
|
1225
|
+
{footer ? footer(footerCtx) : <DefaultFooter ctx={footerCtx} pagination={paginationMode} onChange={onChange} />}
|
|
1226
|
+
{footerExtra && (typeof footerExtra === "function" ? footerExtra(footerCtx) : footerExtra)}
|
|
1227
|
+
</div>
|
|
1228
|
+
)}
|
|
1229
|
+
</div>
|
|
1230
|
+
);
|
|
1231
|
+
}
|