@nickrobison/colophon-table 0.0.0-stage → 0.1.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 (59) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +55 -2
  3. package/package.json +72 -3
  4. package/src/components/CphSkeleton.test.tsx +145 -0
  5. package/src/components/CphSkeleton.tsx +42 -0
  6. package/src/components/CphTableEmpty.test.tsx +122 -0
  7. package/src/components/CphTableEmpty.tsx +42 -0
  8. package/src/components/DataTable/CphDataTable.test.tsx +408 -0
  9. package/src/components/DataTable/CphDataTable.tsx +121 -0
  10. package/src/components/DataTable/pinnedOffsets.ts +94 -0
  11. package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.test.tsx +172 -0
  12. package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.tsx +132 -0
  13. package/src/components/FilterRange/CphTableFilterRange.test.tsx +154 -0
  14. package/src/components/FilterRange/CphTableFilterRange.tsx +109 -0
  15. package/src/components/MobileCards/CphMobileCards.test.tsx +174 -0
  16. package/src/components/MobileCards/CphMobileCards.tsx +117 -0
  17. package/src/components/Pagination/CphCursorPagination.test.tsx +51 -0
  18. package/src/components/Pagination/CphCursorPagination.tsx +74 -0
  19. package/src/components/Pagination/CphPagination.test.tsx +98 -0
  20. package/src/components/Pagination/CphPagination.tsx +94 -0
  21. package/src/components/RowDetail/CphRowDetail.test.tsx +182 -0
  22. package/src/components/RowDetail/CphRowDetail.tsx +66 -0
  23. package/src/components/SimpleTable/CphSimpleTable.test.tsx +235 -0
  24. package/src/components/SimpleTable/CphSimpleTable.tsx +369 -0
  25. package/src/components/SortFilterSheet/CphSortFilterSheet.test.tsx +156 -0
  26. package/src/components/SortFilterSheet/CphSortFilterSheet.tsx +131 -0
  27. package/src/components/StatusChip/CphStatusChip.test.tsx +43 -0
  28. package/src/components/StatusChip/CphStatusChip.tsx +33 -0
  29. package/src/components/Summary/CphTableSummary.test.tsx +486 -0
  30. package/src/components/Summary/CphTableSummary.tsx +130 -0
  31. package/src/components/TableHead/CphTableHead.test.tsx +52 -0
  32. package/src/components/TableHead/CphTableHead.tsx +129 -0
  33. package/src/components/TableHead/HeaderCell.test.tsx +880 -0
  34. package/src/components/TableHead/HeaderCell.tsx +221 -0
  35. package/src/components/TableRow/Cell.test.tsx +306 -0
  36. package/src/components/TableRow/Cell.tsx +74 -0
  37. package/src/components/TableRow/CphTableRow.test.tsx +90 -0
  38. package/src/components/TableRow/CphTableRow.tsx +17 -0
  39. package/src/components/Toolbar/CphToolbar.test.tsx +150 -0
  40. package/src/components/Toolbar/CphToolbar.tsx +138 -0
  41. package/src/components.css +593 -0
  42. package/src/index.ts +69 -0
  43. package/src/table/cellRenderers.tsx +88 -0
  44. package/src/table/column.test.tsx +277 -0
  45. package/src/table/column.tsx +36 -0
  46. package/src/table/features.ts +69 -0
  47. package/src/table/pageSize.test.ts +128 -0
  48. package/src/table/pageSize.ts +47 -0
  49. package/src/table/summary.test.tsx +446 -0
  50. package/src/table/summary.ts +94 -0
  51. package/src/table/useCphTable.ts +90 -0
  52. package/src/table/useCphTableState.test.tsx +697 -0
  53. package/src/table/useCphTableState.ts +412 -0
  54. package/src/table-tokens.css +48 -0
  55. package/src/theme/CphTableDensity.test.tsx +53 -0
  56. package/src/theme/CphTableDensity.tsx +24 -0
  57. package/src/theme/cphTableDensityContext.ts +24 -0
  58. package/src/utils/composeClassName.test.ts +26 -0
  59. package/src/utils/composeClassName.ts +28 -0
@@ -0,0 +1,446 @@
1
+ /** @packageDocumentation Tests for table summary/aggregation utilities. */
2
+
3
+ import { useTable, type Column, type ColumnDef } from "@tanstack/react-table";
4
+ import { render, act } from "@testing-library/react";
5
+ import { describe, it, expect } from "vitest";
6
+
7
+ import { cphColumnHelper } from "./column";
8
+ import { cphTableFeatures, type CphTableFeatures } from "./features";
9
+ import {
10
+ getAggregationValue,
11
+ getSum,
12
+ getMean,
13
+ getCount,
14
+ getFilteredTotal,
15
+ getAggregationByKind,
16
+ } from "./summary";
17
+
18
+ // Test data type
19
+ interface TestRow {
20
+ id: string;
21
+ name: string;
22
+ amount: number;
23
+ category: string;
24
+ }
25
+
26
+ // Create a test table component that exposes the table instance
27
+ function TestTable({
28
+ data,
29
+ columns,
30
+ onTableReady,
31
+ }: {
32
+ data: TestRow[];
33
+ columns: readonly ColumnDef<CphTableFeatures, TestRow>[];
34
+ onTableReady: (table: ReturnType<typeof useTable<CphTableFeatures, TestRow>>) => void;
35
+ }) {
36
+ const table = useTable({
37
+ features: cphTableFeatures,
38
+ columns,
39
+ data,
40
+ enableMultiSort: false,
41
+ });
42
+
43
+ // Call the callback with the table instance
44
+ if (onTableReady) {
45
+ onTableReady(table);
46
+ }
47
+
48
+ return null; // We only need the table instance for testing
49
+ }
50
+
51
+ describe("summary.ts - filter-aware aggregation", () => {
52
+ // Create 15 test rows, 10 of which match the filter "category === 'A'"
53
+ const testData: TestRow[] = [
54
+ { id: "1", name: "Item 1", amount: 100, category: "A" },
55
+ { id: "2", name: "Item 2", amount: 200, category: "A" },
56
+ { id: "3", name: "Item 3", amount: 300, category: "A" },
57
+ { id: "4", name: "Item 4", amount: 400, category: "A" },
58
+ { id: "5", name: "Item 5", amount: 500, category: "A" },
59
+ { id: "6", name: "Item 6", amount: 600, category: "A" },
60
+ { id: "7", name: "Item 7", amount: 700, category: "A" },
61
+ { id: "8", name: "Item 8", amount: 800, category: "A" },
62
+ { id: "9", name: "Item 9", amount: 900, category: "A" },
63
+ { id: "10", name: "Item 10", amount: 1000, category: "A" },
64
+ { id: "11", name: "Item 11", amount: 100, category: "B" },
65
+ { id: "12", name: "Item 12", amount: 200, category: "B" },
66
+ { id: "13", name: "Item 13", amount: 300, category: "B" },
67
+ { id: "14", name: "Item 14", amount: 400, category: "B" },
68
+ { id: "15", name: "Item 15", amount: 500, category: "B" },
69
+ ];
70
+
71
+ const helper = cphColumnHelper<TestRow>();
72
+
73
+ const columns = helper.columns([
74
+ helper.accessor("id", {
75
+ header: "ID",
76
+ cell: (info) => info.getValue(),
77
+ }),
78
+ helper.accessor("name", {
79
+ header: "Name",
80
+ cell: (info) => info.getValue(),
81
+ }),
82
+ helper.accessor("amount", {
83
+ header: "Amount",
84
+ cell: (info) => info.getValue(),
85
+ meta: {
86
+ numeric: true,
87
+ aggregate: "sum",
88
+ },
89
+ }),
90
+ helper.accessor("category", {
91
+ header: "Category",
92
+ cell: (info) => info.getValue(),
93
+ }),
94
+ ]);
95
+
96
+ it("getSum reflects ALL filtered rows (10) while page shows only 8", () => {
97
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
98
+
99
+ // Render the test table to get the table instance
100
+ const { unmount } = render(
101
+ <TestTable
102
+ data={testData}
103
+ columns={columns}
104
+ onTableReady={(table) => {
105
+ tableInstance = table;
106
+ }}
107
+ />,
108
+ );
109
+
110
+ expect(tableInstance).not.toBeNull();
111
+ const table = tableInstance!;
112
+
113
+ // Apply a filter: category === "A" (should match 10 rows)
114
+ act(() => {
115
+ table.setColumnFilters([{ id: "category", value: "A" }]);
116
+ });
117
+
118
+ // Set page size to 8 (so only 8 rows visible per page)
119
+ act(() => {
120
+ table.setPageSize(8);
121
+ });
122
+
123
+ // Get the amount column
124
+ const amountColumn = table.getColumn("amount");
125
+ expect(amountColumn).not.toBeNull();
126
+
127
+ // Get filtered rows (should be 10)
128
+ const filteredRowModel = table.getFilteredRowModel();
129
+ expect(filteredRowModel.rows.length).toBe(10);
130
+
131
+ // Get paginated rows (should be 8 on first page)
132
+ const paginatedRowModel = table.getRowModel();
133
+ expect(paginatedRowModel.rows.length).toBe(8);
134
+
135
+ // Get the sum using our helper with filtered rows
136
+ const sum = getSum(amountColumn!, filteredRowModel.rows);
137
+
138
+ // Sum of amounts for category A: 100+200+300+400+500+600+700+800+900+1000 = 5500
139
+ expect(sum).toBe(5500);
140
+
141
+ // Verify the sum is NOT just the current page (which would be 100+200+300+400+500+600+700+800 = 3600)
142
+ expect(sum).not.toBe(3600);
143
+
144
+ unmount();
145
+ });
146
+
147
+ it("getMean reflects ALL filtered rows", () => {
148
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
149
+
150
+ const { unmount } = render(
151
+ <TestTable
152
+ data={testData}
153
+ columns={columns}
154
+ onTableReady={(table) => {
155
+ tableInstance = table;
156
+ }}
157
+ />,
158
+ );
159
+
160
+ expect(tableInstance).not.toBeNull();
161
+ const table = tableInstance!;
162
+
163
+ act(() => {
164
+ table.setColumnFilters([{ id: "category", value: "A" }]);
165
+ });
166
+
167
+ const amountColumn = table.getColumn("amount");
168
+ expect(amountColumn).not.toBeNull();
169
+
170
+ const filteredRowModel = table.getFilteredRowModel();
171
+ const mean = getMean(amountColumn!, filteredRowModel.rows);
172
+
173
+ // Mean of 100,200,300,400,500,600,700,800,900,1000 = 5500/10 = 550
174
+ expect(mean).toBe(550);
175
+
176
+ unmount();
177
+ });
178
+
179
+ it("getCount reflects ALL filtered rows", () => {
180
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
181
+
182
+ const { unmount } = render(
183
+ <TestTable
184
+ data={testData}
185
+ columns={columns}
186
+ onTableReady={(table) => {
187
+ tableInstance = table;
188
+ }}
189
+ />,
190
+ );
191
+
192
+ expect(tableInstance).not.toBeNull();
193
+ const table = tableInstance!;
194
+
195
+ act(() => {
196
+ table.setColumnFilters([{ id: "category", value: "A" }]);
197
+ });
198
+
199
+ const amountColumn = table.getColumn("amount");
200
+ expect(amountColumn).not.toBeNull();
201
+
202
+ const filteredRowModel = table.getFilteredRowModel();
203
+ const count = getCount(amountColumn!, filteredRowModel.rows);
204
+
205
+ expect(count).toBe(10);
206
+
207
+ unmount();
208
+ });
209
+
210
+ it("getFilteredTotal returns filtered count and total count", () => {
211
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
212
+
213
+ const { unmount } = render(
214
+ <TestTable
215
+ data={testData}
216
+ columns={columns}
217
+ onTableReady={(table) => {
218
+ tableInstance = table;
219
+ }}
220
+ />,
221
+ );
222
+
223
+ expect(tableInstance).not.toBeNull();
224
+ const table = tableInstance!;
225
+
226
+ // Before filtering
227
+ let total = getFilteredTotal(table);
228
+ expect(total.filtered).toBe(15);
229
+ expect(total.total).toBe(15);
230
+
231
+ // After filtering
232
+ act(() => {
233
+ table.setColumnFilters([{ id: "category", value: "A" }]);
234
+ });
235
+
236
+ total = getFilteredTotal(table);
237
+ expect(total.filtered).toBe(10);
238
+ expect(total.total).toBe(15);
239
+
240
+ unmount();
241
+ });
242
+
243
+ it("getAggregationByKind dispatches to correct helper", () => {
244
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
245
+
246
+ const { unmount } = render(
247
+ <TestTable
248
+ data={testData}
249
+ columns={columns}
250
+ onTableReady={(table) => {
251
+ tableInstance = table;
252
+ }}
253
+ />,
254
+ );
255
+
256
+ expect(tableInstance).not.toBeNull();
257
+ const table = tableInstance!;
258
+
259
+ act(() => {
260
+ table.setColumnFilters([{ id: "category", value: "A" }]);
261
+ });
262
+
263
+ const amountColumn = table.getColumn("amount");
264
+ expect(amountColumn).not.toBeNull();
265
+
266
+ const filteredRowModel = table.getFilteredRowModel();
267
+
268
+ const sum = getAggregationByKind("sum", amountColumn!, filteredRowModel.rows);
269
+ const mean = getAggregationByKind("mean", amountColumn!, filteredRowModel.rows);
270
+ const count = getAggregationByKind("count", amountColumn!, filteredRowModel.rows);
271
+
272
+ expect(sum).toBe(5500);
273
+ expect(mean).toBe(550);
274
+ expect(count).toBe(10);
275
+
276
+ unmount();
277
+ });
278
+
279
+ it("getAggregationByKind returns undefined for an unknown kind", () => {
280
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
281
+
282
+ const { unmount } = render(
283
+ <TestTable
284
+ data={testData}
285
+ columns={columns}
286
+ onTableReady={(table) => {
287
+ tableInstance = table;
288
+ }}
289
+ />,
290
+ );
291
+
292
+ expect(tableInstance).not.toBeNull();
293
+ const table = tableInstance!;
294
+ const amountColumn = table.getAllLeafColumns().find((column) => column.id === "amount");
295
+
296
+ expect(amountColumn).not.toBeNull();
297
+
298
+ const filteredRowModel = table.getFilteredRowModel();
299
+ expect(
300
+ getAggregationByKind("bogus" as unknown as "sum", amountColumn!, filteredRowModel.rows),
301
+ ).toBeUndefined();
302
+
303
+ unmount();
304
+ });
305
+
306
+ it("getSum/getMean/getCount delegate to the column when rows are omitted", () => {
307
+ const column = {
308
+ getAggregationValue: () => 42,
309
+ } as unknown as Column<CphTableFeatures, TestRow, unknown>;
310
+
311
+ expect(getSum(column)).toBe(42);
312
+ expect(getMean(column)).toBe(42);
313
+ expect(getCount(column)).toBe(42);
314
+ });
315
+
316
+ it("getAggregationValue forwards only the provided options", () => {
317
+ const seen: unknown[] = [];
318
+ const column = {
319
+ getAggregationValue: (options: unknown) => {
320
+ seen.push(options);
321
+ return "agg";
322
+ },
323
+ } as unknown as Column<CphTableFeatures, TestRow, unknown>;
324
+
325
+ expect(getAggregationValue({ column })).toBe("agg");
326
+ expect(getAggregationValue({ column, maxDepth: 1 })).toBe("agg");
327
+ expect(seen).toEqual([{}, { maxDepth: 1 }]);
328
+ });
329
+
330
+ it("getMean skips non-numeric values, yielding undefined when nothing counts", () => {
331
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
332
+
333
+ const { unmount } = render(
334
+ <TestTable
335
+ data={testData}
336
+ columns={columns}
337
+ onTableReady={(table) => {
338
+ tableInstance = table;
339
+ }}
340
+ />,
341
+ );
342
+
343
+ expect(tableInstance).not.toBeNull();
344
+ const table = tableInstance!;
345
+ const nameColumn = table.getAllLeafColumns().find((column) => column.id === "name");
346
+
347
+ expect(nameColumn).not.toBeNull();
348
+
349
+ const filteredRowModel = table.getFilteredRowModel();
350
+ expect(getMean(nameColumn!, filteredRowModel.rows)).toBeUndefined();
351
+
352
+ unmount();
353
+ });
354
+
355
+ it("getMean skips null values and getSum treats them as zero", () => {
356
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
357
+ const nullData = [{ id: "x", name: "X", amount: null, category: "A" } as unknown as TestRow];
358
+
359
+ const { unmount } = render(
360
+ <TestTable
361
+ data={nullData}
362
+ columns={columns}
363
+ onTableReady={(table) => {
364
+ tableInstance = table;
365
+ }}
366
+ />,
367
+ );
368
+
369
+ expect(tableInstance).not.toBeNull();
370
+ const table = tableInstance!;
371
+ const amountColumn = table.getAllLeafColumns().find((column) => column.id === "amount");
372
+
373
+ expect(amountColumn).not.toBeNull();
374
+
375
+ const filteredRowModel = table.getFilteredRowModel();
376
+ expect(getMean(amountColumn!, filteredRowModel.rows)).toBeUndefined();
377
+ expect(getSum(amountColumn!, filteredRowModel.rows)).toBe(0);
378
+
379
+ unmount();
380
+ });
381
+
382
+ it("getAggregationValue works with bare column and rows", () => {
383
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
384
+
385
+ const { unmount } = render(
386
+ <TestTable
387
+ data={testData}
388
+ columns={columns}
389
+ onTableReady={(table) => {
390
+ tableInstance = table;
391
+ }}
392
+ />,
393
+ );
394
+
395
+ expect(tableInstance).not.toBeNull();
396
+ const table = tableInstance!;
397
+
398
+ act(() => {
399
+ table.setColumnFilters([{ id: "category", value: "A" }]);
400
+ });
401
+
402
+ const amountColumn = table.getColumn("amount");
403
+ expect(amountColumn).not.toBeNull();
404
+
405
+ const filteredRowModel = table.getFilteredRowModel();
406
+
407
+ // Using the bare getAggregationValue directly
408
+ const sum = getAggregationValue({
409
+ column: amountColumn!,
410
+ rows: filteredRowModel.rows,
411
+ });
412
+
413
+ expect(sum).toBe(5500);
414
+
415
+ unmount();
416
+ });
417
+
418
+ it("aggregation without rows uses pre-grouped row model (all data)", () => {
419
+ let tableInstance: ReturnType<typeof useTable<CphTableFeatures, TestRow>> | null = null;
420
+
421
+ const { unmount } = render(
422
+ <TestTable
423
+ data={testData}
424
+ columns={columns}
425
+ onTableReady={(table) => {
426
+ tableInstance = table;
427
+ }}
428
+ />,
429
+ );
430
+
431
+ expect(tableInstance).not.toBeNull();
432
+ const table = tableInstance!;
433
+
434
+ // No filter applied
435
+ const amountColumn = table.getColumn("amount");
436
+ expect(amountColumn).not.toBeNull();
437
+
438
+ // Sum without rows parameter should use all data
439
+ const sum = getSum(amountColumn!);
440
+
441
+ // Sum of all 15 rows: 5500 (A) + 1500 (B) = 7000
442
+ expect(sum).toBe(7000);
443
+
444
+ unmount();
445
+ });
446
+ });
@@ -0,0 +1,94 @@
1
+ /** @packageDocumentation Table summary/aggregation utilities. */
2
+
3
+ import { type Column, type Row, type Table, type RowData } from "@tanstack/react-table";
4
+
5
+ import type { CphTableFeatures } from "./features";
6
+
7
+ export type CphAggregateKind = "sum" | "mean" | "count";
8
+
9
+ export interface FilteredTotal {
10
+ filtered: number;
11
+ total: number;
12
+ }
13
+
14
+ export interface GetAggregationValueOptions<TData extends RowData, TValue = unknown> {
15
+ column: Column<CphTableFeatures, TData, TValue>;
16
+ rows?: ReadonlyArray<Row<CphTableFeatures, TData>>;
17
+ maxDepth?: number;
18
+ }
19
+
20
+ export function getAggregationValue<TData extends RowData, TValue = unknown, TResult = unknown>(
21
+ options: GetAggregationValueOptions<TData, TValue>,
22
+ ): TResult {
23
+ const { column, rows, maxDepth } = options;
24
+ return column.getAggregationValue<TResult>({
25
+ ...(rows !== undefined ? { rows } : {}),
26
+ ...(maxDepth !== undefined ? { maxDepth } : {}),
27
+ });
28
+ }
29
+
30
+ export function getSum<TData extends RowData, TValue = unknown>(
31
+ column: Column<CphTableFeatures, TData, TValue>,
32
+ rows?: ReadonlyArray<Row<CphTableFeatures, TData>>,
33
+ ): number | undefined {
34
+ if (rows === undefined) return column.getAggregationValue<number | undefined>();
35
+ return rows.reduce((sum, row) => {
36
+ const value = row.getValue(column.id);
37
+ return sum + (typeof value === "number" ? value : 0);
38
+ }, 0);
39
+ }
40
+
41
+ export function getMean<TData extends RowData, TValue = unknown>(
42
+ column: Column<CphTableFeatures, TData, TValue>,
43
+ rows?: ReadonlyArray<Row<CphTableFeatures, TData>>,
44
+ ): number | undefined {
45
+ if (rows === undefined) return column.getAggregationValue<number | undefined>();
46
+ let count = 0;
47
+ let sum = 0;
48
+ for (const row of rows) {
49
+ const value = row.getValue(column.id);
50
+ if (value == null) continue;
51
+ const numericValue = typeof value === "number" ? value : Number(value);
52
+ if (!Number.isNaN(numericValue)) {
53
+ count += 1;
54
+ sum += numericValue;
55
+ }
56
+ }
57
+ return count > 0 ? sum / count : undefined;
58
+ }
59
+
60
+ export function getCount<TData extends RowData, TValue = unknown>(
61
+ column: Column<CphTableFeatures, TData, TValue>,
62
+ rows?: ReadonlyArray<Row<CphTableFeatures, TData>>,
63
+ ): number {
64
+ if (rows === undefined) return column.getAggregationValue<number>();
65
+ return rows.length;
66
+ }
67
+
68
+ export function getFilteredTotal<TData extends RowData>(
69
+ table: Table<CphTableFeatures, TData>,
70
+ ): FilteredTotal {
71
+ const filteredRowModel = table.getFilteredRowModel();
72
+ const coreRowModel = table.getCoreRowModel();
73
+ return {
74
+ filtered: filteredRowModel.rows.length,
75
+ total: coreRowModel.rows.length,
76
+ };
77
+ }
78
+
79
+ export function getAggregationByKind<TData extends RowData, TValue = unknown>(
80
+ kind: CphAggregateKind,
81
+ column: Column<CphTableFeatures, TData, TValue>,
82
+ rows?: ReadonlyArray<Row<CphTableFeatures, TData>>,
83
+ ): number | undefined {
84
+ switch (kind) {
85
+ case "sum":
86
+ return getSum(column, rows);
87
+ case "mean":
88
+ return getMean(column, rows);
89
+ case "count":
90
+ return getCount(column, rows);
91
+ default:
92
+ return undefined;
93
+ }
94
+ }
@@ -0,0 +1,90 @@
1
+ import {
2
+ useTable,
3
+ type ColumnDef,
4
+ type RowData,
5
+ type RowSelectionState,
6
+ type SortingState,
7
+ type TableState,
8
+ } from "@tanstack/react-table";
9
+
10
+ import { cphTableFeatures, type CphTableFeatures } from "./features";
11
+ import { useCphTableState, type StateSlice } from "./useCphTableState";
12
+
13
+ export interface CphTableOptions<TData extends RowData> {
14
+ data: TData[];
15
+ columns: ColumnDef<CphTableFeatures, TData, unknown>[];
16
+ /** Stable row identity; required for selection and expansion to survive re-sorts. */
17
+ getRowId?: (row: TData, index: number) => string;
18
+ /**
19
+ * Enables/Disables the ability to remove sorting.
20
+ *
21
+ * - `true` → sort cycle: "none" → "desc" → "asc" → "none" → …
22
+ * - `false` → sort cycle: "none" → first direction → flip → flip → …
23
+ * (the two-state asc↔desc cycle; TanStack defaults to `true`)
24
+ *
25
+ * Colophon's tables spec mandates the two-state cycle, so the headless hook
26
+ * passes `false` unless a consumer opts into removal.
27
+ */
28
+ enableSortingRemoval?: boolean;
29
+ /**
30
+ * Enables multi-row selection. Disable for `selectionMode: "single"` tables
31
+ * so TanStack enforces at most one selected row.
32
+ */
33
+ enableMultiRowSelection?: boolean;
34
+ /** Controlled/uncontrolled row sorting slice. */
35
+ sorting?: StateSlice<SortingState>;
36
+ /** Controlled/uncontrolled row selection slice. */
37
+ rowSelection?: StateSlice<RowSelectionState>;
38
+ }
39
+
40
+ /**
41
+ * Headless table instance.
42
+ *
43
+ * `enableMultiSort: false` is a spec rule, not a preference: the Tables Spec
44
+ * allows exactly one sort column at a time.
45
+ *
46
+ * v9 removed `expandSingleRowOnly`, so single-row expansion is enforced one
47
+ * level up in `useCphTableState`, which only ever writes one expanded id.
48
+ *
49
+ * The optional `selector` projects from the table store; the selected value
50
+ * is exposed on `table.state` and subscribed for re-renders. Pass a narrow
51
+ * selector (e.g. `(state) => ({ pagination: state.pagination })`) when you
52
+ * only need one slice, such as wiring `CphPagination` to a live table.
53
+ */
54
+ export function useCphTable<TData extends RowData, TSelected = TableState<CphTableFeatures>>(
55
+ {
56
+ data,
57
+ columns,
58
+ getRowId,
59
+ enableSortingRemoval = false,
60
+ enableMultiRowSelection,
61
+ sorting,
62
+ rowSelection,
63
+ }: CphTableOptions<TData>,
64
+ selector: (state: TableState<CphTableFeatures>) => TSelected = (state) => state as TSelected,
65
+ ) {
66
+ const tableState = useCphTableState({
67
+ ...(sorting !== undefined ? { sorting } : {}),
68
+ ...(rowSelection !== undefined ? { rowSelection } : {}),
69
+ });
70
+
71
+ return useTable(
72
+ {
73
+ features: cphTableFeatures,
74
+ columns,
75
+ data,
76
+ // Conditional spread, not `getRowId: getRowId` — under
77
+ // exactOptionalPropertyTypes an explicit undefined is not assignable to an
78
+ // optional prop, so passing the variable straight through would fail here.
79
+ ...(getRowId ? { getRowId } : {}),
80
+ enableMultiSort: false,
81
+ // Two-state sort cycle (unsorted → asc → desc → …): sorting can never be
82
+ // removed back to "unsorted" once a column has been sorted.
83
+ enableSortingRemoval,
84
+ // Conditional spread for the optional single/multi toggle.
85
+ ...(enableMultiRowSelection === undefined ? {} : { enableMultiRowSelection }),
86
+ ...tableState,
87
+ },
88
+ selector,
89
+ );
90
+ }