@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.
- package/LICENSE.md +21 -0
- package/README.md +55 -2
- package/package.json +72 -3
- package/src/components/CphSkeleton.test.tsx +145 -0
- package/src/components/CphSkeleton.tsx +42 -0
- package/src/components/CphTableEmpty.test.tsx +122 -0
- package/src/components/CphTableEmpty.tsx +42 -0
- package/src/components/DataTable/CphDataTable.test.tsx +408 -0
- package/src/components/DataTable/CphDataTable.tsx +121 -0
- package/src/components/DataTable/pinnedOffsets.ts +94 -0
- package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.test.tsx +172 -0
- package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.tsx +132 -0
- package/src/components/FilterRange/CphTableFilterRange.test.tsx +154 -0
- package/src/components/FilterRange/CphTableFilterRange.tsx +109 -0
- package/src/components/MobileCards/CphMobileCards.test.tsx +174 -0
- package/src/components/MobileCards/CphMobileCards.tsx +117 -0
- package/src/components/Pagination/CphCursorPagination.test.tsx +51 -0
- package/src/components/Pagination/CphCursorPagination.tsx +74 -0
- package/src/components/Pagination/CphPagination.test.tsx +98 -0
- package/src/components/Pagination/CphPagination.tsx +94 -0
- package/src/components/RowDetail/CphRowDetail.test.tsx +182 -0
- package/src/components/RowDetail/CphRowDetail.tsx +66 -0
- package/src/components/SimpleTable/CphSimpleTable.test.tsx +235 -0
- package/src/components/SimpleTable/CphSimpleTable.tsx +369 -0
- package/src/components/SortFilterSheet/CphSortFilterSheet.test.tsx +156 -0
- package/src/components/SortFilterSheet/CphSortFilterSheet.tsx +131 -0
- package/src/components/StatusChip/CphStatusChip.test.tsx +43 -0
- package/src/components/StatusChip/CphStatusChip.tsx +33 -0
- package/src/components/Summary/CphTableSummary.test.tsx +486 -0
- package/src/components/Summary/CphTableSummary.tsx +130 -0
- package/src/components/TableHead/CphTableHead.test.tsx +52 -0
- package/src/components/TableHead/CphTableHead.tsx +129 -0
- package/src/components/TableHead/HeaderCell.test.tsx +880 -0
- package/src/components/TableHead/HeaderCell.tsx +221 -0
- package/src/components/TableRow/Cell.test.tsx +306 -0
- package/src/components/TableRow/Cell.tsx +74 -0
- package/src/components/TableRow/CphTableRow.test.tsx +90 -0
- package/src/components/TableRow/CphTableRow.tsx +17 -0
- package/src/components/Toolbar/CphToolbar.test.tsx +150 -0
- package/src/components/Toolbar/CphToolbar.tsx +138 -0
- package/src/components.css +593 -0
- package/src/index.ts +69 -0
- package/src/table/cellRenderers.tsx +88 -0
- package/src/table/column.test.tsx +277 -0
- package/src/table/column.tsx +36 -0
- package/src/table/features.ts +69 -0
- package/src/table/pageSize.test.ts +128 -0
- package/src/table/pageSize.ts +47 -0
- package/src/table/summary.test.tsx +446 -0
- package/src/table/summary.ts +94 -0
- package/src/table/useCphTable.ts +90 -0
- package/src/table/useCphTableState.test.tsx +697 -0
- package/src/table/useCphTableState.ts +412 -0
- package/src/table-tokens.css +48 -0
- package/src/theme/CphTableDensity.test.tsx +53 -0
- package/src/theme/CphTableDensity.tsx +24 -0
- package/src/theme/cphTableDensityContext.ts +24 -0
- package/src/utils/composeClassName.test.ts +26 -0
- package/src/utils/composeClassName.ts +28 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
|
|
3
|
+
import { CphStatusChip, type CphStatusTone } from "../components/StatusChip/CphStatusChip";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cell renderer return type.
|
|
7
|
+
*
|
|
8
|
+
* These renderers are simplified value formatters — they take a raw `value`
|
|
9
|
+
* and return renderable content (string or ReactElement). They do NOT receive
|
|
10
|
+
* a TanStack `CellContext`; the table body component is responsible for
|
|
11
|
+
* extracting the value and passing it to the renderer.
|
|
12
|
+
*
|
|
13
|
+
* Composition with {@link Cell} (the `<td>`-rendering component):
|
|
14
|
+
* - The table body component renders `<Cell className={tdClassName}>{renderer(value)}</Cell>`
|
|
15
|
+
* - For numeric columns, the table body should pass `tdClassName="cph-table__numeric"`
|
|
16
|
+
* by reading `column.columnDef.meta.numeric`. This ensures `text-align: right`
|
|
17
|
+
* applies to the `<td>` itself (block-level), which is required for alignment.
|
|
18
|
+
* - The renderer ALSO wraps its output in a block element with the same class
|
|
19
|
+
* as a defense-in-depth measure: if the table body omits the td className,
|
|
20
|
+
* the content wrapper still gets `text-align: right` and `font-variant-numeric`.
|
|
21
|
+
* - `createTruncateCell` returns an inline `<span>` because truncation relies on
|
|
22
|
+
* `overflow: hidden; text-overflow: ellipsis; white-space: nowrap` which works
|
|
23
|
+
* on inline-block/block containers; the `<td>` already provides the container.
|
|
24
|
+
*/
|
|
25
|
+
export type CellRenderer = (value: unknown) => string | ReactElement;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Creates a sort header label renderer.
|
|
29
|
+
*
|
|
30
|
+
* Renders the LABEL ONLY. The interactive sort button and arrow belong to the
|
|
31
|
+
* `HeaderCell` component, which another agent owns. Do not add a `<button>`
|
|
32
|
+
* or an arrow here.
|
|
33
|
+
*/
|
|
34
|
+
export function createSortHeader(label: string): () => ReactElement {
|
|
35
|
+
return () => <span className="cph-table__sort-label">{label}</span>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a numeric cell renderer.
|
|
40
|
+
*
|
|
41
|
+
* Applies `cph-table__numeric` (which carries `text-align: right` and
|
|
42
|
+
* `font-variant-numeric: tabular-nums` in CSS) to a block-level wrapper.
|
|
43
|
+
* Formats via `toLocaleString` by default.
|
|
44
|
+
*
|
|
45
|
+
* The wrapper is a `<div>` (block-level) so `text-align: right` takes effect.
|
|
46
|
+
* The table body component SHOULD also apply `cph-table__numeric` to the `<td>`
|
|
47
|
+
* via `Cell`'s `className` prop for robust alignment.
|
|
48
|
+
*/
|
|
49
|
+
export function createNumericCell(format?: (value: unknown) => string): CellRenderer {
|
|
50
|
+
return (value: unknown) => {
|
|
51
|
+
const formatted = format
|
|
52
|
+
? format(value)
|
|
53
|
+
: typeof value === "number"
|
|
54
|
+
? value.toLocaleString("en-US")
|
|
55
|
+
: String(value ?? "");
|
|
56
|
+
return (
|
|
57
|
+
<div className="cph-table__numeric" data-cph-table="numeric-cell">
|
|
58
|
+
{formatted}
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a status cell renderer.
|
|
66
|
+
*
|
|
67
|
+
* Renders a {@link CphStatusChip} with the given tone.
|
|
68
|
+
*/
|
|
69
|
+
export function createStatusCell(tone: CphStatusTone): CellRenderer {
|
|
70
|
+
return (value: unknown) => <CphStatusChip tone={tone}>{String(value ?? "")}</CphStatusChip>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates a truncating cell renderer.
|
|
75
|
+
*
|
|
76
|
+
* Ellipsis-truncates long text. The `title` attribute keeps the full value
|
|
77
|
+
* reachable on hover, which the expansion panel also exposes.
|
|
78
|
+
*/
|
|
79
|
+
export function createTruncateCell(): CellRenderer {
|
|
80
|
+
return (value: unknown) => {
|
|
81
|
+
const text = String(value ?? "");
|
|
82
|
+
return (
|
|
83
|
+
<span className="cph-table__truncate" title={text} data-cph-table="truncate-cell">
|
|
84
|
+
{text}
|
|
85
|
+
</span>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import type { RowData } from "@tanstack/react-table";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { Cell } from "../components/TableRow/Cell";
|
|
6
|
+
import {
|
|
7
|
+
createNumericCell,
|
|
8
|
+
createSortHeader,
|
|
9
|
+
createStatusCell,
|
|
10
|
+
createTruncateCell,
|
|
11
|
+
} from "./cellRenderers";
|
|
12
|
+
import { cphColumnHelper, metaOf, type CphColumnMeta } from "./column";
|
|
13
|
+
|
|
14
|
+
// Test data type
|
|
15
|
+
type TestRow = RowData & {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
value: number;
|
|
19
|
+
status: string;
|
|
20
|
+
description: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe("column.ts — types and helpers", () => {
|
|
24
|
+
it("cphColumnHelper<TData>() produces defs typed against CphTableFeatures with no any", () => {
|
|
25
|
+
const helper = cphColumnHelper<TestRow>();
|
|
26
|
+
|
|
27
|
+
// These should all typecheck without any casts — the fact that this compiles
|
|
28
|
+
// proves the types are correct. We don't assert on column properties because
|
|
29
|
+
// the column array is a union of different column def types.
|
|
30
|
+
const columns = [
|
|
31
|
+
helper.accessor("id", {
|
|
32
|
+
header: "ID",
|
|
33
|
+
meta: { widthClass: "source" } satisfies CphColumnMeta,
|
|
34
|
+
}),
|
|
35
|
+
helper.accessor("name", {
|
|
36
|
+
header: "Name",
|
|
37
|
+
meta: {} satisfies CphColumnMeta,
|
|
38
|
+
}),
|
|
39
|
+
helper.accessor("value", {
|
|
40
|
+
header: "Value",
|
|
41
|
+
meta: { numeric: true, align: "right" } satisfies CphColumnMeta,
|
|
42
|
+
}),
|
|
43
|
+
helper.accessor("status", {
|
|
44
|
+
header: "Status",
|
|
45
|
+
meta: { statusTone: () => "sage" } satisfies CphColumnMeta,
|
|
46
|
+
}),
|
|
47
|
+
helper.display({
|
|
48
|
+
id: "actions",
|
|
49
|
+
header: "Actions",
|
|
50
|
+
cell: () => "…",
|
|
51
|
+
}),
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
// Verify the array length at runtime
|
|
55
|
+
expect(columns).toHaveLength(5);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("metaOf() returns the meta object when present", () => {
|
|
59
|
+
const helper = cphColumnHelper<TestRow>();
|
|
60
|
+
const def = helper.accessor("value", {
|
|
61
|
+
header: "Value",
|
|
62
|
+
meta: { numeric: true, align: "right", aggregate: "sum" } satisfies CphColumnMeta,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const meta = metaOf(def);
|
|
66
|
+
expect(meta).toEqual({ numeric: true, align: "right", aggregate: "sum" });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("metaOf() returns {} for a def with no meta", () => {
|
|
70
|
+
const helper = cphColumnHelper<TestRow>();
|
|
71
|
+
const def = helper.accessor("name", { header: "Name" });
|
|
72
|
+
|
|
73
|
+
const meta = metaOf(def);
|
|
74
|
+
expect(meta).toEqual({});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("metaOf() returns {} for a display def without meta", () => {
|
|
78
|
+
const helper = cphColumnHelper<TestRow>();
|
|
79
|
+
const def = helper.display({ id: "name", header: "Name" });
|
|
80
|
+
|
|
81
|
+
const meta = metaOf(def);
|
|
82
|
+
expect(meta).toEqual({});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("CphColumnMeta fields respect exactOptionalPropertyTypes (no explicit undefined)", () => {
|
|
86
|
+
// This test ensures the type allows optional fields without requiring explicit undefined
|
|
87
|
+
const meta: CphColumnMeta = {
|
|
88
|
+
numeric: true,
|
|
89
|
+
// align, aggregate, format, widthClass, statusTone are all optional
|
|
90
|
+
};
|
|
91
|
+
expect(meta.numeric).toBe(true);
|
|
92
|
+
expect(meta.align).toBeUndefined();
|
|
93
|
+
expect(meta.aggregate).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("cellRenderers.tsx — reusable cell renderers", () => {
|
|
98
|
+
describe("createSortHeader", () => {
|
|
99
|
+
it("returns a function that renders the label only (no button, no arrow)", () => {
|
|
100
|
+
const renderer = createSortHeader("Name");
|
|
101
|
+
const { container } = render(renderer());
|
|
102
|
+
|
|
103
|
+
const label = container.querySelector(".cph-table__sort-label");
|
|
104
|
+
expect(label).toBeInTheDocument();
|
|
105
|
+
expect(label).toHaveTextContent("Name");
|
|
106
|
+
|
|
107
|
+
// No button or arrow should be present
|
|
108
|
+
expect(container.querySelector("button")).not.toBeInTheDocument();
|
|
109
|
+
expect(container.querySelector(".cph-table__sort-arrow")).not.toBeInTheDocument();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("createNumericCell", () => {
|
|
114
|
+
it("formats numbers via toLocaleString by default", () => {
|
|
115
|
+
const renderer = createNumericCell();
|
|
116
|
+
const { container } = render(renderer(1234567.89));
|
|
117
|
+
|
|
118
|
+
const wrapper = container.querySelector(".cph-table__numeric");
|
|
119
|
+
expect(wrapper).toBeInTheDocument();
|
|
120
|
+
expect(wrapper).toHaveAttribute("data-cph-table", "numeric-cell");
|
|
121
|
+
expect(wrapper).toHaveTextContent("1,234,567.89");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("uses custom format function when provided", () => {
|
|
125
|
+
const renderer = createNumericCell((v) => `$${Number(v).toFixed(2)}`);
|
|
126
|
+
const { container } = render(renderer(42));
|
|
127
|
+
|
|
128
|
+
const wrapper = container.querySelector(".cph-table__numeric");
|
|
129
|
+
expect(wrapper).toBeInTheDocument();
|
|
130
|
+
expect(wrapper).toHaveTextContent("$42.00");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("handles null/undefined values gracefully", () => {
|
|
134
|
+
const renderer = createNumericCell();
|
|
135
|
+
const { container } = render(renderer(null));
|
|
136
|
+
|
|
137
|
+
const wrapper = container.querySelector(".cph-table__numeric");
|
|
138
|
+
expect(wrapper).toBeInTheDocument();
|
|
139
|
+
expect(wrapper).toHaveTextContent("");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("returns a block-level div so text-align: right takes effect", () => {
|
|
143
|
+
const renderer = createNumericCell();
|
|
144
|
+
const { container } = render(renderer(42));
|
|
145
|
+
|
|
146
|
+
const wrapper = container.querySelector(".cph-table__numeric");
|
|
147
|
+
expect(wrapper).toBeInTheDocument();
|
|
148
|
+
expect(wrapper?.tagName.toLowerCase()).toBe("div");
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("createStatusCell", () => {
|
|
153
|
+
it("renders CphStatusChip with the given tone", () => {
|
|
154
|
+
const renderer = createStatusCell("sage");
|
|
155
|
+
const { container } = render(renderer("Active"));
|
|
156
|
+
|
|
157
|
+
const chip = container.querySelector(".cph-table__chip");
|
|
158
|
+
expect(chip).toBeInTheDocument();
|
|
159
|
+
expect(chip).toHaveClass("cph-badge--sage");
|
|
160
|
+
expect(chip).toHaveTextContent("Active");
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("handles null/undefined values gracefully", () => {
|
|
164
|
+
const renderer = createStatusCell("error");
|
|
165
|
+
const { container } = render(renderer(null));
|
|
166
|
+
|
|
167
|
+
const chip = container.querySelector(".cph-table__chip");
|
|
168
|
+
expect(chip).toBeInTheDocument();
|
|
169
|
+
expect(chip).toHaveTextContent("");
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("createTruncateCell", () => {
|
|
174
|
+
it("renders text with cph-table__truncate class and title attribute", () => {
|
|
175
|
+
const renderer = createTruncateCell();
|
|
176
|
+
const { container } = render(renderer("Very long description that should be truncated"));
|
|
177
|
+
|
|
178
|
+
const span = container.querySelector(".cph-table__truncate");
|
|
179
|
+
expect(span).toBeInTheDocument();
|
|
180
|
+
expect(span).toHaveAttribute("data-cph-table", "truncate-cell");
|
|
181
|
+
expect(span).toHaveAttribute("title", "Very long description that should be truncated");
|
|
182
|
+
expect(span).toHaveTextContent("Very long description that should be truncated");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("handles null/undefined values gracefully", () => {
|
|
186
|
+
const renderer = createTruncateCell();
|
|
187
|
+
const { container } = render(renderer(null));
|
|
188
|
+
|
|
189
|
+
const span = container.querySelector(".cph-table__truncate");
|
|
190
|
+
expect(span).toBeInTheDocument();
|
|
191
|
+
expect(span).toHaveAttribute("title", "");
|
|
192
|
+
expect(span).toHaveTextContent("");
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("returns an inline span (truncation CSS works on inline-block container)", () => {
|
|
196
|
+
const renderer = createTruncateCell();
|
|
197
|
+
const { container } = render(renderer("text"));
|
|
198
|
+
|
|
199
|
+
const span = container.querySelector(".cph-table__truncate");
|
|
200
|
+
expect(span).toBeInTheDocument();
|
|
201
|
+
expect(span?.tagName.toLowerCase()).toBe("span");
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe("Integration: numeric meta reaches rendered td class", () => {
|
|
207
|
+
// This test simulates how the table body component would compose the renderer
|
|
208
|
+
// with the Cell component, reading meta.numeric to apply the class to the <td>.
|
|
209
|
+
it("numeric meta causes cph-table__numeric to be applied to the td via Cell", () => {
|
|
210
|
+
const helper = cphColumnHelper<TestRow>();
|
|
211
|
+
const def = helper.accessor("value", {
|
|
212
|
+
header: "Value",
|
|
213
|
+
cell: createNumericCell(),
|
|
214
|
+
meta: { numeric: true } satisfies CphColumnMeta,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Simulate the table body component logic:
|
|
218
|
+
// 1. Read meta from column def
|
|
219
|
+
const meta = metaOf(def);
|
|
220
|
+
const tdClassName = meta.numeric ? "cph-table__numeric" : "";
|
|
221
|
+
|
|
222
|
+
// 2. Get the cell value (in real usage this comes from row data)
|
|
223
|
+
const value = 12345;
|
|
224
|
+
|
|
225
|
+
// 3. Call the cell renderer (def.cell is the renderer function)
|
|
226
|
+
const cellRenderer = def.cell as (value: unknown) => React.ReactElement;
|
|
227
|
+
const cellContent = cellRenderer(value);
|
|
228
|
+
|
|
229
|
+
// 4. Render via Cell component with the td className
|
|
230
|
+
const { container } = render(
|
|
231
|
+
<Cell className={tdClassName} data-cph-table="cell">
|
|
232
|
+
{cellContent}
|
|
233
|
+
</Cell>,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
// The td should have the numeric class
|
|
237
|
+
const td = container.querySelector("td.cph-table__numeric");
|
|
238
|
+
expect(td).toBeInTheDocument();
|
|
239
|
+
|
|
240
|
+
// The content wrapper should ALSO have the class (defense in depth)
|
|
241
|
+
const contentWrapper = container.querySelector(
|
|
242
|
+
".cph-table__numeric[data-cph-table='numeric-cell']",
|
|
243
|
+
);
|
|
244
|
+
expect(contentWrapper).toBeInTheDocument();
|
|
245
|
+
expect(contentWrapper).toHaveTextContent("12,345");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("non-numeric meta does not apply cph-table__numeric to td", () => {
|
|
249
|
+
const helper = cphColumnHelper<TestRow>();
|
|
250
|
+
const def = helper.accessor("name", {
|
|
251
|
+
header: "Name",
|
|
252
|
+
cell: createTruncateCell(),
|
|
253
|
+
meta: { numeric: false } satisfies CphColumnMeta,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const meta = metaOf(def);
|
|
257
|
+
const tdClassName = meta.numeric ? "cph-table__numeric" : "";
|
|
258
|
+
|
|
259
|
+
const value = "Some name";
|
|
260
|
+
const cellRenderer = def.cell as (value: unknown) => React.ReactElement;
|
|
261
|
+
const cellContent = cellRenderer(value);
|
|
262
|
+
|
|
263
|
+
const { container } = render(
|
|
264
|
+
<Cell className={tdClassName} data-cph-table="cell">
|
|
265
|
+
{cellContent}
|
|
266
|
+
</Cell>,
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const td = container.querySelector("td");
|
|
270
|
+
expect(td).toBeInTheDocument();
|
|
271
|
+
expect(td).not.toHaveClass("cph-table__numeric");
|
|
272
|
+
|
|
273
|
+
// The content should have truncate class instead
|
|
274
|
+
const truncateSpan = container.querySelector(".cph-table__truncate");
|
|
275
|
+
expect(truncateSpan).toBeInTheDocument();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createColumnHelper,
|
|
3
|
+
type CellData,
|
|
4
|
+
type ColumnDef,
|
|
5
|
+
type RowData,
|
|
6
|
+
} from "@tanstack/react-table";
|
|
7
|
+
|
|
8
|
+
import type { CphStatusTone } from "../components/StatusChip/CphStatusChip";
|
|
9
|
+
import type { CphTableFeatures } from "./features";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Per-column presentation config, carried on TanStack's `meta` bag so the table
|
|
13
|
+
* primitives stay generic and know nothing about any specific dataset.
|
|
14
|
+
*/
|
|
15
|
+
export interface CphColumnMeta {
|
|
16
|
+
numeric?: boolean;
|
|
17
|
+
align?: "left" | "right";
|
|
18
|
+
aggregate?: "sum" | "mean" | "count";
|
|
19
|
+
format?: (value: unknown) => string;
|
|
20
|
+
widthClass?: string;
|
|
21
|
+
statusTone?: (value: unknown) => CphStatusTone;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type WithMeta = { meta?: CphColumnMeta };
|
|
25
|
+
|
|
26
|
+
/** Creates a column def bound to `TData` with {@link CphColumnMeta} typing. */
|
|
27
|
+
export function cphColumnHelper<TData extends RowData>() {
|
|
28
|
+
return createColumnHelper<CphTableFeatures, TData>();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Reads `meta` off a column def without the generic noise at call sites. */
|
|
32
|
+
export function metaOf<TData extends RowData, TValue extends CellData = CellData>(
|
|
33
|
+
def: ColumnDef<CphTableFeatures, TData, TValue>,
|
|
34
|
+
): CphColumnMeta {
|
|
35
|
+
return (def as WithMeta).meta ?? {};
|
|
36
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
columnFilteringFeature,
|
|
3
|
+
columnOrderingFeature,
|
|
4
|
+
columnPinningFeature,
|
|
5
|
+
columnSizingFeature,
|
|
6
|
+
columnVisibilityFeature,
|
|
7
|
+
createExpandedRowModel,
|
|
8
|
+
createFilteredRowModel,
|
|
9
|
+
createPaginatedRowModel,
|
|
10
|
+
createSortedRowModel,
|
|
11
|
+
filterFn_includesString,
|
|
12
|
+
globalFilteringFeature,
|
|
13
|
+
rowAggregationFeature,
|
|
14
|
+
rowExpandingFeature,
|
|
15
|
+
rowPaginationFeature,
|
|
16
|
+
rowSelectionFeature,
|
|
17
|
+
rowSortingFeature,
|
|
18
|
+
sortFn_alphanumeric,
|
|
19
|
+
sortFn_text,
|
|
20
|
+
aggregationFn_count,
|
|
21
|
+
aggregationFn_extent,
|
|
22
|
+
aggregationFn_mean,
|
|
23
|
+
aggregationFn_sum,
|
|
24
|
+
tableFeatures,
|
|
25
|
+
} from "@tanstack/react-table";
|
|
26
|
+
|
|
27
|
+
import type { CphColumnMeta } from "./column";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The table's feature registry.
|
|
31
|
+
*
|
|
32
|
+
* This MUST stay at module scope: TanStack reads it for type inference and for
|
|
33
|
+
* feature-slot validation, and rebuilding it per render re-validates the whole
|
|
34
|
+
* object on every frame.
|
|
35
|
+
*
|
|
36
|
+
* Two spec rules are enforced here rather than at each call site:
|
|
37
|
+
* `enableMultiSort` and `expandSingleRowOnly` are set in {@link useCphTable}.
|
|
38
|
+
*/
|
|
39
|
+
export const cphTableFeatures = tableFeatures({
|
|
40
|
+
columnFilteringFeature,
|
|
41
|
+
columnOrderingFeature,
|
|
42
|
+
columnPinningFeature,
|
|
43
|
+
columnSizingFeature,
|
|
44
|
+
columnVisibilityFeature,
|
|
45
|
+
globalFilteringFeature,
|
|
46
|
+
rowAggregationFeature,
|
|
47
|
+
rowExpandingFeature,
|
|
48
|
+
rowPaginationFeature,
|
|
49
|
+
rowSelectionFeature,
|
|
50
|
+
rowSortingFeature,
|
|
51
|
+
|
|
52
|
+
sortedRowModel: createSortedRowModel(),
|
|
53
|
+
filteredRowModel: createFilteredRowModel(),
|
|
54
|
+
paginatedRowModel: createPaginatedRowModel(),
|
|
55
|
+
expandedRowModel: createExpandedRowModel(),
|
|
56
|
+
|
|
57
|
+
sortFns: { alphanumeric: sortFn_alphanumeric, text: sortFn_text },
|
|
58
|
+
filterFns: { includesString: filterFn_includesString },
|
|
59
|
+
aggregationFns: {
|
|
60
|
+
sum: aggregationFn_sum,
|
|
61
|
+
mean: aggregationFn_mean,
|
|
62
|
+
count: aggregationFn_count,
|
|
63
|
+
extent: aggregationFn_extent,
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
columnMeta: {} as CphColumnMeta,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export type CphTableFeatures = typeof cphTableFeatures;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/** @packageDocumentation Tests for page size utilities. */
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from "vitest";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
PAGE_SIZES,
|
|
7
|
+
type PageSize,
|
|
8
|
+
DEFAULT_PAGE_SIZE,
|
|
9
|
+
nextPageSize,
|
|
10
|
+
getTotalPages,
|
|
11
|
+
PAGE_SIZE_LABELS,
|
|
12
|
+
} from "./pageSize";
|
|
13
|
+
|
|
14
|
+
describe("pageSize.ts", () => {
|
|
15
|
+
describe("PAGE_SIZES", () => {
|
|
16
|
+
it("is a const tuple of [8, 12, 20]", () => {
|
|
17
|
+
expect(PAGE_SIZES).toEqual([8, 12, 20]);
|
|
18
|
+
expect(PAGE_SIZES.length).toBe(3);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("is readonly (TypeScript enforces this at compile time)", () => {
|
|
22
|
+
// This is a compile-time check only - the following line would cause a TS error:
|
|
23
|
+
// PAGE_SIZES.push(25);
|
|
24
|
+
// We verify the array hasn't been mutated at runtime
|
|
25
|
+
expect(PAGE_SIZES).toEqual([8, 12, 20]);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("PageSize type", () => {
|
|
30
|
+
it("is a union of 8 | 12 | 20", () => {
|
|
31
|
+
// This is a compile-time check; if it compiles, the type is correct
|
|
32
|
+
const sizes: PageSize[] = [8, 12, 20];
|
|
33
|
+
expect(sizes).toEqual([8, 12, 20]);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("DEFAULT_PAGE_SIZE", () => {
|
|
38
|
+
it("is 8", () => {
|
|
39
|
+
expect(DEFAULT_PAGE_SIZE).toBe(8);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("is a valid PageSize", () => {
|
|
43
|
+
const size: PageSize = DEFAULT_PAGE_SIZE;
|
|
44
|
+
expect(PAGE_SIZES.includes(size)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("nextPageSize", () => {
|
|
49
|
+
it("cycles 8 → 12", () => {
|
|
50
|
+
expect(nextPageSize(8)).toBe(12);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("cycles 12 → 20", () => {
|
|
54
|
+
expect(nextPageSize(12)).toBe(20);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("cycles 20 → 8", () => {
|
|
58
|
+
expect(nextPageSize(20)).toBe(8);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("cycles correctly through multiple iterations", () => {
|
|
62
|
+
let current: PageSize = 8;
|
|
63
|
+
expect(nextPageSize(current)).toBe(12);
|
|
64
|
+
current = nextPageSize(current);
|
|
65
|
+
expect(nextPageSize(current)).toBe(20);
|
|
66
|
+
current = nextPageSize(current);
|
|
67
|
+
expect(nextPageSize(current)).toBe(8);
|
|
68
|
+
current = nextPageSize(current);
|
|
69
|
+
expect(nextPageSize(current)).toBe(12);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns a valid PageSize (no non-null assertion in source)", () => {
|
|
73
|
+
// This test verifies the function returns a valid PageSize
|
|
74
|
+
// The source code uses `as PageSize` cast, not `!` non-null assertion
|
|
75
|
+
const result = nextPageSize(8);
|
|
76
|
+
expect(PAGE_SIZES.includes(result)).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe("getTotalPages", () => {
|
|
81
|
+
it("returns 1 for 0 rows", () => {
|
|
82
|
+
expect(getTotalPages(0, 8)).toBe(1);
|
|
83
|
+
expect(getTotalPages(0, 12)).toBe(1);
|
|
84
|
+
expect(getTotalPages(0, 20)).toBe(1);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("returns 1 for row count <= page size", () => {
|
|
88
|
+
expect(getTotalPages(5, 8)).toBe(1);
|
|
89
|
+
expect(getTotalPages(8, 8)).toBe(1);
|
|
90
|
+
expect(getTotalPages(10, 12)).toBe(1);
|
|
91
|
+
expect(getTotalPages(20, 20)).toBe(1);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("returns correct page count for exact multiples", () => {
|
|
95
|
+
expect(getTotalPages(16, 8)).toBe(2);
|
|
96
|
+
expect(getTotalPages(24, 12)).toBe(2);
|
|
97
|
+
expect(getTotalPages(40, 20)).toBe(2);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("returns correct page count for partial pages", () => {
|
|
101
|
+
expect(getTotalPages(9, 8)).toBe(2);
|
|
102
|
+
expect(getTotalPages(10, 8)).toBe(2);
|
|
103
|
+
expect(getTotalPages(15, 8)).toBe(2);
|
|
104
|
+
expect(getTotalPages(17, 8)).toBe(3);
|
|
105
|
+
expect(getTotalPages(25, 12)).toBe(3);
|
|
106
|
+
expect(getTotalPages(41, 20)).toBe(3);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("works with filtered row counts (e.g., 10 filtered rows, page size 8)", () => {
|
|
110
|
+
// This is the key scenario: 10 filtered rows, page size 8 = 2 pages
|
|
111
|
+
expect(getTotalPages(10, 8)).toBe(2);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("PAGE_SIZE_LABELS", () => {
|
|
116
|
+
it("has a label for each PageSize", () => {
|
|
117
|
+
expect(PAGE_SIZE_LABELS[8]).toBe("8 per page");
|
|
118
|
+
expect(PAGE_SIZE_LABELS[12]).toBe("12 per page");
|
|
119
|
+
expect(PAGE_SIZE_LABELS[20]).toBe("20 per page");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("is a const record with all PageSize keys", () => {
|
|
123
|
+
const keys = Object.keys(PAGE_SIZE_LABELS).map(Number);
|
|
124
|
+
// oxlint-disable-next-line unicorn/no-array-sort -- deterministic comparison of constant size keys
|
|
125
|
+
expect(keys.sort((a, b) => a - b)).toEqual([8, 12, 20]);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** @packageDocumentation Page size constants and utilities. */
|
|
2
|
+
|
|
3
|
+
import type { Key } from "react";
|
|
4
|
+
|
|
5
|
+
/** Supported page sizes in ascending order. */
|
|
6
|
+
export const PAGE_SIZES = [8, 12, 20] as const;
|
|
7
|
+
|
|
8
|
+
export type PageSize = (typeof PAGE_SIZES)[number];
|
|
9
|
+
|
|
10
|
+
/** Human-readable labels for the supported page sizes. */
|
|
11
|
+
export const PAGE_SIZE_LABELS: Record<PageSize, string> = {
|
|
12
|
+
8: "8 per page",
|
|
13
|
+
12: "12 per page",
|
|
14
|
+
20: "20 per page",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Default page size. */
|
|
18
|
+
export const DEFAULT_PAGE_SIZE: PageSize = 8;
|
|
19
|
+
|
|
20
|
+
/** Returns at least one page, including when there are no rows. */
|
|
21
|
+
export function getTotalPages(rowCount: number, pageSize: PageSize): number {
|
|
22
|
+
return Math.max(1, Math.ceil(rowCount / pageSize));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Next page size in the 8 -> 12 -> 20 -> 8 cycle. */
|
|
26
|
+
const NEXT_PAGE_SIZE: Record<PageSize, PageSize> = { 8: 12, 12: 20, 20: 8 };
|
|
27
|
+
|
|
28
|
+
/** Get the next page size in the cycle. */
|
|
29
|
+
export function nextPageSize(current: PageSize): PageSize {
|
|
30
|
+
return NEXT_PAGE_SIZE[current];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function isPageSize(value: number): value is PageSize {
|
|
34
|
+
return PAGE_SIZES.some((size) => size === value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolves a React Aria selection key to a page size. Returns undefined for
|
|
39
|
+
* null, non-string, or non-page-size keys so callers can ignore them.
|
|
40
|
+
*/
|
|
41
|
+
export function parsePageSizeKey(value: Key | null): PageSize | undefined {
|
|
42
|
+
if (typeof value !== "string") {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const numericValue = Number(value);
|
|
46
|
+
return isPageSize(numericValue) ? numericValue : undefined;
|
|
47
|
+
}
|