@jielga/tmdatagrid 0.1.0 → 0.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/README.md +35 -2
- package/dist/index.d.ts +792 -327
- package/dist/index.js +1581 -892
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +15 -7
- package/skills/columns/SKILL.md +8 -7
- package/skills/features/SKILL.md +104 -16
- package/skills/getting-started/SKILL.md +78 -20
- package/skills/options/SKILL.md +12 -7
- package/skills/server-side/SKILL.md +5 -2
- package/src/tmdatagrid/TMDataGridContext.ts +1 -1
- package/src/tmdatagrid/components/TMDataGrid.module.css +73 -0
- package/src/tmdatagrid/{TMDataGrid.tsx → components/TMDataGrid.tsx} +15 -4
- package/src/tmdatagrid/components/TMDataGridColumnsPanel.module.css +25 -0
- package/src/tmdatagrid/{TMDataGridColumnsPanel.tsx → components/TMDataGridColumnsPanel.tsx} +10 -4
- package/src/tmdatagrid/components/TMDataGridFilterPanel.module.css +35 -0
- package/src/tmdatagrid/{TMDataGridFilterPanel.tsx → components/TMDataGridFilterPanel.tsx} +91 -14
- package/src/tmdatagrid/components/TMDataGridFilterPills.module.css +35 -0
- package/src/tmdatagrid/components/TMDataGridFilterPills.tsx +135 -0
- package/src/tmdatagrid/components/TMDataGridFooter.module.css +18 -0
- package/src/tmdatagrid/components/TMDataGridFooter.tsx +171 -0
- package/src/tmdatagrid/components/TMDataGridGroupColumn.module.css +48 -0
- package/src/tmdatagrid/components/TMDataGridGroupColumn.tsx +147 -0
- package/src/tmdatagrid/components/TMDataGridHeaderCell.module.css +159 -0
- package/src/tmdatagrid/{TMDataGridHeaderCell.tsx → components/TMDataGridHeaderCell.tsx} +99 -14
- package/src/tmdatagrid/components/TMDataGridSelectColumn.tsx +177 -0
- package/src/tmdatagrid/components/TMDataGridTable.module.css +137 -0
- package/src/tmdatagrid/components/TMDataGridTable.tsx +720 -0
- package/src/tmdatagrid/components/TMDataGridToolbar.module.css +12 -0
- package/src/tmdatagrid/{TMDataGridToolbar.tsx → components/TMDataGridToolbar.tsx} +8 -5
- package/src/tmdatagrid/{icons.ts → components/icons.ts} +5 -0
- package/src/tmdatagrid/components/sticky.module.css +45 -0
- package/src/tmdatagrid/core/capabilities.ts +215 -0
- package/src/tmdatagrid/{columnOrdering.ts → core/columnOrdering.ts} +2 -2
- package/src/tmdatagrid/{columnUtils.ts → core/columnUtils.ts} +1 -1
- package/src/tmdatagrid/{filterOperators.ts → core/filterOperators.ts} +25 -0
- package/src/tmdatagrid/core/grouping.ts +21 -0
- package/src/tmdatagrid/{persistence.ts → core/persistence.ts} +92 -2
- package/src/tmdatagrid/core/rowSelection.ts +202 -0
- package/src/tmdatagrid/index.ts +39 -9
- package/src/tmdatagrid/useTMDataGrid.tsx +318 -66
- package/src/tmdatagrid/TMDataGrid.module.css +0 -410
- package/src/tmdatagrid/TMDataGridFooter.tsx +0 -74
- package/src/tmdatagrid/TMDataGridTable.tsx +0 -297
- package/src/tmdatagrid/capabilities.ts +0 -117
- /package/src/tmdatagrid/{sizes.ts → core/sizes.ts} +0 -0
|
@@ -0,0 +1,720 @@
|
|
|
1
|
+
import { Loader, Menu, type MenuProps, Text } from "@mantine/core";
|
|
2
|
+
import {
|
|
3
|
+
type Cell,
|
|
4
|
+
flexRender,
|
|
5
|
+
type Row,
|
|
6
|
+
type RowData,
|
|
7
|
+
type Table,
|
|
8
|
+
} from "@tanstack/react-table";
|
|
9
|
+
import { useSelector } from "@tanstack/react-store";
|
|
10
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
11
|
+
import {
|
|
12
|
+
type MouseEvent,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
useCallback,
|
|
15
|
+
useEffect,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from "react";
|
|
19
|
+
import classes from "./TMDataGridTable.module.css";
|
|
20
|
+
import sticky from "./sticky.module.css";
|
|
21
|
+
import { type TMDataGridRowData, useTMDataGridContext } from "../TMDataGridContext";
|
|
22
|
+
import { TMDataGridFilterPanel } from "./TMDataGridFilterPanel";
|
|
23
|
+
import { TMDataGridHeaderCell } from "./TMDataGridHeaderCell";
|
|
24
|
+
import { getColumnAlign } from "../core/columnUtils";
|
|
25
|
+
import {
|
|
26
|
+
getDisplayedRows,
|
|
27
|
+
resolveRowSelectionClick,
|
|
28
|
+
} from "../core/rowSelection";
|
|
29
|
+
import { SearchIcon } from "./icons";
|
|
30
|
+
import { SELECT_COLUMN_ID } from "./TMDataGridSelectColumn";
|
|
31
|
+
import type { TMDataGridFeatures } from "../useTMDataGrid";
|
|
32
|
+
|
|
33
|
+
/** Where a column sits relative to the pinned regions, resolved once per render. */
|
|
34
|
+
export type TMDataGridColumnLayout = {
|
|
35
|
+
pinnedAt: "left" | "right" | false;
|
|
36
|
+
/** Distance in px from the grid's left or right edge. */
|
|
37
|
+
offset: number;
|
|
38
|
+
/** Last left-pinned / first right-pinned column — the one that casts the edge. */
|
|
39
|
+
isBoundary: boolean;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const UNPINNED_LAYOUT: TMDataGridColumnLayout = {
|
|
43
|
+
pinnedAt: false,
|
|
44
|
+
offset: 0,
|
|
45
|
+
isBoundary: false,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* What a cell shows once grouping is in play.
|
|
50
|
+
*
|
|
51
|
+
* A group row carries a cell for every column, but only some of them have
|
|
52
|
+
* anything to say:
|
|
53
|
+
*
|
|
54
|
+
* | Cell | Renders |
|
|
55
|
+
* | ---- | ------- |
|
|
56
|
+
* | On a data row | the column's `cell`, as always |
|
|
57
|
+
* | Placeholder — a grouped column other than this row's own | nothing |
|
|
58
|
+
* | Aggregated, column declares `aggregatedCell` | that |
|
|
59
|
+
* | Aggregated, column declares an `aggregationFn` | the column's `cell`, over the aggregate |
|
|
60
|
+
* | Aggregated, column declares neither | nothing |
|
|
61
|
+
*
|
|
62
|
+
* That last row is the one worth spelling out. Without an aggregation function
|
|
63
|
+
* `getValue()` is `undefined` on a group row, so the column's own renderer
|
|
64
|
+
* would be handed nothing — and a renderer that formats what it gets
|
|
65
|
+
* (`value.toFixed(2)`) would throw. Blank is both the safe answer and the right
|
|
66
|
+
* one: a plain "group by" is a tree, not a summary, and a column only joins in
|
|
67
|
+
* once it has been told how.
|
|
68
|
+
*/
|
|
69
|
+
function renderCellContent(
|
|
70
|
+
cell: Cell<TMDataGridFeatures, TMDataGridRowData, unknown>,
|
|
71
|
+
) {
|
|
72
|
+
if (cell.getIsPlaceholder()) return null;
|
|
73
|
+
if (!cell.getIsAggregated()) {
|
|
74
|
+
return flexRender(cell.column.columnDef.cell, cell.getContext());
|
|
75
|
+
}
|
|
76
|
+
const { aggregatedCell, aggregationFn } = cell.column.columnDef;
|
|
77
|
+
if (aggregatedCell !== undefined) {
|
|
78
|
+
return flexRender(aggregatedCell, cell.getContext());
|
|
79
|
+
}
|
|
80
|
+
if (aggregationFn === undefined) return null;
|
|
81
|
+
return flexRender(cell.column.columnDef.cell, cell.getContext());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function TMDataGridBodyCell({
|
|
85
|
+
cell,
|
|
86
|
+
rowHeight,
|
|
87
|
+
layout,
|
|
88
|
+
onContextMenu,
|
|
89
|
+
}: {
|
|
90
|
+
cell: Cell<TMDataGridFeatures, TMDataGridRowData, unknown>;
|
|
91
|
+
rowHeight: number;
|
|
92
|
+
layout: TMDataGridColumnLayout;
|
|
93
|
+
onContextMenu?: (event: MouseEvent<HTMLDivElement>) => void;
|
|
94
|
+
}) {
|
|
95
|
+
return (
|
|
96
|
+
<div
|
|
97
|
+
role="cell"
|
|
98
|
+
data-align={getColumnAlign(cell.column)}
|
|
99
|
+
// The checkbox lane is a fixed track, so it cannot take the cell padding
|
|
100
|
+
// the scale grows for text. See TMDataGridTable.module.css.
|
|
101
|
+
data-select-column={cell.column.id === SELECT_COLUMN_ID}
|
|
102
|
+
onContextMenu={onContextMenu}
|
|
103
|
+
className={[
|
|
104
|
+
classes.bodyCell,
|
|
105
|
+
layout.isBoundary && layout.pinnedAt === "left" ? sticky.stickyLeft : "",
|
|
106
|
+
layout.isBoundary && layout.pinnedAt === "right"
|
|
107
|
+
? sticky.stickyRight
|
|
108
|
+
: "",
|
|
109
|
+
]
|
|
110
|
+
.filter(Boolean)
|
|
111
|
+
.join(" ")}
|
|
112
|
+
style={{
|
|
113
|
+
minHeight: rowHeight,
|
|
114
|
+
left: layout.pinnedAt === "left" ? layout.offset : undefined,
|
|
115
|
+
right: layout.pinnedAt === "right" ? layout.offset : undefined,
|
|
116
|
+
position: layout.pinnedAt ? "sticky" : undefined,
|
|
117
|
+
zIndex: layout.pinnedAt ? 2 : undefined,
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
<span className={classes.cellContent}>{renderCellContent(cell)}</span>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The virtualized body rows, wrapped in the single Menu that serves all of them
|
|
127
|
+
* when `rowContextMenu` is set.
|
|
128
|
+
*
|
|
129
|
+
* One instance for the whole body rather than one per row. A Mantine `Menu` is
|
|
130
|
+
* a `Popover`, and a closed Popover still runs its hooks on every render — and
|
|
131
|
+
* the body re-renders on every scroll frame, because that is how virtualization
|
|
132
|
+
* works. Wrapping each of ~20 mounted rows measured a fifth onto a body render;
|
|
133
|
+
* wrapping the group is the same behaviour for one instance.
|
|
134
|
+
*
|
|
135
|
+
* `display: contents` keeps the group out of the grid's layout while still
|
|
136
|
+
* taking the right-click, carrying `Menu.ContextMenu`'s handlers, and passing
|
|
137
|
+
* its inherited `user-select: none` down to the rows.
|
|
138
|
+
*/
|
|
139
|
+
function TMDataGridBodyRowGroup({
|
|
140
|
+
enabled,
|
|
141
|
+
menuContent,
|
|
142
|
+
menuProps,
|
|
143
|
+
onContextMenu,
|
|
144
|
+
onClose,
|
|
145
|
+
children,
|
|
146
|
+
}: {
|
|
147
|
+
enabled: boolean;
|
|
148
|
+
menuContent: ReactNode;
|
|
149
|
+
menuProps?: Omit<MenuProps, "opened" | "onChange" | "children">;
|
|
150
|
+
onContextMenu: () => void;
|
|
151
|
+
onClose: () => void;
|
|
152
|
+
children: ReactNode;
|
|
153
|
+
}) {
|
|
154
|
+
const rowGroup = (
|
|
155
|
+
<div
|
|
156
|
+
role="rowgroup"
|
|
157
|
+
style={{ display: "contents" }}
|
|
158
|
+
// Runs before Mantine's own handler, which `Menu.ContextMenu` composes on
|
|
159
|
+
// top of this one — so the row and cell are known by the time the
|
|
160
|
+
// dropdown is built and opened at the pointer.
|
|
161
|
+
onContextMenu={enabled ? onContextMenu : undefined}
|
|
162
|
+
>
|
|
163
|
+
{children}
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
if (!enabled) return rowGroup;
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<Menu
|
|
171
|
+
// Open only once the render prop has returned something: a row it answers
|
|
172
|
+
// `null` for never opens an empty dropdown.
|
|
173
|
+
opened={menuContent !== null}
|
|
174
|
+
onChange={(opened) => {
|
|
175
|
+
if (!opened) onClose();
|
|
176
|
+
}}
|
|
177
|
+
position="bottom-start"
|
|
178
|
+
shadow="md"
|
|
179
|
+
width={220}
|
|
180
|
+
{...menuProps}
|
|
181
|
+
>
|
|
182
|
+
<Menu.ContextMenu>{rowGroup}</Menu.ContextMenu>
|
|
183
|
+
<Menu.Dropdown>{menuContent}</Menu.Dropdown>
|
|
184
|
+
</Menu>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** What a `rowContextMenu` render prop is handed for the right-clicked row. */
|
|
189
|
+
export type TMDataGridRowContextMenuArgs<TData extends RowData> = {
|
|
190
|
+
table: Table<TMDataGridFeatures, TData>;
|
|
191
|
+
row: Row<TMDataGridFeatures, TData>;
|
|
192
|
+
/**
|
|
193
|
+
* The cell under the pointer. `null` only when the right-click landed on the
|
|
194
|
+
* row but on no cell — which a custom cell renderer that stops propagation
|
|
195
|
+
* can cause.
|
|
196
|
+
*/
|
|
197
|
+
cell: Cell<TMDataGridFeatures, TData, unknown> | null;
|
|
198
|
+
/**
|
|
199
|
+
* Closes the menu. `Menu.Item` already closes on click (Mantine's
|
|
200
|
+
* `closeOnItemClick`), so this is for content that isn't a menu item.
|
|
201
|
+
*/
|
|
202
|
+
close: () => void;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Builds the contents of a row's context menu — `Menu.Item`, `Menu.Label`,
|
|
207
|
+
* `Menu.Divider`, `Menu.Sub`, or any other node. The grid owns the `Menu` and
|
|
208
|
+
* its `Menu.Dropdown`; this fills the dropdown.
|
|
209
|
+
*/
|
|
210
|
+
export type TMDataGridRowContextMenu<TData extends RowData> = (
|
|
211
|
+
args: TMDataGridRowContextMenuArgs<TData>,
|
|
212
|
+
) => ReactNode;
|
|
213
|
+
|
|
214
|
+
export type TMDataGridTableProps<TData extends RowData> = {
|
|
215
|
+
/**
|
|
216
|
+
* Called when a body row is clicked. Runs in addition to row selection under
|
|
217
|
+
* `selectionMode: "row"`, not instead of it.
|
|
218
|
+
*/
|
|
219
|
+
onRowClick?: (row: Row<TMDataGridFeatures, TData>) => void;
|
|
220
|
+
/**
|
|
221
|
+
* Contents of the menu a right-click on a row opens, at the pointer. Return
|
|
222
|
+
* `null` to leave a row without one — the browser's own menu stays suppressed
|
|
223
|
+
* either way.
|
|
224
|
+
*
|
|
225
|
+
* ```tsx
|
|
226
|
+
* <TMDataGrid.Table<Employee>
|
|
227
|
+
* rowContextMenu={({ row, close }) => (
|
|
228
|
+
* <>
|
|
229
|
+
* <Menu.Label>{row.original.firstName}</Menu.Label>
|
|
230
|
+
* <Menu.Item onClick={() => open(row.original.id)}>Open</Menu.Item>
|
|
231
|
+
* <Menu.Divider />
|
|
232
|
+
* <Menu.Item color="red" onClick={() => remove(row.original.id)}>
|
|
233
|
+
* Delete
|
|
234
|
+
* </Menu.Item>
|
|
235
|
+
* </>
|
|
236
|
+
* )}
|
|
237
|
+
* />
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* Called during render, and only for the row whose menu is open — so it stays
|
|
241
|
+
* a pure function of the row, and cost per row does not matter.
|
|
242
|
+
*/
|
|
243
|
+
rowContextMenu?: TMDataGridRowContextMenu<TData>;
|
|
244
|
+
/**
|
|
245
|
+
* Passed to the Mantine `Menu` the grid wraps the row in — `width`, `shadow`,
|
|
246
|
+
* `position`, `transitionProps` and the rest. Its open state is the grid's.
|
|
247
|
+
*/
|
|
248
|
+
rowContextMenuProps?: Omit<MenuProps, "opened" | "onChange" | "children">;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The scrollable grid surface. Always virtualized: only the rows inside the
|
|
253
|
+
* viewport (plus overscan) are mounted — which is what makes the default
|
|
254
|
+
* no-pagination mode viable at any row count. Pagination is opt-in via
|
|
255
|
+
* `enablePagination` (or implied by `manualPagination`).
|
|
256
|
+
*/
|
|
257
|
+
export function TMDataGridTable<TData extends RowData = TMDataGridRowData>({
|
|
258
|
+
onRowClick,
|
|
259
|
+
rowContextMenu,
|
|
260
|
+
rowContextMenuProps,
|
|
261
|
+
}: TMDataGridTableProps<TData>) {
|
|
262
|
+
const { table, ui, features, rowHeight, controlSize } =
|
|
263
|
+
useTMDataGridContext();
|
|
264
|
+
|
|
265
|
+
// The body depends on every state slice (sorting, filters, paging, sizing,
|
|
266
|
+
// visibility, selection), so it subscribes to the whole table store.
|
|
267
|
+
useSelector(table.store);
|
|
268
|
+
// The active row lives in the chrome store, so it needs its own subscription.
|
|
269
|
+
const highlightedRowId = useSelector(ui, (state) => state.highlightedRowId);
|
|
270
|
+
|
|
271
|
+
const { loading, noResultsLabel = "No rows match your filters" } =
|
|
272
|
+
table.options.meta ?? {};
|
|
273
|
+
|
|
274
|
+
const scrollContainerRef = useRef<HTMLDivElement>(null);
|
|
275
|
+
// Pagination off (the default): every filtered+sorted row, virtualized.
|
|
276
|
+
// Pagination on: the current page; under `manualPagination` TanStack returns
|
|
277
|
+
// the rows as delivered, so the same branch serves server paging.
|
|
278
|
+
const rows = getDisplayedRows(table, features);
|
|
279
|
+
|
|
280
|
+
// A persisted pageIndex can outlive the data that produced it; TanStack only
|
|
281
|
+
// auto-resets on live filter/sort/data changes, not on restored state. The
|
|
282
|
+
// guard makes the dependency-free effect idempotent.
|
|
283
|
+
useEffect(() => {
|
|
284
|
+
if (!features.pagination) return;
|
|
285
|
+
const pageCount = table.getPageCount();
|
|
286
|
+
if (pageCount > 0 && table.store.state.pagination.pageIndex >= pageCount) {
|
|
287
|
+
table.setPageIndex(pageCount - 1);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
const virtualizer = useVirtualizer({
|
|
292
|
+
count: rows.length,
|
|
293
|
+
getScrollElement: () => scrollContainerRef.current,
|
|
294
|
+
estimateSize: useCallback(() => rowHeight, [rowHeight]),
|
|
295
|
+
getItemKey: useCallback((index: number) => rows[index]?.id ?? index, [rows]),
|
|
296
|
+
overscan: 6,
|
|
297
|
+
initialRect: { height: 600, width: 1200 },
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const virtualItems = virtualizer.getVirtualItems();
|
|
301
|
+
const paddingTop = virtualItems[0]?.start ?? 0;
|
|
302
|
+
const paddingBottom =
|
|
303
|
+
virtualizer.getTotalSize() - (virtualItems.at(-1)?.end ?? 0);
|
|
304
|
+
|
|
305
|
+
// The *Visible* variants throughout: `getLeftLeafColumns()` and friends
|
|
306
|
+
// include hidden columns, while the cells below come from
|
|
307
|
+
// `row.getLeftVisibleCells()`, which does not. Mixing the two would lay down
|
|
308
|
+
// a grid track for a column that renders no cell, shifting every column after
|
|
309
|
+
// it out of its header. The tree column is hidden exactly this way while
|
|
310
|
+
// nothing is grouped.
|
|
311
|
+
const leftLeafColumns = table.getLeftVisibleLeafColumns();
|
|
312
|
+
const rightLeafColumns = table.getRightVisibleLeafColumns();
|
|
313
|
+
const centerLeafColumns = table.getCenterVisibleLeafColumns();
|
|
314
|
+
|
|
315
|
+
const orderedColumns = [
|
|
316
|
+
...leftLeafColumns,
|
|
317
|
+
...centerLeafColumns,
|
|
318
|
+
...rightLeafColumns,
|
|
319
|
+
];
|
|
320
|
+
|
|
321
|
+
// Headers and cells must follow the same left → center → right order as the
|
|
322
|
+
// column tracks, otherwise pinning a column would shuffle it out of its lane.
|
|
323
|
+
const centerHeaderGroups = table.getCenterHeaderGroups();
|
|
324
|
+
const leftHeaderGroups = table.getLeftHeaderGroups();
|
|
325
|
+
const rightHeaderGroups = table.getRightHeaderGroups();
|
|
326
|
+
const headerGroups = centerHeaderGroups.map((group, index) => ({
|
|
327
|
+
id: group.id,
|
|
328
|
+
headers: [
|
|
329
|
+
...(leftHeaderGroups[index]?.headers ?? []),
|
|
330
|
+
...group.headers,
|
|
331
|
+
...(rightHeaderGroups[index]?.headers ?? []),
|
|
332
|
+
],
|
|
333
|
+
}));
|
|
334
|
+
// Unresized columns stay fluid (`minmax(min, flex fr)`) so the grid fills the
|
|
335
|
+
// viewport; dragging a resize handle pins that column to an exact pixel width.
|
|
336
|
+
// minWidth is the sum of those floors — anything wider and the grid would
|
|
337
|
+
// force a horizontal scrollbar it doesn't need.
|
|
338
|
+
//
|
|
339
|
+
// Pinned columns are always exact: their sticky offsets come from
|
|
340
|
+
// `getStart()` / `getAfter()`, which sum `getSize()` and cannot see an `fr`.
|
|
341
|
+
const columnSizing = table.store.state.columnSizing;
|
|
342
|
+
const columnTracks = orderedColumns.map((column) => {
|
|
343
|
+
const isFixed = column.columnDef.minSize === column.columnDef.maxSize;
|
|
344
|
+
const isResized = column.id in columnSizing;
|
|
345
|
+
const isPinned = column.getIsPinned() !== false;
|
|
346
|
+
const minWidth = column.columnDef.minSize ?? 80;
|
|
347
|
+
if (isFixed || isResized || isPinned) {
|
|
348
|
+
return { track: `${column.getSize()}px`, minWidth: column.getSize() };
|
|
349
|
+
}
|
|
350
|
+
return {
|
|
351
|
+
track: `minmax(${minWidth}px, ${column.columnDef.meta?.flex ?? 1}fr)`,
|
|
352
|
+
minWidth,
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
const gridTemplateColumns = columnTracks.map((c) => c.track).join(" ");
|
|
356
|
+
const gridMinWidth = columnTracks.reduce((sum, c) => sum + c.minWidth, 0);
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Sticky offsets are derived here, from state this component subscribes to,
|
|
360
|
+
* and handed down as props.
|
|
361
|
+
*
|
|
362
|
+
* Deriving them inside the cell via `column.getIsPinned()` /
|
|
363
|
+
* `column.getStart()` looks equivalent but isn't: those are plain calls on a
|
|
364
|
+
* `column` object whose identity survives a pin, so the React Compiler caches
|
|
365
|
+
* the result and the cell keeps rendering with stale pinning. Building the
|
|
366
|
+
* offsets from the same widths as the grid tracks also guarantees the two
|
|
367
|
+
* agree to the pixel.
|
|
368
|
+
*/
|
|
369
|
+
const columnLayout = new Map<string, TMDataGridColumnLayout>();
|
|
370
|
+
const widthOf = (columnId: string) =>
|
|
371
|
+
columnTracks[orderedColumns.findIndex((c) => c.id === columnId)]?.minWidth ??
|
|
372
|
+
0;
|
|
373
|
+
|
|
374
|
+
let leftOffset = 0;
|
|
375
|
+
for (const column of leftLeafColumns) {
|
|
376
|
+
columnLayout.set(column.id, {
|
|
377
|
+
pinnedAt: "left",
|
|
378
|
+
offset: leftOffset,
|
|
379
|
+
isBoundary: column.id === leftLeafColumns.at(-1)?.id,
|
|
380
|
+
});
|
|
381
|
+
leftOffset += widthOf(column.id);
|
|
382
|
+
}
|
|
383
|
+
let rightOffset = 0;
|
|
384
|
+
for (const column of [...rightLeafColumns].reverse()) {
|
|
385
|
+
columnLayout.set(column.id, {
|
|
386
|
+
pinnedAt: "right",
|
|
387
|
+
offset: rightOffset,
|
|
388
|
+
isBoundary: column.id === rightLeafColumns[0]?.id,
|
|
389
|
+
});
|
|
390
|
+
rightOffset += widthOf(column.id);
|
|
391
|
+
}
|
|
392
|
+
for (const column of centerLeafColumns) {
|
|
393
|
+
columnLayout.set(column.id, {
|
|
394
|
+
pinnedAt: false,
|
|
395
|
+
offset: 0,
|
|
396
|
+
isBoundary: false,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
const layoutFor = (columnId: string) =>
|
|
400
|
+
columnLayout.get(columnId) ?? UNPINNED_LAYOUT;
|
|
401
|
+
|
|
402
|
+
const isEmpty = rows.length === 0;
|
|
403
|
+
|
|
404
|
+
// "row" mode: the row itself is the selection control, with the modifier
|
|
405
|
+
// conventions of any desktop list — see resolveRowSelectionClick.
|
|
406
|
+
const selectsOnRowClick = features.rowClickSelects;
|
|
407
|
+
const rowIsInteractive =
|
|
408
|
+
selectsOnRowClick || features.highlightRow || onRowClick !== undefined;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Group rows sit out every row-level gesture.
|
|
412
|
+
*
|
|
413
|
+
* Not squeamishness: TanStack builds a group row on top of its first leaf's
|
|
414
|
+
* record (`constructRow(table, id, leafRows[0].original, …)`), so an
|
|
415
|
+
* `onRowClick` firing here would hand a consumer a real-looking row that is
|
|
416
|
+
* the wrong one — a detail panel would open on an arbitrary child. The same
|
|
417
|
+
* goes for the highlight, which is what a detail panel follows.
|
|
418
|
+
*
|
|
419
|
+
* Selecting a group is still possible, through its checkbox, which resolves
|
|
420
|
+
* to the descendants rather than to the group itself. See
|
|
421
|
+
* getSelectableRowIds.
|
|
422
|
+
*/
|
|
423
|
+
const rowGesturesFor = (row: Row<TMDataGridFeatures, TMDataGridRowData>) =>
|
|
424
|
+
row.getIsGrouped() ? false : rowIsInteractive;
|
|
425
|
+
|
|
426
|
+
const handleRowActivate = (
|
|
427
|
+
row: Row<TMDataGridFeatures, TMDataGridRowData>,
|
|
428
|
+
modifiers: { toggle: boolean; extend: boolean },
|
|
429
|
+
) => {
|
|
430
|
+
if (selectsOnRowClick && row.getCanSelect()) {
|
|
431
|
+
const resolved = resolveRowSelectionClick({
|
|
432
|
+
rows,
|
|
433
|
+
rowId: row.id,
|
|
434
|
+
anchorRowId: ui.state.selectionAnchorRowId,
|
|
435
|
+
modifiers,
|
|
436
|
+
selection: table.store.state.rowSelection,
|
|
437
|
+
canReplaceSelection: true,
|
|
438
|
+
});
|
|
439
|
+
table.setRowSelection(resolved.selection);
|
|
440
|
+
ui.actions.setSelectionAnchor(resolved.anchorRowId);
|
|
441
|
+
}
|
|
442
|
+
// Set, never toggle: a second click on the highlighted row must not close
|
|
443
|
+
// the detail panel showing it. Clearing is
|
|
444
|
+
// `ui.actions.setHighlightedRow(null)`.
|
|
445
|
+
if (features.highlightRow) ui.actions.setHighlightedRow(row.id);
|
|
446
|
+
onRowClick?.(row as unknown as Row<TMDataGridFeatures, TData>);
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
// Which row's context menu is open, and which cell it was opened from.
|
|
450
|
+
const [contextMenuTarget, setContextMenuTarget] = useState<{
|
|
451
|
+
rowId: string;
|
|
452
|
+
columnId: string | null;
|
|
453
|
+
} | null>(null);
|
|
454
|
+
const closeContextMenu = () => setContextMenuTarget(null);
|
|
455
|
+
|
|
456
|
+
// Row and cell each record themselves on the way up, so that by the time the
|
|
457
|
+
// rowgroup's handler opens the menu it knows what was hit. Refs rather than
|
|
458
|
+
// state: written and read inside the same event, before the render it causes.
|
|
459
|
+
const contextMenuRowRef = useRef<string | null>(null);
|
|
460
|
+
const contextMenuColumnRef = useRef<string | null>(null);
|
|
461
|
+
|
|
462
|
+
const handleContextMenu = () => {
|
|
463
|
+
const rowId = contextMenuRowRef.current;
|
|
464
|
+
const columnId = contextMenuColumnRef.current;
|
|
465
|
+
contextMenuRowRef.current = null;
|
|
466
|
+
contextMenuColumnRef.current = null;
|
|
467
|
+
setContextMenuTarget(rowId === null ? null : { rowId, columnId });
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
const contextMenuRow =
|
|
471
|
+
contextMenuTarget === null
|
|
472
|
+
? undefined
|
|
473
|
+
: rows.find((row) => row.id === contextMenuTarget.rowId);
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Built for the open row only, so a consumer's render prop is called once per
|
|
477
|
+
* opened menu rather than once per mounted row. `null` back from it means
|
|
478
|
+
* this row has no menu, which is also how the menu stays shut.
|
|
479
|
+
*
|
|
480
|
+
* The casts put back the concrete row type the context boundary erased — the
|
|
481
|
+
* same crossing `onRowClick` makes.
|
|
482
|
+
*/
|
|
483
|
+
const contextMenuContent =
|
|
484
|
+
rowContextMenu && contextMenuTarget && contextMenuRow
|
|
485
|
+
? rowContextMenu({
|
|
486
|
+
table: table as unknown as Table<TMDataGridFeatures, TData>,
|
|
487
|
+
row: contextMenuRow as unknown as Row<TMDataGridFeatures, TData>,
|
|
488
|
+
cell: (contextMenuRow
|
|
489
|
+
.getAllCells()
|
|
490
|
+
.find((cell) => cell.column.id === contextMenuTarget.columnId) ??
|
|
491
|
+
null) as unknown as Cell<TMDataGridFeatures, TData, unknown> | null,
|
|
492
|
+
close: closeContextMenu,
|
|
493
|
+
}) ?? null
|
|
494
|
+
: null;
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* The dropdown is anchored to a fixed viewport point, so it does not travel
|
|
498
|
+
* with the row when the body scrolls — and under virtualization the row it
|
|
499
|
+
* belongs to may unmount entirely. Closing is the honest answer, and matches
|
|
500
|
+
* what every desktop context menu does.
|
|
501
|
+
*/
|
|
502
|
+
const handleScroll = contextMenuTarget ? closeContextMenu : undefined;
|
|
503
|
+
|
|
504
|
+
return (
|
|
505
|
+
<div className={classes.tableWrapper}>
|
|
506
|
+
<div
|
|
507
|
+
ref={scrollContainerRef}
|
|
508
|
+
className={classes.scrollContainer}
|
|
509
|
+
onScroll={handleScroll}
|
|
510
|
+
>
|
|
511
|
+
<div
|
|
512
|
+
role="table"
|
|
513
|
+
className={classes.grid}
|
|
514
|
+
// Virtualization means most rows are not in the DOM, so the counts
|
|
515
|
+
// have to be stated rather than inferred from what is rendered. Every
|
|
516
|
+
// row carries its aria-rowindex for the same reason.
|
|
517
|
+
aria-rowcount={rows.length + headerGroups.length}
|
|
518
|
+
aria-colcount={orderedColumns.length}
|
|
519
|
+
style={{ gridTemplateColumns, minWidth: gridMinWidth }}
|
|
520
|
+
>
|
|
521
|
+
<div role="rowgroup" style={{ display: "contents" }}>
|
|
522
|
+
{headerGroups.map((headerGroup, groupIndex) => (
|
|
523
|
+
<div
|
|
524
|
+
key={headerGroup.id}
|
|
525
|
+
role="row"
|
|
526
|
+
aria-rowindex={groupIndex + 1}
|
|
527
|
+
className={classes.headerRow}
|
|
528
|
+
>
|
|
529
|
+
{headerGroup.headers.map((header) => (
|
|
530
|
+
<TMDataGridHeaderCell
|
|
531
|
+
key={header.id}
|
|
532
|
+
header={header}
|
|
533
|
+
layout={layoutFor(header.column.id)}
|
|
534
|
+
/>
|
|
535
|
+
))}
|
|
536
|
+
</div>
|
|
537
|
+
))}
|
|
538
|
+
</div>
|
|
539
|
+
|
|
540
|
+
{paddingTop > 0 && (
|
|
541
|
+
<div aria-hidden style={{ gridColumn: "1/-1", height: paddingTop }} />
|
|
542
|
+
)}
|
|
543
|
+
|
|
544
|
+
<TMDataGridBodyRowGroup
|
|
545
|
+
enabled={rowContextMenu !== undefined}
|
|
546
|
+
menuContent={contextMenuContent}
|
|
547
|
+
menuProps={rowContextMenuProps}
|
|
548
|
+
onContextMenu={handleContextMenu}
|
|
549
|
+
onClose={closeContextMenu}
|
|
550
|
+
>
|
|
551
|
+
{virtualItems.map((virtualItem) => {
|
|
552
|
+
const row = rows[virtualItem.index];
|
|
553
|
+
if (!row) return null;
|
|
554
|
+
const isGroupRow = row.getIsGrouped();
|
|
555
|
+
const isInteractive = rowGesturesFor(row);
|
|
556
|
+
const takesKeyboard =
|
|
557
|
+
!isGroupRow && (selectsOnRowClick || features.highlightRow);
|
|
558
|
+
return (
|
|
559
|
+
<div
|
|
560
|
+
key={virtualItem.key}
|
|
561
|
+
role="row"
|
|
562
|
+
aria-rowindex={virtualItem.index + headerGroups.length + 1}
|
|
563
|
+
data-testid={`dg-row-${row.id}`}
|
|
564
|
+
// The tree's own rows. `data-grouped` rather than a class, to
|
|
565
|
+
// match how the rest of the row's state is published — and so
|
|
566
|
+
// a consumer can restyle them without reaching into modules.
|
|
567
|
+
data-grouped={isGroupRow}
|
|
568
|
+
data-depth={row.depth}
|
|
569
|
+
// Gated on the flag, not just on `getIsSelected()`: under
|
|
570
|
+
// `"highlight"` there is no selection, but `rowSelection` may
|
|
571
|
+
// still hold ids from before the mode changed — TanStack never
|
|
572
|
+
// prunes it — and a row must not report itself selected in a
|
|
573
|
+
// mode where selecting is not a thing.
|
|
574
|
+
data-selected={features.rowSelection && row.getIsSelected()}
|
|
575
|
+
// Being selected is state; painting it is a display choice, so
|
|
576
|
+
// the two are separate attributes.
|
|
577
|
+
data-selected-bg={
|
|
578
|
+
features.rowSelection &&
|
|
579
|
+
features.showSelectedBackground &&
|
|
580
|
+
row.getIsSelected()
|
|
581
|
+
}
|
|
582
|
+
// The highlighted row is its own concept, so its own attribute
|
|
583
|
+
// pair — `data-highlighted` / `aria-current` against
|
|
584
|
+
// `data-selected` / `aria-selected` for selection. Under
|
|
585
|
+
// `"checkboxAndHighlight"` a row can carry both.
|
|
586
|
+
//
|
|
587
|
+
// Not `data-active`: header cells already use that for
|
|
588
|
+
// sorted-or-filtered, and Mantine puts it on its own controls,
|
|
589
|
+
// so a consumer styling or querying rows by it would cast far
|
|
590
|
+
// wider than they meant to.
|
|
591
|
+
data-highlighted={
|
|
592
|
+
features.highlightRow && row.id === highlightedRowId
|
|
593
|
+
}
|
|
594
|
+
// The menu is anchored to the rowgroup, so Mantine's own
|
|
595
|
+
// `data-expanded` lands there rather than on a row. This is
|
|
596
|
+
// what says which row the open menu is about.
|
|
597
|
+
data-context-menu={
|
|
598
|
+
contextMenuContent !== null &&
|
|
599
|
+
contextMenuTarget?.rowId === row.id
|
|
600
|
+
}
|
|
601
|
+
data-selects-on-click={selectsOnRowClick && !isGroupRow}
|
|
602
|
+
aria-selected={
|
|
603
|
+
features.rowSelection ? row.getIsSelected() : undefined
|
|
604
|
+
}
|
|
605
|
+
aria-current={
|
|
606
|
+
features.highlightRow && row.id === highlightedRowId
|
|
607
|
+
? true
|
|
608
|
+
: undefined
|
|
609
|
+
}
|
|
610
|
+
className={classes.bodyRow}
|
|
611
|
+
style={{
|
|
612
|
+
minHeight: rowHeight,
|
|
613
|
+
cursor: isInteractive ? "pointer" : undefined,
|
|
614
|
+
}}
|
|
615
|
+
// Keyboard parity with the checkbox the row replaces.
|
|
616
|
+
tabIndex={takesKeyboard ? 0 : undefined}
|
|
617
|
+
// Shift-click extends the browser's text selection across
|
|
618
|
+
// every row it passes. `user-select: none` would fix it too,
|
|
619
|
+
// but at the cost of ever copying a cell value, so the smear
|
|
620
|
+
// is stopped at the one gesture that causes it.
|
|
621
|
+
onMouseDown={
|
|
622
|
+
selectsOnRowClick && !isGroupRow
|
|
623
|
+
? (event) => {
|
|
624
|
+
if (event.shiftKey) event.preventDefault();
|
|
625
|
+
}
|
|
626
|
+
: undefined
|
|
627
|
+
}
|
|
628
|
+
onClick={
|
|
629
|
+
isInteractive
|
|
630
|
+
? (event) =>
|
|
631
|
+
handleRowActivate(row, {
|
|
632
|
+
toggle: event.ctrlKey || event.metaKey,
|
|
633
|
+
extend: event.shiftKey,
|
|
634
|
+
})
|
|
635
|
+
: undefined
|
|
636
|
+
}
|
|
637
|
+
onKeyDown={
|
|
638
|
+
takesKeyboard
|
|
639
|
+
? (event) => {
|
|
640
|
+
if (event.key !== " " && event.key !== "Enter") return;
|
|
641
|
+
if (event.target !== event.currentTarget) return;
|
|
642
|
+
event.preventDefault();
|
|
643
|
+
// Space/Enter toggle rather than replace, so a
|
|
644
|
+
// keyboard-only user can still build a selection.
|
|
645
|
+
handleRowActivate(row, {
|
|
646
|
+
toggle: true,
|
|
647
|
+
extend: event.shiftKey,
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
: undefined
|
|
651
|
+
}
|
|
652
|
+
// Bubbles to the rowgroup, where the menu opens — so the row
|
|
653
|
+
// is recorded before the dropdown is built.
|
|
654
|
+
//
|
|
655
|
+
// Group rows record nothing, which leaves the ref null and the
|
|
656
|
+
// menu shut. Same reason they sit out `onRowClick`: TanStack
|
|
657
|
+
// builds a group row on its first child's record, so a render
|
|
658
|
+
// prop reaching for `row.original` would be handed a real
|
|
659
|
+
// employee that has nothing to do with the group. The browser
|
|
660
|
+
// menu stays suppressed over them either way.
|
|
661
|
+
onContextMenu={
|
|
662
|
+
rowContextMenu && !isGroupRow
|
|
663
|
+
? () => {
|
|
664
|
+
contextMenuRowRef.current = row.id;
|
|
665
|
+
}
|
|
666
|
+
: undefined
|
|
667
|
+
}
|
|
668
|
+
>
|
|
669
|
+
{[
|
|
670
|
+
...row.getLeftVisibleCells(),
|
|
671
|
+
...row.getCenterVisibleCells(),
|
|
672
|
+
...row.getRightVisibleCells(),
|
|
673
|
+
].map((cell) => (
|
|
674
|
+
<TMDataGridBodyCell
|
|
675
|
+
key={cell.id}
|
|
676
|
+
cell={cell}
|
|
677
|
+
rowHeight={rowHeight}
|
|
678
|
+
layout={layoutFor(cell.column.id)}
|
|
679
|
+
onContextMenu={
|
|
680
|
+
rowContextMenu
|
|
681
|
+
? () => {
|
|
682
|
+
contextMenuColumnRef.current = cell.column.id;
|
|
683
|
+
}
|
|
684
|
+
: undefined
|
|
685
|
+
}
|
|
686
|
+
/>
|
|
687
|
+
))}
|
|
688
|
+
</div>
|
|
689
|
+
);
|
|
690
|
+
})}
|
|
691
|
+
</TMDataGridBodyRowGroup>
|
|
692
|
+
|
|
693
|
+
{isEmpty && loading === true && (
|
|
694
|
+
<div role="row" className={classes.messageRow}>
|
|
695
|
+
<Loader size="lg" />
|
|
696
|
+
</div>
|
|
697
|
+
)}
|
|
698
|
+
|
|
699
|
+
{isEmpty && loading !== true && (
|
|
700
|
+
<div role="row" className={classes.messageRow}>
|
|
701
|
+
<SearchIcon size={40} stroke={1.4} opacity={0.4} />
|
|
702
|
+
<Text size={controlSize} c="dimmed">
|
|
703
|
+
{noResultsLabel}
|
|
704
|
+
</Text>
|
|
705
|
+
</div>
|
|
706
|
+
)}
|
|
707
|
+
|
|
708
|
+
{paddingBottom > 0 && (
|
|
709
|
+
<div
|
|
710
|
+
aria-hidden
|
|
711
|
+
style={{ gridColumn: "1/-1", height: paddingBottom }}
|
|
712
|
+
/>
|
|
713
|
+
)}
|
|
714
|
+
</div>
|
|
715
|
+
</div>
|
|
716
|
+
|
|
717
|
+
<TMDataGridFilterPanel />
|
|
718
|
+
</div>
|
|
719
|
+
);
|
|
720
|
+
}
|