@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.
Files changed (47) hide show
  1. package/README.md +35 -2
  2. package/dist/index.d.ts +792 -327
  3. package/dist/index.js +1581 -892
  4. package/dist/index.js.map +1 -1
  5. package/dist/styles.css +1 -1
  6. package/package.json +15 -7
  7. package/skills/columns/SKILL.md +8 -7
  8. package/skills/features/SKILL.md +104 -16
  9. package/skills/getting-started/SKILL.md +78 -20
  10. package/skills/options/SKILL.md +12 -7
  11. package/skills/server-side/SKILL.md +5 -2
  12. package/src/tmdatagrid/TMDataGridContext.ts +1 -1
  13. package/src/tmdatagrid/components/TMDataGrid.module.css +73 -0
  14. package/src/tmdatagrid/{TMDataGrid.tsx → components/TMDataGrid.tsx} +15 -4
  15. package/src/tmdatagrid/components/TMDataGridColumnsPanel.module.css +25 -0
  16. package/src/tmdatagrid/{TMDataGridColumnsPanel.tsx → components/TMDataGridColumnsPanel.tsx} +10 -4
  17. package/src/tmdatagrid/components/TMDataGridFilterPanel.module.css +35 -0
  18. package/src/tmdatagrid/{TMDataGridFilterPanel.tsx → components/TMDataGridFilterPanel.tsx} +91 -14
  19. package/src/tmdatagrid/components/TMDataGridFilterPills.module.css +35 -0
  20. package/src/tmdatagrid/components/TMDataGridFilterPills.tsx +135 -0
  21. package/src/tmdatagrid/components/TMDataGridFooter.module.css +18 -0
  22. package/src/tmdatagrid/components/TMDataGridFooter.tsx +171 -0
  23. package/src/tmdatagrid/components/TMDataGridGroupColumn.module.css +48 -0
  24. package/src/tmdatagrid/components/TMDataGridGroupColumn.tsx +147 -0
  25. package/src/tmdatagrid/components/TMDataGridHeaderCell.module.css +159 -0
  26. package/src/tmdatagrid/{TMDataGridHeaderCell.tsx → components/TMDataGridHeaderCell.tsx} +99 -14
  27. package/src/tmdatagrid/components/TMDataGridSelectColumn.tsx +177 -0
  28. package/src/tmdatagrid/components/TMDataGridTable.module.css +137 -0
  29. package/src/tmdatagrid/components/TMDataGridTable.tsx +720 -0
  30. package/src/tmdatagrid/components/TMDataGridToolbar.module.css +12 -0
  31. package/src/tmdatagrid/{TMDataGridToolbar.tsx → components/TMDataGridToolbar.tsx} +8 -5
  32. package/src/tmdatagrid/{icons.ts → components/icons.ts} +5 -0
  33. package/src/tmdatagrid/components/sticky.module.css +45 -0
  34. package/src/tmdatagrid/core/capabilities.ts +215 -0
  35. package/src/tmdatagrid/{columnOrdering.ts → core/columnOrdering.ts} +2 -2
  36. package/src/tmdatagrid/{columnUtils.ts → core/columnUtils.ts} +1 -1
  37. package/src/tmdatagrid/{filterOperators.ts → core/filterOperators.ts} +25 -0
  38. package/src/tmdatagrid/core/grouping.ts +21 -0
  39. package/src/tmdatagrid/{persistence.ts → core/persistence.ts} +92 -2
  40. package/src/tmdatagrid/core/rowSelection.ts +202 -0
  41. package/src/tmdatagrid/index.ts +39 -9
  42. package/src/tmdatagrid/useTMDataGrid.tsx +318 -66
  43. package/src/tmdatagrid/TMDataGrid.module.css +0 -410
  44. package/src/tmdatagrid/TMDataGridFooter.tsx +0 -74
  45. package/src/tmdatagrid/TMDataGridTable.tsx +0 -297
  46. package/src/tmdatagrid/capabilities.ts +0 -117
  47. /package/src/tmdatagrid/{sizes.ts → core/sizes.ts} +0 -0
@@ -1,297 +0,0 @@
1
- import { Loader, Text } from "@mantine/core";
2
- import {
3
- type Cell,
4
- flexRender,
5
- type Row,
6
- type RowData,
7
- } from "@tanstack/react-table";
8
- import { useSelector } from "@tanstack/react-store";
9
- import { useVirtualizer } from "@tanstack/react-virtual";
10
- import { useCallback, useRef } from "react";
11
- import classes from "./TMDataGrid.module.css";
12
- import { type TMDataGridRowData, useTMDataGridContext } from "./TMDataGridContext";
13
- import { TMDataGridFilterPanel } from "./TMDataGridFilterPanel";
14
- import { TMDataGridHeaderCell } from "./TMDataGridHeaderCell";
15
- import { getColumnAlign } from "./columnUtils";
16
- import { SearchIcon } from "./icons";
17
- import type { TMDataGridFeatures } from "./useTMDataGrid";
18
-
19
- /** Where a column sits relative to the pinned regions, resolved once per render. */
20
- export type TMDataGridColumnLayout = {
21
- pinnedAt: "left" | "right" | false;
22
- /** Distance in px from the grid's left or right edge. */
23
- offset: number;
24
- /** Last left-pinned / first right-pinned column — the one that casts the edge. */
25
- isBoundary: boolean;
26
- };
27
-
28
- const UNPINNED_LAYOUT: TMDataGridColumnLayout = {
29
- pinnedAt: false,
30
- offset: 0,
31
- isBoundary: false,
32
- };
33
-
34
- function TMDataGridBodyCell({
35
- cell,
36
- rowHeight,
37
- layout,
38
- }: {
39
- cell: Cell<TMDataGridFeatures, TMDataGridRowData, unknown>;
40
- rowHeight: number;
41
- layout: TMDataGridColumnLayout;
42
- }) {
43
- return (
44
- <div
45
- role="cell"
46
- data-align={getColumnAlign(cell.column)}
47
- className={[
48
- classes.bodyCell,
49
- layout.isBoundary && layout.pinnedAt === "left" ? classes.stickyLeft : "",
50
- layout.isBoundary && layout.pinnedAt === "right"
51
- ? classes.stickyRight
52
- : "",
53
- ]
54
- .filter(Boolean)
55
- .join(" ")}
56
- style={{
57
- minHeight: rowHeight,
58
- left: layout.pinnedAt === "left" ? layout.offset : undefined,
59
- right: layout.pinnedAt === "right" ? layout.offset : undefined,
60
- position: layout.pinnedAt ? "sticky" : undefined,
61
- zIndex: layout.pinnedAt ? 2 : undefined,
62
- }}
63
- >
64
- <span className={classes.cellContent}>
65
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
66
- </span>
67
- </div>
68
- );
69
- }
70
-
71
- export type TMDataGridTableProps<TData extends RowData> = {
72
- /** Called when a body row is clicked. */
73
- onRowClick?: (row: Row<TMDataGridFeatures, TData>) => void;
74
- };
75
-
76
- /**
77
- * The scrollable grid surface. Always virtualized: only the rows inside the
78
- * viewport (plus overscan) are mounted, regardless of page size.
79
- */
80
- export function TMDataGridTable<TData extends RowData = TMDataGridRowData>({
81
- onRowClick,
82
- }: TMDataGridTableProps<TData>) {
83
- const { table, rowHeight, controlSize } = useTMDataGridContext();
84
-
85
- // The body depends on every state slice (sorting, filters, paging, sizing,
86
- // visibility, selection), so it subscribes to the whole table store.
87
- useSelector(table.store);
88
-
89
- const { loading, noResultsLabel = "No rows match your filters" } =
90
- table.options.meta ?? {};
91
-
92
- const scrollContainerRef = useRef<HTMLDivElement>(null);
93
- // Respects `manualPagination`: server-paged grids get the rows as delivered.
94
- const rows = table.getPaginatedRowModel().rows;
95
-
96
- const virtualizer = useVirtualizer({
97
- count: rows.length,
98
- getScrollElement: () => scrollContainerRef.current,
99
- estimateSize: useCallback(() => rowHeight, [rowHeight]),
100
- getItemKey: useCallback((index: number) => rows[index]?.id ?? index, [rows]),
101
- overscan: 6,
102
- initialRect: { height: 600, width: 1200 },
103
- });
104
-
105
- const virtualItems = virtualizer.getVirtualItems();
106
- const paddingTop = virtualItems[0]?.start ?? 0;
107
- const paddingBottom =
108
- virtualizer.getTotalSize() - (virtualItems.at(-1)?.end ?? 0);
109
-
110
- const leftLeafColumns = table.getLeftLeafColumns();
111
- const rightLeafColumns = table.getRightLeafColumns();
112
- const centerLeafColumns = table.getCenterLeafColumns();
113
-
114
- const orderedColumns = [
115
- ...leftLeafColumns,
116
- ...centerLeafColumns,
117
- ...rightLeafColumns,
118
- ];
119
-
120
- // Headers and cells must follow the same left → center → right order as the
121
- // column tracks, otherwise pinning a column would shuffle it out of its lane.
122
- const centerHeaderGroups = table.getCenterHeaderGroups();
123
- const leftHeaderGroups = table.getLeftHeaderGroups();
124
- const rightHeaderGroups = table.getRightHeaderGroups();
125
- const headerGroups = centerHeaderGroups.map((group, index) => ({
126
- id: group.id,
127
- headers: [
128
- ...(leftHeaderGroups[index]?.headers ?? []),
129
- ...group.headers,
130
- ...(rightHeaderGroups[index]?.headers ?? []),
131
- ],
132
- }));
133
- // Unresized columns stay fluid (`minmax(min, flex fr)`) so the grid fills the
134
- // viewport; dragging a resize handle pins that column to an exact pixel width.
135
- // minWidth is the sum of those floors — anything wider and the grid would
136
- // force a horizontal scrollbar it doesn't need.
137
- //
138
- // Pinned columns are always exact: their sticky offsets come from
139
- // `getStart()` / `getAfter()`, which sum `getSize()` and cannot see an `fr`.
140
- const columnSizing = table.store.state.columnSizing;
141
- const columnTracks = orderedColumns.map((column) => {
142
- const isFixed = column.columnDef.minSize === column.columnDef.maxSize;
143
- const isResized = column.id in columnSizing;
144
- const isPinned = column.getIsPinned() !== false;
145
- const minWidth = column.columnDef.minSize ?? 80;
146
- if (isFixed || isResized || isPinned) {
147
- return { track: `${column.getSize()}px`, minWidth: column.getSize() };
148
- }
149
- return {
150
- track: `minmax(${minWidth}px, ${column.columnDef.meta?.flex ?? 1}fr)`,
151
- minWidth,
152
- };
153
- });
154
- const gridTemplateColumns = columnTracks.map((c) => c.track).join(" ");
155
- const gridMinWidth = columnTracks.reduce((sum, c) => sum + c.minWidth, 0);
156
-
157
- /**
158
- * Sticky offsets are derived here, from state this component subscribes to,
159
- * and handed down as props.
160
- *
161
- * Deriving them inside the cell via `column.getIsPinned()` /
162
- * `column.getStart()` looks equivalent but isn't: those are plain calls on a
163
- * `column` object whose identity survives a pin, so the React Compiler caches
164
- * the result and the cell keeps rendering with stale pinning. Building the
165
- * offsets from the same widths as the grid tracks also guarantees the two
166
- * agree to the pixel.
167
- */
168
- const columnLayout = new Map<string, TMDataGridColumnLayout>();
169
- const widthOf = (columnId: string) =>
170
- columnTracks[orderedColumns.findIndex((c) => c.id === columnId)]?.minWidth ??
171
- 0;
172
-
173
- let leftOffset = 0;
174
- for (const column of leftLeafColumns) {
175
- columnLayout.set(column.id, {
176
- pinnedAt: "left",
177
- offset: leftOffset,
178
- isBoundary: column.id === leftLeafColumns.at(-1)?.id,
179
- });
180
- leftOffset += widthOf(column.id);
181
- }
182
- let rightOffset = 0;
183
- for (const column of [...rightLeafColumns].reverse()) {
184
- columnLayout.set(column.id, {
185
- pinnedAt: "right",
186
- offset: rightOffset,
187
- isBoundary: column.id === rightLeafColumns[0]?.id,
188
- });
189
- rightOffset += widthOf(column.id);
190
- }
191
- for (const column of centerLeafColumns) {
192
- columnLayout.set(column.id, {
193
- pinnedAt: false,
194
- offset: 0,
195
- isBoundary: false,
196
- });
197
- }
198
- const layoutFor = (columnId: string) =>
199
- columnLayout.get(columnId) ?? UNPINNED_LAYOUT;
200
-
201
- const isEmpty = rows.length === 0;
202
-
203
- return (
204
- <div className={classes.tableWrapper}>
205
- <div ref={scrollContainerRef} className={classes.scrollContainer}>
206
- <div
207
- role="table"
208
- className={classes.grid}
209
- style={{ gridTemplateColumns, minWidth: gridMinWidth }}
210
- >
211
- <div role="rowgroup" style={{ display: "contents" }}>
212
- {headerGroups.map((headerGroup) => (
213
- <div key={headerGroup.id} role="row" className={classes.headerRow}>
214
- {headerGroup.headers.map((header) => (
215
- <TMDataGridHeaderCell
216
- key={header.id}
217
- header={header}
218
- layout={layoutFor(header.column.id)}
219
- />
220
- ))}
221
- </div>
222
- ))}
223
- </div>
224
-
225
- {paddingTop > 0 && (
226
- <div aria-hidden style={{ gridColumn: "1/-1", height: paddingTop }} />
227
- )}
228
-
229
- <div role="rowgroup" style={{ display: "contents" }}>
230
- {virtualItems.map((virtualItem) => {
231
- const row = rows[virtualItem.index];
232
- if (!row) return null;
233
- return (
234
- <div
235
- key={virtualItem.key}
236
- role="row"
237
- data-testid={`dg-row-${row.id}`}
238
- data-selected={row.getIsSelected()}
239
- className={classes.bodyRow}
240
- style={{
241
- minHeight: rowHeight,
242
- cursor: onRowClick ? "pointer" : undefined,
243
- }}
244
- onClick={
245
- onRowClick
246
- ? () =>
247
- onRowClick(
248
- row as unknown as Row<TMDataGridFeatures, TData>,
249
- )
250
- : undefined
251
- }
252
- >
253
- {[
254
- ...row.getLeftVisibleCells(),
255
- ...row.getCenterVisibleCells(),
256
- ...row.getRightVisibleCells(),
257
- ].map((cell) => (
258
- <TMDataGridBodyCell
259
- key={cell.id}
260
- cell={cell}
261
- rowHeight={rowHeight}
262
- layout={layoutFor(cell.column.id)}
263
- />
264
- ))}
265
- </div>
266
- );
267
- })}
268
- </div>
269
-
270
- {isEmpty && loading === true && (
271
- <div role="row" className={classes.messageRow}>
272
- <Loader size="lg" />
273
- </div>
274
- )}
275
-
276
- {isEmpty && loading !== true && (
277
- <div role="row" className={classes.messageRow}>
278
- <SearchIcon size={40} stroke={1.4} opacity={0.4} />
279
- <Text size={controlSize} c="dimmed">
280
- {noResultsLabel}
281
- </Text>
282
- </div>
283
- )}
284
-
285
- {paddingBottom > 0 && (
286
- <div
287
- aria-hidden
288
- style={{ gridColumn: "1/-1", height: paddingBottom }}
289
- />
290
- )}
291
- </div>
292
- </div>
293
-
294
- <TMDataGridFilterPanel />
295
- </div>
296
- );
297
- }
@@ -1,117 +0,0 @@
1
- import type { Column, RowData } from "@tanstack/react-table";
2
- import type { TMDataGridRowData } from "./TMDataGridContext";
3
- import { isColumnReorderable } from "./columnUtils";
4
- import type {
5
- TMDataGridFeatures,
6
- TMDataGridTable,
7
- UseTMDataGridOptions,
8
- } from "./useTMDataGrid";
9
-
10
- /**
11
- * Table-level feature switches, read straight off the options object.
12
- *
13
- * Why not just call `column.getCanSort()` everywhere? Because that is a method
14
- * call on a `column` whose identity survives an options change, so the React
15
- * Compiler caches the result and the chrome keeps rendering for a feature that
16
- * was switched off. Reading the options into a fresh object each render gives
17
- * every derived value something that actually changes to invalidate on.
18
- *
19
- * TanStack still decides: these flags are combined with `getCanX()`, which is
20
- * what applies the per-column overrides.
21
- */
22
- export type TMDataGridFeatureFlags = {
23
- sorting: boolean;
24
- filtering: boolean;
25
- hiding: boolean;
26
- pinning: boolean;
27
- resizing: boolean;
28
- ordering: boolean;
29
- rowSelection: boolean;
30
- };
31
-
32
- export function readFeatureFlags<TData extends RowData>(
33
- options: Pick<
34
- UseTMDataGridOptions<TData>,
35
- | "enableSorting"
36
- | "enableColumnFilters"
37
- | "enableHiding"
38
- | "enableColumnPinning"
39
- | "enableColumnResizing"
40
- | "enableColumnOrdering"
41
- | "enableRowSelection"
42
- >,
43
- ): TMDataGridFeatureFlags {
44
- return {
45
- sorting: options.enableSorting !== false,
46
- filtering: options.enableColumnFilters !== false,
47
- hiding: options.enableHiding !== false,
48
- pinning: options.enableColumnPinning !== false,
49
- resizing: options.enableColumnResizing !== false,
50
- ordering: options.enableColumnOrdering !== false,
51
- rowSelection: options.enableRowSelection !== false,
52
- };
53
- }
54
-
55
- /**
56
- * What one column's header may offer.
57
- *
58
- * | Affordance | Turned off by |
59
- * | ---------- | ------------- |
60
- * | Sort arrow, Sort ASC/DESC | `enableSorting` (table or column) |
61
- * | Filter item, panel entry | `enableColumnFilters` / `enableColumnFilter` |
62
- * | Hide column | `enableHiding` (table or column) |
63
- * | Pin to left / right | `enableColumnPinning` / `enablePinning` |
64
- * | Resize dragging | `enableColumnResizing` / `enableResizing` |
65
- * | Header dragging, Move left / right | `enableColumnOrdering` / `meta.enableOrdering` |
66
- */
67
- export type TMDataGridColumnCapabilities = {
68
- canSort: boolean;
69
- canFilter: boolean;
70
- canHide: boolean;
71
- canPin: boolean;
72
- canResize: boolean;
73
- canReorder: boolean;
74
- };
75
-
76
- export function getColumnCapabilities(
77
- column: Column<TMDataGridFeatures, TMDataGridRowData, unknown>,
78
- features: TMDataGridFeatureFlags,
79
- ): TMDataGridColumnCapabilities {
80
- return {
81
- canSort: features.sorting && column.getCanSort(),
82
- canFilter: features.filtering && column.getCanFilter(),
83
- canHide: features.hiding && column.getCanHide(),
84
- canPin: features.pinning && column.getCanPin(),
85
- canResize: features.resizing && column.getCanResize(),
86
- // Ordering has no TanStack capability method — see isColumnReorderable.
87
- canReorder: features.ordering && isColumnReorderable(column),
88
- };
89
- }
90
-
91
- /** The same questions, asked of the whole grid — used to hide toolbar buttons. */
92
- export type TMDataGridCapabilities = {
93
- canSortAny: boolean;
94
- canFilterAny: boolean;
95
- canHideAny: boolean;
96
- canPinAny: boolean;
97
- canReorderAny: boolean;
98
- canSelectRows: boolean;
99
- };
100
-
101
- export function getGridCapabilities(
102
- table: TMDataGridTable<TMDataGridRowData>,
103
- features: TMDataGridFeatureFlags,
104
- ): TMDataGridCapabilities {
105
- const columns = table.getAllLeafColumns();
106
- const any = (predicate: (capabilities: TMDataGridColumnCapabilities) => boolean) =>
107
- columns.some((column) => predicate(getColumnCapabilities(column, features)));
108
-
109
- return {
110
- canSortAny: any((c) => c.canSort),
111
- canFilterAny: any((c) => c.canFilter),
112
- canHideAny: any((c) => c.canHide),
113
- canPinAny: any((c) => c.canPin),
114
- canReorderAny: any((c) => c.canReorder),
115
- canSelectRows: features.rowSelection,
116
- };
117
- }
File without changes