@brightlocal/ui-components 2.1.0 → 2.3.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/AI_USAGE.md +1 -1
- package/README.md +3 -3
- package/component-meta.json +54 -10
- package/dist/blocks/data-table.cjs +1 -1
- package/dist/blocks/data-table.d.ts +380 -196
- package/dist/blocks/data-table.d.ts.map +1 -1
- package/dist/blocks/data-table.js +452 -449
- package/dist/callout.cjs +1 -0
- package/dist/callout.d.ts +51 -0
- package/dist/callout.d.ts.map +1 -0
- package/dist/callout.js +114 -0
- package/dist/carousel.cjs +1 -1
- package/dist/carousel.d.ts +40 -4
- package/dist/carousel.d.ts.map +1 -1
- package/dist/carousel.js +156 -101
- package/dist/combobox.cjs +3 -3
- package/dist/combobox.d.ts +26 -5
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +381 -346
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +436 -426
- package/dist/input-group.cjs +1 -1
- package/dist/input-group.js +1 -1
- package/dist/sheet.d.ts +1 -1
- package/package.json +1 -1
- package/src/blocks/data-table.tsx +865 -679
- package/src/components/callout.tsx +182 -0
- package/src/components/carousel.tsx +121 -3
- package/src/components/combobox.tsx +81 -10
- package/src/components/input-group.tsx +1 -1
- package/src/index.ts +1 -0
|
@@ -1,31 +1,43 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* DataTable — composable data table primitives built on TanStack Table.
|
|
5
|
+
*
|
|
6
|
+
* Architecture:
|
|
7
|
+
* - `useDataTable` hook: owns TanStack Table wiring, feature toggles,
|
|
8
|
+
* and controlled/uncontrolled state orchestration.
|
|
9
|
+
* - `DataTable`: generic renderer for headers, rows, loading skeletons,
|
|
10
|
+
* and the empty state.
|
|
11
|
+
* - `VirtualizedDataTable`: drop-in alternative to `DataTable` that
|
|
12
|
+
* renders only visible rows via `@tanstack/react-virtual`. Use for large
|
|
13
|
+
* datasets (1 000+ rows) where full DOM rendering is too expensive.
|
|
14
|
+
* - Helpers: `DataTableToolbar`, `DataTableSearch`, `DataTablePagination`,
|
|
15
|
+
* `DataTableColumnHeader`, `DataTableRowActions`,
|
|
16
|
+
* `DataTableSelectAllCheckbox`, `DataTableSelectRowCheckbox`.
|
|
17
|
+
*
|
|
18
|
+
* Block composition example — see `apps/sandbox/components/audit-table.tsx`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
3
21
|
import * as React from "react";
|
|
4
22
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
23
|
+
type Column,
|
|
24
|
+
type ColumnDef,
|
|
25
|
+
type ExpandedState,
|
|
26
|
+
type PaginationState,
|
|
27
|
+
type Row,
|
|
28
|
+
type RowSelectionState,
|
|
29
|
+
type SortingState,
|
|
30
|
+
type Table as TanStackTable,
|
|
31
|
+
createColumnHelper,
|
|
11
32
|
flexRender,
|
|
12
33
|
getCoreRowModel,
|
|
13
34
|
getExpandedRowModel,
|
|
14
|
-
getFacetedMinMaxValues,
|
|
15
|
-
getFacetedRowModel,
|
|
16
|
-
getFacetedUniqueValues,
|
|
17
35
|
getFilteredRowModel,
|
|
18
|
-
getGroupedRowModel,
|
|
19
36
|
getPaginationRowModel,
|
|
20
37
|
getSortedRowModel,
|
|
21
|
-
SortingState,
|
|
22
|
-
ColumnFiltersState,
|
|
23
|
-
RowSelectionState,
|
|
24
38
|
useReactTable,
|
|
25
|
-
Table as TanStackTable,
|
|
26
|
-
Column,
|
|
27
|
-
Row,
|
|
28
39
|
} from "@tanstack/react-table";
|
|
40
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
29
41
|
import {
|
|
30
42
|
ArrowDown,
|
|
31
43
|
ArrowUp,
|
|
@@ -37,607 +49,759 @@ import {
|
|
|
37
49
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
38
50
|
import { Button } from "../components/button";
|
|
39
51
|
import { Checkbox } from "../components/checkbox";
|
|
40
|
-
import {
|
|
41
|
-
InputGroup,
|
|
42
|
-
InputGroupInput,
|
|
43
|
-
InputGroupAddon,
|
|
44
|
-
} from "../components/input-group";
|
|
45
|
-
import { Skeleton } from "../components/skeleton";
|
|
46
52
|
import {
|
|
47
53
|
DropdownMenu,
|
|
48
54
|
DropdownMenuContent,
|
|
49
55
|
DropdownMenuTrigger,
|
|
50
56
|
} from "../components/dropdown-menu";
|
|
51
57
|
import {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
TableHeader,
|
|
57
|
-
TableRow,
|
|
58
|
-
} from "../components/table";
|
|
58
|
+
InputGroup,
|
|
59
|
+
InputGroupAddon,
|
|
60
|
+
InputGroupInput,
|
|
61
|
+
} from "../components/input-group";
|
|
59
62
|
import {
|
|
60
63
|
Pagination,
|
|
61
64
|
PaginationContent,
|
|
65
|
+
PaginationEllipsis,
|
|
62
66
|
PaginationItem,
|
|
63
67
|
PaginationLink,
|
|
64
|
-
PaginationPrevious,
|
|
65
68
|
PaginationNext,
|
|
66
|
-
|
|
69
|
+
PaginationPrevious,
|
|
67
70
|
usePagination,
|
|
68
71
|
} from "../components/pagination";
|
|
72
|
+
import { Skeleton } from "../components/skeleton";
|
|
73
|
+
import {
|
|
74
|
+
Table,
|
|
75
|
+
TableBody,
|
|
76
|
+
TableCell,
|
|
77
|
+
TableHead,
|
|
78
|
+
TableHeader,
|
|
79
|
+
TableRow,
|
|
80
|
+
} from "../components/table";
|
|
81
|
+
|
|
82
|
+
/* -------------------------------------------------------------------------------------------------
|
|
83
|
+
* useDataTable (hook)
|
|
84
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
69
85
|
|
|
70
86
|
/**
|
|
71
|
-
*
|
|
87
|
+
* Small controlled/uncontrolled helper.
|
|
88
|
+
*
|
|
89
|
+
* Resolves a "controlled or internal" value and returns a setter compatible
|
|
90
|
+
* with TanStack Table's `SetStateAction<T>` updater signature. When the
|
|
91
|
+
* `controlled` value is defined, the parent owns state and `onChange` is
|
|
92
|
+
* called with the next value. When undefined, state is kept internally and
|
|
93
|
+
* `onChange` is still invoked (the common "notify but self-manage" pattern).
|
|
72
94
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
} from "@tanstack/react-table";
|
|
95
|
+
function useControllable<T>(
|
|
96
|
+
controlled: T | undefined,
|
|
97
|
+
initialValue: T,
|
|
98
|
+
onChange?: (next: T) => void
|
|
99
|
+
): [T, (updater: T | ((prev: T) => T)) => void] {
|
|
100
|
+
const [internal, setInternal] = React.useState<T>(initialValue);
|
|
101
|
+
const value = controlled !== undefined ? controlled : internal;
|
|
102
|
+
|
|
103
|
+
// Keep ref in sync so the setter always reads the latest value.
|
|
104
|
+
const valueRef = React.useRef(value);
|
|
105
|
+
React.useEffect(() => {
|
|
106
|
+
valueRef.current = value;
|
|
107
|
+
});
|
|
87
108
|
|
|
88
|
-
|
|
109
|
+
const isControlled = controlled !== undefined;
|
|
89
110
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
const setValue = React.useCallback(
|
|
112
|
+
(updater: T | ((prev: T) => T)) => {
|
|
113
|
+
const next =
|
|
114
|
+
typeof updater === "function"
|
|
115
|
+
? (updater as (prev: T) => T)(valueRef.current)
|
|
116
|
+
: updater;
|
|
117
|
+
if (!isControlled) {
|
|
118
|
+
setInternal(next);
|
|
119
|
+
}
|
|
120
|
+
onChange?.(next);
|
|
121
|
+
},
|
|
122
|
+
[isControlled, onChange]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
return [value, setValue];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Configuration for `useDataTable`.
|
|
130
|
+
*
|
|
131
|
+
* Props are tiered as:
|
|
132
|
+
* - **Common**: sorting, global filtering, row selection, pagination.
|
|
133
|
+
* - **Advanced**: column ordering, expanding.
|
|
134
|
+
*
|
|
135
|
+
* All feature flags default to `false` except `enableSorting` which defaults
|
|
136
|
+
* to `true` — tables are nearly always sortable.
|
|
137
|
+
*
|
|
138
|
+
* Every feature exposes an optional controlled state pair
|
|
139
|
+
* (`<feature>` + `on<Feature>Change`). Provide neither for internal state,
|
|
140
|
+
* both for fully controlled state, or just the callback to observe internal
|
|
141
|
+
* state changes.
|
|
142
|
+
*
|
|
143
|
+
* **Performance:** per TanStack's guidance, memoize `columns` and `data` in
|
|
144
|
+
* the parent component (e.g., `React.useMemo` or module-scope constants) to
|
|
145
|
+
* avoid re-creating the table instance on every render.
|
|
146
|
+
*
|
|
147
|
+
* **Server-side row selection:** pass `getRowId` to derive stable row IDs
|
|
148
|
+
* from your data. Without it, selection keys are row array indices, which
|
|
149
|
+
* become stale across server-side page changes or re-sorts.
|
|
150
|
+
*/
|
|
151
|
+
export interface UseDataTableOptions<TData> {
|
|
152
|
+
/** Column definitions. Memoize in parent to avoid re-creating the table. */
|
|
153
|
+
columns: ColumnDef<TData, unknown>[];
|
|
154
|
+
/** Row data. Memoize in parent to avoid re-creating the table. */
|
|
94
155
|
data: TData[];
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Derive a stable row ID from the row data. Required for server-side
|
|
158
|
+
* pagination with row selection — otherwise selection keys are array
|
|
159
|
+
* indices and become stale when the page changes.
|
|
160
|
+
* @example (row) => row.id
|
|
161
|
+
*/
|
|
162
|
+
getRowId?: (originalRow: TData, index: number, parent?: Row<TData>) => string;
|
|
101
163
|
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
/** Enable sorting */
|
|
164
|
+
// ---------- Common features ----------
|
|
165
|
+
|
|
166
|
+
/** Enable column sorting. @default true */
|
|
106
167
|
enableSorting?: boolean;
|
|
107
|
-
/** Enable
|
|
108
|
-
enableFiltering?: boolean;
|
|
109
|
-
/** Enable global filtering */
|
|
168
|
+
/** Enable global text search across rows (substring match). @default false */
|
|
110
169
|
enableGlobalFiltering?: boolean;
|
|
111
|
-
/** Enable
|
|
170
|
+
/** Enable row selection via checkboxes. @default false */
|
|
171
|
+
enableRowSelection?: boolean;
|
|
172
|
+
/** Enable client-side pagination. @default false */
|
|
112
173
|
enablePagination?: boolean;
|
|
113
|
-
/**
|
|
114
|
-
enableColumnPinning?: boolean;
|
|
115
|
-
/** Enable row pinning */
|
|
116
|
-
enableRowPinning?: boolean;
|
|
117
|
-
/** Enable column ordering/reordering */
|
|
118
|
-
enableColumnOrdering?: boolean;
|
|
119
|
-
/** Enable grouping */
|
|
120
|
-
enableGrouping?: boolean;
|
|
121
|
-
/** Enable expanding rows */
|
|
122
|
-
enableExpanding?: boolean;
|
|
123
|
-
/** Enable faceted values (for filter dropdowns) */
|
|
124
|
-
enableFaceting?: boolean;
|
|
125
|
-
|
|
126
|
-
/** Initial page size for pagination */
|
|
174
|
+
/** Initial page size when pagination is enabled. @default 10 */
|
|
127
175
|
pageSize?: number;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
176
|
+
|
|
177
|
+
// ---------- Server-side / manual modes ----------
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Skip client-side sorting — the server pre-sorts `data`.
|
|
181
|
+
* Sorting state and UI indicators still work; only the client-side
|
|
182
|
+
* `getSortedRowModel` transform is suppressed.
|
|
183
|
+
* @default false
|
|
184
|
+
*/
|
|
185
|
+
manualSorting?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Skip client-side filtering — the server pre-filters `data`.
|
|
188
|
+
* Global filter state still works; only the client-side
|
|
189
|
+
* `getFilteredRowModel` transform is suppressed.
|
|
190
|
+
* @default false
|
|
191
|
+
*/
|
|
192
|
+
manualFiltering?: boolean;
|
|
193
|
+
/**
|
|
194
|
+
* Skip client-side pagination — the server pre-slices `data`.
|
|
195
|
+
* Pagination state still works; only the client-side
|
|
196
|
+
* `getPaginationRowModel` transform is suppressed.
|
|
197
|
+
* Pair with `rowCount` or `pageCount` so the table knows the total.
|
|
198
|
+
* @default false
|
|
199
|
+
*/
|
|
200
|
+
manualPagination?: boolean;
|
|
201
|
+
/**
|
|
202
|
+
* Total row count across all server pages. Used to derive `pageCount`
|
|
203
|
+
* when `manualPagination` is enabled.
|
|
204
|
+
*/
|
|
205
|
+
rowCount?: number;
|
|
206
|
+
/**
|
|
207
|
+
* Total page count. Takes precedence over the value derived from
|
|
208
|
+
* `rowCount`. Use `-1` when the total is unknown (infinite scroll).
|
|
209
|
+
*/
|
|
210
|
+
pageCount?: number;
|
|
211
|
+
|
|
212
|
+
// ---------- Advanced features ----------
|
|
213
|
+
|
|
214
|
+
/** Advanced: enable expandable rows. @default false */
|
|
215
|
+
enableExpanding?: boolean;
|
|
216
|
+
/** Advanced: return sub-rows for tree/expanding tables. */
|
|
135
217
|
getSubRows?: (originalRow: TData, index: number) => TData[] | undefined;
|
|
136
218
|
|
|
137
|
-
// Controlled
|
|
138
|
-
|
|
219
|
+
// ---------- Controlled state (all optional) ----------
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Controlled pagination state. When provided, the consumer owns
|
|
223
|
+
* `pageIndex` and `pageSize`; the hook calls `onPaginationChange`
|
|
224
|
+
* instead of updating internal state. This is especially useful in
|
|
225
|
+
* manual pagination mode where the server drives page state.
|
|
226
|
+
*
|
|
227
|
+
* When omitted, pagination state is managed internally (seeded from
|
|
228
|
+
* the `pageSize` option, defaulting to page index 0).
|
|
229
|
+
*/
|
|
230
|
+
pagination?: PaginationState;
|
|
231
|
+
/** Called when pagination state changes (page navigation or page size). */
|
|
232
|
+
onPaginationChange?: (pagination: PaginationState) => void;
|
|
139
233
|
sorting?: SortingState;
|
|
140
|
-
/** Callback when sorting changes */
|
|
141
234
|
onSortingChange?: (sorting: SortingState) => void;
|
|
142
|
-
/** Controlled column filters state */
|
|
143
|
-
columnFilters?: ColumnFiltersState;
|
|
144
|
-
/** Callback when column filters change */
|
|
145
|
-
onColumnFiltersChange?: (filters: ColumnFiltersState) => void;
|
|
146
|
-
/** Controlled global filter state */
|
|
147
235
|
globalFilter?: string;
|
|
148
|
-
/** Callback when global filter changes */
|
|
149
236
|
onGlobalFilterChange?: (filter: string) => void;
|
|
150
|
-
/** Controlled row selection state */
|
|
151
237
|
rowSelection?: RowSelectionState;
|
|
152
|
-
/** Callback when row selection changes */
|
|
153
238
|
onRowSelectionChange?: (selection: RowSelectionState) => void;
|
|
154
|
-
/** Controlled column order state */
|
|
155
|
-
columnOrder?: ColumnOrderState;
|
|
156
|
-
/** Callback when column order changes */
|
|
157
|
-
onColumnOrderChange?: (order: ColumnOrderState) => void;
|
|
158
|
-
/** Controlled column pinning state */
|
|
159
|
-
columnPinning?: ColumnPinningState;
|
|
160
|
-
/** Callback when column pinning changes */
|
|
161
|
-
onColumnPinningChange?: (pinning: ColumnPinningState) => void;
|
|
162
|
-
/** Controlled row pinning state */
|
|
163
|
-
rowPinning?: RowPinningState;
|
|
164
|
-
/** Callback when row pinning changes */
|
|
165
|
-
onRowPinningChange?: (pinning: RowPinningState) => void;
|
|
166
|
-
/** Controlled grouping state */
|
|
167
|
-
grouping?: GroupingState;
|
|
168
|
-
/** Callback when grouping changes */
|
|
169
|
-
onGroupingChange?: (grouping: GroupingState) => void;
|
|
170
|
-
/** Controlled expanded state */
|
|
171
239
|
expanded?: ExpandedState;
|
|
172
|
-
/** Callback when expanded changes */
|
|
173
240
|
onExpandedChange?: (expanded: ExpandedState) => void;
|
|
174
|
-
|
|
175
|
-
/** Additional class name for the table container */
|
|
176
|
-
className?: string;
|
|
177
|
-
/** Render function for custom table toolbar */
|
|
178
|
-
renderToolbar?: (table: TanStackTable<TData>) => React.ReactNode;
|
|
179
|
-
/** Render function for custom table footer (for summary rows, totals, etc.) */
|
|
180
|
-
renderFooter?: (table: TanStackTable<TData>) => React.ReactNode;
|
|
181
|
-
/** Render function for custom pagination controls */
|
|
182
|
-
renderPagination?: (table: TanStackTable<TData>) => React.ReactNode;
|
|
183
241
|
}
|
|
184
242
|
|
|
185
|
-
|
|
186
|
-
|
|
243
|
+
/**
|
|
244
|
+
* Core DataTable behavior hook.
|
|
245
|
+
*
|
|
246
|
+
* Owns the TanStack Table instance, feature enablement, and
|
|
247
|
+
* controlled/uncontrolled state orchestration. Return the table instance for
|
|
248
|
+
* consumers to pass into `DataTable`, `DataTablePagination`,
|
|
249
|
+
* `DataTableSearch`, etc.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* const table = useDataTable({
|
|
253
|
+
* columns,
|
|
254
|
+
* data,
|
|
255
|
+
* enableSorting: true,
|
|
256
|
+
* enablePagination: true,
|
|
257
|
+
* pageSize: 25,
|
|
258
|
+
* });
|
|
259
|
+
*
|
|
260
|
+
* return <DataTable table={table} dataHook="locations-table" />;
|
|
261
|
+
*/
|
|
262
|
+
export function useDataTable<TData>(
|
|
263
|
+
options: UseDataTableOptions<TData>
|
|
264
|
+
): TanStackTable<TData> {
|
|
265
|
+
const {
|
|
187
266
|
columns,
|
|
188
267
|
data,
|
|
189
|
-
dataHook,
|
|
190
|
-
trackingEl,
|
|
191
|
-
trackingLabel,
|
|
192
|
-
enableRowSelection = false,
|
|
193
268
|
enableSorting = true,
|
|
194
|
-
enableFiltering = false,
|
|
195
269
|
enableGlobalFiltering = false,
|
|
270
|
+
enableRowSelection = false,
|
|
196
271
|
enablePagination = false,
|
|
197
|
-
enableColumnPinning = false,
|
|
198
|
-
enableRowPinning = false,
|
|
199
|
-
enableColumnOrdering = false,
|
|
200
|
-
enableGrouping = false,
|
|
201
|
-
enableExpanding = false,
|
|
202
|
-
enableFaceting = false,
|
|
203
272
|
pageSize = 10,
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
273
|
+
manualSorting = false,
|
|
274
|
+
manualFiltering = false,
|
|
275
|
+
manualPagination = false,
|
|
276
|
+
rowCount,
|
|
277
|
+
pageCount,
|
|
278
|
+
enableExpanding = false,
|
|
207
279
|
getSubRows,
|
|
208
|
-
|
|
280
|
+
getRowId,
|
|
281
|
+
pagination: paginationProp,
|
|
282
|
+
onPaginationChange: onPaginationChangeProp,
|
|
283
|
+
sorting,
|
|
209
284
|
onSortingChange,
|
|
210
|
-
|
|
211
|
-
onColumnFiltersChange,
|
|
212
|
-
globalFilter: controlledGlobalFilter,
|
|
285
|
+
globalFilter,
|
|
213
286
|
onGlobalFilterChange,
|
|
214
|
-
rowSelection
|
|
287
|
+
rowSelection,
|
|
215
288
|
onRowSelectionChange,
|
|
216
|
-
|
|
217
|
-
onColumnOrderChange,
|
|
218
|
-
columnPinning: controlledColumnPinning,
|
|
219
|
-
onColumnPinningChange,
|
|
220
|
-
rowPinning: controlledRowPinning,
|
|
221
|
-
onRowPinningChange,
|
|
222
|
-
grouping: controlledGrouping,
|
|
223
|
-
onGroupingChange,
|
|
224
|
-
expanded: controlledExpanded,
|
|
289
|
+
expanded,
|
|
225
290
|
onExpandedChange,
|
|
226
|
-
|
|
227
|
-
renderToolbar,
|
|
228
|
-
renderFooter,
|
|
229
|
-
renderPagination,
|
|
230
|
-
}: DataTableProps<TData, TValue>,
|
|
231
|
-
ref: React.ForwardedRef<HTMLDivElement>
|
|
232
|
-
) {
|
|
233
|
-
// Internal state for uncontrolled mode
|
|
234
|
-
const [internalSorting, setInternalSorting] = React.useState<SortingState>(
|
|
235
|
-
[]
|
|
236
|
-
);
|
|
237
|
-
const [internalColumnFilters, setInternalColumnFilters] =
|
|
238
|
-
React.useState<ColumnFiltersState>([]);
|
|
239
|
-
const [internalGlobalFilter, setInternalGlobalFilter] =
|
|
240
|
-
React.useState<string>("");
|
|
241
|
-
const [internalRowSelection, setInternalRowSelection] =
|
|
242
|
-
React.useState<RowSelectionState>({});
|
|
243
|
-
const [internalColumnOrder, setInternalColumnOrder] =
|
|
244
|
-
React.useState<ColumnOrderState>([]);
|
|
245
|
-
const [internalColumnPinning, setInternalColumnPinning] =
|
|
246
|
-
React.useState<ColumnPinningState>({});
|
|
247
|
-
const [internalRowPinning, setInternalRowPinning] =
|
|
248
|
-
React.useState<RowPinningState>({});
|
|
249
|
-
const [internalGrouping, setInternalGrouping] = React.useState<GroupingState>(
|
|
250
|
-
[]
|
|
251
|
-
);
|
|
252
|
-
const [internalExpanded, setInternalExpanded] = React.useState<ExpandedState>(
|
|
253
|
-
{}
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
// Use controlled state if provided, otherwise use internal state
|
|
257
|
-
const sorting = controlledSorting ?? internalSorting;
|
|
258
|
-
const columnFilters = controlledColumnFilters ?? internalColumnFilters;
|
|
259
|
-
const globalFilter = controlledGlobalFilter ?? internalGlobalFilter;
|
|
260
|
-
const rowSelection = controlledRowSelection ?? internalRowSelection;
|
|
261
|
-
const columnOrder = controlledColumnOrder ?? internalColumnOrder;
|
|
262
|
-
const columnPinning = controlledColumnPinning ?? internalColumnPinning;
|
|
263
|
-
const rowPinning = controlledRowPinning ?? internalRowPinning;
|
|
264
|
-
const grouping = controlledGrouping ?? internalGrouping;
|
|
265
|
-
const expanded = controlledExpanded ?? internalExpanded;
|
|
266
|
-
|
|
267
|
-
// State change handlers
|
|
268
|
-
const handleSortingChange = React.useCallback(
|
|
269
|
-
(updater: SortingState | ((old: SortingState) => SortingState)) => {
|
|
270
|
-
const newValue =
|
|
271
|
-
typeof updater === "function" ? updater(sorting) : updater;
|
|
272
|
-
if (onSortingChange) {
|
|
273
|
-
onSortingChange(newValue);
|
|
274
|
-
} else {
|
|
275
|
-
setInternalSorting(newValue);
|
|
276
|
-
}
|
|
277
|
-
},
|
|
278
|
-
[sorting, onSortingChange]
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
const handleColumnFiltersChange = React.useCallback(
|
|
282
|
-
(
|
|
283
|
-
updater:
|
|
284
|
-
| ColumnFiltersState
|
|
285
|
-
| ((old: ColumnFiltersState) => ColumnFiltersState)
|
|
286
|
-
) => {
|
|
287
|
-
const newValue =
|
|
288
|
-
typeof updater === "function" ? updater(columnFilters) : updater;
|
|
289
|
-
if (onColumnFiltersChange) {
|
|
290
|
-
onColumnFiltersChange(newValue);
|
|
291
|
-
} else {
|
|
292
|
-
setInternalColumnFilters(newValue);
|
|
293
|
-
}
|
|
294
|
-
},
|
|
295
|
-
[columnFilters, onColumnFiltersChange]
|
|
296
|
-
);
|
|
297
|
-
|
|
298
|
-
const handleGlobalFilterChange = React.useCallback(
|
|
299
|
-
(updater: string | ((old: string) => string)) => {
|
|
300
|
-
const newValue =
|
|
301
|
-
typeof updater === "function" ? updater(globalFilter) : updater;
|
|
302
|
-
if (onGlobalFilterChange) {
|
|
303
|
-
onGlobalFilterChange(newValue);
|
|
304
|
-
} else {
|
|
305
|
-
setInternalGlobalFilter(newValue);
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
[globalFilter, onGlobalFilterChange]
|
|
309
|
-
);
|
|
291
|
+
} = options;
|
|
310
292
|
|
|
311
|
-
const
|
|
312
|
-
(
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
)
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
} else {
|
|
322
|
-
setInternalRowSelection(newValue);
|
|
323
|
-
}
|
|
324
|
-
},
|
|
325
|
-
[rowSelection, onRowSelectionChange]
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
const handleColumnOrderChange = React.useCallback(
|
|
329
|
-
(
|
|
330
|
-
updater: ColumnOrderState | ((old: ColumnOrderState) => ColumnOrderState)
|
|
331
|
-
) => {
|
|
332
|
-
const newValue =
|
|
333
|
-
typeof updater === "function" ? updater(columnOrder) : updater;
|
|
334
|
-
if (onColumnOrderChange) {
|
|
335
|
-
onColumnOrderChange(newValue);
|
|
336
|
-
} else {
|
|
337
|
-
setInternalColumnOrder(newValue);
|
|
338
|
-
}
|
|
339
|
-
},
|
|
340
|
-
[columnOrder, onColumnOrderChange]
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
const handleColumnPinningChange = React.useCallback(
|
|
344
|
-
(
|
|
345
|
-
updater:
|
|
346
|
-
| ColumnPinningState
|
|
347
|
-
| ((old: ColumnPinningState) => ColumnPinningState)
|
|
348
|
-
) => {
|
|
349
|
-
const newValue =
|
|
350
|
-
typeof updater === "function" ? updater(columnPinning) : updater;
|
|
351
|
-
if (onColumnPinningChange) {
|
|
352
|
-
onColumnPinningChange(newValue);
|
|
353
|
-
} else {
|
|
354
|
-
setInternalColumnPinning(newValue);
|
|
355
|
-
}
|
|
356
|
-
},
|
|
357
|
-
[columnPinning, onColumnPinningChange]
|
|
293
|
+
const [paginationState, setPaginationState] =
|
|
294
|
+
useControllable<PaginationState>(
|
|
295
|
+
paginationProp,
|
|
296
|
+
{ pageIndex: 0, pageSize },
|
|
297
|
+
onPaginationChangeProp
|
|
298
|
+
);
|
|
299
|
+
const [sortingState, setSortingState] = useControllable<SortingState>(
|
|
300
|
+
sorting,
|
|
301
|
+
[],
|
|
302
|
+
onSortingChange
|
|
358
303
|
);
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
) => {
|
|
364
|
-
const newValue =
|
|
365
|
-
typeof updater === "function" ? updater(rowPinning) : updater;
|
|
366
|
-
if (onRowPinningChange) {
|
|
367
|
-
onRowPinningChange(newValue);
|
|
368
|
-
} else {
|
|
369
|
-
setInternalRowPinning(newValue);
|
|
370
|
-
}
|
|
371
|
-
},
|
|
372
|
-
[rowPinning, onRowPinningChange]
|
|
304
|
+
const [globalFilterState, setGlobalFilterState] = useControllable<string>(
|
|
305
|
+
globalFilter,
|
|
306
|
+
"",
|
|
307
|
+
onGlobalFilterChange
|
|
373
308
|
);
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
onGroupingChange(newValue);
|
|
381
|
-
} else {
|
|
382
|
-
setInternalGrouping(newValue);
|
|
383
|
-
}
|
|
384
|
-
},
|
|
385
|
-
[grouping, onGroupingChange]
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
const handleExpandedChange = React.useCallback(
|
|
389
|
-
(updater: ExpandedState | ((old: ExpandedState) => ExpandedState)) => {
|
|
390
|
-
const newValue =
|
|
391
|
-
typeof updater === "function" ? updater(expanded) : updater;
|
|
392
|
-
if (onExpandedChange) {
|
|
393
|
-
onExpandedChange(newValue);
|
|
394
|
-
} else {
|
|
395
|
-
setInternalExpanded(newValue);
|
|
396
|
-
}
|
|
397
|
-
},
|
|
398
|
-
[expanded, onExpandedChange]
|
|
309
|
+
const [rowSelectionState, setRowSelectionState] =
|
|
310
|
+
useControllable<RowSelectionState>(rowSelection, {}, onRowSelectionChange);
|
|
311
|
+
const [expandedState, setExpandedState] = useControllable<ExpandedState>(
|
|
312
|
+
expanded,
|
|
313
|
+
{},
|
|
314
|
+
onExpandedChange
|
|
399
315
|
);
|
|
400
316
|
|
|
401
317
|
// eslint-disable-next-line react-hooks/incompatible-library -- TanStack Table API is designed for React and works correctly with this pattern
|
|
402
|
-
|
|
318
|
+
return useReactTable<TData>({
|
|
403
319
|
data,
|
|
404
|
-
columns
|
|
320
|
+
columns,
|
|
405
321
|
getCoreRowModel: getCoreRowModel(),
|
|
322
|
+
...(getRowId && { getRowId }),
|
|
406
323
|
|
|
407
|
-
|
|
324
|
+
...((enablePagination || manualPagination) && {
|
|
325
|
+
onPaginationChange: setPaginationState,
|
|
326
|
+
}),
|
|
408
327
|
...(enablePagination && {
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
pageSize,
|
|
413
|
-
},
|
|
414
|
-
},
|
|
328
|
+
...(!manualPagination && {
|
|
329
|
+
getPaginationRowModel: getPaginationRowModel(),
|
|
330
|
+
}),
|
|
415
331
|
}),
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
onSortingChange: handleSortingChange,
|
|
332
|
+
...(manualPagination && {
|
|
333
|
+
manualPagination: true,
|
|
334
|
+
...(rowCount != null && { rowCount }),
|
|
335
|
+
...(pageCount != null && { pageCount }),
|
|
421
336
|
}),
|
|
422
337
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
338
|
+
...(enableSorting && {
|
|
339
|
+
...(!manualSorting && {
|
|
340
|
+
getSortedRowModel: getSortedRowModel(),
|
|
341
|
+
}),
|
|
342
|
+
onSortingChange: setSortingState,
|
|
343
|
+
...(manualSorting && { manualSorting: true }),
|
|
427
344
|
}),
|
|
428
345
|
|
|
429
|
-
// Global Filtering
|
|
430
346
|
...(enableGlobalFiltering && {
|
|
431
|
-
|
|
432
|
-
|
|
347
|
+
...(!manualFiltering && {
|
|
348
|
+
getFilteredRowModel: getFilteredRowModel(),
|
|
349
|
+
}),
|
|
350
|
+
...(manualFiltering && { manualFiltering: true }),
|
|
351
|
+
onGlobalFilterChange: setGlobalFilterState,
|
|
433
352
|
globalFilterFn: "includesString",
|
|
434
353
|
}),
|
|
435
354
|
|
|
436
|
-
// Row Selection
|
|
437
355
|
...(enableRowSelection && {
|
|
438
356
|
enableRowSelection: true,
|
|
439
|
-
onRowSelectionChange:
|
|
357
|
+
onRowSelectionChange: setRowSelectionState,
|
|
440
358
|
}),
|
|
441
359
|
|
|
442
|
-
// Column Ordering
|
|
443
|
-
...(enableColumnOrdering && {
|
|
444
|
-
onColumnOrderChange: handleColumnOrderChange,
|
|
445
|
-
}),
|
|
446
|
-
|
|
447
|
-
// Column Pinning
|
|
448
|
-
...(enableColumnPinning && {
|
|
449
|
-
onColumnPinningChange: handleColumnPinningChange,
|
|
450
|
-
}),
|
|
451
|
-
|
|
452
|
-
// Row Pinning
|
|
453
|
-
...(enableRowPinning && {
|
|
454
|
-
enableRowPinning: true,
|
|
455
|
-
onRowPinningChange: handleRowPinningChange,
|
|
456
|
-
keepPinnedRows: true,
|
|
457
|
-
}),
|
|
458
|
-
|
|
459
|
-
// Grouping
|
|
460
|
-
...(enableGrouping && {
|
|
461
|
-
getGroupedRowModel: getGroupedRowModel(),
|
|
462
|
-
getExpandedRowModel: getExpandedRowModel(),
|
|
463
|
-
onGroupingChange: handleGroupingChange,
|
|
464
|
-
onExpandedChange: handleExpandedChange,
|
|
465
|
-
}),
|
|
466
|
-
|
|
467
|
-
// Expanding
|
|
468
360
|
...(enableExpanding && {
|
|
469
361
|
getExpandedRowModel: getExpandedRowModel(),
|
|
470
|
-
onExpandedChange:
|
|
362
|
+
onExpandedChange: setExpandedState,
|
|
471
363
|
getSubRows,
|
|
472
364
|
}),
|
|
473
365
|
|
|
474
|
-
// Faceting (for filter dropdowns)
|
|
475
|
-
...(enableFaceting && {
|
|
476
|
-
getFacetedRowModel: getFacetedRowModel(),
|
|
477
|
-
getFacetedUniqueValues: getFacetedUniqueValues(),
|
|
478
|
-
getFacetedMinMaxValues: getFacetedMinMaxValues(),
|
|
479
|
-
}),
|
|
480
|
-
|
|
481
|
-
// State
|
|
482
366
|
state: {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
globalFilter,
|
|
486
|
-
rowSelection,
|
|
487
|
-
|
|
488
|
-
columnPinning,
|
|
489
|
-
rowPinning,
|
|
490
|
-
grouping,
|
|
491
|
-
expanded,
|
|
367
|
+
pagination: paginationState,
|
|
368
|
+
sorting: sortingState,
|
|
369
|
+
globalFilter: globalFilterState,
|
|
370
|
+
rowSelection: rowSelectionState,
|
|
371
|
+
expanded: expandedState,
|
|
492
372
|
},
|
|
493
373
|
});
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/* -------------------------------------------------------------------------------------------------
|
|
377
|
+
* DataTable (renderer)
|
|
378
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Props for `DataTable`.
|
|
382
|
+
*
|
|
383
|
+
* `DataTable` is the generic rendering layer for a DataTable. It takes an
|
|
384
|
+
* already-constructed TanStack Table instance (typically from
|
|
385
|
+
* `useDataTable`) and renders the primitives. It is purposefully
|
|
386
|
+
* rendering-focused — wire toolbars, pagination, and search components
|
|
387
|
+
* outside of it via composition.
|
|
388
|
+
*/
|
|
389
|
+
export interface DataTableProps<TData> {
|
|
390
|
+
/** TanStack Table instance (usually returned from `useDataTable`). */
|
|
391
|
+
table: TanStackTable<TData>;
|
|
392
|
+
/** Data hook for automated testing (REQUIRED). */
|
|
393
|
+
dataHook: string;
|
|
394
|
+
/** Analytics element identifier. */
|
|
395
|
+
trackingEl?: string;
|
|
396
|
+
/** Analytics label. */
|
|
397
|
+
trackingLabel?: string;
|
|
398
|
+
/** Show skeleton rows instead of data while loading async data. */
|
|
399
|
+
isLoading?: boolean;
|
|
400
|
+
/** Number of skeleton rows when loading. Falls back to the table's page size, then 10. */
|
|
401
|
+
skeletonRowCount?: number;
|
|
402
|
+
/** Empty state message. @default "No results." */
|
|
403
|
+
noResultsMessage?: string;
|
|
404
|
+
/**
|
|
405
|
+
* Pin the `<thead>` to the top of the scroll container so column headers
|
|
406
|
+
* remain visible while scrolling. The header receives an opaque background
|
|
407
|
+
* (`bg-background`) to separate it from body rows.
|
|
408
|
+
* @default false
|
|
409
|
+
*/
|
|
410
|
+
stickyHeader?: boolean;
|
|
411
|
+
/** Additional class name for the outer wrapper. */
|
|
412
|
+
className?: string;
|
|
413
|
+
/**
|
|
414
|
+
* Optional footer rendered inside the `<table>` element after `<tbody>`.
|
|
415
|
+
* Use for summary rows or totals. Pass a `<TableFooter>` with rows.
|
|
416
|
+
*/
|
|
417
|
+
footer?: React.ReactNode;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Generic DataTable renderer.
|
|
422
|
+
*
|
|
423
|
+
* Renders header groups, body rows, loading skeletons, and empty state using
|
|
424
|
+
* the table primitives. Keep it focused on rendering — toolbars, search,
|
|
425
|
+
* pagination, etc. are composed around it.
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* const table = useDataTable({ columns, data, enableSorting: true });
|
|
429
|
+
* return <DataTable table={table} dataHook="my-table" />;
|
|
430
|
+
*/
|
|
431
|
+
export function DataTable<TData>({
|
|
432
|
+
table,
|
|
433
|
+
dataHook,
|
|
434
|
+
trackingEl,
|
|
435
|
+
trackingLabel,
|
|
436
|
+
isLoading = false,
|
|
437
|
+
skeletonRowCount,
|
|
438
|
+
noResultsMessage = "No results.",
|
|
439
|
+
stickyHeader = false,
|
|
440
|
+
className,
|
|
441
|
+
footer,
|
|
442
|
+
}: DataTableProps<TData>) {
|
|
443
|
+
const columnCount = table.getVisibleLeafColumns().length;
|
|
444
|
+
const resolvedSkeletonCount =
|
|
445
|
+
skeletonRowCount ?? table.getState().pagination.pageSize ?? 10;
|
|
494
446
|
|
|
495
447
|
return (
|
|
496
448
|
<div
|
|
497
|
-
|
|
498
|
-
className={className}
|
|
449
|
+
aria-busy={isLoading || undefined}
|
|
450
|
+
className={cn(className)}
|
|
499
451
|
data-hook={dataHook}
|
|
500
|
-
data-slot="data-table"
|
|
452
|
+
data-slot="data-table-view"
|
|
501
453
|
data-tracking-el={trackingEl}
|
|
502
454
|
data-tracking-label={trackingLabel}
|
|
503
455
|
>
|
|
504
|
-
{renderToolbar && renderToolbar(table)}
|
|
505
|
-
|
|
506
456
|
<Table dataHook={`${dataHook}-table`}>
|
|
507
|
-
<TableHeader
|
|
457
|
+
<TableHeader
|
|
458
|
+
className={cn(stickyHeader && "bg-background sticky top-0 z-10")}
|
|
459
|
+
>
|
|
508
460
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
509
461
|
<TableRow key={headerGroup.id}>
|
|
510
|
-
{headerGroup.headers.map((header) =>
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
462
|
+
{headerGroup.headers.map((header) => {
|
|
463
|
+
const sortDirection = header.column.getCanSort()
|
|
464
|
+
? header.column.getIsSorted()
|
|
465
|
+
: false;
|
|
466
|
+
const ariaSort =
|
|
467
|
+
sortDirection === "asc"
|
|
468
|
+
? "ascending"
|
|
469
|
+
: sortDirection === "desc"
|
|
470
|
+
? "descending"
|
|
471
|
+
: header.column.getCanSort()
|
|
472
|
+
? "none"
|
|
473
|
+
: undefined;
|
|
474
|
+
return (
|
|
475
|
+
<TableHead
|
|
476
|
+
key={header.id}
|
|
477
|
+
aria-sort={ariaSort}
|
|
478
|
+
colSpan={header.colSpan}
|
|
479
|
+
>
|
|
480
|
+
{header.isPlaceholder
|
|
481
|
+
? null
|
|
482
|
+
: flexRender(
|
|
483
|
+
header.column.columnDef.header,
|
|
484
|
+
header.getContext()
|
|
485
|
+
)}
|
|
486
|
+
</TableHead>
|
|
487
|
+
);
|
|
488
|
+
})}
|
|
520
489
|
</TableRow>
|
|
521
490
|
))}
|
|
522
491
|
</TableHeader>
|
|
523
492
|
<TableBody>
|
|
524
|
-
{/* Loading skeleton rows */}
|
|
525
493
|
{isLoading ? (
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
494
|
+
<>
|
|
495
|
+
<LoadingRows
|
|
496
|
+
colCount={columnCount}
|
|
497
|
+
rowCount={resolvedSkeletonCount}
|
|
498
|
+
/>
|
|
499
|
+
<TableRow className="sr-only">
|
|
500
|
+
<TableCell colSpan={columnCount}>
|
|
501
|
+
<div aria-live="polite" role="status">
|
|
502
|
+
Loading data…
|
|
503
|
+
</div>
|
|
504
|
+
</TableCell>
|
|
505
|
+
</TableRow>
|
|
506
|
+
</>
|
|
537
507
|
) : (
|
|
538
508
|
<>
|
|
539
|
-
{
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
<
|
|
543
|
-
key={row.id}
|
|
544
|
-
data-state={row.getIsSelected() && "selected"}
|
|
545
|
-
>
|
|
546
|
-
{row.getVisibleCells().map((cell) => (
|
|
547
|
-
<TableCell key={cell.id}>
|
|
548
|
-
{flexRender(
|
|
549
|
-
cell.column.columnDef.cell,
|
|
550
|
-
cell.getContext()
|
|
551
|
-
)}
|
|
552
|
-
</TableCell>
|
|
553
|
-
))}
|
|
554
|
-
</TableRow>
|
|
555
|
-
))}
|
|
556
|
-
|
|
557
|
-
{/* Regular rows */}
|
|
558
|
-
{table.getRowModel().rows?.length ? (
|
|
559
|
-
table.getRowModel().rows.map((row) => (
|
|
560
|
-
<TableRow
|
|
561
|
-
key={row.id}
|
|
562
|
-
data-state={row.getIsSelected() && "selected"}
|
|
563
|
-
>
|
|
564
|
-
{row.getVisibleCells().map((cell) => (
|
|
565
|
-
<TableCell key={cell.id}>
|
|
566
|
-
{flexRender(
|
|
567
|
-
cell.column.columnDef.cell,
|
|
568
|
-
cell.getContext()
|
|
569
|
-
)}
|
|
570
|
-
</TableCell>
|
|
571
|
-
))}
|
|
572
|
-
</TableRow>
|
|
573
|
-
))
|
|
509
|
+
{table.getRowModel().rows.length > 0 ? (
|
|
510
|
+
table
|
|
511
|
+
.getRowModel()
|
|
512
|
+
.rows.map((row) => <DataTableRow key={row.id} row={row} />)
|
|
574
513
|
) : (
|
|
575
514
|
<TableRow>
|
|
576
|
-
<TableCell colSpan={
|
|
577
|
-
|
|
515
|
+
<TableCell colSpan={columnCount}>
|
|
516
|
+
<div aria-live="polite" role="status">
|
|
517
|
+
{noResultsMessage}
|
|
518
|
+
</div>
|
|
578
519
|
</TableCell>
|
|
579
520
|
</TableRow>
|
|
580
521
|
)}
|
|
522
|
+
</>
|
|
523
|
+
)}
|
|
524
|
+
</TableBody>
|
|
525
|
+
{footer}
|
|
526
|
+
</Table>
|
|
527
|
+
</div>
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
DataTable.displayName = "DataTable";
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Internal: render a single data (or pinned) row.
|
|
535
|
+
* Kept internal because pinned rows share the same visual contract as data rows.
|
|
536
|
+
*/
|
|
537
|
+
function DataTableRow<TData>({ row }: { row: Row<TData> }) {
|
|
538
|
+
return (
|
|
539
|
+
<TableRow data-state={row.getIsSelected() ? "selected" : undefined}>
|
|
540
|
+
{row.getVisibleCells().map((cell) => (
|
|
541
|
+
<TableCell key={cell.id}>
|
|
542
|
+
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
543
|
+
</TableCell>
|
|
544
|
+
))}
|
|
545
|
+
</TableRow>
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Internal: render `rowCount` skeleton rows as a loading placeholder.
|
|
551
|
+
*/
|
|
552
|
+
function LoadingRows({
|
|
553
|
+
rowCount,
|
|
554
|
+
colCount,
|
|
555
|
+
}: {
|
|
556
|
+
rowCount: number;
|
|
557
|
+
colCount: number;
|
|
558
|
+
}) {
|
|
559
|
+
return (
|
|
560
|
+
<>
|
|
561
|
+
{Array.from({ length: rowCount }).map((_, rowIndex) => (
|
|
562
|
+
<TableRow key={`skeleton-${rowIndex}`}>
|
|
563
|
+
{Array.from({ length: colCount }).map((_, colIndex) => (
|
|
564
|
+
<TableCell key={`skeleton-${rowIndex}-${colIndex}`}>
|
|
565
|
+
<Skeleton className="h-4 w-full" />
|
|
566
|
+
</TableCell>
|
|
567
|
+
))}
|
|
568
|
+
</TableRow>
|
|
569
|
+
))}
|
|
570
|
+
</>
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/* -------------------------------------------------------------------------------------------------
|
|
575
|
+
* VirtualizedDataTable (virtualized renderer)
|
|
576
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
581
577
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
578
|
+
/**
|
|
579
|
+
* Props for `VirtualizedDataTable`.
|
|
580
|
+
*
|
|
581
|
+
* Same contract as `DataTable` but renders only the visible rows using
|
|
582
|
+
* `@tanstack/react-virtual`. The outer wrapper becomes the scroll container,
|
|
583
|
+
* so a bounded `height` is required.
|
|
584
|
+
*/
|
|
585
|
+
export interface VirtualizedDataTableProps<
|
|
586
|
+
TData,
|
|
587
|
+
> extends DataTableProps<TData> {
|
|
588
|
+
/**
|
|
589
|
+
* Height of the scroll container in pixels. Required — virtualization
|
|
590
|
+
* needs a bounded viewport. Pass a CSS value via `className` instead
|
|
591
|
+
* if you need responsive heights (e.g. `className="h-[80vh]"`), and
|
|
592
|
+
* omit this prop.
|
|
593
|
+
*/
|
|
594
|
+
height?: number;
|
|
595
|
+
/**
|
|
596
|
+
* Estimated height of a single row in pixels. The virtualizer uses this
|
|
597
|
+
* for initial layout before measuring real DOM nodes.
|
|
598
|
+
* @default 48
|
|
599
|
+
*/
|
|
600
|
+
estimateSize?: number;
|
|
601
|
+
/**
|
|
602
|
+
* Number of rows to render outside the visible viewport. Higher values
|
|
603
|
+
* reduce blank-flash on fast scrolling at the cost of more DOM nodes.
|
|
604
|
+
* @default 5
|
|
605
|
+
*/
|
|
606
|
+
overscan?: number;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Virtualized DataTable renderer for large datasets.
|
|
611
|
+
*
|
|
612
|
+
* Drop-in alternative to `DataTable` that renders only the rows visible
|
|
613
|
+
* in the scroll viewport (plus `overscan` extras). Uses
|
|
614
|
+
* `@tanstack/react-virtual` internally.
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* const table = useDataTable({
|
|
618
|
+
* columns,
|
|
619
|
+
* data: largeDataset, // 10 000+ rows
|
|
620
|
+
* enableSorting: true,
|
|
621
|
+
* manualPagination: true, // skip client pagination
|
|
622
|
+
* });
|
|
623
|
+
*
|
|
624
|
+
* return (
|
|
625
|
+
* <VirtualizedDataTable
|
|
626
|
+
* table={table}
|
|
627
|
+
* dataHook="big-table"
|
|
628
|
+
* height={600}
|
|
629
|
+
* estimateSize={48}
|
|
630
|
+
* />
|
|
631
|
+
* );
|
|
632
|
+
*/
|
|
633
|
+
export function VirtualizedDataTable<TData>({
|
|
634
|
+
table,
|
|
635
|
+
dataHook,
|
|
636
|
+
trackingEl,
|
|
637
|
+
trackingLabel,
|
|
638
|
+
isLoading = false,
|
|
639
|
+
skeletonRowCount,
|
|
640
|
+
noResultsMessage = "No results.",
|
|
641
|
+
stickyHeader = false,
|
|
642
|
+
className,
|
|
643
|
+
footer,
|
|
644
|
+
height,
|
|
645
|
+
estimateSize = 48,
|
|
646
|
+
overscan = 5,
|
|
647
|
+
}: VirtualizedDataTableProps<TData>) {
|
|
648
|
+
const columnCount = table.getVisibleLeafColumns().length;
|
|
649
|
+
const resolvedSkeletonCount =
|
|
650
|
+
skeletonRowCount ?? table.getState().pagination.pageSize ?? 10;
|
|
651
|
+
|
|
652
|
+
const scrollContainerRef = React.useRef<HTMLDivElement>(null);
|
|
653
|
+
|
|
654
|
+
const rows = table.getRowModel().rows;
|
|
655
|
+
|
|
656
|
+
// eslint-disable-next-line react-hooks/incompatible-library -- TanStack Virtual API is designed for React and works correctly with this pattern
|
|
657
|
+
const virtualizer = useVirtualizer({
|
|
658
|
+
count: rows.length,
|
|
659
|
+
getScrollElement: () => scrollContainerRef.current,
|
|
660
|
+
estimateSize: () => estimateSize,
|
|
661
|
+
overscan,
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
const virtualItems = virtualizer.getVirtualItems();
|
|
665
|
+
const totalSize = virtualizer.getTotalSize();
|
|
666
|
+
|
|
667
|
+
// Spacer heights push visible rows into position within <tbody>.
|
|
668
|
+
const paddingTop = virtualItems.length > 0 ? virtualItems[0]!.start : 0;
|
|
669
|
+
const paddingBottom =
|
|
670
|
+
virtualItems.length > 0
|
|
671
|
+
? totalSize - virtualItems[virtualItems.length - 1]!.end
|
|
672
|
+
: 0;
|
|
673
|
+
|
|
674
|
+
return (
|
|
675
|
+
<div
|
|
676
|
+
ref={scrollContainerRef}
|
|
677
|
+
aria-busy={isLoading || undefined}
|
|
678
|
+
className={cn("overflow-auto", className)}
|
|
679
|
+
data-hook={dataHook}
|
|
680
|
+
data-slot="data-table-view"
|
|
681
|
+
data-tracking-el={trackingEl}
|
|
682
|
+
data-tracking-label={trackingLabel}
|
|
683
|
+
style={height != null ? { height } : undefined}
|
|
684
|
+
>
|
|
685
|
+
<Table dataHook={`${dataHook}-table`}>
|
|
686
|
+
<TableHeader
|
|
687
|
+
className={cn(stickyHeader && "bg-background sticky top-0 z-10")}
|
|
688
|
+
>
|
|
689
|
+
{table.getHeaderGroups().map((headerGroup) => (
|
|
690
|
+
<TableRow key={headerGroup.id}>
|
|
691
|
+
{headerGroup.headers.map((header) => {
|
|
692
|
+
const sortDirection = header.column.getCanSort()
|
|
693
|
+
? header.column.getIsSorted()
|
|
694
|
+
: false;
|
|
695
|
+
const ariaSort =
|
|
696
|
+
sortDirection === "asc"
|
|
697
|
+
? "ascending"
|
|
698
|
+
: sortDirection === "desc"
|
|
699
|
+
? "descending"
|
|
700
|
+
: header.column.getCanSort()
|
|
701
|
+
? "none"
|
|
702
|
+
: undefined;
|
|
703
|
+
return (
|
|
704
|
+
<TableHead
|
|
705
|
+
key={header.id}
|
|
706
|
+
aria-sort={ariaSort}
|
|
707
|
+
colSpan={header.colSpan}
|
|
588
708
|
>
|
|
589
|
-
{
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
709
|
+
{header.isPlaceholder
|
|
710
|
+
? null
|
|
711
|
+
: flexRender(
|
|
712
|
+
header.column.columnDef.header,
|
|
713
|
+
header.getContext()
|
|
594
714
|
)}
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
715
|
+
</TableHead>
|
|
716
|
+
);
|
|
717
|
+
})}
|
|
718
|
+
</TableRow>
|
|
719
|
+
))}
|
|
720
|
+
</TableHeader>
|
|
721
|
+
<TableBody>
|
|
722
|
+
{isLoading ? (
|
|
723
|
+
<>
|
|
724
|
+
<LoadingRows
|
|
725
|
+
colCount={columnCount}
|
|
726
|
+
rowCount={resolvedSkeletonCount}
|
|
727
|
+
/>
|
|
728
|
+
<TableRow className="sr-only">
|
|
729
|
+
<TableCell colSpan={columnCount}>
|
|
730
|
+
<div aria-live="polite" role="status">
|
|
731
|
+
Loading data…
|
|
732
|
+
</div>
|
|
733
|
+
</TableCell>
|
|
734
|
+
</TableRow>
|
|
599
735
|
</>
|
|
736
|
+
) : rows.length > 0 ? (
|
|
737
|
+
<>
|
|
738
|
+
{paddingTop > 0 && (
|
|
739
|
+
<tr aria-hidden="true">
|
|
740
|
+
<td colSpan={columnCount} style={{ height: paddingTop }} />
|
|
741
|
+
</tr>
|
|
742
|
+
)}
|
|
743
|
+
{virtualItems.map((virtualItem) => {
|
|
744
|
+
const row = rows[virtualItem.index]!;
|
|
745
|
+
return <DataTableRow key={row.id} row={row} />;
|
|
746
|
+
})}
|
|
747
|
+
{paddingBottom > 0 && (
|
|
748
|
+
<tr aria-hidden="true">
|
|
749
|
+
<td colSpan={columnCount} style={{ height: paddingBottom }} />
|
|
750
|
+
</tr>
|
|
751
|
+
)}
|
|
752
|
+
</>
|
|
753
|
+
) : (
|
|
754
|
+
<TableRow>
|
|
755
|
+
<TableCell colSpan={columnCount}>
|
|
756
|
+
<div aria-live="polite" role="status">
|
|
757
|
+
{noResultsMessage}
|
|
758
|
+
</div>
|
|
759
|
+
</TableCell>
|
|
760
|
+
</TableRow>
|
|
600
761
|
)}
|
|
601
762
|
</TableBody>
|
|
602
|
-
{
|
|
763
|
+
{footer}
|
|
603
764
|
</Table>
|
|
604
|
-
|
|
605
|
-
{renderPagination && renderPagination(table)}
|
|
606
765
|
</div>
|
|
607
766
|
);
|
|
608
767
|
}
|
|
609
768
|
|
|
610
|
-
|
|
611
|
-
* DataTable component with forwardRef support for generic types
|
|
612
|
-
*/
|
|
613
|
-
const DataTable = React.forwardRef(DataTableInner) as <TData, TValue>(
|
|
614
|
-
props: DataTableProps<TData, TValue> & {
|
|
615
|
-
ref?: React.ForwardedRef<HTMLDivElement>;
|
|
616
|
-
}
|
|
617
|
-
) => React.ReactElement;
|
|
769
|
+
VirtualizedDataTable.displayName = "VirtualizedDataTable";
|
|
618
770
|
|
|
619
|
-
|
|
771
|
+
/* -------------------------------------------------------------------------------------------------
|
|
772
|
+
* DataTableColumnHeader (sortable header)
|
|
773
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
620
774
|
|
|
621
775
|
/**
|
|
622
|
-
* DataTableColumnHeader
|
|
623
|
-
* Matches Figma TableHead/Button variant
|
|
776
|
+
* Props for `DataTableColumnHeader` — a sortable column header with
|
|
777
|
+
* directional sort indicators. Matches the Figma TableHead/Button variant.
|
|
624
778
|
*/
|
|
625
|
-
interface DataTableColumnHeaderProps<
|
|
779
|
+
export interface DataTableColumnHeaderProps<
|
|
626
780
|
TData,
|
|
627
781
|
TValue,
|
|
628
782
|
> extends React.HTMLAttributes<HTMLDivElement> {
|
|
783
|
+
/** TanStack column instance (from the `header` render slot). */
|
|
629
784
|
column: Column<TData, TValue>;
|
|
785
|
+
/** Header label. */
|
|
630
786
|
title: string;
|
|
787
|
+
/** Data hook for automated testing. */
|
|
631
788
|
dataHook?: string;
|
|
632
|
-
/**
|
|
789
|
+
/** Analytics element identifier. */
|
|
633
790
|
trackingEl?: string;
|
|
634
|
-
/**
|
|
791
|
+
/** Analytics label. */
|
|
635
792
|
trackingLabel?: string;
|
|
636
|
-
/**
|
|
793
|
+
/** Alignment — should match the cell's alignment. @default "left" */
|
|
637
794
|
align?: "left" | "center" | "right";
|
|
638
795
|
}
|
|
639
796
|
|
|
640
|
-
|
|
797
|
+
/**
|
|
798
|
+
* Sortable column header with sort indicator icons.
|
|
799
|
+
*
|
|
800
|
+
* Falls back to a non-interactive label when the column is not sortable.
|
|
801
|
+
* Otherwise renders a ghost button that toggles sort on click, with an
|
|
802
|
+
* accessible label describing the current sort state.
|
|
803
|
+
*/
|
|
804
|
+
export function DataTableColumnHeader<TData, TValue>({
|
|
641
805
|
column,
|
|
642
806
|
title,
|
|
643
807
|
className,
|
|
@@ -685,10 +849,9 @@ function DataTableColumnHeader<TData, TValue>({
|
|
|
685
849
|
data-tracking-el={trackingEl}
|
|
686
850
|
data-tracking-label={trackingLabel}
|
|
687
851
|
>
|
|
688
|
-
{/* Override padding for inline header appearance - consider adding size="icon" variant */}
|
|
689
852
|
<Button
|
|
690
853
|
aria-label={sortLabel}
|
|
691
|
-
className=
|
|
854
|
+
className="text-muted-foreground h-auto p-0 text-sm font-medium"
|
|
692
855
|
dataHook={dataHook || `column-header-${column.id}`}
|
|
693
856
|
size="sm"
|
|
694
857
|
variant="ghost"
|
|
@@ -714,23 +877,32 @@ function DataTableColumnHeader<TData, TValue>({
|
|
|
714
877
|
|
|
715
878
|
DataTableColumnHeader.displayName = "DataTableColumnHeader";
|
|
716
879
|
|
|
880
|
+
/* -------------------------------------------------------------------------------------------------
|
|
881
|
+
* DataTableRowActions (row ellipsis menu)
|
|
882
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
883
|
+
|
|
717
884
|
/**
|
|
718
|
-
* DataTableRowActions
|
|
719
|
-
* Matches Figma TableCell/Action variant design
|
|
885
|
+
* Props for `DataTableRowActions` — the standard row ellipsis actions menu.
|
|
720
886
|
*/
|
|
721
|
-
interface DataTableRowActionsProps<TData> {
|
|
887
|
+
export interface DataTableRowActionsProps<TData> {
|
|
888
|
+
/** TanStack row instance (from the `cell` render slot). */
|
|
722
889
|
row: Row<TData>;
|
|
890
|
+
/** Data hook for automated testing. */
|
|
723
891
|
dataHook?: string;
|
|
724
|
-
/**
|
|
892
|
+
/** Analytics element identifier. */
|
|
725
893
|
trackingEl?: string;
|
|
726
|
-
/**
|
|
894
|
+
/** Analytics label. */
|
|
727
895
|
trackingLabel?: string;
|
|
728
|
-
/** Accessible label for the actions
|
|
896
|
+
/** Accessible label for the trigger. @default "Open row actions menu" */
|
|
729
897
|
ariaLabel?: string;
|
|
898
|
+
/** `DropdownMenuItem`s to render inside the menu. */
|
|
730
899
|
children?: React.ReactNode;
|
|
731
900
|
}
|
|
732
901
|
|
|
733
|
-
|
|
902
|
+
/**
|
|
903
|
+
* Row-level ellipsis dropdown menu. Pass `DropdownMenuItem`s as children.
|
|
904
|
+
*/
|
|
905
|
+
export function DataTableRowActions<TData>({
|
|
734
906
|
row,
|
|
735
907
|
dataHook,
|
|
736
908
|
trackingEl,
|
|
@@ -752,7 +924,10 @@ function DataTableRowActions<TData>({
|
|
|
752
924
|
<Ellipsis />
|
|
753
925
|
</Button>
|
|
754
926
|
</DropdownMenuTrigger>
|
|
755
|
-
<DropdownMenuContent
|
|
927
|
+
<DropdownMenuContent
|
|
928
|
+
align="end"
|
|
929
|
+
dataHook={dataHook ? `${dataHook}-menu` : `row-actions-${row.id}-menu`}
|
|
930
|
+
>
|
|
756
931
|
{children}
|
|
757
932
|
</DropdownMenuContent>
|
|
758
933
|
</DropdownMenu>
|
|
@@ -761,56 +936,70 @@ function DataTableRowActions<TData>({
|
|
|
761
936
|
|
|
762
937
|
DataTableRowActions.displayName = "DataTableRowActions";
|
|
763
938
|
|
|
939
|
+
/* -------------------------------------------------------------------------------------------------
|
|
940
|
+
* DataTableToolbar (layout container)
|
|
941
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
942
|
+
|
|
764
943
|
/**
|
|
765
|
-
*
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
*
|
|
782
|
-
* <DataTablePagination table={table} />
|
|
783
|
-
* </DataTableToolbar.Right>
|
|
784
|
-
* </DataTableToolbar>
|
|
944
|
+
* Props for the root `DataTableToolbar` container.
|
|
945
|
+
*/
|
|
946
|
+
export interface DataTableToolbarProps {
|
|
947
|
+
/** Data hook for automated testing. */
|
|
948
|
+
dataHook?: string;
|
|
949
|
+
/** Analytics element identifier. */
|
|
950
|
+
trackingEl?: string;
|
|
951
|
+
/** Analytics label. */
|
|
952
|
+
trackingLabel?: string;
|
|
953
|
+
/** Additional class name. */
|
|
954
|
+
className?: string;
|
|
955
|
+
/** Toolbar content — usually `<DataTableToolbarLeft>` / `<DataTableToolbarRight>`. */
|
|
956
|
+
children?: React.ReactNode;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/**
|
|
960
|
+
* Props for `DataTableToolbarLeft`.
|
|
785
961
|
*/
|
|
786
|
-
interface
|
|
787
|
-
/** Data hook for automated testing */
|
|
962
|
+
export interface DataTableToolbarLeftProps {
|
|
963
|
+
/** Data hook for automated testing. */
|
|
788
964
|
dataHook?: string;
|
|
789
|
-
/**
|
|
965
|
+
/** Analytics element identifier. */
|
|
790
966
|
trackingEl?: string;
|
|
791
|
-
/**
|
|
967
|
+
/** Analytics label. */
|
|
792
968
|
trackingLabel?: string;
|
|
793
|
-
/** Additional class name
|
|
969
|
+
/** Additional class name. */
|
|
794
970
|
className?: string;
|
|
795
|
-
/** Content to render
|
|
971
|
+
/** Content to render on the left side. */
|
|
796
972
|
children?: React.ReactNode;
|
|
797
973
|
}
|
|
798
974
|
|
|
799
|
-
|
|
975
|
+
/**
|
|
976
|
+
* Props for `DataTableToolbarRight`.
|
|
977
|
+
*/
|
|
978
|
+
export interface DataTableToolbarRightProps {
|
|
979
|
+
/** Data hook for automated testing. */
|
|
980
|
+
dataHook?: string;
|
|
981
|
+
/** Analytics element identifier. */
|
|
982
|
+
trackingEl?: string;
|
|
983
|
+
/** Analytics label. */
|
|
984
|
+
trackingLabel?: string;
|
|
985
|
+
/** Additional class name. */
|
|
986
|
+
className?: string;
|
|
987
|
+
/** Content to render on the right side. */
|
|
988
|
+
children?: React.ReactNode;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
export function DataTableToolbarLeft({
|
|
800
992
|
dataHook,
|
|
801
993
|
trackingEl,
|
|
802
994
|
trackingLabel,
|
|
803
995
|
className,
|
|
804
996
|
children,
|
|
805
|
-
}:
|
|
997
|
+
}: DataTableToolbarLeftProps) {
|
|
806
998
|
return (
|
|
807
999
|
<div
|
|
808
|
-
className={cn(
|
|
809
|
-
"flex flex-wrap items-center justify-between gap-2 py-4",
|
|
810
|
-
className
|
|
811
|
-
)}
|
|
1000
|
+
className={cn("flex items-center gap-2", className)}
|
|
812
1001
|
data-hook={dataHook}
|
|
813
|
-
data-slot="data-table-toolbar"
|
|
1002
|
+
data-slot="data-table-toolbar-left"
|
|
814
1003
|
data-tracking-el={trackingEl}
|
|
815
1004
|
data-tracking-label={trackingLabel}
|
|
816
1005
|
>
|
|
@@ -819,35 +1008,20 @@ function DataTableToolbar({
|
|
|
819
1008
|
);
|
|
820
1009
|
}
|
|
821
1010
|
|
|
822
|
-
|
|
823
|
-
* DataTableToolbar.Left - Left section of the toolbar
|
|
824
|
-
* Use for search, filters, and other primary actions
|
|
825
|
-
*/
|
|
826
|
-
interface DataTableToolbarLeftProps {
|
|
827
|
-
/** Data hook for automated testing */
|
|
828
|
-
dataHook?: string;
|
|
829
|
-
/** Tracking element identifier for analytics */
|
|
830
|
-
trackingEl?: string;
|
|
831
|
-
/** Tracking label for analytics context */
|
|
832
|
-
trackingLabel?: string;
|
|
833
|
-
/** Additional class name for styling */
|
|
834
|
-
className?: string;
|
|
835
|
-
/** Content to render on the left side */
|
|
836
|
-
children?: React.ReactNode;
|
|
837
|
-
}
|
|
1011
|
+
DataTableToolbarLeft.displayName = "DataTableToolbarLeft";
|
|
838
1012
|
|
|
839
|
-
function
|
|
1013
|
+
export function DataTableToolbarRight({
|
|
840
1014
|
dataHook,
|
|
841
1015
|
trackingEl,
|
|
842
1016
|
trackingLabel,
|
|
843
1017
|
className,
|
|
844
1018
|
children,
|
|
845
|
-
}:
|
|
1019
|
+
}: DataTableToolbarRightProps) {
|
|
846
1020
|
return (
|
|
847
1021
|
<div
|
|
848
1022
|
className={cn("flex items-center gap-2", className)}
|
|
849
1023
|
data-hook={dataHook}
|
|
850
|
-
data-slot="data-table-toolbar-
|
|
1024
|
+
data-slot="data-table-toolbar-right"
|
|
851
1025
|
data-tracking-el={trackingEl}
|
|
852
1026
|
data-tracking-label={trackingLabel}
|
|
853
1027
|
>
|
|
@@ -856,37 +1030,36 @@ function DataTableToolbarLeft({
|
|
|
856
1030
|
);
|
|
857
1031
|
}
|
|
858
1032
|
|
|
859
|
-
|
|
1033
|
+
DataTableToolbarRight.displayName = "DataTableToolbarRight";
|
|
860
1034
|
|
|
861
1035
|
/**
|
|
862
|
-
*
|
|
863
|
-
*
|
|
1036
|
+
* Flexible two-column toolbar for composing DataTable UIs.
|
|
1037
|
+
*
|
|
1038
|
+
* @example
|
|
1039
|
+
* <DataTableToolbar dataHook="toolbar">
|
|
1040
|
+
* <DataTableToolbarLeft>
|
|
1041
|
+
* <DataTableSearch table={table} dataHook="search" />
|
|
1042
|
+
* </DataTableToolbarLeft>
|
|
1043
|
+
* <DataTableToolbarRight>
|
|
1044
|
+
* <DataTablePagination table={table} dataHook="pagination" />
|
|
1045
|
+
* </DataTableToolbarRight>
|
|
1046
|
+
* </DataTableToolbar>
|
|
864
1047
|
*/
|
|
865
|
-
|
|
866
|
-
/** Data hook for automated testing */
|
|
867
|
-
dataHook?: string;
|
|
868
|
-
/** Tracking element identifier for analytics */
|
|
869
|
-
trackingEl?: string;
|
|
870
|
-
/** Tracking label for analytics context */
|
|
871
|
-
trackingLabel?: string;
|
|
872
|
-
/** Additional class name for styling */
|
|
873
|
-
className?: string;
|
|
874
|
-
/** Content to render on the right side */
|
|
875
|
-
children?: React.ReactNode;
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
function DataTableToolbarRight({
|
|
1048
|
+
export function DataTableToolbar({
|
|
879
1049
|
dataHook,
|
|
880
1050
|
trackingEl,
|
|
881
1051
|
trackingLabel,
|
|
882
1052
|
className,
|
|
883
1053
|
children,
|
|
884
|
-
}:
|
|
1054
|
+
}: DataTableToolbarProps) {
|
|
885
1055
|
return (
|
|
886
1056
|
<div
|
|
887
|
-
className={cn(
|
|
1057
|
+
className={cn(
|
|
1058
|
+
"flex flex-wrap items-center justify-between gap-2 py-4",
|
|
1059
|
+
className
|
|
1060
|
+
)}
|
|
888
1061
|
data-hook={dataHook}
|
|
889
|
-
data-slot="data-table-toolbar
|
|
1062
|
+
data-slot="data-table-toolbar"
|
|
890
1063
|
data-tracking-el={trackingEl}
|
|
891
1064
|
data-tracking-label={trackingLabel}
|
|
892
1065
|
>
|
|
@@ -895,106 +1068,111 @@ function DataTableToolbarRight({
|
|
|
895
1068
|
);
|
|
896
1069
|
}
|
|
897
1070
|
|
|
898
|
-
DataTableToolbarRight.displayName = "DataTableToolbar.Right";
|
|
899
|
-
|
|
900
|
-
// Attach sub-components to DataTableToolbar
|
|
901
|
-
DataTableToolbar.Left = DataTableToolbarLeft;
|
|
902
|
-
DataTableToolbar.Right = DataTableToolbarRight;
|
|
903
1071
|
DataTableToolbar.displayName = "DataTableToolbar";
|
|
904
1072
|
|
|
1073
|
+
/* -------------------------------------------------------------------------------------------------
|
|
1074
|
+
* DataTableSearch (filter input)
|
|
1075
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
1076
|
+
|
|
905
1077
|
/**
|
|
906
|
-
*
|
|
907
|
-
*
|
|
1078
|
+
* Props for `DataTableSearch`.
|
|
1079
|
+
*
|
|
1080
|
+
* Extends `InputGroup` props — pass `className`, `size`, `error`, etc. to
|
|
1081
|
+
* style the input. No custom styling is applied; appearance matches a
|
|
1082
|
+
* default `InputGroup`.
|
|
908
1083
|
*/
|
|
909
|
-
interface DataTableSearchProps<TData>
|
|
910
|
-
|
|
1084
|
+
export interface DataTableSearchProps<TData> extends Omit<
|
|
1085
|
+
React.ComponentPropsWithoutRef<typeof InputGroup>,
|
|
1086
|
+
"children" | "onChange"
|
|
1087
|
+
> {
|
|
1088
|
+
/** TanStack Table instance. */
|
|
911
1089
|
table: TanStackTable<TData>;
|
|
912
|
-
/**
|
|
913
|
-
dataHook: string;
|
|
914
|
-
/** Tracking element identifier for analytics */
|
|
915
|
-
trackingEl?: string;
|
|
916
|
-
/** Tracking label for analytics context */
|
|
917
|
-
trackingLabel?: string;
|
|
918
|
-
/** Accessible label for the search input (defaults to "Search table") */
|
|
1090
|
+
/** Accessible label for the input. @default "Search table" */
|
|
919
1091
|
ariaLabel?: string;
|
|
920
|
-
/**
|
|
1092
|
+
/** Input placeholder. @default "Search..." */
|
|
921
1093
|
placeholder?: string;
|
|
922
|
-
/** Column ID to filter. If not provided, uses global filter. */
|
|
923
|
-
column?: string;
|
|
924
|
-
/** Width of the search input (e.g., "180px", "200px") */
|
|
925
|
-
width?: string;
|
|
926
|
-
/** Additional class name for styling */
|
|
927
|
-
className?: string;
|
|
928
1094
|
}
|
|
929
1095
|
|
|
930
|
-
|
|
1096
|
+
/**
|
|
1097
|
+
* Search input for filtering DataTable rows.
|
|
1098
|
+
*
|
|
1099
|
+
* Composes `InputGroup` + `InputGroupInput` + `InputGroupAddon` with a
|
|
1100
|
+
* search icon. Inherits all `InputGroup` styling — pass `className` to
|
|
1101
|
+
* customize width or shape.
|
|
1102
|
+
*
|
|
1103
|
+
* Uses the table's global filter (requires `enableGlobalFiltering` on the hook).
|
|
1104
|
+
*/
|
|
1105
|
+
export function DataTableSearch<TData>({
|
|
931
1106
|
table,
|
|
932
|
-
dataHook,
|
|
933
|
-
trackingEl,
|
|
934
|
-
trackingLabel,
|
|
935
1107
|
ariaLabel = "Search table",
|
|
936
1108
|
placeholder = "Search...",
|
|
937
|
-
|
|
938
|
-
width,
|
|
939
|
-
className,
|
|
1109
|
+
...inputGroupProps
|
|
940
1110
|
}: DataTableSearchProps<TData>) {
|
|
941
|
-
const filterValue =
|
|
942
|
-
? ((table.getColumn(column)?.getFilterValue() as string) ?? "")
|
|
943
|
-
: (table.getState().globalFilter ?? "");
|
|
1111
|
+
const filterValue = (table.getState().globalFilter ?? "") as string;
|
|
944
1112
|
|
|
945
1113
|
const handleFilterChange = (value: string) => {
|
|
946
|
-
|
|
947
|
-
table.getColumn(column)?.setFilterValue(value);
|
|
948
|
-
} else {
|
|
949
|
-
table.setGlobalFilter(value);
|
|
950
|
-
}
|
|
1114
|
+
table.setGlobalFilter(value);
|
|
951
1115
|
};
|
|
952
1116
|
|
|
953
1117
|
return (
|
|
954
|
-
<
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
value={filterValue}
|
|
967
|
-
onChange={(event) => handleFilterChange(event.target.value)}
|
|
968
|
-
/>
|
|
969
|
-
<InputGroupAddon className="pr-3">
|
|
970
|
-
<Search />
|
|
971
|
-
</InputGroupAddon>
|
|
972
|
-
</InputGroup>
|
|
973
|
-
</div>
|
|
1118
|
+
<InputGroup {...inputGroupProps}>
|
|
1119
|
+
<InputGroupInput
|
|
1120
|
+
aria-label={ariaLabel}
|
|
1121
|
+
placeholder={placeholder}
|
|
1122
|
+
type="text"
|
|
1123
|
+
value={filterValue}
|
|
1124
|
+
onChange={(event) => handleFilterChange(event.target.value)}
|
|
1125
|
+
/>
|
|
1126
|
+
<InputGroupAddon className="pr-3">
|
|
1127
|
+
<Search />
|
|
1128
|
+
</InputGroupAddon>
|
|
1129
|
+
</InputGroup>
|
|
974
1130
|
);
|
|
975
1131
|
}
|
|
976
1132
|
|
|
977
1133
|
DataTableSearch.displayName = "DataTableSearch";
|
|
978
1134
|
|
|
1135
|
+
/* -------------------------------------------------------------------------------------------------
|
|
1136
|
+
* DataTablePagination (pagination controls)
|
|
1137
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
1138
|
+
|
|
979
1139
|
/**
|
|
980
|
-
*
|
|
981
|
-
*
|
|
1140
|
+
* Props for `DataTablePagination`.
|
|
1141
|
+
*
|
|
1142
|
+
* All pagination state is derived from the TanStack Table instance.
|
|
1143
|
+
* Configure pagination behavior on `useDataTable`:
|
|
1144
|
+
* - Client-side: `enablePagination: true`
|
|
1145
|
+
* - Server-side: `manualPagination: true` + `rowCount` (or `pageCount`)
|
|
1146
|
+
* + controlled `pagination` / `onPaginationChange`
|
|
982
1147
|
*/
|
|
983
|
-
interface DataTablePaginationProps<TData> {
|
|
1148
|
+
export interface DataTablePaginationProps<TData> {
|
|
1149
|
+
/** TanStack Table instance. */
|
|
984
1150
|
table: TanStackTable<TData>;
|
|
985
|
-
/** Data hook for automated testing (REQUIRED) */
|
|
1151
|
+
/** Data hook for automated testing (REQUIRED). */
|
|
986
1152
|
dataHook: string;
|
|
987
|
-
/**
|
|
1153
|
+
/** Analytics element identifier. */
|
|
988
1154
|
trackingEl?: string;
|
|
989
|
-
/**
|
|
1155
|
+
/** Analytics label. */
|
|
990
1156
|
trackingLabel?: string;
|
|
991
|
-
/** Additional class name
|
|
1157
|
+
/** Additional class name. */
|
|
992
1158
|
className?: string;
|
|
993
|
-
/** Accessible label for the
|
|
1159
|
+
/** Accessible label for the nav landmark. @default "Table pagination" */
|
|
994
1160
|
ariaLabel?: string;
|
|
995
1161
|
}
|
|
996
1162
|
|
|
997
|
-
|
|
1163
|
+
/**
|
|
1164
|
+
* Pagination controls with row count info for a DataTable.
|
|
1165
|
+
*
|
|
1166
|
+
* Displays "start–end of total" plus previous/next/page links. Derives all
|
|
1167
|
+
* state from the TanStack Table instance — uses `table.getRowCount()`,
|
|
1168
|
+
* `table.getPageCount()`, `table.getCanPreviousPage()`,
|
|
1169
|
+
* `table.getCanNextPage()`, and `table.setPageIndex()`.
|
|
1170
|
+
*
|
|
1171
|
+
* Works transparently in both client-side and server-side (manual) modes
|
|
1172
|
+
* because TanStack Table handles the distinction internally when
|
|
1173
|
+
* `manualPagination` + `rowCount` are set on the hook.
|
|
1174
|
+
*/
|
|
1175
|
+
export function DataTablePagination<TData>({
|
|
998
1176
|
table,
|
|
999
1177
|
dataHook,
|
|
1000
1178
|
trackingEl,
|
|
@@ -1002,14 +1180,14 @@ function DataTablePagination<TData>({
|
|
|
1002
1180
|
className,
|
|
1003
1181
|
ariaLabel = "Table pagination",
|
|
1004
1182
|
}: DataTablePaginationProps<TData>) {
|
|
1005
|
-
const
|
|
1183
|
+
const { pageIndex, pageSize } = table.getState().pagination;
|
|
1184
|
+
const totalRows = table.getRowCount();
|
|
1006
1185
|
const totalPages = table.getPageCount();
|
|
1007
|
-
const
|
|
1008
|
-
const
|
|
1186
|
+
const canPrev = table.getCanPreviousPage();
|
|
1187
|
+
const canNext = table.getCanNextPage();
|
|
1009
1188
|
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
const startRow = totalRows === 0 ? 0 : (currentPage - 1) * pageSize + 1;
|
|
1189
|
+
const currentPage = pageIndex + 1;
|
|
1190
|
+
const startRow = totalRows === 0 ? 0 : pageIndex * pageSize + 1;
|
|
1013
1191
|
const endRow = Math.min(currentPage * pageSize, totalRows);
|
|
1014
1192
|
|
|
1015
1193
|
const paginationItems = usePagination({
|
|
@@ -1041,7 +1219,7 @@ function DataTablePagination<TData>({
|
|
|
1041
1219
|
return (
|
|
1042
1220
|
<PaginationItem key="previous">
|
|
1043
1221
|
<PaginationPrevious
|
|
1044
|
-
disabled={!
|
|
1222
|
+
disabled={!canPrev}
|
|
1045
1223
|
onClick={() => table.previousPage()}
|
|
1046
1224
|
/>
|
|
1047
1225
|
</PaginationItem>
|
|
@@ -1051,7 +1229,7 @@ function DataTablePagination<TData>({
|
|
|
1051
1229
|
return (
|
|
1052
1230
|
<PaginationItem key="next">
|
|
1053
1231
|
<PaginationNext
|
|
1054
|
-
disabled={!
|
|
1232
|
+
disabled={!canNext}
|
|
1055
1233
|
onClick={() => table.nextPage()}
|
|
1056
1234
|
/>
|
|
1057
1235
|
</PaginationItem>
|
|
@@ -1082,20 +1260,30 @@ function DataTablePagination<TData>({
|
|
|
1082
1260
|
|
|
1083
1261
|
DataTablePagination.displayName = "DataTablePagination";
|
|
1084
1262
|
|
|
1263
|
+
/* -------------------------------------------------------------------------------------------------
|
|
1264
|
+
* DataTableSelectAllCheckbox / DataTableSelectRowCheckbox
|
|
1265
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
1266
|
+
|
|
1085
1267
|
/**
|
|
1086
|
-
* DataTableSelectAllCheckbox
|
|
1087
|
-
*
|
|
1268
|
+
* Props for `DataTableSelectAllCheckbox` — the header checkbox that selects
|
|
1269
|
+
* all rows on the current page.
|
|
1088
1270
|
*/
|
|
1089
|
-
interface DataTableSelectAllCheckboxProps<TData> {
|
|
1271
|
+
export interface DataTableSelectAllCheckboxProps<TData> {
|
|
1272
|
+
/** TanStack Table instance. */
|
|
1090
1273
|
table: TanStackTable<TData>;
|
|
1274
|
+
/** Data hook for automated testing. @default "select-all-checkbox" */
|
|
1091
1275
|
dataHook?: string;
|
|
1092
|
-
/**
|
|
1276
|
+
/** Analytics element identifier. */
|
|
1093
1277
|
trackingEl?: string;
|
|
1094
|
-
/**
|
|
1278
|
+
/** Analytics label. */
|
|
1095
1279
|
trackingLabel?: string;
|
|
1096
1280
|
}
|
|
1097
1281
|
|
|
1098
|
-
|
|
1282
|
+
/**
|
|
1283
|
+
* Header checkbox for selecting all visible page rows. Renders as
|
|
1284
|
+
* indeterminate when some (but not all) rows are selected.
|
|
1285
|
+
*/
|
|
1286
|
+
export function DataTableSelectAllCheckbox<TData>({
|
|
1099
1287
|
table,
|
|
1100
1288
|
dataHook,
|
|
1101
1289
|
trackingEl,
|
|
@@ -1119,19 +1307,23 @@ function DataTableSelectAllCheckbox<TData>({
|
|
|
1119
1307
|
DataTableSelectAllCheckbox.displayName = "DataTableSelectAllCheckbox";
|
|
1120
1308
|
|
|
1121
1309
|
/**
|
|
1122
|
-
*
|
|
1123
|
-
* Matches Figma TableCell/Checkbox variant
|
|
1310
|
+
* Props for `DataTableSelectRowCheckbox`.
|
|
1124
1311
|
*/
|
|
1125
|
-
interface DataTableSelectRowCheckboxProps<TData> {
|
|
1312
|
+
export interface DataTableSelectRowCheckboxProps<TData> {
|
|
1313
|
+
/** TanStack row instance. */
|
|
1126
1314
|
row: Row<TData>;
|
|
1315
|
+
/** Data hook for automated testing. @default `select-row-{row.id}` */
|
|
1127
1316
|
dataHook?: string;
|
|
1128
|
-
/**
|
|
1317
|
+
/** Analytics element identifier. */
|
|
1129
1318
|
trackingEl?: string;
|
|
1130
|
-
/**
|
|
1319
|
+
/** Analytics label. */
|
|
1131
1320
|
trackingLabel?: string;
|
|
1132
1321
|
}
|
|
1133
1322
|
|
|
1134
|
-
|
|
1323
|
+
/**
|
|
1324
|
+
* Row-level selection checkbox. Disabled when the row cannot be selected.
|
|
1325
|
+
*/
|
|
1326
|
+
export function DataTableSelectRowCheckbox<TData>({
|
|
1135
1327
|
row,
|
|
1136
1328
|
dataHook,
|
|
1137
1329
|
trackingEl,
|
|
@@ -1152,25 +1344,19 @@ function DataTableSelectRowCheckbox<TData>({
|
|
|
1152
1344
|
|
|
1153
1345
|
DataTableSelectRowCheckbox.displayName = "DataTableSelectRowCheckbox";
|
|
1154
1346
|
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
DataTablePagination,
|
|
1162
|
-
DataTableSelectAllCheckbox,
|
|
1163
|
-
DataTableSelectRowCheckbox,
|
|
1164
|
-
};
|
|
1347
|
+
/* -------------------------------------------------------------------------------------------------
|
|
1348
|
+
* Re-exported TanStack Table helpers
|
|
1349
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
1350
|
+
|
|
1351
|
+
export { flexRender, createColumnHelper };
|
|
1352
|
+
|
|
1165
1353
|
export type {
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
DataTableSelectAllCheckboxProps,
|
|
1175
|
-
DataTableSelectRowCheckboxProps,
|
|
1354
|
+
Column,
|
|
1355
|
+
ColumnDef,
|
|
1356
|
+
ExpandedState,
|
|
1357
|
+
PaginationState,
|
|
1358
|
+
Row,
|
|
1359
|
+
RowSelectionState,
|
|
1360
|
+
SortingState,
|
|
1361
|
+
TanStackTable,
|
|
1176
1362
|
};
|