@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,182 @@
1
+ import { render, screen } from "@testing-library/react";
2
+ import { describe, expect, it } from "vitest";
3
+
4
+ import { CphRowDetail } from "./CphRowDetail";
5
+
6
+ const getByCphTable = (container: HTMLElement, value: string) =>
7
+ container.querySelector(`[data-cph-table="${value}"]`) as HTMLElement;
8
+
9
+ describe("CphRowDetail", () => {
10
+ it("renders the detail row with the correct colSpan from visibleCellsCount", () => {
11
+ const { container } = render(
12
+ <CphRowDetail visibleCellsCount={5} expanded={true}>
13
+ Detail content
14
+ </CphRowDetail>,
15
+ );
16
+ const td = getByCphTable(container, "detail-cell");
17
+ expect(td).toHaveAttribute("colSpan", "5");
18
+ });
19
+
20
+ it("does not hardcode colSpan — it varies with visibleCellsCount", () => {
21
+ const { container, rerender } = render(
22
+ <CphRowDetail visibleCellsCount={3} expanded={true}>
23
+ Detail content
24
+ </CphRowDetail>,
25
+ );
26
+ expect(getByCphTable(container, "detail-cell")).toHaveAttribute("colSpan", "3");
27
+
28
+ rerender(
29
+ <CphRowDetail visibleCellsCount={7} expanded={true}>
30
+ Detail content
31
+ </CphRowDetail>,
32
+ );
33
+ expect(getByCphTable(container, "detail-cell")).toHaveAttribute("colSpan", "7");
34
+ });
35
+
36
+ it("uses grid-template-rows animation (0fr/1fr), not height or max-height", () => {
37
+ const { container } = render(
38
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
39
+ Detail content
40
+ </CphRowDetail>,
41
+ );
42
+ const animationWrapper = getByCphTable(container, "detail-animation");
43
+ const style = animationWrapper.style;
44
+
45
+ expect(style.display).toBe("grid");
46
+ expect(style.gridTemplateRows).toBe("1fr");
47
+ expect(style.transition).toContain("grid-template-rows");
48
+ expect(style.transition).not.toContain("height");
49
+ expect(style.transition).not.toContain("max-height");
50
+ });
51
+
52
+ it("has transition-duration of 0.2s (200ms)", () => {
53
+ const { container } = render(
54
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
55
+ Detail content
56
+ </CphRowDetail>,
57
+ );
58
+ const animationWrapper = getByCphTable(container, "detail-animation");
59
+ const style = animationWrapper.style;
60
+
61
+ expect(style.transition).toContain("0.2s");
62
+ });
63
+
64
+ it("exposes aria-expanded=true when expanded", () => {
65
+ const { container } = render(
66
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
67
+ Detail content
68
+ </CphRowDetail>,
69
+ );
70
+ const row = getByCphTable(container, "detail-row");
71
+ expect(row).toHaveAttribute("aria-expanded", "true");
72
+ expect(row).toHaveAttribute("data-expanded", "true");
73
+ });
74
+
75
+ it("exposes aria-expanded=false when collapsed", () => {
76
+ const { container } = render(
77
+ <CphRowDetail visibleCellsCount={4} expanded={false}>
78
+ Detail content
79
+ </CphRowDetail>,
80
+ );
81
+ const row = getByCphTable(container, "detail-row");
82
+ expect(row).toHaveAttribute("aria-expanded", "false");
83
+ expect(row).toHaveAttribute("data-expanded", "false");
84
+ });
85
+
86
+ it("renders detail content in the serif face (--cph-font-serif)", () => {
87
+ const { container } = render(
88
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
89
+ Detail content
90
+ </CphRowDetail>,
91
+ );
92
+ const content = getByCphTable(container, "detail-content");
93
+ expect(content.style.fontFamily).toBe("var(--cph-font-serif)");
94
+ });
95
+
96
+ it("keeps the row in the DOM when collapsed (uses grid-rows, not display:none)", () => {
97
+ const { container } = render(
98
+ <CphRowDetail visibleCellsCount={4} expanded={false}>
99
+ Detail content
100
+ </CphRowDetail>,
101
+ );
102
+ const row = getByCphTable(container, "detail-row");
103
+ expect(row).toBeInTheDocument();
104
+ expect(row.style.display).not.toBe("none");
105
+ });
106
+
107
+ it("applies grid-template-rows: 0fr when collapsed", () => {
108
+ const { container } = render(
109
+ <CphRowDetail visibleCellsCount={4} expanded={false}>
110
+ Detail content
111
+ </CphRowDetail>,
112
+ );
113
+ const animationWrapper = getByCphTable(container, "detail-animation");
114
+ expect(animationWrapper.style.gridTemplateRows).toBe("0fr");
115
+ });
116
+
117
+ it("applies grid-template-rows: 1fr when expanded", () => {
118
+ const { container } = render(
119
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
120
+ Detail content
121
+ </CphRowDetail>,
122
+ );
123
+ const animationWrapper = getByCphTable(container, "detail-animation");
124
+ expect(animationWrapper.style.gridTemplateRows).toBe("1fr");
125
+ });
126
+
127
+ it("hides collapsed content from assistive technology", () => {
128
+ const { container } = render(
129
+ <CphRowDetail visibleCellsCount={4} expanded={false}>
130
+ Detail content
131
+ </CphRowDetail>,
132
+ );
133
+ const content = getByCphTable(container, "detail-content");
134
+ expect(content).toHaveAttribute("aria-hidden", "true");
135
+ expect(content).toHaveAttribute("inert", "");
136
+ });
137
+
138
+ it("exposes expanded content to assistive technology", () => {
139
+ const { container } = render(
140
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
141
+ Detail content
142
+ </CphRowDetail>,
143
+ );
144
+ const content = getByCphTable(container, "detail-content");
145
+ expect(content).toHaveAttribute("aria-hidden", "false");
146
+ expect(content).not.toHaveAttribute("inert");
147
+ });
148
+
149
+ it("zeroes the cell height when collapsed so no blank row remains", () => {
150
+ const { container } = render(
151
+ <CphRowDetail visibleCellsCount={4} expanded={false}>
152
+ Detail content
153
+ </CphRowDetail>,
154
+ );
155
+ expect(getByCphTable(container, "detail-cell")).toHaveStyle({ height: "0px" });
156
+ });
157
+
158
+ it("leaves the cell height alone when expanded", () => {
159
+ const { container } = render(
160
+ <CphRowDetail visibleCellsCount={4} expanded={true}>
161
+ Detail content
162
+ </CphRowDetail>,
163
+ );
164
+ const cell = getByCphTable(container, "detail-cell");
165
+ expect(cell.style.height).toBe("");
166
+ });
167
+
168
+ it("forwards extra props to the tr element", () => {
169
+ render(
170
+ <CphRowDetail
171
+ visibleCellsCount={4}
172
+ expanded={true}
173
+ className="cph-test-custom-class"
174
+ data-testid="custom-row"
175
+ >
176
+ Detail content
177
+ </CphRowDetail>,
178
+ );
179
+ const row = screen.getByTestId("custom-row");
180
+ expect(row).toHaveClass("cph-test-custom-class");
181
+ });
182
+ });
@@ -0,0 +1,66 @@
1
+ /** @packageDocumentation Expandable row detail content. */
2
+
3
+ import type { HTMLAttributes, ReactNode } from "react";
4
+
5
+ export interface CphRowDetailProps extends HTMLAttributes<HTMLTableRowElement> {
6
+ children: ReactNode;
7
+ /** Number of visible cells to determine colspan. */
8
+ visibleCellsCount: number;
9
+ /** Whether row is expanded. */
10
+ expanded: boolean;
11
+ }
12
+
13
+ /**
14
+ * Detail row that expands when parent row is expanded.
15
+ *
16
+ * Uses a 200ms grid-rows animation for smooth expansion.
17
+ * ColSpan covers all visible cells.
18
+ * Detail content renders in the serif face (--cph-font-serif).
19
+ * The expanded state is exposed via aria-expanded for accessibility.
20
+ */
21
+ export function CphRowDetail({
22
+ children,
23
+ visibleCellsCount,
24
+ expanded,
25
+ ...props
26
+ }: Readonly<CphRowDetailProps>) {
27
+ return (
28
+ <tr
29
+ {...props}
30
+ data-cph-table="detail-row"
31
+ data-expanded={expanded ? "true" : "false"}
32
+ aria-expanded={expanded}
33
+ >
34
+ <td
35
+ colSpan={visibleCellsCount}
36
+ data-cph-table="detail-cell"
37
+ style={expanded ? { padding: 0 } : { padding: 0, height: 0 }}
38
+ >
39
+ <div
40
+ style={{
41
+ display: "grid",
42
+ gridTemplateRows: expanded ? "1fr" : "0fr",
43
+ transition: "grid-template-rows 0.2s var(--cph-ease-out)",
44
+ overflow: "hidden",
45
+ minHeight: 0,
46
+ }}
47
+ data-cph-table="detail-animation"
48
+ >
49
+ <div
50
+ style={{
51
+ minHeight: 0,
52
+ overflow: "hidden",
53
+ fontFamily: "var(--cph-font-serif)",
54
+ padding: "var(--cph-table-cell-y) var(--cph-space-3)",
55
+ }}
56
+ data-cph-table="detail-content"
57
+ aria-hidden={!expanded}
58
+ inert={!expanded}
59
+ >
60
+ {children}
61
+ </div>
62
+ </div>
63
+ </td>
64
+ </tr>
65
+ );
66
+ }
@@ -0,0 +1,235 @@
1
+ import { render, screen, within } from "@testing-library/react";
2
+ import userEvent from "@testing-library/user-event";
3
+ import { describe, expect, it, vi } from "vitest";
4
+
5
+ import { CphSimpleTable, type CphSimpleTableColumn } from "./CphSimpleTable";
6
+
7
+ type Row = { id: string; name: string; sources: number; owner: string };
8
+
9
+ const columns: CphSimpleTableColumn<Row>[] = [
10
+ { id: "name", header: "Inquiry", accessor: (row) => row.name, allowsSorting: true },
11
+ { id: "owner", header: "Owner", accessor: (row) => row.owner, allowsSorting: true },
12
+ {
13
+ id: "sources",
14
+ header: "Sources",
15
+ accessor: (row) => row.sources,
16
+ allowsSorting: true,
17
+ align: "right",
18
+ },
19
+ ];
20
+
21
+ const rows: Row[] = [
22
+ { id: "r1", name: "Beta", owner: "Ada", sources: 10 },
23
+ { id: "r2", name: "Alpha", owner: "Grace", sources: 2 },
24
+ { id: "r3", name: "Gamma", owner: "Lin", sources: 30 },
25
+ ];
26
+
27
+ /** First-cell values of every data row, in render order. */
28
+ function firstCellValues(): string[] {
29
+ return screen
30
+ .getAllByRole("row")
31
+ .filter((row) => row.querySelector("td") !== null)
32
+ .map((row) => row.querySelector("td")?.textContent?.trim() ?? "");
33
+ }
34
+
35
+ describe("CphSimpleTable", () => {
36
+ it("renders rows and columns through the table role", () => {
37
+ render(<CphSimpleTable aria-label="Inquiries" columns={columns} rows={rows} />);
38
+
39
+ expect(screen.getByRole("table", { name: "Inquiries" })).toBeInTheDocument();
40
+ expect(screen.getByRole("columnheader", { name: /Inquiry/ })).toBeInTheDocument();
41
+ expect(firstCellValues()).toEqual(["Beta", "Alpha", "Gamma"]);
42
+ });
43
+
44
+ it("cycles sort ascending then descending on header click", async () => {
45
+ const user = userEvent.setup();
46
+ render(<CphSimpleTable aria-label="Inquiries" columns={columns} rows={rows} />);
47
+
48
+ const nameHeader = screen.getByRole("columnheader", { name: /Inquiry/ });
49
+ await user.click(within(nameHeader).getByRole("button", { name: /sort ascending/i }));
50
+ expect(nameHeader).toHaveAttribute("aria-sort", "ascending");
51
+ expect(firstCellValues()).toEqual(["Alpha", "Beta", "Gamma"]);
52
+
53
+ await user.click(within(nameHeader).getByRole("button", { name: /sort descending/i }));
54
+ expect(nameHeader).toHaveAttribute("aria-sort", "descending");
55
+ expect(firstCellValues()).toEqual(["Gamma", "Beta", "Alpha"]);
56
+ });
57
+
58
+ it("applies defaultSortDescriptor on initial render", () => {
59
+ render(
60
+ <CphSimpleTable
61
+ aria-label="Inquiries"
62
+ columns={columns}
63
+ rows={rows}
64
+ defaultSortDescriptor={{ column: "name", direction: "descending" }}
65
+ />,
66
+ );
67
+
68
+ expect(screen.getByRole("columnheader", { name: /Inquiry/ })).toHaveAttribute(
69
+ "aria-sort",
70
+ "descending",
71
+ );
72
+ expect(firstCellValues()).toEqual(["Gamma", "Beta", "Alpha"]);
73
+ });
74
+
75
+ it("supports controlled sorting round-trips", async () => {
76
+ const user = userEvent.setup();
77
+ const onSortChange = vi.fn();
78
+ const sortDescriptor = { column: "name", direction: "ascending" as const };
79
+
80
+ const { rerender } = render(
81
+ <CphSimpleTable
82
+ aria-label="Inquiries"
83
+ columns={columns}
84
+ rows={rows}
85
+ sortDescriptor={sortDescriptor}
86
+ onSortChange={onSortChange}
87
+ />,
88
+ );
89
+
90
+ expect(firstCellValues()).toEqual(["Alpha", "Beta", "Gamma"]);
91
+ await user.click(screen.getByRole("button", { name: /sort descending/i }));
92
+ expect(onSortChange).toHaveBeenCalledWith({ column: "name", direction: "descending" });
93
+
94
+ rerender(
95
+ <CphSimpleTable
96
+ aria-label="Inquiries"
97
+ columns={columns}
98
+ rows={rows}
99
+ sortDescriptor={{ column: "name", direction: "descending" }}
100
+ onSortChange={onSortChange}
101
+ />,
102
+ );
103
+
104
+ expect(screen.getByRole("columnheader", { name: /Inquiry/ })).toHaveAttribute(
105
+ "aria-sort",
106
+ "descending",
107
+ );
108
+ });
109
+
110
+ it("sorts by sortValue instead of the displayed accessor value", () => {
111
+ const sortColumns: CphSimpleTableColumn<{ id: string; label: string; score: number }>[] = [
112
+ {
113
+ id: "label",
114
+ header: "Label",
115
+ accessor: (row) => row.label,
116
+ sortValue: (row) => row.score,
117
+ },
118
+ ];
119
+ const sortRows = [
120
+ { id: "1", label: "Gamma", score: 10 },
121
+ { id: "2", label: "Alpha", score: 30 },
122
+ { id: "3", label: "Beta", score: 20 },
123
+ ];
124
+
125
+ render(
126
+ <CphSimpleTable
127
+ aria-label="Scores"
128
+ columns={sortColumns}
129
+ rows={sortRows}
130
+ sortDescriptor={{ column: "label", direction: "ascending" }}
131
+ />,
132
+ );
133
+
134
+ expect(screen.getByRole("columnheader", { name: /Label/ })).toHaveAttribute(
135
+ "aria-sort",
136
+ "ascending",
137
+ );
138
+ expect(screen.getAllByRole("cell").map((cell) => cell.textContent)).toEqual([
139
+ "Gamma",
140
+ "Beta",
141
+ "Alpha",
142
+ ]);
143
+ });
144
+
145
+ it("renders no sort affordance when allowsSorting is false", () => {
146
+ render(
147
+ <CphSimpleTable
148
+ aria-label="Inquiries"
149
+ columns={[{ id: "name", header: "Inquiry", allowsSorting: false }]}
150
+ rows={rows}
151
+ />,
152
+ );
153
+
154
+ expect(screen.queryByRole("button", { name: /sort/i })).not.toBeInTheDocument();
155
+ expect(screen.getByRole("columnheader", { name: /Inquiry/ })).toBeInTheDocument();
156
+ });
157
+
158
+ it("supports controlled selection and select-all", async () => {
159
+ const user = userEvent.setup();
160
+ const onSelectionChange = vi.fn();
161
+ render(
162
+ <CphSimpleTable
163
+ aria-label="Inquiries"
164
+ columns={columns}
165
+ rows={rows}
166
+ selectionMode="multiple"
167
+ selectedKeys={new Set(["r1"])}
168
+ onSelectionChange={onSelectionChange}
169
+ />,
170
+ );
171
+
172
+ const betaRow = screen.getByRole("row", { name: /Beta/ });
173
+ expect(betaRow).toHaveAttribute("data-selected", "true");
174
+
175
+ const checkbox = within(betaRow).getByRole("checkbox", { name: /Select row r1/ });
176
+ await user.click(checkbox);
177
+ expect(onSelectionChange).toHaveBeenCalled();
178
+
179
+ const selectAll = screen.getByRole("checkbox", { name: /Select all rows/ });
180
+ await user.click(selectAll);
181
+ expect(onSelectionChange).toHaveBeenCalledWith("all");
182
+ });
183
+
184
+ it("supports self-managed selection", async () => {
185
+ const user = userEvent.setup();
186
+ render(
187
+ <CphSimpleTable
188
+ aria-label="Inquiries"
189
+ columns={columns}
190
+ rows={rows}
191
+ selectionMode="multiple"
192
+ defaultSelectedKeys={new Set()}
193
+ />,
194
+ );
195
+
196
+ const betaRow = screen.getByRole("row", { name: /Beta/ });
197
+ const checkbox = within(betaRow).getByRole("checkbox", { name: /Select row r1/ });
198
+ await user.click(checkbox);
199
+ expect(checkbox).toBeChecked();
200
+ });
201
+
202
+ it("renders cursor pagination controls and fires callbacks", async () => {
203
+ const user = userEvent.setup();
204
+ const onNextPage = vi.fn();
205
+ const onPreviousPage = vi.fn();
206
+ render(
207
+ <CphSimpleTable
208
+ aria-label="Inquiries"
209
+ columns={columns}
210
+ rows={rows}
211
+ pagination={{
212
+ hasNextPage: true,
213
+ hasPreviousPage: true,
214
+ startCursor: "start",
215
+ endCursor: "end",
216
+ onNextPage,
217
+ onPreviousPage,
218
+ }}
219
+ />,
220
+ );
221
+
222
+ expect(screen.getByRole("navigation", { name: "Table pagination" })).toBeInTheDocument();
223
+ await user.click(screen.getByRole("button", { name: "Next" }));
224
+ expect(onNextPage).toHaveBeenCalledWith("end");
225
+ await user.click(screen.getByRole("button", { name: "Previous" }));
226
+ expect(onPreviousPage).toHaveBeenCalledWith("start");
227
+ });
228
+
229
+ it("renders the empty state when there are no rows", () => {
230
+ render(<CphSimpleTable aria-label="Inquiries" columns={columns} rows={[]} />);
231
+
232
+ expect(screen.getByRole("table")).toBeInTheDocument();
233
+ expect(screen.getByText("No results")).toBeInTheDocument();
234
+ });
235
+ });