-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDataTable.tsx
More file actions
358 lines (343 loc) · 12.5 KB
/
Copy pathDataTable.tsx
File metadata and controls
358 lines (343 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import type { ColumnDef, RowData, SortingState, VisibilityState } from '@tanstack/react-table';
import type { Ref } from 'react';
import type { Selection as AriaSelection, Key, SortDescriptor } from 'react-aria-components';
import { Icon } from '@launchpad-ui/icons';
import {
flexRender,
getCoreRowModel,
getFilteredRowModel,
getSortedRowModel,
useReactTable,
} from '@tanstack/react-table';
import { useState } from 'react';
import { Group } from './Group';
import { IconButton } from './IconButton';
import { Input } from './Input';
import { Menu, MenuItem, MenuTrigger } from './Menu';
import { Popover } from './Popover';
import { SearchField } from './SearchField';
import styles from './styles/DataTable.module.css';
import { Cell, Column, ResizableTableContainer, Row, Table, TableBody, TableHeader } from './Table';
// Augment TanStack column meta with the extra hints DataTable needs. `label` gives
// the column-visibility menu a human string when `header` is a render function;
// `isRowHeader` marks which column RAC should treat as the row's header cell.
declare module '@tanstack/react-table' {
// biome-ignore lint/correctness/noUnusedVariables: augmentation must match TanStack's generic signature
interface ColumnMeta<TData extends RowData, TValue> {
label?: string;
isRowHeader?: boolean;
/** Horizontal alignment; `end` right-aligns header + cells (numeric/currency/delta). */
align?: 'start' | 'end';
/** Set false to pin a column (excluded from drag-reorder, e.g. a row-actions column). */
enableReorder?: boolean;
}
}
interface DataTableProps<TData> {
/** Accessible name for the table. */
'aria-label': string;
/** Row data. */
data: TData[];
/** TanStack column definitions (accessorKey/accessorFn, header, cell, enableSorting, enableHiding, meta). */
columns: ColumnDef<TData, unknown>[];
/** Stable row id — used as the RAC row key and for selection. Defaults to the row index. */
getRowId?: (row: TData, index: number) => string;
/** Initial sort (uncontrolled), e.g. `[{ id: 'status', desc: true }]`. */
defaultSorting?: SortingState;
/** Row density, matching the Figma table system. Default `default`. */
density?: 'default' | 'compact' | 'tight';
/** RAC selection mode. Selection is delegated to RAC (checkbox column injected automatically). */
selectionMode?: 'none' | 'single' | 'multiple';
/**
* `toggle` (default) shows a checkbox column; `replace` selects the whole row on click with
* a highlight and no checkbox — the list/detail pattern (click a trace row to open it).
*/
selectionBehavior?: 'toggle' | 'replace';
selectedKeys?: AriaSelection;
onSelectionChange?: (keys: AriaSelection) => void;
/**
* Called when a row is activated (click or Enter) — the click-row-to-open-detail pattern
* (e.g. a traces list opening a trace). Receives the row id. RAC suppresses this when the
* press lands on an interactive cell (checkbox, actions menu).
*/
onRowAction?: (id: string) => void;
/** Show the freeform search field in the toolbar (TanStack global filter). Default true. */
enableGlobalFilter?: boolean;
/** Show the column-visibility menu in the toolbar (TanStack column visibility). Default true. */
enableColumnVisibility?: boolean;
/** Wrap in a RAC ResizableTableContainer so columns can be resized. Default false. */
enableColumnResize?: boolean;
/**
* Allow reordering columns by dragging a grip handle in the header (TanStack `columnOrder`).
* RAC has no native column DnD, so this uses lightweight HTML5 drag — mouse-based; keyboard
* reorder is a follow-up. Pin a column with `meta.enableReorder: false`.
*/
enableColumnReorder?: boolean;
ref?: Ref<HTMLTableElement>;
}
function columnLabel<TData>(columnDef: ColumnDef<TData, unknown>, id: string): string {
if (columnDef.meta?.label) {
return columnDef.meta.label;
}
if (typeof columnDef.header === 'string') {
return columnDef.header;
}
return id;
}
/**
* A data table takes declarative `columns` + `data` and layers the data-heavy interactions
* product surfaces expect — freeform search, sorting, row selection, column visibility, and
* resize — on top of the LaunchPad RAC `Table`.
*
* Division of labor: RAC owns markup, keyboard nav, a11y, selection, sort UI, DnD, and resize;
* TanStack Table v8 is the headless model for filtering, sorting, and column visibility.
*/
const DataTable = <TData,>({
data,
columns,
getRowId,
defaultSorting,
density = 'default',
selectionMode = 'none',
selectionBehavior,
selectedKeys,
onSelectionChange,
onRowAction,
enableGlobalFilter = true,
enableColumnVisibility = true,
enableColumnResize = false,
enableColumnReorder = false,
ref,
...props
}: DataTableProps<TData>) => {
const [sorting, setSorting] = useState<SortingState>(defaultSorting ?? []);
const [globalFilter, setGlobalFilter] = useState('');
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
const [columnOrder, setColumnOrder] = useState<string[]>([]);
// Column drag-reorder (HTML5 DnD): the column being dragged and the current drop target.
const [dragColumnId, setDragColumnId] = useState<string | null>(null);
const [dropColumnId, setDropColumnId] = useState<string | null>(null);
const table = useReactTable({
data,
columns,
state: { sorting, globalFilter, columnVisibility, columnOrder },
getRowId,
onSortingChange: setSorting,
onGlobalFilterChange: setGlobalFilter,
onColumnVisibilityChange: setColumnVisibility,
onColumnOrderChange: setColumnOrder,
getCoreRowModel: getCoreRowModel(),
getFilteredRowModel: getFilteredRowModel(),
getSortedRowModel: getSortedRowModel(),
enableSortingRemoval: false,
});
// Move `fromId` to `toId`'s slot in the full leaf-column order and commit it to TanStack,
// which re-derives both header and cell order (RAC then re-renders in lockstep).
const reorderColumn = (fromId: string, toId: string) => {
const order = table.getAllLeafColumns().map((column) => column.id);
const from = order.indexOf(fromId);
const to = order.indexOf(toId);
if (from === -1 || to === -1 || from === to) {
return;
}
order.splice(from, 1);
order.splice(to, 0, fromId);
setColumnOrder(order);
};
// Bridge: derive RAC's single-column SortDescriptor from TanStack sorting state, and
// translate RAC sort changes back into TanStack state so the sort UI is RAC's but the
// actual ordering is computed by TanStack's row model.
const sort = sorting[0];
const sortDescriptor: SortDescriptor | undefined = sort
? { column: sort.id, direction: sort.desc ? 'descending' : 'ascending' }
: undefined;
const onSortChange = (descriptor: SortDescriptor) => {
setSorting([{ id: String(descriptor.column), desc: descriptor.direction === 'descending' }]);
};
const headers = table.getHeaderGroups()[0]?.headers ?? [];
const rows = table.getRowModel().rows;
// RAC memoizes dynamic-collection rows by their stable id, so toggling column
// visibility/order updates the (statically-mapped) header but leaves cached cells
// behind — tripping RAC's "cell count must match column count" invariant. Feeding
// a `dependencies` value that changes with the visible columns invalidates that
// cache in lockstep. It must be a single, constant-size entry: RAC spreads
// `dependencies` into a useMemo deps array, and React requires a stable size.
const visibleColumnsKey = table
.getVisibleLeafColumns()
.map((column) => column.id)
.join('|');
const hideableColumns = table.getAllLeafColumns().filter((column) => column.getCanHide());
const visibleColumnKeys = new Set<Key>(
hideableColumns.filter((column) => column.getIsVisible()).map((column) => column.id),
);
const onColumnSelectionChange = (keys: AriaSelection) => {
const next: VisibilityState = {};
for (const column of hideableColumns) {
next[column.id] = keys === 'all' || keys.has(column.id);
}
setColumnVisibility(next);
};
const tableElement = (
<Table
aria-label={props['aria-label']}
ref={ref}
selectionMode={selectionMode === 'none' ? undefined : selectionMode}
selectionBehavior={selectionBehavior}
selectedKeys={selectedKeys}
onSelectionChange={onSelectionChange}
onRowAction={onRowAction ? (key) => onRowAction(String(key)) : undefined}
sortDescriptor={sortDescriptor}
onSortChange={onSortChange}
>
<TableHeader>
{headers.map((header, index) => {
const { columnDef } = header.column;
const columnId = header.column.id;
const isRowHeader = columnDef.meta?.isRowHeader ?? index === 0;
const reorderable = enableColumnReorder && columnDef.meta?.enableReorder !== false;
const content = flexRender(columnDef.header, header.getContext());
return (
<Column
key={header.id}
id={columnId}
isRowHeader={isRowHeader}
allowsSorting={header.column.getCanSort()}
className={columnDef.meta?.align === 'end' ? styles.alignEnd : undefined}
>
{reorderable ? (
// biome-ignore lint/a11y/noStaticElementInteractions: HTML5 drop target for column reorder; keyboard reorder is a documented follow-up
<span
className={
dropColumnId === columnId
? `${styles.reorderHeader} ${styles.dropTarget}`
: styles.reorderHeader
}
onDragOver={(e) => {
if (dragColumnId && dragColumnId !== columnId) {
e.preventDefault();
setDropColumnId(columnId);
}
}}
onDragLeave={() =>
setDropColumnId((current) => (current === columnId ? null : current))
}
onDrop={(e) => {
e.preventDefault();
if (dragColumnId) {
reorderColumn(dragColumnId, columnId);
}
setDragColumnId(null);
setDropColumnId(null);
}}
>
{/* Grip is the drag origin; stop pointer/click from reaching RAC's sort press. */}
<span
className={styles.grip}
role="button"
tabIndex={-1}
draggable
aria-label="Drag to reorder column"
onPointerDown={(e) => e.stopPropagation()}
onDragStart={(e) => {
setDragColumnId(columnId);
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', columnId);
}}
onDragEnd={() => {
setDragColumnId(null);
setDropColumnId(null);
}}
>
<Icon name="grip-horiz" size="small" />
</span>
{content}
</span>
) : (
content
)}
</Column>
);
})}
</TableHeader>
<TableBody
items={rows}
dependencies={[visibleColumnsKey]}
renderEmptyState={() => 'No results found'}
>
{(row) => (
<Row id={row.id}>
{row.getVisibleCells().map((cell) => (
<Cell
key={cell.id}
className={
cell.column.columnDef.meta?.align === 'end' ? styles.alignEnd : undefined
}
>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</Cell>
))}
</Row>
)}
</TableBody>
</Table>
);
const showToolbar = enableGlobalFilter || enableColumnVisibility;
return (
<div className={styles.root} data-density={density}>
{showToolbar && (
<div className={styles.toolbar}>
{enableGlobalFilter && (
<SearchField
aria-label="Search"
value={globalFilter}
onChange={setGlobalFilter}
className={styles.search}
>
<Group>
<Icon name="search" size="small" />
<Input placeholder="Search" />
<IconButton
icon="cancel-circle-outline"
aria-label="Clear search"
size="small"
variant="minimal"
/>
</Group>
</SearchField>
)}
{enableColumnVisibility && hideableColumns.length > 0 && (
<span className={styles.columnMenu}>
<MenuTrigger>
<IconButton
icon="more-horiz"
aria-label="Column settings"
variant="minimal"
size="small"
/>
<Popover>
<Menu
selectionMode="multiple"
selectedKeys={visibleColumnKeys}
onSelectionChange={onColumnSelectionChange}
>
{hideableColumns.map((column) => (
<MenuItem key={column.id} id={column.id}>
{columnLabel(column.columnDef, column.id)}
</MenuItem>
))}
</Menu>
</Popover>
</MenuTrigger>
</span>
)}
</div>
)}
{enableColumnResize ? (
<ResizableTableContainer>{tableElement}</ResizableTableContainer>
) : (
tableElement
)}
</div>
);
};
export { DataTable };
export type { DataTableProps };