@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,221 @@
|
|
|
1
|
+
/** @packageDocumentation Individual header cell with sorting, pinning, and selection. */
|
|
2
|
+
|
|
3
|
+
import type { ColumnPinningPosition } from "@tanstack/react-table";
|
|
4
|
+
import { useRef, useEffect, type ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
import { usePinnedOffset } from "../DataTable/pinnedOffsets";
|
|
7
|
+
|
|
8
|
+
/** Minimal header interface for HeaderCell — only the properties/methods we actually use. */
|
|
9
|
+
export interface HeaderCellHeader {
|
|
10
|
+
id: string;
|
|
11
|
+
isPlaceholder: boolean;
|
|
12
|
+
placeholderId?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Minimal column interface for HeaderCell — only the methods we actually call. */
|
|
16
|
+
export interface HeaderCellColumn {
|
|
17
|
+
getCanSort: () => boolean;
|
|
18
|
+
getIsSorted: () => boolean | "asc" | "desc";
|
|
19
|
+
getSortIndex: () => number;
|
|
20
|
+
getIsPinned: () => ColumnPinningPosition;
|
|
21
|
+
getCanPin: () => boolean;
|
|
22
|
+
getIsLastColumn: (position?: ColumnPinningPosition | "center") => boolean;
|
|
23
|
+
toggleSorting: () => void;
|
|
24
|
+
pin: (position: ColumnPinningPosition) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface HeaderCellProps {
|
|
28
|
+
/** The TanStack header instance (minimal shape). */
|
|
29
|
+
header: HeaderCellHeader;
|
|
30
|
+
/** The TanStack column instance (minimal shape). */
|
|
31
|
+
column: HeaderCellColumn;
|
|
32
|
+
/** Whether this column can be sorted. */
|
|
33
|
+
canSort: boolean;
|
|
34
|
+
/** Current sort direction: "asc", "desc", or undefined for unsorted. */
|
|
35
|
+
sortDirection: "asc" | "desc" | undefined;
|
|
36
|
+
/** Pin state: "start", "end", or undefined. */
|
|
37
|
+
pinned: "start" | "end" | false | undefined;
|
|
38
|
+
/** Whether this column can be pinned. */
|
|
39
|
+
canPin: boolean;
|
|
40
|
+
/** Whether this is the last start-pinned column. */
|
|
41
|
+
isLastPinned: boolean;
|
|
42
|
+
/** Optional select-all checkbox state. */
|
|
43
|
+
selectAll?: boolean;
|
|
44
|
+
/** Optional indeterminate state for select-all. */
|
|
45
|
+
selectAllIndeterminate?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Called when the select-all checkbox is toggled. When omitted, the
|
|
48
|
+
* checkbox renders read-only as a status indicator.
|
|
49
|
+
*/
|
|
50
|
+
onSelectAllChange?: (selected: boolean) => void;
|
|
51
|
+
/** Column alignment; "right" right-aligns (numeric) the header. */
|
|
52
|
+
align?: "left" | "right";
|
|
53
|
+
/** Header label/content. */
|
|
54
|
+
children: ReactNode;
|
|
55
|
+
/** Additional className (string or React Aria render-props callback). */
|
|
56
|
+
className?: string | ((values: { defaultClassName: string | undefined }) => string);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Sort arrow glyph only — no words like "ascending" in the arrow.
|
|
61
|
+
*/
|
|
62
|
+
function SortArrow({ direction }: { direction: "asc" | "desc" | undefined }) {
|
|
63
|
+
if (direction === "asc")
|
|
64
|
+
return (
|
|
65
|
+
<span className="cph-table__sort-arrow" aria-hidden="true">
|
|
66
|
+
▲
|
|
67
|
+
</span>
|
|
68
|
+
);
|
|
69
|
+
if (direction === "desc")
|
|
70
|
+
return (
|
|
71
|
+
<span className="cph-table__sort-arrow" aria-hidden="true">
|
|
72
|
+
▼
|
|
73
|
+
</span>
|
|
74
|
+
);
|
|
75
|
+
return <span className="cph-table__sort-arrow" aria-hidden="true" />;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Pin toggle button — calls column.pin('start') / column.pin(false).
|
|
80
|
+
*/
|
|
81
|
+
function PinToggle({
|
|
82
|
+
column,
|
|
83
|
+
pinned,
|
|
84
|
+
canPin,
|
|
85
|
+
}: {
|
|
86
|
+
column: HeaderCellColumn;
|
|
87
|
+
pinned: "start" | "end" | false | undefined;
|
|
88
|
+
canPin: boolean;
|
|
89
|
+
}) {
|
|
90
|
+
if (!canPin) return null;
|
|
91
|
+
|
|
92
|
+
const isPinned = pinned === "start";
|
|
93
|
+
return (
|
|
94
|
+
<button
|
|
95
|
+
type="button"
|
|
96
|
+
className="cph-table__pin-toggle"
|
|
97
|
+
aria-label={isPinned ? "Unpin column" : "Pin column"}
|
|
98
|
+
aria-pressed={isPinned}
|
|
99
|
+
onClick={() => column.pin(isPinned ? false : "start")}
|
|
100
|
+
>
|
|
101
|
+
{isPinned ? "📌" : "📍"}
|
|
102
|
+
</button>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function HeaderCell({
|
|
107
|
+
header,
|
|
108
|
+
column,
|
|
109
|
+
canSort,
|
|
110
|
+
sortDirection,
|
|
111
|
+
pinned,
|
|
112
|
+
canPin,
|
|
113
|
+
selectAll,
|
|
114
|
+
selectAllIndeterminate,
|
|
115
|
+
onSelectAllChange,
|
|
116
|
+
align,
|
|
117
|
+
children,
|
|
118
|
+
className,
|
|
119
|
+
}: Readonly<HeaderCellProps>) {
|
|
120
|
+
const checkboxRef = useRef<HTMLInputElement>(null);
|
|
121
|
+
const offset = usePinnedOffset(header.id);
|
|
122
|
+
|
|
123
|
+
// Handle indeterminate via ref callback (React has no indeterminate prop)
|
|
124
|
+
useEffect(() => {
|
|
125
|
+
if (checkboxRef.current && selectAllIndeterminate !== undefined) {
|
|
126
|
+
checkboxRef.current.indeterminate = selectAllIndeterminate;
|
|
127
|
+
}
|
|
128
|
+
}, [selectAllIndeterminate]);
|
|
129
|
+
|
|
130
|
+
const isPinnedStart = pinned === "start";
|
|
131
|
+
const isPinnedEnd = pinned === "end";
|
|
132
|
+
const pinnedStyle =
|
|
133
|
+
offset === undefined
|
|
134
|
+
? undefined
|
|
135
|
+
: isPinnedStart
|
|
136
|
+
? { left: `${offset.left}px` }
|
|
137
|
+
: isPinnedEnd
|
|
138
|
+
? { left: "auto", right: `${offset.right ?? 0}px` }
|
|
139
|
+
: undefined;
|
|
140
|
+
|
|
141
|
+
// Build className: base + pinned classes + align + custom className
|
|
142
|
+
const baseClasses = ["cph-table__header-cell"];
|
|
143
|
+
if (isPinnedStart) {
|
|
144
|
+
baseClasses.push("cph-table__pinned", "cph-table__pinned-head");
|
|
145
|
+
}
|
|
146
|
+
if (align === "right") {
|
|
147
|
+
// Mirrors Cell: right alignment implies numeric (right + tabular) styling.
|
|
148
|
+
baseClasses.push("cph-table__numeric");
|
|
149
|
+
}
|
|
150
|
+
const baseClass = baseClasses.join(" ");
|
|
151
|
+
|
|
152
|
+
// For native elements, we need a string className.
|
|
153
|
+
// If className is a function (render-props), we can't use it directly on native elements.
|
|
154
|
+
// We'll call it with minimal render props if it's a function.
|
|
155
|
+
let composedClassName: string;
|
|
156
|
+
if (typeof className === "function") {
|
|
157
|
+
composedClassName = `${baseClass} ${className({ defaultClassName: undefined })}`;
|
|
158
|
+
} else {
|
|
159
|
+
composedClassName = className ? `${baseClass} ${className}` : baseClass;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// aria-sort: ONLY when sorted — "ascending" / "descending".
|
|
163
|
+
// When unsorted the attribute must be ABSENT from the DOM, never "none".
|
|
164
|
+
const ariaSort =
|
|
165
|
+
sortDirection === "asc" ? "ascending" : sortDirection === "desc" ? "descending" : undefined;
|
|
166
|
+
|
|
167
|
+
// For placeholder headers, render empty th
|
|
168
|
+
if (header.isPlaceholder) {
|
|
169
|
+
return (
|
|
170
|
+
<th
|
|
171
|
+
key={header.placeholderId ?? header.id}
|
|
172
|
+
className={composedClassName}
|
|
173
|
+
data-cph-table="placeholder"
|
|
174
|
+
/>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<th
|
|
180
|
+
className={composedClassName}
|
|
181
|
+
style={pinnedStyle}
|
|
182
|
+
data-cph-table="header-cell"
|
|
183
|
+
data-sort={sortDirection}
|
|
184
|
+
data-pinned={pinned}
|
|
185
|
+
aria-sort={ariaSort}
|
|
186
|
+
>
|
|
187
|
+
<div className="cph-table__header-cell-content">
|
|
188
|
+
{selectAll !== undefined && (
|
|
189
|
+
<input
|
|
190
|
+
ref={checkboxRef}
|
|
191
|
+
type="checkbox"
|
|
192
|
+
className="cph-table__checkbox"
|
|
193
|
+
checked={selectAll}
|
|
194
|
+
aria-label="Select all rows"
|
|
195
|
+
{...(onSelectAllChange
|
|
196
|
+
? {
|
|
197
|
+
onChange: (event) => onSelectAllChange(event.target.checked),
|
|
198
|
+
}
|
|
199
|
+
: { readOnly: true })}
|
|
200
|
+
/>
|
|
201
|
+
)}
|
|
202
|
+
|
|
203
|
+
{canSort ? (
|
|
204
|
+
<button
|
|
205
|
+
type="button"
|
|
206
|
+
className="cph-table__sort"
|
|
207
|
+
onClick={() => column.toggleSorting()}
|
|
208
|
+
aria-label={`Sort ${sortDirection === "asc" ? "descending" : "ascending"}`}
|
|
209
|
+
>
|
|
210
|
+
<span className="cph-table__sort-label">{children}</span>
|
|
211
|
+
<SortArrow direction={sortDirection} />
|
|
212
|
+
</button>
|
|
213
|
+
) : (
|
|
214
|
+
<span className="cph-table__sort-label">{children}</span>
|
|
215
|
+
)}
|
|
216
|
+
|
|
217
|
+
<PinToggle column={column} pinned={pinned} canPin={canPin} />
|
|
218
|
+
</div>
|
|
219
|
+
</th>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import type { RowData, Table } from "@tanstack/react-table";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import type { CphTableFeatures } from "../../table/features";
|
|
6
|
+
import { CphDataTable } from "../DataTable/CphDataTable";
|
|
7
|
+
import { PinnedOffsetsContext } from "../DataTable/pinnedOffsets";
|
|
8
|
+
import { Cell } from "./Cell";
|
|
9
|
+
|
|
10
|
+
function pinHeader(id: string, size: number, isPinned: "start" | "end" | false, isLast: boolean) {
|
|
11
|
+
return {
|
|
12
|
+
id,
|
|
13
|
+
getSize: vi.fn(() => size),
|
|
14
|
+
column: {
|
|
15
|
+
getIsPinned: vi.fn(() => isPinned),
|
|
16
|
+
getIsLastColumn: vi.fn(() => isLast),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const sparseEndOffsets = { offsets: new Map([["z", { left: 0, isLast: true }]]) };
|
|
22
|
+
|
|
23
|
+
function pinTable() {
|
|
24
|
+
return {
|
|
25
|
+
getStartLeafHeaders: vi.fn(() => [
|
|
26
|
+
pinHeader("a", 100, "start", false),
|
|
27
|
+
pinHeader("b", 120, "start", true),
|
|
28
|
+
]),
|
|
29
|
+
getEndLeafHeaders: vi.fn(() => [pinHeader("z", 90, "end", true)]),
|
|
30
|
+
getStartVisibleLeafColumns: vi.fn(() => []),
|
|
31
|
+
getCenterVisibleLeafColumns: vi.fn(() => []),
|
|
32
|
+
getEndVisibleLeafColumns: vi.fn(() => []),
|
|
33
|
+
} as unknown as Table<CphTableFeatures, RowData>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function renderCell(columnId: string | undefined, pinned: "start" | "end" | undefined) {
|
|
37
|
+
render(
|
|
38
|
+
<CphDataTable table={pinTable()}>
|
|
39
|
+
<tbody>
|
|
40
|
+
<tr>
|
|
41
|
+
<Cell
|
|
42
|
+
{...(columnId === undefined ? {} : { columnId })}
|
|
43
|
+
{...(pinned === undefined ? {} : { pinned })}
|
|
44
|
+
>
|
|
45
|
+
Pinned
|
|
46
|
+
</Cell>
|
|
47
|
+
</tr>
|
|
48
|
+
</tbody>
|
|
49
|
+
</CphDataTable>,
|
|
50
|
+
);
|
|
51
|
+
return screen.getByText("Pinned").closest("td");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
describe("Cell", () => {
|
|
55
|
+
it("renders a <td> with the cph-table cell data attribute", () => {
|
|
56
|
+
render(<Cell>Content</Cell>);
|
|
57
|
+
const cell = screen.getByRole("cell");
|
|
58
|
+
expect(cell).toHaveAttribute("data-cph-table", "cell");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("renders children as cell content", () => {
|
|
62
|
+
render(<Cell>Hello world</Cell>);
|
|
63
|
+
expect(screen.getByText("Hello world")).toBeInTheDocument();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("applies data-pinned='start' when pinned is start", () => {
|
|
67
|
+
render(<Cell pinned="start">Pinned</Cell>);
|
|
68
|
+
const cell = screen.getByRole("cell");
|
|
69
|
+
expect(cell).toHaveAttribute("data-pinned", "start");
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("applies data-pinned='end' when pinned is end", () => {
|
|
73
|
+
render(<Cell pinned="end">Pinned</Cell>);
|
|
74
|
+
const cell = screen.getByRole("cell");
|
|
75
|
+
expect(cell).toHaveAttribute("data-pinned", "end");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("omits data-pinned when pinned is undefined", () => {
|
|
79
|
+
render(<Cell>Not pinned</Cell>);
|
|
80
|
+
const cell = screen.getByRole("cell");
|
|
81
|
+
expect(cell).not.toHaveAttribute("data-pinned");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("applies data-selected='true' when selected is true", () => {
|
|
85
|
+
render(<Cell selected>Selected</Cell>);
|
|
86
|
+
const cell = screen.getByRole("cell");
|
|
87
|
+
expect(cell).toHaveAttribute("data-selected", "true");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("omits data-selected when selected is false or undefined", () => {
|
|
91
|
+
render(<Cell selected={false}>Not selected</Cell>);
|
|
92
|
+
const cell = screen.getByRole("cell");
|
|
93
|
+
expect(cell).not.toHaveAttribute("data-selected");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("applies data-col-index when colIndex is provided", () => {
|
|
97
|
+
render(<Cell colIndex={2}>Indexed</Cell>);
|
|
98
|
+
const cell = screen.getByRole("cell");
|
|
99
|
+
expect(cell).toHaveAttribute("data-col-index", "2");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("omits data-col-index when colIndex is undefined", () => {
|
|
103
|
+
render(<Cell>No index</Cell>);
|
|
104
|
+
const cell = screen.getByRole("cell");
|
|
105
|
+
expect(cell).not.toHaveAttribute("data-col-index");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("applies cph-table__pinned class when pinned is start", () => {
|
|
109
|
+
render(<Cell pinned="start">Pinned start</Cell>);
|
|
110
|
+
const cell = screen.getByRole("cell");
|
|
111
|
+
expect(cell).toHaveClass("cph-table__pinned");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("applies cph-table__pinned class when pinned is end", () => {
|
|
115
|
+
render(<Cell pinned="end">Pinned end</Cell>);
|
|
116
|
+
const cell = screen.getByRole("cell");
|
|
117
|
+
expect(cell).toHaveClass("cph-table__pinned");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("does not apply cph-table__pinned class when not pinned", () => {
|
|
121
|
+
render(<Cell>Not pinned</Cell>);
|
|
122
|
+
const cell = screen.getByRole("cell");
|
|
123
|
+
expect(cell).not.toHaveClass("cph-table__pinned");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("applies cph-table__numeric class when numeric is true", () => {
|
|
127
|
+
render(<Cell numeric>12345</Cell>);
|
|
128
|
+
const cell = screen.getByRole("cell");
|
|
129
|
+
expect(cell).toHaveClass("cph-table__numeric");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("applies cph-table__numeric class when align is right", () => {
|
|
133
|
+
render(<Cell align="right">12345</Cell>);
|
|
134
|
+
const cell = screen.getByRole("cell");
|
|
135
|
+
expect(cell).toHaveClass("cph-table__numeric");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("does not apply cph-table__numeric when numeric is false and align is left", () => {
|
|
139
|
+
render(
|
|
140
|
+
<Cell numeric={false} align="left">
|
|
141
|
+
Text
|
|
142
|
+
</Cell>,
|
|
143
|
+
);
|
|
144
|
+
const cell = screen.getByRole("cell");
|
|
145
|
+
expect(cell).not.toHaveClass("cph-table__numeric");
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("applies cph-table__truncate class when truncate is true", () => {
|
|
149
|
+
render(<Cell truncate>Very long text that should truncate</Cell>);
|
|
150
|
+
const cell = screen.getByRole("cell");
|
|
151
|
+
expect(cell).toHaveClass("cph-table__truncate");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("does not apply cph-table__truncate when truncate is false", () => {
|
|
155
|
+
render(<Cell truncate={false}>Short</Cell>);
|
|
156
|
+
const cell = screen.getByRole("cell");
|
|
157
|
+
expect(cell).not.toHaveClass("cph-table__truncate");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("forwards extra className without losing component classes", () => {
|
|
161
|
+
render(
|
|
162
|
+
<Cell className="cph-test-custom-class" numeric>
|
|
163
|
+
Content
|
|
164
|
+
</Cell>,
|
|
165
|
+
);
|
|
166
|
+
const cell = screen.getByRole("cell");
|
|
167
|
+
expect(cell).toHaveClass("cph-table__numeric");
|
|
168
|
+
expect(cell).toHaveClass("cph-test-custom-class");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("forwards arbitrary props (e.g., data-testid) to the <td>", () => {
|
|
172
|
+
render(<Cell data-testid="my-cell">Content</Cell>);
|
|
173
|
+
const cell = screen.getByRole("cell");
|
|
174
|
+
expect(cell).toHaveAttribute("data-testid", "my-cell");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("combines pinned, numeric, and truncate classes together", () => {
|
|
178
|
+
render(
|
|
179
|
+
<Cell pinned="start" numeric truncate>
|
|
180
|
+
Pinned numeric truncated
|
|
181
|
+
</Cell>,
|
|
182
|
+
);
|
|
183
|
+
const cell = screen.getByRole("cell");
|
|
184
|
+
expect(cell).toHaveClass("cph-table__pinned");
|
|
185
|
+
expect(cell).toHaveClass("cph-table__numeric");
|
|
186
|
+
expect(cell).toHaveClass("cph-table__truncate");
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe("structural preconditions for row-background indirection", () => {
|
|
190
|
+
it("pinned cell has cph-table__pinned class AND sits inside a cph-table__row ancestor", () => {
|
|
191
|
+
// This test asserts the DOM structure that makes the CSS indirection work:
|
|
192
|
+
// .cph-table__row .cph-table__pinned { background: var(--cph-table-row-bg); }
|
|
193
|
+
// The pinned cell MUST be a descendant of an element with cph-table__row class.
|
|
194
|
+
// jsdom does not compute styles, so we assert the structural precondition.
|
|
195
|
+
// Visual verification of resolved background colour is done in Playwright.
|
|
196
|
+
const { container } = render(
|
|
197
|
+
<table>
|
|
198
|
+
<tbody>
|
|
199
|
+
<tr className="cph-table__row" data-cph-table="row">
|
|
200
|
+
<Cell pinned="start">Pinned cell</Cell>
|
|
201
|
+
<Cell>Regular cell</Cell>
|
|
202
|
+
</tr>
|
|
203
|
+
</tbody>
|
|
204
|
+
</table>,
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const pinnedCell = container.querySelector('[data-cph-table="cell"][data-pinned="start"]');
|
|
208
|
+
expect(pinnedCell).not.toBeNull();
|
|
209
|
+
expect(pinnedCell).toHaveClass("cph-table__pinned");
|
|
210
|
+
|
|
211
|
+
const rowAncestor = pinnedCell?.closest(".cph-table__row");
|
|
212
|
+
expect(rowAncestor).not.toBeNull();
|
|
213
|
+
expect(rowAncestor).toHaveClass("cph-table__row");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("selected row with pinned cell has data-selected on row AND cph-table__pinned on cell", () => {
|
|
217
|
+
// This structure enables the CSS rule:
|
|
218
|
+
// .cph-table__row[data-selected="true"] .cph-table__pinned { box-shadow: inset 3px 0 var(--cph-orange); }
|
|
219
|
+
// jsdom does not compute box-shadow; structural assertion only.
|
|
220
|
+
const { container } = render(
|
|
221
|
+
<table>
|
|
222
|
+
<tbody>
|
|
223
|
+
<tr className="cph-table__row" data-cph-table="row" data-selected="true">
|
|
224
|
+
<Cell pinned="start" selected>
|
|
225
|
+
Selected pinned
|
|
226
|
+
</Cell>
|
|
227
|
+
<Cell selected>Selected regular</Cell>
|
|
228
|
+
</tr>
|
|
229
|
+
</tbody>
|
|
230
|
+
</table>,
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const row = container.querySelector(".cph-table__row[data-selected='true']");
|
|
234
|
+
expect(row).not.toBeNull();
|
|
235
|
+
|
|
236
|
+
const pinnedCell = container.querySelector('[data-cph-table="cell"][data-pinned="start"]');
|
|
237
|
+
expect(pinnedCell).not.toBeNull();
|
|
238
|
+
expect(pinnedCell).toHaveClass("cph-table__pinned");
|
|
239
|
+
|
|
240
|
+
// The pinned cell must be a descendant of the selected row
|
|
241
|
+
expect(row?.contains(pinnedCell)).toBe(true);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("hovered row structure enables --cph-table-row-bg override for pinned cell", () => {
|
|
245
|
+
// CSS: .cph-table__row:hover { --cph-table-row-bg: var(--cph-table-hover); }
|
|
246
|
+
// .cph-table__row .cph-table__pinned { background: var(--cph-table-row-bg); }
|
|
247
|
+
// The pinned cell inherits the row's custom property via the descendant selector.
|
|
248
|
+
// jsdom does not evaluate :hover or custom properties; structural assertion only.
|
|
249
|
+
const { container } = render(
|
|
250
|
+
<table>
|
|
251
|
+
<tbody>
|
|
252
|
+
<tr className="cph-table__row" data-cph-table="row">
|
|
253
|
+
<Cell pinned="start">Pinned on hover</Cell>
|
|
254
|
+
</tr>
|
|
255
|
+
</tbody>
|
|
256
|
+
</table>,
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
const row = container.querySelector(".cph-table__row");
|
|
260
|
+
expect(row).not.toBeNull();
|
|
261
|
+
|
|
262
|
+
const pinnedCell = container.querySelector('[data-cph-table="cell"][data-pinned="start"]');
|
|
263
|
+
expect(pinnedCell).not.toBeNull();
|
|
264
|
+
expect(pinnedCell).toHaveClass("cph-table__pinned");
|
|
265
|
+
|
|
266
|
+
// The pinned cell is a descendant of .cph-table__row, so it will read
|
|
267
|
+
// var(--cph-table-row-bg) which the row overrides on :hover
|
|
268
|
+
expect(row?.contains(pinnedCell)).toBe(true);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe("pinned offsets from table context", () => {
|
|
273
|
+
it("applies the accumulated left offset for start-pinned cells", () => {
|
|
274
|
+
expect(renderCell("b", "start")).toHaveStyle({ left: "100px" });
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("applies right offset with auto left for end-pinned cells", () => {
|
|
278
|
+
const cell = renderCell("z", "end");
|
|
279
|
+
expect(cell).toHaveStyle({ left: "auto", right: "0px" });
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it("renders no inline offset without a columnId", () => {
|
|
283
|
+
expect(renderCell(undefined, "start")).not.toHaveAttribute("style");
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("falls back to zero right offset for sparse entries", () => {
|
|
287
|
+
render(
|
|
288
|
+
<PinnedOffsetsContext.Provider value={sparseEndOffsets}>
|
|
289
|
+
<table>
|
|
290
|
+
<tbody>
|
|
291
|
+
<tr>
|
|
292
|
+
<Cell columnId="z" pinned="end">
|
|
293
|
+
Pinned
|
|
294
|
+
</Cell>
|
|
295
|
+
</tr>
|
|
296
|
+
</tbody>
|
|
297
|
+
</table>
|
|
298
|
+
</PinnedOffsetsContext.Provider>,
|
|
299
|
+
);
|
|
300
|
+
expect(screen.getByText("Pinned").closest("td")).toHaveStyle({
|
|
301
|
+
left: "auto",
|
|
302
|
+
right: "0px",
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** @packageDocumentation Individual table cell. */
|
|
2
|
+
|
|
3
|
+
import type { CSSProperties, HTMLAttributes, ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
import { usePinnedOffset } from "../DataTable/pinnedOffsets";
|
|
6
|
+
|
|
7
|
+
export interface CellProps extends HTMLAttributes<HTMLTableCellElement> {
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
/** Is this cell pinned? */
|
|
10
|
+
pinned?: "start" | "end";
|
|
11
|
+
/**
|
|
12
|
+
* Column id used to look up the accumulated sticky offset from the
|
|
13
|
+
* table's pinned-offsets context. Without it, pinned cells fall back to
|
|
14
|
+
* the CSS `left: 0` and stacked pinned columns overlap.
|
|
15
|
+
*/
|
|
16
|
+
columnId?: string;
|
|
17
|
+
/** Is this cell selected? */
|
|
18
|
+
selected?: boolean;
|
|
19
|
+
/** Column index for colgroup reference. */
|
|
20
|
+
colIndex?: number;
|
|
21
|
+
/** Apply numeric styling (tabular-nums, right-align). */
|
|
22
|
+
numeric?: boolean;
|
|
23
|
+
/** Apply truncation styling (ellipsis overflow). */
|
|
24
|
+
truncate?: boolean;
|
|
25
|
+
/** Text alignment; "right" implies numeric styling. */
|
|
26
|
+
align?: "left" | "right";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function Cell({
|
|
30
|
+
children,
|
|
31
|
+
pinned,
|
|
32
|
+
selected,
|
|
33
|
+
colIndex,
|
|
34
|
+
numeric,
|
|
35
|
+
truncate,
|
|
36
|
+
align,
|
|
37
|
+
className,
|
|
38
|
+
columnId,
|
|
39
|
+
style,
|
|
40
|
+
...props
|
|
41
|
+
}: Readonly<CellProps>) {
|
|
42
|
+
const isNumeric = numeric ?? align === "right";
|
|
43
|
+
const offset = usePinnedOffset(columnId ?? "");
|
|
44
|
+
const pinnedStyle =
|
|
45
|
+
pinned !== undefined && columnId !== undefined && offset !== undefined
|
|
46
|
+
? pinned === "start"
|
|
47
|
+
? { left: `${offset.left}px` }
|
|
48
|
+
: { left: "auto", right: `${offset.right ?? 0}px` }
|
|
49
|
+
: undefined;
|
|
50
|
+
const classes = [
|
|
51
|
+
className,
|
|
52
|
+
isNumeric && "cph-table__numeric",
|
|
53
|
+
truncate && "cph-table__truncate",
|
|
54
|
+
pinned && "cph-table__pinned",
|
|
55
|
+
]
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.join(" ");
|
|
58
|
+
const mergedStyle: CSSProperties | undefined =
|
|
59
|
+
pinnedStyle !== undefined || style !== undefined ? { ...style, ...pinnedStyle } : undefined;
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<td
|
|
63
|
+
{...props}
|
|
64
|
+
className={classes}
|
|
65
|
+
{...(mergedStyle !== undefined ? { style: mergedStyle } : {})}
|
|
66
|
+
data-cph-table="cell"
|
|
67
|
+
data-pinned={pinned}
|
|
68
|
+
data-selected={selected ? "true" : undefined}
|
|
69
|
+
data-col-index={colIndex?.toString()}
|
|
70
|
+
>
|
|
71
|
+
{children}
|
|
72
|
+
</td>
|
|
73
|
+
);
|
|
74
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { CphTableRow } from "./CphTableRow";
|
|
5
|
+
|
|
6
|
+
describe("CphTableRow", () => {
|
|
7
|
+
it("renders a <tr> with the cph-table row data attribute", () => {
|
|
8
|
+
render(
|
|
9
|
+
<CphTableRow>
|
|
10
|
+
<td>Cell</td>
|
|
11
|
+
</CphTableRow>,
|
|
12
|
+
);
|
|
13
|
+
const row = screen.getByRole("row");
|
|
14
|
+
expect(row).toHaveAttribute("data-cph-table", "row");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("forwards children as cell content", () => {
|
|
18
|
+
render(
|
|
19
|
+
<CphTableRow>
|
|
20
|
+
<td>First</td>
|
|
21
|
+
<td>Second</td>
|
|
22
|
+
</CphTableRow>,
|
|
23
|
+
);
|
|
24
|
+
expect(screen.getByText("First")).toBeInTheDocument();
|
|
25
|
+
expect(screen.getByText("Second")).toBeInTheDocument();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("applies data-state='selected' when state is selected", () => {
|
|
29
|
+
render(
|
|
30
|
+
<CphTableRow state="selected">
|
|
31
|
+
<td>Cell</td>
|
|
32
|
+
</CphTableRow>,
|
|
33
|
+
);
|
|
34
|
+
const row = screen.getByRole("row");
|
|
35
|
+
expect(row).toHaveAttribute("data-state", "selected");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("applies data-state='hovered' when state is hovered", () => {
|
|
39
|
+
render(
|
|
40
|
+
<CphTableRow state="hovered">
|
|
41
|
+
<td>Cell</td>
|
|
42
|
+
</CphTableRow>,
|
|
43
|
+
);
|
|
44
|
+
const row = screen.getByRole("row");
|
|
45
|
+
expect(row).toHaveAttribute("data-state", "hovered");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("omits data-state when state is undefined", () => {
|
|
49
|
+
render(
|
|
50
|
+
<CphTableRow>
|
|
51
|
+
<td>Cell</td>
|
|
52
|
+
</CphTableRow>,
|
|
53
|
+
);
|
|
54
|
+
const row = screen.getByRole("row");
|
|
55
|
+
expect(row).not.toHaveAttribute("data-state");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("forwards extra className without losing the row attribute", () => {
|
|
59
|
+
render(
|
|
60
|
+
<CphTableRow className="cph-test-custom-class">
|
|
61
|
+
<td>Cell</td>
|
|
62
|
+
</CphTableRow>,
|
|
63
|
+
);
|
|
64
|
+
const row = screen.getByRole("row");
|
|
65
|
+
expect(row).toHaveAttribute("data-cph-table", "row");
|
|
66
|
+
expect(row).toHaveClass("cph-test-custom-class");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("forwards arbitrary props (e.g., data-testid) to the <tr>", () => {
|
|
70
|
+
render(
|
|
71
|
+
<CphTableRow data-testid="my-row">
|
|
72
|
+
<td>Cell</td>
|
|
73
|
+
</CphTableRow>,
|
|
74
|
+
);
|
|
75
|
+
const row = screen.getByRole("row");
|
|
76
|
+
expect(row).toHaveAttribute("data-testid", "my-row");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("forwards onClick handler to the <tr>", () => {
|
|
80
|
+
const handleClick = vi.fn();
|
|
81
|
+
render(
|
|
82
|
+
<CphTableRow onClick={handleClick}>
|
|
83
|
+
<td>Cell</td>
|
|
84
|
+
</CphTableRow>,
|
|
85
|
+
);
|
|
86
|
+
const row = screen.getByRole("row");
|
|
87
|
+
row.click();
|
|
88
|
+
expect(handleClick).toHaveBeenCalledOnce();
|
|
89
|
+
});
|
|
90
|
+
});
|