@dashforge/tw 0.6.0-beta → 0.8.0-beta
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/CHANGELOG.md +451 -0
- package/THEME-AUDIT.md +175 -0
- package/dist/index.esm.js +2009 -87
- package/dist/src/components/Accordion/accordion.variants.d.ts.map +1 -1
- package/dist/src/components/Box/box.variants.d.ts.map +1 -1
- package/dist/src/components/DataGrid/DataGrid.d.ts +29 -0
- package/dist/src/components/DataGrid/DataGrid.d.ts.map +1 -0
- package/dist/src/components/DataGrid/dataGrid.types.d.ts +214 -0
- package/dist/src/components/DataGrid/dataGrid.types.d.ts.map +1 -0
- package/dist/src/components/DataGrid/dataGrid.variants.d.ts +222 -0
- package/dist/src/components/DataGrid/dataGrid.variants.d.ts.map +1 -0
- package/dist/src/components/DataGrid/filters/ColumnFilters.d.ts +23 -0
- package/dist/src/components/DataGrid/filters/ColumnFilters.d.ts.map +1 -0
- package/dist/src/components/DataGrid/reorder/useColumnReorder.d.ts +50 -0
- package/dist/src/components/DataGrid/reorder/useColumnReorder.d.ts.map +1 -0
- package/dist/src/components/DataGrid/resize/useColumnResize.d.ts +32 -0
- package/dist/src/components/DataGrid/resize/useColumnResize.d.ts.map +1 -0
- package/dist/src/components/DataGrid/visibility/ColumnVisibilityDialog.d.ts +33 -0
- package/dist/src/components/DataGrid/visibility/ColumnVisibilityDialog.d.ts.map +1 -0
- package/dist/src/components/Dialog/dialog.variants.d.ts.map +1 -1
- package/dist/src/components/Divider/divider.variants.d.ts.map +1 -1
- package/dist/src/components/Pagination/Pagination.d.ts.map +1 -1
- package/dist/src/components/Pagination/pagination.variants.d.ts.map +1 -1
- package/dist/src/components/Popover/popover.variants.d.ts.map +1 -1
- package/dist/src/components/Skeleton/skeleton.variants.d.ts.map +1 -1
- package/dist/src/components/Table/Table.d.ts.map +1 -1
- package/dist/src/components/Table/table.types.d.ts +77 -5
- package/dist/src/components/Table/table.types.d.ts.map +1 -1
- package/dist/src/components/Tabs/tabs.variants.d.ts.map +1 -1
- package/dist/src/components/Tooltip/tooltip.variants.d.ts.map +1 -1
- package/dist/src/components/Typography/typography.variants.d.ts.map +1 -1
- package/dist/src/components/_shared/data/getNestedValue.d.ts.map +1 -0
- package/dist/src/components/{Table/_internal → _shared/data}/useColumnAutoDetect.d.ts +1 -1
- package/dist/src/components/_shared/data/useColumnAutoDetect.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useControllableState.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useDebouncedValue.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useTableFilter.d.ts +36 -0
- package/dist/src/components/_shared/data/useTableFilter.d.ts.map +1 -0
- package/dist/src/components/{Table/_internal → _shared/data}/useTableSearch.d.ts +1 -1
- package/dist/src/components/_shared/data/useTableSearch.d.ts.map +1 -0
- package/dist/src/components/{Table/_internal → _shared/data}/useTableSelection.d.ts +1 -1
- package/dist/src/components/_shared/data/useTableSelection.d.ts.map +1 -0
- package/dist/src/components/{Table/_internal → _shared/data}/useTableSort.d.ts +1 -1
- package/dist/src/components/_shared/data/useTableSort.d.ts.map +1 -0
- package/dist/src/components/_shared/data/useVirtualizer.d.ts +107 -0
- package/dist/src/components/_shared/data/useVirtualizer.d.ts.map +1 -0
- package/dist/src/index.d.ts +6 -3
- package/dist/src/index.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/components/Accordion/accordion.variants.ts +5 -3
- package/src/components/Box/Box.test.tsx +32 -11
- package/src/components/Box/box.variants.ts +23 -4
- package/src/components/DataGrid/DataGrid.test.tsx +917 -0
- package/src/components/DataGrid/DataGrid.tsx +1199 -0
- package/src/components/DataGrid/dataGrid.types.ts +238 -0
- package/src/components/DataGrid/dataGrid.variants.ts +152 -0
- package/src/components/DataGrid/filters/ColumnFilters.tsx +432 -0
- package/src/components/DataGrid/reorder/useColumnReorder.test.ts +38 -0
- package/src/components/DataGrid/reorder/useColumnReorder.ts +144 -0
- package/src/components/DataGrid/resize/useColumnResize.ts +106 -0
- package/src/components/DataGrid/visibility/ColumnVisibilityDialog.tsx +200 -0
- package/src/components/Dialog/dialog.variants.ts +10 -6
- package/src/components/Divider/Divider.test.tsx +3 -1
- package/src/components/Divider/Divider.tsx +1 -1
- package/src/components/Divider/divider.variants.ts +2 -1
- package/src/components/Pagination/Pagination.tsx +3 -1
- package/src/components/Pagination/pagination.variants.ts +19 -13
- package/src/components/Popover/popover.variants.ts +10 -4
- package/src/components/Skeleton/skeleton.variants.ts +2 -1
- package/src/components/Table/Table.tsx +22 -8
- package/src/components/Table/table.types.ts +83 -5
- package/src/components/Tabs/tabs.variants.ts +19 -8
- package/src/components/Tooltip/tooltip.variants.ts +12 -2
- package/src/components/Typography/Typography.test.tsx +3 -1
- package/src/components/Typography/typography.variants.ts +6 -2
- package/src/components/{Table/_internal → _shared/data}/useColumnAutoDetect.test.ts +1 -1
- package/src/components/{Table/_internal → _shared/data}/useColumnAutoDetect.ts +1 -1
- package/src/components/_shared/data/useTableFilter.test.ts +166 -0
- package/src/components/_shared/data/useTableFilter.ts +118 -0
- package/src/components/{Table/_internal → _shared/data}/useTableSearch.test.ts +1 -1
- package/src/components/{Table/_internal → _shared/data}/useTableSearch.ts +1 -1
- package/src/components/{Table/_internal → _shared/data}/useTableSelection.ts +1 -1
- package/src/components/{Table/_internal → _shared/data}/useTableSort.test.ts +1 -1
- package/src/components/{Table/_internal → _shared/data}/useTableSort.ts +1 -1
- package/src/components/_shared/data/useVirtualizer.test.ts +158 -0
- package/src/components/_shared/data/useVirtualizer.ts +199 -0
- package/src/components/_shared/themeIdentity.test.ts +113 -0
- package/src/index.ts +20 -2
- package/dist/src/components/Table/_internal/getNestedValue.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useColumnAutoDetect.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useControllableState.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useDebouncedValue.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useTableFilter.d.ts +0 -13
- package/dist/src/components/Table/_internal/useTableFilter.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useTableSearch.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useTableSelection.d.ts.map +0 -1
- package/dist/src/components/Table/_internal/useTableSort.d.ts.map +0 -1
- package/src/components/Table/_internal/themeIdentity.test.ts +0 -84
- package/src/components/Table/_internal/useTableFilter.ts +0 -37
- /package/dist/src/components/{Table/_internal → _shared/data}/getNestedValue.d.ts +0 -0
- /package/dist/src/components/{Table/_internal → _shared/data}/useControllableState.d.ts +0 -0
- /package/dist/src/components/{Table/_internal → _shared/data}/useDebouncedValue.d.ts +0 -0
- /package/src/components/{Table/_internal → _shared/data}/getNestedValue.test.ts +0 -0
- /package/src/components/{Table/_internal → _shared/data}/getNestedValue.ts +0 -0
- /package/src/components/{Table/_internal → _shared/data}/useControllableState.ts +0 -0
- /package/src/components/{Table/_internal → _shared/data}/useDebouncedValue.ts +0 -0
- /package/src/components/{Table/_internal → _shared/data}/useTableSelection.test.ts +0 -0
|
@@ -0,0 +1,1199 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useMemo,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
type MouseEvent,
|
|
5
|
+
type PointerEvent as RPointerEvent,
|
|
6
|
+
} from 'react';
|
|
7
|
+
import { cn } from '../../utils/cn.js';
|
|
8
|
+
import { useAccessState } from '../../hooks/useAccessState.js';
|
|
9
|
+
import { Skeleton } from '../Skeleton/Skeleton.js';
|
|
10
|
+
import { Pagination } from '../Pagination/Pagination.js';
|
|
11
|
+
import { dataGridVariants } from './dataGrid.variants.js';
|
|
12
|
+
import { useControllableState } from '../_shared/data/useControllableState.js';
|
|
13
|
+
import { useDebouncedValue } from '../_shared/data/useDebouncedValue.js';
|
|
14
|
+
import { useTableSearch } from '../_shared/data/useTableSearch.js';
|
|
15
|
+
import { useTableFilter } from '../_shared/data/useTableFilter.js';
|
|
16
|
+
import { useTableSort, cycleSortFor } from '../_shared/data/useTableSort.js';
|
|
17
|
+
import { useTableSelection } from '../_shared/data/useTableSelection.js';
|
|
18
|
+
import { getNestedValue } from '../_shared/data/getNestedValue.js';
|
|
19
|
+
import { useVirtualizer } from '../_shared/data/useVirtualizer.js';
|
|
20
|
+
import {
|
|
21
|
+
useColumnAutoDetect,
|
|
22
|
+
resolveAlign,
|
|
23
|
+
resolveMonospace,
|
|
24
|
+
resolveTabularNums,
|
|
25
|
+
} from '../_shared/data/useColumnAutoDetect.js';
|
|
26
|
+
import type {
|
|
27
|
+
TableColumn,
|
|
28
|
+
TableColumnInferredType,
|
|
29
|
+
TableFilterType,
|
|
30
|
+
TableSortModel,
|
|
31
|
+
TableFilterModel,
|
|
32
|
+
TableLabels,
|
|
33
|
+
} from '../Table/table.types.js';
|
|
34
|
+
import type { DataGridProps } from './dataGrid.types.js';
|
|
35
|
+
import { ColumnFilterTrigger } from './filters/ColumnFilters.js';
|
|
36
|
+
import { ColumnVisibilityTrigger } from './visibility/ColumnVisibilityDialog.js';
|
|
37
|
+
import { useColumnResize } from './resize/useColumnResize.js';
|
|
38
|
+
import {
|
|
39
|
+
useColumnReorder,
|
|
40
|
+
type ColumnReorderHandlers,
|
|
41
|
+
} from './reorder/useColumnReorder.js';
|
|
42
|
+
|
|
43
|
+
const DEFAULT_LABELS: Required<TableLabels> = {
|
|
44
|
+
searchPlaceholder: 'Search...',
|
|
45
|
+
noData: 'No data',
|
|
46
|
+
loading: 'Loading…',
|
|
47
|
+
ariaSortNone: 'Not sorted. Click to sort ascending.',
|
|
48
|
+
ariaSortAscending: 'Sorted ascending. Click to sort descending.',
|
|
49
|
+
ariaSortDescending: 'Sorted descending. Click to clear sort.',
|
|
50
|
+
ariaSelectRow: 'Select row',
|
|
51
|
+
ariaSelectAllRows: 'Select all rows',
|
|
52
|
+
ariaExpandRow: 'Expand row',
|
|
53
|
+
ariaCollapseRow: 'Collapse row',
|
|
54
|
+
selectedCount: '{count} selected',
|
|
55
|
+
clearSelection: 'Clear selection',
|
|
56
|
+
filterColumn: 'Filter',
|
|
57
|
+
filterApply: 'Apply',
|
|
58
|
+
filterClear: 'Clear',
|
|
59
|
+
filterMin: 'Min',
|
|
60
|
+
filterMax: 'Max',
|
|
61
|
+
filterFrom: 'From',
|
|
62
|
+
filterTo: 'To',
|
|
63
|
+
filterAll: 'All',
|
|
64
|
+
filterTrue: 'True',
|
|
65
|
+
filterFalse: 'False',
|
|
66
|
+
density: 'Density',
|
|
67
|
+
densityCompact: 'Compact',
|
|
68
|
+
densityComfortable: 'Comfortable',
|
|
69
|
+
densitySpacious: 'Spacious',
|
|
70
|
+
columnsButton: 'Columns',
|
|
71
|
+
columnsTitle: 'Manage columns',
|
|
72
|
+
columnsDescription:
|
|
73
|
+
'Show or hide columns. Required columns cannot be toggled.',
|
|
74
|
+
columnsShowAll: 'Show all',
|
|
75
|
+
columnsHideAll: 'Hide all',
|
|
76
|
+
columnsDone: 'Done',
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Dashforge TW `<DataGrid>` — virtualized data table.
|
|
81
|
+
*
|
|
82
|
+
* Companion to `<Table>` for large data sets (~500+ rows up to
|
|
83
|
+
* millions via virtualization). Same column model, sort/search/
|
|
84
|
+
* filter/selection logic (shared via `_shared/data/` helpers) —
|
|
85
|
+
* differs in render path: only the visible window of rows is mounted
|
|
86
|
+
* in DOM, with spacer `<tr>` above and below to preserve scrollbar
|
|
87
|
+
* size.
|
|
88
|
+
*
|
|
89
|
+
* Virtualization: hand-rolled (`useVirtualizer`) using
|
|
90
|
+
* `IntersectionObserver` + scroll-event + `requestAnimationFrame`
|
|
91
|
+
* debounce + `ResizeObserver`. Zero new runtime deps.
|
|
92
|
+
*
|
|
93
|
+
* Server-side mode (4 independent opt-in flags):
|
|
94
|
+
* - `serverSideSort` — emits `onSortChange` but does NOT sort locally
|
|
95
|
+
* - `serverSideFilter` — emits `onFilterChange` but does NOT filter
|
|
96
|
+
* - `serverSideSearch` — emits `onSearchQueryChange` (debounced)
|
|
97
|
+
* - `serverSidePagination` — `totalCount` prop sizes the virtual
|
|
98
|
+
* scrollbar; `rows` is the page slice from the server
|
|
99
|
+
*
|
|
100
|
+
* Selection at scale: `selectAllScope`:
|
|
101
|
+
* - `'allLoaded'` (default) — header checkbox toggles all rows in
|
|
102
|
+
* `rows` prop
|
|
103
|
+
* - `'visible'` — toggles only the current viewport window
|
|
104
|
+
*/
|
|
105
|
+
export function DataGrid<T extends object>(props: DataGridProps<T>) {
|
|
106
|
+
const {
|
|
107
|
+
rows,
|
|
108
|
+
cols,
|
|
109
|
+
getRowId,
|
|
110
|
+
totalCount: totalCountProp,
|
|
111
|
+
|
|
112
|
+
rowHeight,
|
|
113
|
+
overscan = 5,
|
|
114
|
+
height,
|
|
115
|
+
|
|
116
|
+
sortModel: sortModelProp,
|
|
117
|
+
onSortChange,
|
|
118
|
+
defaultSortModel,
|
|
119
|
+
serverSideSort = false,
|
|
120
|
+
|
|
121
|
+
enableSearch = false,
|
|
122
|
+
searchQuery: searchQueryProp,
|
|
123
|
+
onSearchQueryChange,
|
|
124
|
+
searchPlaceholder,
|
|
125
|
+
searchDebounceMs = 200,
|
|
126
|
+
serverSideSearch = false,
|
|
127
|
+
|
|
128
|
+
filterModel: filterModelProp,
|
|
129
|
+
onFilterChange,
|
|
130
|
+
serverSideFilter = false,
|
|
131
|
+
|
|
132
|
+
hiddenColumns: hiddenColumnsProp,
|
|
133
|
+
onHiddenColumnsChange,
|
|
134
|
+
enableColumnVisibility = true,
|
|
135
|
+
|
|
136
|
+
columnWidths: columnWidthsProp,
|
|
137
|
+
onColumnWidthsChange,
|
|
138
|
+
enableColumnResize = true,
|
|
139
|
+
|
|
140
|
+
columnOrder: columnOrderProp,
|
|
141
|
+
onColumnOrderChange,
|
|
142
|
+
enableColumnReorder = true,
|
|
143
|
+
|
|
144
|
+
rowSelection = 'none',
|
|
145
|
+
selectedRowIds: selectedRowIdsProp,
|
|
146
|
+
onSelectionChange,
|
|
147
|
+
bulkActions,
|
|
148
|
+
selectAllScope = 'allLoaded',
|
|
149
|
+
|
|
150
|
+
pagination,
|
|
151
|
+
serverSidePagination = false,
|
|
152
|
+
|
|
153
|
+
rowActions,
|
|
154
|
+
|
|
155
|
+
loading = false,
|
|
156
|
+
loadingRowCount = 8,
|
|
157
|
+
emptyState,
|
|
158
|
+
|
|
159
|
+
stickyHeader = true,
|
|
160
|
+
caption,
|
|
161
|
+
showCaption = false,
|
|
162
|
+
|
|
163
|
+
access: gridAccess,
|
|
164
|
+
|
|
165
|
+
labels: labelsProp,
|
|
166
|
+
|
|
167
|
+
variant,
|
|
168
|
+
size,
|
|
169
|
+
density,
|
|
170
|
+
|
|
171
|
+
sx,
|
|
172
|
+
slotProps,
|
|
173
|
+
} = props;
|
|
174
|
+
|
|
175
|
+
const labels = { ...DEFAULT_LABELS, ...labelsProp };
|
|
176
|
+
const gridAccessState = useAccessState(gridAccess);
|
|
177
|
+
|
|
178
|
+
// ───── Controllable state ─────
|
|
179
|
+
|
|
180
|
+
const [sortModel, setSortModel] = useControllableState<TableSortModel>({
|
|
181
|
+
controlledValue: sortModelProp,
|
|
182
|
+
defaultValue: defaultSortModel ?? [],
|
|
183
|
+
onChange: onSortChange,
|
|
184
|
+
});
|
|
185
|
+
const [searchQuery, setSearchQuery] = useControllableState<string>({
|
|
186
|
+
controlledValue: searchQueryProp,
|
|
187
|
+
defaultValue: '',
|
|
188
|
+
onChange: onSearchQueryChange,
|
|
189
|
+
});
|
|
190
|
+
// filterModel: in v1-bis the per-column header UI commits filter
|
|
191
|
+
// items to this controllable state. Consumer can lift it via
|
|
192
|
+
// filterModelProp + onFilterChange to drive server-side filtering.
|
|
193
|
+
const [filterModel, setFilterModel] = useControllableState<TableFilterModel>({
|
|
194
|
+
controlledValue: filterModelProp,
|
|
195
|
+
defaultValue: [],
|
|
196
|
+
onChange: onFilterChange,
|
|
197
|
+
});
|
|
198
|
+
const [selectedRowIds, setSelectedRowIds] = useControllableState<string[]>({
|
|
199
|
+
controlledValue: selectedRowIdsProp,
|
|
200
|
+
defaultValue: [],
|
|
201
|
+
onChange: onSelectionChange,
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// ───── Column visibility ─────
|
|
205
|
+
// Seed default hidden set from `defaultHidden` flag on columns.
|
|
206
|
+
// Memoized to avoid the seed shifting on every render — the
|
|
207
|
+
// controllable state keeps internal state stable thereafter.
|
|
208
|
+
const defaultHiddenColumns = useMemo(
|
|
209
|
+
() =>
|
|
210
|
+
cols
|
|
211
|
+
.filter((c) => c.defaultHidden === true)
|
|
212
|
+
.map((c) => c.field as string),
|
|
213
|
+
[cols],
|
|
214
|
+
);
|
|
215
|
+
const [hiddenColumns, setHiddenColumns] = useControllableState<string[]>({
|
|
216
|
+
controlledValue: hiddenColumnsProp,
|
|
217
|
+
defaultValue: defaultHiddenColumns,
|
|
218
|
+
onChange: onHiddenColumnsChange,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// ───── Column widths (resize state) ─────
|
|
222
|
+
// Seed from any `col.width` declared statically. Internal state is
|
|
223
|
+
// applied only when the user actually starts dragging; until then
|
|
224
|
+
// we fall back to the static width from the column def.
|
|
225
|
+
const defaultColumnWidths = useMemo(() => {
|
|
226
|
+
const map: Record<string, number> = {};
|
|
227
|
+
for (const c of cols) {
|
|
228
|
+
if (typeof c.width === 'number') {
|
|
229
|
+
map[c.field as string] = c.width;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return map;
|
|
233
|
+
}, [cols]);
|
|
234
|
+
const [columnWidths, setColumnWidths] = useControllableState<
|
|
235
|
+
Record<string, number>
|
|
236
|
+
>({
|
|
237
|
+
controlledValue: columnWidthsProp,
|
|
238
|
+
defaultValue: defaultColumnWidths,
|
|
239
|
+
onChange: onColumnWidthsChange,
|
|
240
|
+
});
|
|
241
|
+
const { startResize } = useColumnResize({
|
|
242
|
+
widths: columnWidths,
|
|
243
|
+
onChange: setColumnWidths,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// ───── Column order (reorder state) ─────
|
|
247
|
+
const defaultColumnOrder = useMemo(
|
|
248
|
+
() => cols.map((c) => c.field as string),
|
|
249
|
+
[cols],
|
|
250
|
+
);
|
|
251
|
+
const [columnOrder, setColumnOrder] = useControllableState<string[]>({
|
|
252
|
+
controlledValue: columnOrderProp,
|
|
253
|
+
defaultValue: defaultColumnOrder,
|
|
254
|
+
onChange: onColumnOrderChange,
|
|
255
|
+
});
|
|
256
|
+
const reorderHandlers = useColumnReorder({
|
|
257
|
+
order: columnOrder,
|
|
258
|
+
onChange: setColumnOrder,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Apply the active order to the columns list. Cols not in the
|
|
262
|
+
// order array (e.g. newly-added columns at runtime) are appended
|
|
263
|
+
// in their original position from `cols`.
|
|
264
|
+
const orderedCols = useMemo(() => {
|
|
265
|
+
if (!columnOrder || columnOrder.length === 0) return cols;
|
|
266
|
+
const byField = new Map(
|
|
267
|
+
cols.map((c) => [c.field as string, c] as const),
|
|
268
|
+
);
|
|
269
|
+
const seen = new Set<string>();
|
|
270
|
+
const result: typeof cols = [];
|
|
271
|
+
for (const field of columnOrder) {
|
|
272
|
+
const c = byField.get(field);
|
|
273
|
+
if (c) {
|
|
274
|
+
result.push(c);
|
|
275
|
+
seen.add(field);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
for (const c of cols) {
|
|
279
|
+
if (!seen.has(c.field as string)) result.push(c);
|
|
280
|
+
}
|
|
281
|
+
return result;
|
|
282
|
+
}, [cols, columnOrder]);
|
|
283
|
+
|
|
284
|
+
// ───── RBAC + visibility + reorder: derive final column list ─────
|
|
285
|
+
// - `access.onUnauthorized === 'hide'` removes a column entirely.
|
|
286
|
+
// - `hiddenColumns` is the user-toggled hidden set from the
|
|
287
|
+
// column-visibility dialog.
|
|
288
|
+
// - `orderedCols` is the user-reordered version of `cols`.
|
|
289
|
+
// NOTE: filtering happens on `orderedCols` so the active order is
|
|
290
|
+
// preserved through visibility changes.
|
|
291
|
+
const visibleCols = useMemo(
|
|
292
|
+
() =>
|
|
293
|
+
orderedCols.filter((col) => {
|
|
294
|
+
if (col.access && col.access.onUnauthorized === 'hide') return false;
|
|
295
|
+
if (hiddenColumns.includes(col.field as string)) return false;
|
|
296
|
+
return true;
|
|
297
|
+
}),
|
|
298
|
+
[orderedCols, hiddenColumns],
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
// ───── Smart-default column types ─────
|
|
302
|
+
const columnTypes = useColumnAutoDetect(rows, visibleCols);
|
|
303
|
+
|
|
304
|
+
// ───── Pipeline: search → filter → sort (skip when server-side) ─────
|
|
305
|
+
//
|
|
306
|
+
// Each pipeline hook is ALWAYS called (React hooks rules). The
|
|
307
|
+
// server-side flags decide whether to use the local-computed result
|
|
308
|
+
// or pass the upstream rows through unchanged. The hooks are
|
|
309
|
+
// `useMemo`-based internally so they're cheap when their inputs
|
|
310
|
+
// are unchanged.
|
|
311
|
+
|
|
312
|
+
const debouncedSearchQuery = useDebouncedValue(searchQuery, searchDebounceMs);
|
|
313
|
+
const localSearched = useTableSearch(rows, visibleCols, debouncedSearchQuery);
|
|
314
|
+
const searchedRows = serverSideSearch ? rows : localSearched;
|
|
315
|
+
|
|
316
|
+
const localFiltered = useTableFilter(searchedRows, filterModel);
|
|
317
|
+
const filteredRows = serverSideFilter ? searchedRows : localFiltered;
|
|
318
|
+
|
|
319
|
+
const localSorted = useTableSort(filteredRows, visibleCols, sortModel);
|
|
320
|
+
const sortedRows = serverSideSort ? filteredRows : localSorted;
|
|
321
|
+
|
|
322
|
+
// ───── Virtualization ─────
|
|
323
|
+
|
|
324
|
+
// Effective row count for the virtual scrollbar:
|
|
325
|
+
// - Client-side: actual sorted rows count
|
|
326
|
+
// - Server-side pagination: caller-provided totalCount
|
|
327
|
+
const effectiveTotalCount = serverSidePagination
|
|
328
|
+
? totalCountProp ?? sortedRows.length
|
|
329
|
+
: sortedRows.length;
|
|
330
|
+
|
|
331
|
+
const initialViewportHeight = height ? parseInt(height, 10) || 400 : 400;
|
|
332
|
+
const { scrollRef, window: vWindow } = useVirtualizer({
|
|
333
|
+
totalCount: effectiveTotalCount,
|
|
334
|
+
rowHeight,
|
|
335
|
+
overscan,
|
|
336
|
+
initialViewportHeight,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// The rows actually mounted in DOM.
|
|
340
|
+
const renderedRows = useMemo(() => {
|
|
341
|
+
if (serverSidePagination) {
|
|
342
|
+
// In server-side pagination, `rows` IS the page slice; we
|
|
343
|
+
// render all of it (virtualization within the page is still
|
|
344
|
+
// useful for very large page sizes).
|
|
345
|
+
return rows.slice(vWindow.startIndex, vWindow.endIndex + 1);
|
|
346
|
+
}
|
|
347
|
+
return sortedRows.slice(vWindow.startIndex, vWindow.endIndex + 1);
|
|
348
|
+
}, [serverSidePagination, rows, sortedRows, vWindow.startIndex, vWindow.endIndex]);
|
|
349
|
+
|
|
350
|
+
// ───── Selection helpers ─────
|
|
351
|
+
|
|
352
|
+
// For `selectAllScope='visible'`, the select-all scope is the
|
|
353
|
+
// current window. For `'allLoaded'`, it's the whole sorted set.
|
|
354
|
+
const selectAllPool = useMemo(() => {
|
|
355
|
+
if (selectAllScope === 'visible') {
|
|
356
|
+
return renderedRows.map((row, idx) =>
|
|
357
|
+
getRowId(row, vWindow.startIndex + idx),
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
return sortedRows.map((row, idx) => getRowId(row, idx));
|
|
361
|
+
}, [selectAllScope, renderedRows, sortedRows, getRowId, vWindow.startIndex]);
|
|
362
|
+
|
|
363
|
+
const selection = useTableSelection(rowSelection, selectedRowIds, setSelectedRowIds);
|
|
364
|
+
|
|
365
|
+
const selectedRows = useMemo(
|
|
366
|
+
() => sortedRows.filter((row, idx) => selection.isSelected(getRowId(row, idx))),
|
|
367
|
+
[sortedRows, selection, getRowId],
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
// ───── Variant recipe ─────
|
|
371
|
+
const v = dataGridVariants({ variant, size, density });
|
|
372
|
+
|
|
373
|
+
// ───── Render ─────
|
|
374
|
+
|
|
375
|
+
if (!gridAccessState.visible) return null;
|
|
376
|
+
|
|
377
|
+
const isInteractive = !gridAccessState.disabled;
|
|
378
|
+
const showSelectionColumn = rowSelection !== 'none';
|
|
379
|
+
const showRowActionsColumn = rowActions != null;
|
|
380
|
+
|
|
381
|
+
const totalColumnCount =
|
|
382
|
+
visibleCols.length +
|
|
383
|
+
(showSelectionColumn ? 1 : 0) +
|
|
384
|
+
(showRowActionsColumn ? 1 : 0);
|
|
385
|
+
|
|
386
|
+
return (
|
|
387
|
+
<div
|
|
388
|
+
className={cn(v.root(), sx, slotProps?.root?.className)}
|
|
389
|
+
data-disabled={!isInteractive ? 'true' : undefined}
|
|
390
|
+
>
|
|
391
|
+
{/* ───── Toolbar ───── */}
|
|
392
|
+
{(enableSearch || enableColumnVisibility) && (
|
|
393
|
+
<div className={cn(v.toolbar(), slotProps?.toolbar?.className)}>
|
|
394
|
+
{enableSearch && (
|
|
395
|
+
<label className={cn(v.search(), slotProps?.search?.className)}>
|
|
396
|
+
<span className="sr-only">{labels.searchPlaceholder}</span>
|
|
397
|
+
<SearchIcon className="ml-2 shrink-0 text-neutral-500" />
|
|
398
|
+
<input
|
|
399
|
+
type="text"
|
|
400
|
+
value={searchQuery}
|
|
401
|
+
onChange={(e) => setSearchQuery(e.target.value)}
|
|
402
|
+
placeholder={searchPlaceholder ?? labels.searchPlaceholder}
|
|
403
|
+
disabled={!isInteractive}
|
|
404
|
+
className={cn(
|
|
405
|
+
'flex-1 bg-transparent border-0 outline-none',
|
|
406
|
+
'px-2 py-1.5',
|
|
407
|
+
'placeholder:text-neutral-400',
|
|
408
|
+
'disabled:opacity-50 disabled:cursor-not-allowed',
|
|
409
|
+
)}
|
|
410
|
+
/>
|
|
411
|
+
</label>
|
|
412
|
+
)}
|
|
413
|
+
{enableColumnVisibility && (
|
|
414
|
+
<ColumnVisibilityTrigger
|
|
415
|
+
cols={cols.filter(
|
|
416
|
+
(c) =>
|
|
417
|
+
// RBAC-hidden columns are never offered in the dialog.
|
|
418
|
+
!c.access || c.access.onUnauthorized !== 'hide',
|
|
419
|
+
)}
|
|
420
|
+
hiddenColumns={hiddenColumns}
|
|
421
|
+
onChange={setHiddenColumns}
|
|
422
|
+
disabled={!isInteractive}
|
|
423
|
+
labels={{
|
|
424
|
+
columnsButton: labels.columnsButton,
|
|
425
|
+
columnsTitle: labels.columnsTitle,
|
|
426
|
+
columnsDescription: labels.columnsDescription,
|
|
427
|
+
columnsShowAll: labels.columnsShowAll,
|
|
428
|
+
columnsHideAll: labels.columnsHideAll,
|
|
429
|
+
columnsDone: labels.columnsDone,
|
|
430
|
+
}}
|
|
431
|
+
/>
|
|
432
|
+
)}
|
|
433
|
+
</div>
|
|
434
|
+
)}
|
|
435
|
+
|
|
436
|
+
{/* ───── Scroll wrapper (bounded height — required for virt) ───── */}
|
|
437
|
+
<div
|
|
438
|
+
ref={scrollRef}
|
|
439
|
+
className={cn(v.scroll(), slotProps?.scroll?.className)}
|
|
440
|
+
style={height ? { height, maxHeight: height } : undefined}
|
|
441
|
+
>
|
|
442
|
+
<table className={cn(v.table(), slotProps?.table?.className)}>
|
|
443
|
+
{caption != null && (
|
|
444
|
+
<caption className={cn(!showCaption && 'sr-only', 'py-2 text-sm text-neutral-500')}>
|
|
445
|
+
{caption}
|
|
446
|
+
</caption>
|
|
447
|
+
)}
|
|
448
|
+
|
|
449
|
+
<thead
|
|
450
|
+
className={cn(
|
|
451
|
+
stickyHeader ? v.thead() : 'bg-neutral-50',
|
|
452
|
+
slotProps?.thead?.className,
|
|
453
|
+
)}
|
|
454
|
+
>
|
|
455
|
+
<tr className={cn(v.headerRow(), slotProps?.headerRow?.className)}>
|
|
456
|
+
{showSelectionColumn && (
|
|
457
|
+
<th
|
|
458
|
+
scope="col"
|
|
459
|
+
className={cn(
|
|
460
|
+
v.headerCell(),
|
|
461
|
+
v.selectionCell(),
|
|
462
|
+
v.stickyLeftHeaderCell(),
|
|
463
|
+
slotProps?.headerCell?.className,
|
|
464
|
+
slotProps?.selectionCell?.className,
|
|
465
|
+
)}
|
|
466
|
+
>
|
|
467
|
+
{rowSelection === 'multiple' && (
|
|
468
|
+
<input
|
|
469
|
+
type="checkbox"
|
|
470
|
+
aria-label={labels.ariaSelectAllRows}
|
|
471
|
+
checked={selection.isAllSelected(selectAllPool)}
|
|
472
|
+
ref={(el) => {
|
|
473
|
+
if (el)
|
|
474
|
+
el.indeterminate = selection.isIndeterminate(selectAllPool);
|
|
475
|
+
}}
|
|
476
|
+
onChange={() => selection.toggleAll(selectAllPool)}
|
|
477
|
+
disabled={!isInteractive}
|
|
478
|
+
className="cursor-pointer"
|
|
479
|
+
/>
|
|
480
|
+
)}
|
|
481
|
+
</th>
|
|
482
|
+
)}
|
|
483
|
+
|
|
484
|
+
{visibleCols.map((col) => (
|
|
485
|
+
<HeaderCell
|
|
486
|
+
key={col.field as string}
|
|
487
|
+
col={col}
|
|
488
|
+
sortModel={sortModel}
|
|
489
|
+
onSortClick={(e) =>
|
|
490
|
+
handleSortClick(col, e, sortModel, setSortModel)
|
|
491
|
+
}
|
|
492
|
+
inferredType={columnTypes.get(col.field as string)}
|
|
493
|
+
filterModel={filterModel}
|
|
494
|
+
onFilterChange={setFilterModel}
|
|
495
|
+
effectiveWidth={resolveColumnWidth(col, columnWidths)}
|
|
496
|
+
resizable={
|
|
497
|
+
enableColumnResize && col.resizable !== false
|
|
498
|
+
}
|
|
499
|
+
startResize={startResize}
|
|
500
|
+
reorderable={
|
|
501
|
+
enableColumnReorder && col.reorderable !== false
|
|
502
|
+
}
|
|
503
|
+
reorderHandlers={reorderHandlers}
|
|
504
|
+
labels={labels}
|
|
505
|
+
className={cn(
|
|
506
|
+
v.headerCell(),
|
|
507
|
+
col.sticky === 'left' && v.stickyLeftHeaderCell(),
|
|
508
|
+
col.sticky === 'right' && v.stickyRightHeaderCell(),
|
|
509
|
+
slotProps?.headerCell?.className,
|
|
510
|
+
)}
|
|
511
|
+
buttonClassName={v.headerCellButton()}
|
|
512
|
+
disabled={!isInteractive}
|
|
513
|
+
/>
|
|
514
|
+
))}
|
|
515
|
+
|
|
516
|
+
{showRowActionsColumn && (
|
|
517
|
+
<th
|
|
518
|
+
scope="col"
|
|
519
|
+
aria-hidden="true"
|
|
520
|
+
className={cn(
|
|
521
|
+
v.headerCell(),
|
|
522
|
+
'w-12',
|
|
523
|
+
slotProps?.headerCell?.className,
|
|
524
|
+
)}
|
|
525
|
+
/>
|
|
526
|
+
)}
|
|
527
|
+
</tr>
|
|
528
|
+
</thead>
|
|
529
|
+
|
|
530
|
+
<tbody className={cn(v.tbody(), slotProps?.tbody?.className)}>
|
|
531
|
+
{loading
|
|
532
|
+
? renderLoadingRows({
|
|
533
|
+
count: loadingRowCount,
|
|
534
|
+
visibleCols,
|
|
535
|
+
showSelectionColumn,
|
|
536
|
+
showRowActionsColumn,
|
|
537
|
+
rowHeight,
|
|
538
|
+
rowClass: cn(v.row(), slotProps?.row?.className),
|
|
539
|
+
cellClass: cn(v.cell(), slotProps?.cell?.className),
|
|
540
|
+
})
|
|
541
|
+
: sortedRows.length === 0
|
|
542
|
+
? renderEmptyState({
|
|
543
|
+
totalColumnCount,
|
|
544
|
+
emptyState,
|
|
545
|
+
fallback: labels.noData,
|
|
546
|
+
cellClass: cn(
|
|
547
|
+
v.cell(),
|
|
548
|
+
v.emptyState(),
|
|
549
|
+
slotProps?.emptyState?.className,
|
|
550
|
+
),
|
|
551
|
+
})
|
|
552
|
+
: renderVirtualizedBody({
|
|
553
|
+
vWindow,
|
|
554
|
+
rowHeight,
|
|
555
|
+
renderedRows,
|
|
556
|
+
visibleCols,
|
|
557
|
+
columnTypes,
|
|
558
|
+
columnWidths,
|
|
559
|
+
selection,
|
|
560
|
+
rowSelection,
|
|
561
|
+
showSelectionColumn,
|
|
562
|
+
showRowActionsColumn,
|
|
563
|
+
rowActions,
|
|
564
|
+
getRowId,
|
|
565
|
+
isInteractive,
|
|
566
|
+
labels,
|
|
567
|
+
serverSidePagination,
|
|
568
|
+
classes: {
|
|
569
|
+
row: cn(v.row(), slotProps?.row?.className),
|
|
570
|
+
cell: cn(v.cell(), slotProps?.cell?.className),
|
|
571
|
+
selectionCell: cn(
|
|
572
|
+
v.cell(),
|
|
573
|
+
v.selectionCell(),
|
|
574
|
+
v.stickyLeftCell(),
|
|
575
|
+
slotProps?.cell?.className,
|
|
576
|
+
slotProps?.selectionCell?.className,
|
|
577
|
+
),
|
|
578
|
+
stickyLeftCell: v.stickyLeftCell(),
|
|
579
|
+
stickyRightCell: v.stickyRightCell(),
|
|
580
|
+
rowActionsCell: cn(
|
|
581
|
+
v.cell(),
|
|
582
|
+
v.rowActionsCell(),
|
|
583
|
+
slotProps?.cell?.className,
|
|
584
|
+
slotProps?.rowActionsCell?.className,
|
|
585
|
+
),
|
|
586
|
+
},
|
|
587
|
+
})}
|
|
588
|
+
</tbody>
|
|
589
|
+
</table>
|
|
590
|
+
</div>
|
|
591
|
+
|
|
592
|
+
{/* ───── Bulk action footer ───── */}
|
|
593
|
+
{bulkActions && selectedRows.length > 0 && (
|
|
594
|
+
<div
|
|
595
|
+
className={cn(v.bulkActionFooter(), slotProps?.bulkActionFooter?.className)}
|
|
596
|
+
role="region"
|
|
597
|
+
aria-label={labels.selectedCount.replace(
|
|
598
|
+
'{count}',
|
|
599
|
+
String(selectedRows.length),
|
|
600
|
+
)}
|
|
601
|
+
>
|
|
602
|
+
<span className="text-sm text-neutral-700">
|
|
603
|
+
{labels.selectedCount.replace('{count}', String(selectedRows.length))}
|
|
604
|
+
</span>
|
|
605
|
+
<div className="flex items-center gap-2">
|
|
606
|
+
{bulkActions(selectedRows)}
|
|
607
|
+
<button
|
|
608
|
+
type="button"
|
|
609
|
+
onClick={selection.clearSelection}
|
|
610
|
+
className={cn(
|
|
611
|
+
'text-sm text-neutral-500 hover:text-neutral-900',
|
|
612
|
+
'underline-offset-2 hover:underline',
|
|
613
|
+
'focus:outline-none focus-visible:underline',
|
|
614
|
+
)}
|
|
615
|
+
>
|
|
616
|
+
{labels.clearSelection}
|
|
617
|
+
</button>
|
|
618
|
+
</div>
|
|
619
|
+
</div>
|
|
620
|
+
)}
|
|
621
|
+
|
|
622
|
+
{/* ───── Optional pagination footer ───── */}
|
|
623
|
+
{pagination && (
|
|
624
|
+
<div className={cn(v.paginationFooter(), slotProps?.pagination?.className)}>
|
|
625
|
+
<Pagination
|
|
626
|
+
page={pagination.page}
|
|
627
|
+
pageSize={pagination.pageSize}
|
|
628
|
+
totalCount={totalCountProp ?? sortedRows.length}
|
|
629
|
+
onPageChange={pagination.onPageChange}
|
|
630
|
+
onPageSizeChange={pagination.onPageSizeChange}
|
|
631
|
+
pageSizeOptions={pagination.pageSizeOptions}
|
|
632
|
+
/>
|
|
633
|
+
</div>
|
|
634
|
+
)}
|
|
635
|
+
</div>
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// ───── Header cell ─────
|
|
640
|
+
|
|
641
|
+
function HeaderCell<T extends object>(props: {
|
|
642
|
+
col: TableColumn<T>;
|
|
643
|
+
sortModel: TableSortModel;
|
|
644
|
+
onSortClick: (e: MouseEvent<HTMLButtonElement>) => void;
|
|
645
|
+
inferredType: string | undefined;
|
|
646
|
+
filterModel: TableFilterModel;
|
|
647
|
+
onFilterChange: (model: TableFilterModel) => void;
|
|
648
|
+
effectiveWidth: number | undefined;
|
|
649
|
+
resizable: boolean;
|
|
650
|
+
startResize: (
|
|
651
|
+
field: string,
|
|
652
|
+
startWidth: number,
|
|
653
|
+
clampMin: number,
|
|
654
|
+
clampMax: number,
|
|
655
|
+
) => (e: RPointerEvent<HTMLElement>) => void;
|
|
656
|
+
reorderable: boolean;
|
|
657
|
+
reorderHandlers: ColumnReorderHandlers;
|
|
658
|
+
labels: Required<TableLabels>;
|
|
659
|
+
className: string;
|
|
660
|
+
buttonClassName: string;
|
|
661
|
+
disabled: boolean;
|
|
662
|
+
}) {
|
|
663
|
+
const {
|
|
664
|
+
col,
|
|
665
|
+
sortModel,
|
|
666
|
+
onSortClick,
|
|
667
|
+
inferredType,
|
|
668
|
+
filterModel,
|
|
669
|
+
onFilterChange,
|
|
670
|
+
effectiveWidth,
|
|
671
|
+
resizable,
|
|
672
|
+
startResize,
|
|
673
|
+
reorderable,
|
|
674
|
+
reorderHandlers,
|
|
675
|
+
labels,
|
|
676
|
+
className,
|
|
677
|
+
buttonClassName,
|
|
678
|
+
disabled,
|
|
679
|
+
} = props;
|
|
680
|
+
|
|
681
|
+
const align = resolveAlign(col, inferredType as never);
|
|
682
|
+
const alignClass =
|
|
683
|
+
align === 'right' ? 'text-right' : align === 'center' ? 'text-center' : 'text-left';
|
|
684
|
+
|
|
685
|
+
const sortEntry = sortModel.find((s) => s.field === col.field);
|
|
686
|
+
const ariaSort: 'ascending' | 'descending' | 'none' =
|
|
687
|
+
sortEntry?.direction === 'asc'
|
|
688
|
+
? 'ascending'
|
|
689
|
+
: sortEntry?.direction === 'desc'
|
|
690
|
+
? 'descending'
|
|
691
|
+
: 'none';
|
|
692
|
+
|
|
693
|
+
const headerContent =
|
|
694
|
+
typeof col.header === 'function' ? col.header() : col.header;
|
|
695
|
+
|
|
696
|
+
const cellStyle = {
|
|
697
|
+
width: effectiveWidth != null ? `${effectiveWidth}px` : undefined,
|
|
698
|
+
minWidth: col.minWidth != null ? `${col.minWidth}px` : undefined,
|
|
699
|
+
flex: effectiveWidth != null ? undefined : col.flex,
|
|
700
|
+
position: resizable || reorderable ? ('relative' as const) : undefined,
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
// Drop indicator for reorder: a 2px vertical bar at the LEFT or
|
|
704
|
+
// RIGHT edge of this th when it's the active drop target.
|
|
705
|
+
const dropTarget = reorderHandlers.dropTarget;
|
|
706
|
+
const dropIndicatorClass =
|
|
707
|
+
dropTarget != null && dropTarget.field === (col.field as string)
|
|
708
|
+
? dropTarget.side === 'left'
|
|
709
|
+
? 'before:absolute before:top-0 before:bottom-0 before:left-0 before:w-0.5 before:bg-primary-500'
|
|
710
|
+
: 'after:absolute after:top-0 after:bottom-0 after:right-0 after:w-0.5 after:bg-primary-500'
|
|
711
|
+
: '';
|
|
712
|
+
|
|
713
|
+
const isDragging = reorderHandlers.draggingField === (col.field as string);
|
|
714
|
+
|
|
715
|
+
// Drag attributes only attach when reorderable; we DO NOT make the
|
|
716
|
+
// entire th `draggable` if it isn't — keeps the keyboard focus
|
|
717
|
+
// behavior unchanged.
|
|
718
|
+
const dragAttrs = reorderable
|
|
719
|
+
? {
|
|
720
|
+
draggable: true,
|
|
721
|
+
onDragStart: reorderHandlers.dragStart(col.field as string),
|
|
722
|
+
onDragOver: reorderHandlers.dragOver(col.field as string),
|
|
723
|
+
onDrop: reorderHandlers.drop(col.field as string),
|
|
724
|
+
onDragEnd: reorderHandlers.dragEnd,
|
|
725
|
+
}
|
|
726
|
+
: {};
|
|
727
|
+
|
|
728
|
+
const resizeHandle = resizable ? (
|
|
729
|
+
<span
|
|
730
|
+
role="separator"
|
|
731
|
+
aria-orientation="vertical"
|
|
732
|
+
aria-label={`Resize ${typeof col.header === 'string' ? col.header : col.field}`}
|
|
733
|
+
onPointerDown={(e) => {
|
|
734
|
+
const handle = e.currentTarget;
|
|
735
|
+
const th = handle.parentElement as HTMLElement | null;
|
|
736
|
+
const startWidth =
|
|
737
|
+
effectiveWidth ?? th?.getBoundingClientRect().width ?? 120;
|
|
738
|
+
const min = col.minWidth ?? 40;
|
|
739
|
+
const max = col.maxWidth ?? 1200;
|
|
740
|
+
startResize(col.field as string, startWidth, min, max)(e);
|
|
741
|
+
}}
|
|
742
|
+
className={cn(
|
|
743
|
+
'absolute top-0 bottom-0 right-0 w-1.5',
|
|
744
|
+
'cursor-col-resize select-none touch-none',
|
|
745
|
+
'hover:bg-primary-300 active:bg-primary-400',
|
|
746
|
+
'transition-colors motion-reduce:transition-none',
|
|
747
|
+
)}
|
|
748
|
+
/>
|
|
749
|
+
) : null;
|
|
750
|
+
|
|
751
|
+
const filterType = col.filterable
|
|
752
|
+
? resolveFilterType(col, inferredType as TableColumnInferredType | undefined)
|
|
753
|
+
: null;
|
|
754
|
+
|
|
755
|
+
const filterTrigger =
|
|
756
|
+
col.filterable && filterType ? (
|
|
757
|
+
<ColumnFilterTrigger
|
|
758
|
+
field={col.field as string}
|
|
759
|
+
filterType={filterType}
|
|
760
|
+
filterModel={filterModel}
|
|
761
|
+
onFilterChange={onFilterChange}
|
|
762
|
+
disabled={disabled}
|
|
763
|
+
labels={{
|
|
764
|
+
filterColumn: labels.filterColumn,
|
|
765
|
+
filterApply: labels.filterApply,
|
|
766
|
+
filterClear: labels.filterClear,
|
|
767
|
+
filterMin: labels.filterMin,
|
|
768
|
+
filterMax: labels.filterMax,
|
|
769
|
+
filterFrom: labels.filterFrom,
|
|
770
|
+
filterTo: labels.filterTo,
|
|
771
|
+
filterAll: labels.filterAll,
|
|
772
|
+
filterTrue: labels.filterTrue,
|
|
773
|
+
filterFalse: labels.filterFalse,
|
|
774
|
+
}}
|
|
775
|
+
/>
|
|
776
|
+
) : null;
|
|
777
|
+
|
|
778
|
+
// Layout strategy:
|
|
779
|
+
// - sortable only → sort button is the cell content
|
|
780
|
+
// - filterable only → header text + filter trigger in a flex row
|
|
781
|
+
// - both → sort button + filter trigger in a flex row
|
|
782
|
+
// - neither → plain header text
|
|
783
|
+
const wrapperJustify =
|
|
784
|
+
align === 'right'
|
|
785
|
+
? 'justify-end'
|
|
786
|
+
: align === 'center'
|
|
787
|
+
? 'justify-center'
|
|
788
|
+
: 'justify-start';
|
|
789
|
+
|
|
790
|
+
if (col.sortable) {
|
|
791
|
+
const sortButton = (
|
|
792
|
+
<button
|
|
793
|
+
type="button"
|
|
794
|
+
onClick={onSortClick}
|
|
795
|
+
disabled={disabled}
|
|
796
|
+
aria-label={
|
|
797
|
+
ariaSort === 'ascending'
|
|
798
|
+
? labels.ariaSortAscending
|
|
799
|
+
: ariaSort === 'descending'
|
|
800
|
+
? labels.ariaSortDescending
|
|
801
|
+
: labels.ariaSortNone
|
|
802
|
+
}
|
|
803
|
+
className={cn(
|
|
804
|
+
buttonClassName,
|
|
805
|
+
align === 'right' && 'flex-row-reverse',
|
|
806
|
+
'disabled:cursor-not-allowed disabled:opacity-50',
|
|
807
|
+
)}
|
|
808
|
+
>
|
|
809
|
+
<span>{headerContent}</span>
|
|
810
|
+
<SortIcon direction={sortEntry?.direction} />
|
|
811
|
+
</button>
|
|
812
|
+
);
|
|
813
|
+
return (
|
|
814
|
+
<th
|
|
815
|
+
scope="col"
|
|
816
|
+
aria-sort={ariaSort}
|
|
817
|
+
className={cn(
|
|
818
|
+
className,
|
|
819
|
+
alignClass,
|
|
820
|
+
dropIndicatorClass,
|
|
821
|
+
isDragging && 'opacity-50',
|
|
822
|
+
)}
|
|
823
|
+
style={cellStyle}
|
|
824
|
+
{...dragAttrs}
|
|
825
|
+
>
|
|
826
|
+
{filterTrigger ? (
|
|
827
|
+
<div className={cn('flex items-center gap-1', wrapperJustify)}>
|
|
828
|
+
{sortButton}
|
|
829
|
+
{filterTrigger}
|
|
830
|
+
</div>
|
|
831
|
+
) : (
|
|
832
|
+
sortButton
|
|
833
|
+
)}
|
|
834
|
+
{resizeHandle}
|
|
835
|
+
</th>
|
|
836
|
+
);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
return (
|
|
840
|
+
<th
|
|
841
|
+
scope="col"
|
|
842
|
+
className={cn(
|
|
843
|
+
className,
|
|
844
|
+
alignClass,
|
|
845
|
+
dropIndicatorClass,
|
|
846
|
+
isDragging && 'opacity-50',
|
|
847
|
+
)}
|
|
848
|
+
style={cellStyle}
|
|
849
|
+
{...dragAttrs}
|
|
850
|
+
>
|
|
851
|
+
{filterTrigger ? (
|
|
852
|
+
<div className={cn('flex items-center gap-1', wrapperJustify)}>
|
|
853
|
+
<span>{headerContent}</span>
|
|
854
|
+
{filterTrigger}
|
|
855
|
+
</div>
|
|
856
|
+
) : (
|
|
857
|
+
headerContent
|
|
858
|
+
)}
|
|
859
|
+
{resizeHandle}
|
|
860
|
+
</th>
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/**
|
|
865
|
+
* Resolve the effective width (px) for a column. User-driven resize
|
|
866
|
+
* (`columnWidths[field]`) wins over the static `col.width`. Returns
|
|
867
|
+
* `undefined` if neither source has a width set — the consumer falls
|
|
868
|
+
* back to the column's `flex` value or browser auto-layout.
|
|
869
|
+
*/
|
|
870
|
+
function resolveColumnWidth<T extends object>(
|
|
871
|
+
col: TableColumn<T>,
|
|
872
|
+
columnWidths: Record<string, number>,
|
|
873
|
+
): number | undefined {
|
|
874
|
+
const resized = columnWidths[col.field as string];
|
|
875
|
+
if (resized != null) return resized;
|
|
876
|
+
return col.width;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/**
|
|
880
|
+
* Resolve the filter UI type for a column. Explicit `col.filterType`
|
|
881
|
+
* wins; otherwise we map the auto-detected primitive type:
|
|
882
|
+
* - `number` → number range
|
|
883
|
+
* - `boolean` → true/false/all select
|
|
884
|
+
* - `date` → date range
|
|
885
|
+
* - anything else (string / unknown) → text contains
|
|
886
|
+
*/
|
|
887
|
+
function resolveFilterType<T extends object>(
|
|
888
|
+
col: TableColumn<T>,
|
|
889
|
+
inferred: TableColumnInferredType | undefined,
|
|
890
|
+
): TableFilterType {
|
|
891
|
+
if (col.filterType) return col.filterType;
|
|
892
|
+
switch (inferred) {
|
|
893
|
+
case 'number':
|
|
894
|
+
return 'number';
|
|
895
|
+
case 'boolean':
|
|
896
|
+
return 'boolean';
|
|
897
|
+
case 'date':
|
|
898
|
+
return 'date';
|
|
899
|
+
default:
|
|
900
|
+
return 'text';
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// ───── Virtualized body ─────
|
|
905
|
+
|
|
906
|
+
function renderVirtualizedBody<T extends object>(args: {
|
|
907
|
+
vWindow: { startIndex: number; endIndex: number; paddingTop: number; paddingBottom: number };
|
|
908
|
+
rowHeight: number;
|
|
909
|
+
renderedRows: T[];
|
|
910
|
+
visibleCols: TableColumn<T>[];
|
|
911
|
+
columnTypes: Map<string, string>;
|
|
912
|
+
columnWidths: Record<string, number>;
|
|
913
|
+
selection: ReturnType<typeof useTableSelection>;
|
|
914
|
+
rowSelection: 'none' | 'single' | 'multiple';
|
|
915
|
+
showSelectionColumn: boolean;
|
|
916
|
+
showRowActionsColumn: boolean;
|
|
917
|
+
rowActions: ((row: T) => ReactNode) | undefined;
|
|
918
|
+
getRowId: (row: T, index: number) => string;
|
|
919
|
+
isInteractive: boolean;
|
|
920
|
+
labels: Required<TableLabels>;
|
|
921
|
+
serverSidePagination: boolean;
|
|
922
|
+
classes: {
|
|
923
|
+
row: string;
|
|
924
|
+
cell: string;
|
|
925
|
+
selectionCell: string;
|
|
926
|
+
stickyLeftCell: string;
|
|
927
|
+
stickyRightCell: string;
|
|
928
|
+
rowActionsCell: string;
|
|
929
|
+
};
|
|
930
|
+
}): ReactNode {
|
|
931
|
+
const {
|
|
932
|
+
vWindow,
|
|
933
|
+
renderedRows,
|
|
934
|
+
visibleCols,
|
|
935
|
+
columnTypes,
|
|
936
|
+
columnWidths,
|
|
937
|
+
selection,
|
|
938
|
+
rowSelection,
|
|
939
|
+
showSelectionColumn,
|
|
940
|
+
showRowActionsColumn,
|
|
941
|
+
rowActions,
|
|
942
|
+
getRowId,
|
|
943
|
+
isInteractive,
|
|
944
|
+
labels,
|
|
945
|
+
classes,
|
|
946
|
+
} = args;
|
|
947
|
+
|
|
948
|
+
return (
|
|
949
|
+
<>
|
|
950
|
+
{vWindow.paddingTop > 0 && (
|
|
951
|
+
<tr aria-hidden="true" style={{ height: vWindow.paddingTop }} />
|
|
952
|
+
)}
|
|
953
|
+
{renderedRows.map((row, localIdx) => {
|
|
954
|
+
const absoluteIdx = vWindow.startIndex + localIdx;
|
|
955
|
+
const rowId = getRowId(row, absoluteIdx);
|
|
956
|
+
const isSelected = selection.isSelected(rowId);
|
|
957
|
+
return (
|
|
958
|
+
<tr
|
|
959
|
+
key={rowId}
|
|
960
|
+
data-selected={isSelected ? 'true' : 'false'}
|
|
961
|
+
aria-selected={rowSelection !== 'none' ? isSelected : undefined}
|
|
962
|
+
className={classes.row}
|
|
963
|
+
>
|
|
964
|
+
{showSelectionColumn && (
|
|
965
|
+
<td className={classes.selectionCell}>
|
|
966
|
+
<input
|
|
967
|
+
type="checkbox"
|
|
968
|
+
aria-label={labels.ariaSelectRow}
|
|
969
|
+
checked={isSelected}
|
|
970
|
+
onChange={() => selection.toggleRow(rowId)}
|
|
971
|
+
disabled={!isInteractive}
|
|
972
|
+
className="cursor-pointer"
|
|
973
|
+
/>
|
|
974
|
+
</td>
|
|
975
|
+
)}
|
|
976
|
+
{visibleCols.map((col) => (
|
|
977
|
+
<DataCell
|
|
978
|
+
key={col.field as string}
|
|
979
|
+
col={col}
|
|
980
|
+
row={row}
|
|
981
|
+
rowIndex={absoluteIdx}
|
|
982
|
+
isSelected={isSelected}
|
|
983
|
+
inferredType={columnTypes.get(col.field as string)}
|
|
984
|
+
effectiveWidth={resolveColumnWidth(col, columnWidths)}
|
|
985
|
+
cellClass={classes.cell}
|
|
986
|
+
stickyLeftClass={classes.stickyLeftCell}
|
|
987
|
+
stickyRightClass={classes.stickyRightCell}
|
|
988
|
+
/>
|
|
989
|
+
))}
|
|
990
|
+
{showRowActionsColumn && rowActions && (
|
|
991
|
+
<td className={classes.rowActionsCell}>{rowActions(row)}</td>
|
|
992
|
+
)}
|
|
993
|
+
</tr>
|
|
994
|
+
);
|
|
995
|
+
})}
|
|
996
|
+
{vWindow.paddingBottom > 0 && (
|
|
997
|
+
<tr aria-hidden="true" style={{ height: vWindow.paddingBottom }} />
|
|
998
|
+
)}
|
|
999
|
+
</>
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// ───── Data cell ─────
|
|
1004
|
+
|
|
1005
|
+
function DataCell<T extends object>(props: {
|
|
1006
|
+
col: TableColumn<T>;
|
|
1007
|
+
row: T;
|
|
1008
|
+
rowIndex: number;
|
|
1009
|
+
isSelected: boolean;
|
|
1010
|
+
inferredType: string | undefined;
|
|
1011
|
+
effectiveWidth: number | undefined;
|
|
1012
|
+
cellClass: string;
|
|
1013
|
+
stickyLeftClass: string;
|
|
1014
|
+
stickyRightClass: string;
|
|
1015
|
+
}) {
|
|
1016
|
+
const {
|
|
1017
|
+
col,
|
|
1018
|
+
row,
|
|
1019
|
+
rowIndex,
|
|
1020
|
+
isSelected,
|
|
1021
|
+
inferredType,
|
|
1022
|
+
effectiveWidth,
|
|
1023
|
+
cellClass,
|
|
1024
|
+
stickyLeftClass,
|
|
1025
|
+
stickyRightClass,
|
|
1026
|
+
} = props;
|
|
1027
|
+
const value = getNestedValue(row, col.field as string);
|
|
1028
|
+
const align = resolveAlign(col, inferredType as never);
|
|
1029
|
+
const tabularNums = resolveTabularNums(col, inferredType as never);
|
|
1030
|
+
const monospace = resolveMonospace(col);
|
|
1031
|
+
const alignClass =
|
|
1032
|
+
align === 'right' ? 'text-right' : align === 'center' ? 'text-center' : 'text-left';
|
|
1033
|
+
|
|
1034
|
+
const columnAccessState = col.access
|
|
1035
|
+
? {
|
|
1036
|
+
hidden: col.access.onUnauthorized === 'hide',
|
|
1037
|
+
disabled: col.access.onUnauthorized === 'disable',
|
|
1038
|
+
readonly: col.access.onUnauthorized === 'readonly',
|
|
1039
|
+
}
|
|
1040
|
+
: null;
|
|
1041
|
+
|
|
1042
|
+
const content = col.cellRenderer
|
|
1043
|
+
? col.cellRenderer({
|
|
1044
|
+
row,
|
|
1045
|
+
value,
|
|
1046
|
+
rowIndex,
|
|
1047
|
+
isSelected,
|
|
1048
|
+
isExpanded: false, // DataGrid v1 doesn't support expansion (defer)
|
|
1049
|
+
access: columnAccessState,
|
|
1050
|
+
})
|
|
1051
|
+
: renderDefaultCell(value);
|
|
1052
|
+
|
|
1053
|
+
return (
|
|
1054
|
+
<td
|
|
1055
|
+
className={cn(
|
|
1056
|
+
cellClass,
|
|
1057
|
+
alignClass,
|
|
1058
|
+
tabularNums && 'tabular-nums',
|
|
1059
|
+
monospace && 'font-mono',
|
|
1060
|
+
col.sticky === 'left' && stickyLeftClass,
|
|
1061
|
+
col.sticky === 'right' && stickyRightClass,
|
|
1062
|
+
)}
|
|
1063
|
+
style={{
|
|
1064
|
+
width: effectiveWidth != null ? `${effectiveWidth}px` : undefined,
|
|
1065
|
+
minWidth: col.minWidth != null ? `${col.minWidth}px` : undefined,
|
|
1066
|
+
}}
|
|
1067
|
+
>
|
|
1068
|
+
{content}
|
|
1069
|
+
</td>
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
// ───── Helpers ─────
|
|
1074
|
+
|
|
1075
|
+
function renderDefaultCell(value: unknown): ReactNode {
|
|
1076
|
+
if (value == null) return null;
|
|
1077
|
+
if (typeof value === 'boolean') return value ? '✓' : '✗';
|
|
1078
|
+
if (value instanceof Date) return value.toISOString().slice(0, 10);
|
|
1079
|
+
if (typeof value === 'object') return JSON.stringify(value);
|
|
1080
|
+
return value as ReactNode;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
function handleSortClick<T extends object>(
|
|
1084
|
+
col: TableColumn<T>,
|
|
1085
|
+
event: MouseEvent<HTMLButtonElement>,
|
|
1086
|
+
current: TableSortModel,
|
|
1087
|
+
setSortModel: (
|
|
1088
|
+
next: TableSortModel | ((prev: TableSortModel) => TableSortModel),
|
|
1089
|
+
) => void,
|
|
1090
|
+
) {
|
|
1091
|
+
if (!col.sortable) return;
|
|
1092
|
+
const multi = event.shiftKey;
|
|
1093
|
+
setSortModel((prev) => cycleSortFor(prev, col.field as string, multi));
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
function renderLoadingRows<T extends object>(args: {
|
|
1097
|
+
count: number;
|
|
1098
|
+
visibleCols: TableColumn<T>[];
|
|
1099
|
+
showSelectionColumn: boolean;
|
|
1100
|
+
showRowActionsColumn: boolean;
|
|
1101
|
+
rowHeight: number;
|
|
1102
|
+
rowClass: string;
|
|
1103
|
+
cellClass: string;
|
|
1104
|
+
}): ReactNode {
|
|
1105
|
+
const {
|
|
1106
|
+
count,
|
|
1107
|
+
visibleCols,
|
|
1108
|
+
showSelectionColumn,
|
|
1109
|
+
showRowActionsColumn,
|
|
1110
|
+
rowHeight,
|
|
1111
|
+
rowClass,
|
|
1112
|
+
cellClass,
|
|
1113
|
+
} = args;
|
|
1114
|
+
return Array.from({ length: count }).map((_, i) => (
|
|
1115
|
+
<tr
|
|
1116
|
+
key={`loading-${i}`}
|
|
1117
|
+
className={rowClass}
|
|
1118
|
+
aria-busy="true"
|
|
1119
|
+
style={{ height: rowHeight }}
|
|
1120
|
+
>
|
|
1121
|
+
{showSelectionColumn && (
|
|
1122
|
+
<td className={cellClass}>
|
|
1123
|
+
<Skeleton variant="rectangle" width="16px" height="16px" />
|
|
1124
|
+
</td>
|
|
1125
|
+
)}
|
|
1126
|
+
{visibleCols.map((col) => (
|
|
1127
|
+
<td key={col.field as string} className={cellClass}>
|
|
1128
|
+
<Skeleton variant="text" width="80%" />
|
|
1129
|
+
</td>
|
|
1130
|
+
))}
|
|
1131
|
+
{showRowActionsColumn && (
|
|
1132
|
+
<td className={cellClass}>
|
|
1133
|
+
<Skeleton variant="rectangle" width="24px" height="24px" />
|
|
1134
|
+
</td>
|
|
1135
|
+
)}
|
|
1136
|
+
</tr>
|
|
1137
|
+
));
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
function renderEmptyState(args: {
|
|
1141
|
+
totalColumnCount: number;
|
|
1142
|
+
emptyState: ReactNode;
|
|
1143
|
+
fallback: string;
|
|
1144
|
+
cellClass: string;
|
|
1145
|
+
}): ReactNode {
|
|
1146
|
+
return (
|
|
1147
|
+
<tr>
|
|
1148
|
+
<td colSpan={args.totalColumnCount} className={args.cellClass}>
|
|
1149
|
+
{args.emptyState ?? args.fallback}
|
|
1150
|
+
</td>
|
|
1151
|
+
</tr>
|
|
1152
|
+
);
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
// ───── Icons ─────
|
|
1156
|
+
|
|
1157
|
+
function SortIcon(props: { direction?: 'asc' | 'desc' }) {
|
|
1158
|
+
return (
|
|
1159
|
+
<svg
|
|
1160
|
+
width="0.75em"
|
|
1161
|
+
height="0.75em"
|
|
1162
|
+
viewBox="0 0 16 16"
|
|
1163
|
+
fill="currentColor"
|
|
1164
|
+
aria-hidden="true"
|
|
1165
|
+
className="shrink-0 opacity-60 group-hover:opacity-100"
|
|
1166
|
+
>
|
|
1167
|
+
{props.direction === 'asc' ? (
|
|
1168
|
+
<path d="M8 4l4 6H4z" />
|
|
1169
|
+
) : props.direction === 'desc' ? (
|
|
1170
|
+
<path d="M8 12L4 6h8z" />
|
|
1171
|
+
) : (
|
|
1172
|
+
<>
|
|
1173
|
+
<path d="M8 3l3 4H5z" opacity="0.5" />
|
|
1174
|
+
<path d="M8 13l-3-4h6z" opacity="0.5" />
|
|
1175
|
+
</>
|
|
1176
|
+
)}
|
|
1177
|
+
</svg>
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
function SearchIcon(props: { className?: string }) {
|
|
1182
|
+
return (
|
|
1183
|
+
<svg
|
|
1184
|
+
width="0.875em"
|
|
1185
|
+
height="0.875em"
|
|
1186
|
+
viewBox="0 0 16 16"
|
|
1187
|
+
fill="none"
|
|
1188
|
+
stroke="currentColor"
|
|
1189
|
+
strokeWidth="1.5"
|
|
1190
|
+
strokeLinecap="round"
|
|
1191
|
+
strokeLinejoin="round"
|
|
1192
|
+
aria-hidden="true"
|
|
1193
|
+
className={props.className}
|
|
1194
|
+
>
|
|
1195
|
+
<circle cx="7" cy="7" r="4.5" />
|
|
1196
|
+
<path d="M11 11l3 3" />
|
|
1197
|
+
</svg>
|
|
1198
|
+
);
|
|
1199
|
+
}
|