@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,369 @@
|
|
|
1
|
+
/** @packageDocumentation Simple generic sortable/selectable data table. */
|
|
2
|
+
|
|
3
|
+
import type { ColumnDef, RowSelectionState, SortingState, TableState } from "@tanstack/react-table";
|
|
4
|
+
import type { ReactNode } from "react";
|
|
5
|
+
import type { Selection, SortDescriptor } from "react-aria-components";
|
|
6
|
+
|
|
7
|
+
import { cphColumnHelper, type CphColumnMeta } from "../../table/column";
|
|
8
|
+
import type { CphTableFeatures } from "../../table/features";
|
|
9
|
+
import { useCphTable } from "../../table/useCphTable";
|
|
10
|
+
import type { StateSlice } from "../../table/useCphTableState";
|
|
11
|
+
import { CphTableEmpty } from "../CphTableEmpty";
|
|
12
|
+
import { CphDataTable } from "../DataTable/CphDataTable";
|
|
13
|
+
import {
|
|
14
|
+
CphCursorPagination,
|
|
15
|
+
type CphCursorPaginationProps,
|
|
16
|
+
} from "../Pagination/CphCursorPagination";
|
|
17
|
+
import { CphTableHead, type CphTableHeadSelectAll } from "../TableHead/CphTableHead";
|
|
18
|
+
import { Cell } from "../TableRow/Cell";
|
|
19
|
+
import { CphTableRow } from "../TableRow/CphTableRow";
|
|
20
|
+
|
|
21
|
+
/** Stable column id for the internal selection column. */
|
|
22
|
+
const SELECT_COLUMN_ID = "__selection__";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Column definition for {@link CphSimpleTable}.
|
|
26
|
+
*
|
|
27
|
+
* Presentation-only: the sort key comes from `sortValue` (falling back to
|
|
28
|
+
* `accessor`), and display values come from `accessor`.
|
|
29
|
+
*/
|
|
30
|
+
export interface CphSimpleTableColumn<T> {
|
|
31
|
+
/** Stable column id, also used as the sort key. */
|
|
32
|
+
id: string;
|
|
33
|
+
/** Header content. */
|
|
34
|
+
header: ReactNode;
|
|
35
|
+
/** Cell display value. */
|
|
36
|
+
accessor?: (row: T) => ReactNode;
|
|
37
|
+
/**
|
|
38
|
+
* Sort key; falls back to `accessor`. Non-scalar display values should
|
|
39
|
+
* provide this (e.g. a primitive extracted from the displayed value).
|
|
40
|
+
*/
|
|
41
|
+
sortValue?: (row: T) => string | number | boolean | null | undefined;
|
|
42
|
+
/** Whether this column can be sorted. Defaults to true when sortValue/accessor exists. */
|
|
43
|
+
allowsSorting?: boolean;
|
|
44
|
+
/** Text alignment for header and cells. */
|
|
45
|
+
align?: "left" | "right";
|
|
46
|
+
/** Escape hatch for column presentation (e.g. widthClass) not modeled here. */
|
|
47
|
+
meta?: CphColumnMeta;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface CphSimpleTableProps<T extends { id: string | number }> {
|
|
51
|
+
/** Accessible name for the table. */
|
|
52
|
+
"aria-label"?: string;
|
|
53
|
+
/** Column definitions. */
|
|
54
|
+
columns: CphSimpleTableColumn<T>[];
|
|
55
|
+
/** Row data. Each row must have a stable id. */
|
|
56
|
+
rows: T[];
|
|
57
|
+
/** Selection mode. Omit or "none" for a non-selectable table. */
|
|
58
|
+
selectionMode?: "none" | "single" | "multiple";
|
|
59
|
+
/** Controlled selected keys. */
|
|
60
|
+
selectedKeys?: Selection;
|
|
61
|
+
/** Controlled selection change handler. */
|
|
62
|
+
onSelectionChange?: (keys: Selection) => void;
|
|
63
|
+
/** Uncontrolled initial selected keys. */
|
|
64
|
+
defaultSelectedKeys?: Selection;
|
|
65
|
+
/** Controlled sort descriptor. */
|
|
66
|
+
sortDescriptor?: SortDescriptor;
|
|
67
|
+
/** Controlled sort change handler. */
|
|
68
|
+
onSortChange?: (descriptor: SortDescriptor) => void;
|
|
69
|
+
/** Uncontrolled initial sort descriptor. */
|
|
70
|
+
defaultSortDescriptor?: SortDescriptor;
|
|
71
|
+
/** Cursor pagination controls. */
|
|
72
|
+
pagination?: CphCursorPaginationProps;
|
|
73
|
+
/** Optional class name for the outer wrap. */
|
|
74
|
+
className?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function selectionToState(
|
|
78
|
+
selection: Selection | undefined,
|
|
79
|
+
rows: { id: string | number }[],
|
|
80
|
+
): RowSelectionState {
|
|
81
|
+
const state: RowSelectionState = {};
|
|
82
|
+
if (selection === undefined) {
|
|
83
|
+
return state;
|
|
84
|
+
}
|
|
85
|
+
const ids = selection === "all" ? rows.map((row) => String(row.id)) : [...selection].map(String);
|
|
86
|
+
for (const id of ids) {
|
|
87
|
+
state[id] = true;
|
|
88
|
+
}
|
|
89
|
+
return state;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function selectionToKeys(state: RowSelectionState, rows: { id: string | number }[]): Selection {
|
|
93
|
+
const selected = new Set<string>();
|
|
94
|
+
for (const key of Object.keys(state)) {
|
|
95
|
+
if (state[key]) {
|
|
96
|
+
selected.add(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (rows.length > 0) {
|
|
100
|
+
const allSelected =
|
|
101
|
+
selected.size > 0 &&
|
|
102
|
+
[...new Set(rows.map((row) => String(row.id)))].every((id) => selected.has(id));
|
|
103
|
+
if (allSelected) {
|
|
104
|
+
return "all";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return selected;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function paginationSpreadProps(
|
|
111
|
+
pagination: CphCursorPaginationProps,
|
|
112
|
+
): Partial<CphCursorPaginationProps> {
|
|
113
|
+
const props: Partial<CphCursorPaginationProps> = {};
|
|
114
|
+
if (pagination.hasNextPage !== undefined) {
|
|
115
|
+
props.hasNextPage = pagination.hasNextPage;
|
|
116
|
+
}
|
|
117
|
+
if (pagination.hasPreviousPage !== undefined) {
|
|
118
|
+
props.hasPreviousPage = pagination.hasPreviousPage;
|
|
119
|
+
}
|
|
120
|
+
if (pagination.startCursor !== undefined) {
|
|
121
|
+
props.startCursor = pagination.startCursor;
|
|
122
|
+
}
|
|
123
|
+
if (pagination.endCursor !== undefined) {
|
|
124
|
+
props.endCursor = pagination.endCursor;
|
|
125
|
+
}
|
|
126
|
+
if (pagination.isLoading !== undefined) {
|
|
127
|
+
props.isLoading = pagination.isLoading;
|
|
128
|
+
}
|
|
129
|
+
if (pagination.onNextPage !== undefined) {
|
|
130
|
+
props.onNextPage = pagination.onNextPage;
|
|
131
|
+
}
|
|
132
|
+
if (pagination.onPreviousPage !== undefined) {
|
|
133
|
+
props.onPreviousPage = pagination.onPreviousPage;
|
|
134
|
+
}
|
|
135
|
+
return props;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Generic simple data table: consumer-supplied columns/rows with TanStack
|
|
140
|
+
* sorting, checkbox selection, and cursor pagination — rendered through the
|
|
141
|
+
* package's existing plain-HTML table primitives.
|
|
142
|
+
*
|
|
143
|
+
* Sorting follows a two-state ascending/descending cycle (unsorted → ascending
|
|
144
|
+
* → descending → …) and is never removable once a column has been sorted.
|
|
145
|
+
*/
|
|
146
|
+
export function CphSimpleTable<T extends { id: string | number }>({
|
|
147
|
+
"aria-label": ariaLabel,
|
|
148
|
+
columns,
|
|
149
|
+
rows,
|
|
150
|
+
selectionMode = "none",
|
|
151
|
+
selectedKeys,
|
|
152
|
+
onSelectionChange,
|
|
153
|
+
defaultSelectedKeys,
|
|
154
|
+
sortDescriptor,
|
|
155
|
+
onSortChange,
|
|
156
|
+
defaultSortDescriptor,
|
|
157
|
+
pagination,
|
|
158
|
+
className,
|
|
159
|
+
}: Readonly<CphSimpleTableProps<T>>) {
|
|
160
|
+
const isSelectionEnabled = selectionMode !== "none";
|
|
161
|
+
const isSortControlled = sortDescriptor !== undefined || onSortChange !== undefined;
|
|
162
|
+
const isSelectionControlled = selectedKeys !== undefined || onSelectionChange !== undefined;
|
|
163
|
+
|
|
164
|
+
const tanstackColumns = () => {
|
|
165
|
+
const helper = cphColumnHelper<T>();
|
|
166
|
+
const built: ColumnDef<CphTableFeatures, T, unknown>[] = [];
|
|
167
|
+
|
|
168
|
+
if (isSelectionEnabled) {
|
|
169
|
+
built.push(
|
|
170
|
+
helper.display({
|
|
171
|
+
id: SELECT_COLUMN_ID,
|
|
172
|
+
header: "",
|
|
173
|
+
meta: { widthClass: "select" },
|
|
174
|
+
}),
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const column of columns) {
|
|
179
|
+
const sortable = column.allowsSorting ?? Boolean(column.sortValue ?? column.accessor);
|
|
180
|
+
const meta: CphColumnMeta = {
|
|
181
|
+
...(column.align ? { align: column.align } : undefined),
|
|
182
|
+
...(column.meta ?? undefined),
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
if (!sortable) {
|
|
186
|
+
built.push(
|
|
187
|
+
helper.display({
|
|
188
|
+
id: column.id,
|
|
189
|
+
header: String(column.header ?? ""),
|
|
190
|
+
...(meta ? { meta } : {}),
|
|
191
|
+
}),
|
|
192
|
+
);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const accessorFn = (row: T): unknown =>
|
|
197
|
+
column.sortValue
|
|
198
|
+
? column.sortValue(row)
|
|
199
|
+
: column.accessor
|
|
200
|
+
? column.accessor(row)
|
|
201
|
+
: undefined;
|
|
202
|
+
|
|
203
|
+
built.push(
|
|
204
|
+
helper.accessor(accessorFn, {
|
|
205
|
+
id: column.id,
|
|
206
|
+
header: String(column.header ?? ""),
|
|
207
|
+
enableSorting: true,
|
|
208
|
+
...(meta ? { meta } : {}),
|
|
209
|
+
}),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return built;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const table = useCphTable<T, TableState<CphTableFeatures>>({
|
|
217
|
+
data: rows,
|
|
218
|
+
columns: tanstackColumns(),
|
|
219
|
+
getRowId: (row) => String(row.id),
|
|
220
|
+
enableSortingRemoval: false,
|
|
221
|
+
...(selectionMode === "single" ? { enableMultiRowSelection: false } : {}),
|
|
222
|
+
...(isSortControlled
|
|
223
|
+
? {
|
|
224
|
+
sorting: {
|
|
225
|
+
controlled: true,
|
|
226
|
+
value: sortDescriptor
|
|
227
|
+
? [
|
|
228
|
+
{
|
|
229
|
+
id: String(sortDescriptor.column),
|
|
230
|
+
desc: sortDescriptor.direction === "descending",
|
|
231
|
+
},
|
|
232
|
+
]
|
|
233
|
+
: [],
|
|
234
|
+
onChange: (updater) => {
|
|
235
|
+
const next = typeof updater === "function" ? updater(table.state.sorting) : updater;
|
|
236
|
+
const nextDescriptor: SortDescriptor | undefined = next[0]
|
|
237
|
+
? { column: next[0].id, direction: next[0].desc ? "descending" : "ascending" }
|
|
238
|
+
: undefined;
|
|
239
|
+
if (nextDescriptor) {
|
|
240
|
+
onSortChange?.(nextDescriptor);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
} satisfies StateSlice<SortingState>,
|
|
244
|
+
}
|
|
245
|
+
: {
|
|
246
|
+
sorting: {
|
|
247
|
+
controlled: false,
|
|
248
|
+
initialValue: defaultSortDescriptor
|
|
249
|
+
? [
|
|
250
|
+
{
|
|
251
|
+
id: String(defaultSortDescriptor.column),
|
|
252
|
+
desc: defaultSortDescriptor.direction === "descending",
|
|
253
|
+
},
|
|
254
|
+
]
|
|
255
|
+
: [],
|
|
256
|
+
} satisfies StateSlice<SortingState>,
|
|
257
|
+
}),
|
|
258
|
+
...(isSelectionControlled
|
|
259
|
+
? {
|
|
260
|
+
rowSelection: {
|
|
261
|
+
controlled: true,
|
|
262
|
+
value: selectionToState(selectedKeys, rows),
|
|
263
|
+
onChange: (updater) => {
|
|
264
|
+
const next =
|
|
265
|
+
typeof updater === "function" ? updater(table.state.rowSelection) : updater;
|
|
266
|
+
onSelectionChange?.(selectionToKeys(next, rows));
|
|
267
|
+
},
|
|
268
|
+
} satisfies StateSlice<RowSelectionState>,
|
|
269
|
+
}
|
|
270
|
+
: {
|
|
271
|
+
rowSelection: {
|
|
272
|
+
controlled: false,
|
|
273
|
+
initialValue: selectionToState(defaultSelectedKeys, rows),
|
|
274
|
+
} satisfies StateSlice<RowSelectionState>,
|
|
275
|
+
}),
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const renderedRows = table.getRowModel().rows;
|
|
279
|
+
const isEmpty = renderedRows.length === 0;
|
|
280
|
+
const allSelected = table.getIsAllRowsSelected();
|
|
281
|
+
const someSelected = table.getIsSomeRowsSelected();
|
|
282
|
+
|
|
283
|
+
const selectAll: CphTableHeadSelectAll | undefined = isSelectionEnabled
|
|
284
|
+
? {
|
|
285
|
+
columnId: SELECT_COLUMN_ID,
|
|
286
|
+
checked: allSelected,
|
|
287
|
+
indeterminate: someSelected && !allSelected,
|
|
288
|
+
onChange: (checked) => {
|
|
289
|
+
table.toggleAllRowsSelected(checked);
|
|
290
|
+
},
|
|
291
|
+
}
|
|
292
|
+
: undefined;
|
|
293
|
+
|
|
294
|
+
const content = isEmpty ? (
|
|
295
|
+
<CphTableEmpty
|
|
296
|
+
message="No results"
|
|
297
|
+
visibleColumnCount={columns.length + (isSelectionEnabled ? 1 : 0)}
|
|
298
|
+
/>
|
|
299
|
+
) : (
|
|
300
|
+
<tbody>
|
|
301
|
+
{renderedRows.map((row) => {
|
|
302
|
+
const rowId = row.id;
|
|
303
|
+
const isRowSelected = row.getIsSelected();
|
|
304
|
+
return (
|
|
305
|
+
<CphTableRow
|
|
306
|
+
key={rowId}
|
|
307
|
+
className="cph-table__row"
|
|
308
|
+
data-selected={isRowSelected ? "true" : undefined}
|
|
309
|
+
>
|
|
310
|
+
{isSelectionEnabled && (
|
|
311
|
+
<Cell className="cph-table__selection-cell">
|
|
312
|
+
<input
|
|
313
|
+
type="checkbox"
|
|
314
|
+
className="cph-table__checkbox"
|
|
315
|
+
aria-label={`Select row ${rowId}`}
|
|
316
|
+
checked={isRowSelected}
|
|
317
|
+
onChange={() => row.toggleSelected()}
|
|
318
|
+
/>
|
|
319
|
+
</Cell>
|
|
320
|
+
)}
|
|
321
|
+
{columns.map((column) => {
|
|
322
|
+
const value: ReactNode = column.accessor
|
|
323
|
+
? column.accessor(row.original)
|
|
324
|
+
: ((row.original as Record<string, unknown>)[column.id] as ReactNode);
|
|
325
|
+
return (
|
|
326
|
+
<Cell
|
|
327
|
+
key={column.id}
|
|
328
|
+
columnId={column.id}
|
|
329
|
+
{...(column.meta?.numeric === undefined ? {} : { numeric: column.meta.numeric })}
|
|
330
|
+
{...(column.align === undefined ? {} : { align: column.align })}
|
|
331
|
+
>
|
|
332
|
+
{value}
|
|
333
|
+
</Cell>
|
|
334
|
+
);
|
|
335
|
+
})}
|
|
336
|
+
</CphTableRow>
|
|
337
|
+
);
|
|
338
|
+
})}
|
|
339
|
+
</tbody>
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
const showPagination =
|
|
343
|
+
pagination !== undefined &&
|
|
344
|
+
(pagination.onNextPage !== undefined ||
|
|
345
|
+
pagination.onPreviousPage !== undefined ||
|
|
346
|
+
pagination.hasNextPage !== undefined ||
|
|
347
|
+
pagination.hasPreviousPage !== undefined ||
|
|
348
|
+
pagination.startCursor !== undefined ||
|
|
349
|
+
pagination.endCursor !== undefined);
|
|
350
|
+
|
|
351
|
+
const dataTableProps = {
|
|
352
|
+
table,
|
|
353
|
+
showSummary: false,
|
|
354
|
+
className: className ? `cph-table-simple ${className}` : "cph-table-simple",
|
|
355
|
+
...(ariaLabel !== undefined ? { tableLabel: ariaLabel } : {}),
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
return (
|
|
359
|
+
<>
|
|
360
|
+
<CphDataTable {...dataTableProps}>
|
|
361
|
+
<CphTableHead table={table} {...(selectAll === undefined ? {} : { selectAll })} />
|
|
362
|
+
{content}
|
|
363
|
+
</CphDataTable>
|
|
364
|
+
{showPagination && pagination ? (
|
|
365
|
+
<CphCursorPagination {...paginationSpreadProps(pagination)} />
|
|
366
|
+
) : null}
|
|
367
|
+
</>
|
|
368
|
+
);
|
|
369
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { CphSortFilterSheet } from "./CphSortFilterSheet";
|
|
6
|
+
|
|
7
|
+
describe("CphSortFilterSheet", () => {
|
|
8
|
+
const defaultProps = {
|
|
9
|
+
children: <div data-testid="sheet-content">Sheet content</div>,
|
|
10
|
+
trigger: <button data-testid="trigger">Open Sheet</button>,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
it("renders trigger element", () => {
|
|
14
|
+
render(<CphSortFilterSheet {...defaultProps} />);
|
|
15
|
+
expect(screen.getByTestId("trigger")).toBeInTheDocument();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("opens sheet when trigger is clicked", async () => {
|
|
19
|
+
const user = userEvent.setup();
|
|
20
|
+
render(<CphSortFilterSheet {...defaultProps} />);
|
|
21
|
+
await user.click(screen.getByTestId("trigger"));
|
|
22
|
+
expect(screen.getByTestId("sort-filter-sheet")).toBeInTheDocument();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("renders dialog semantics when open", async () => {
|
|
26
|
+
const user = userEvent.setup();
|
|
27
|
+
render(<CphSortFilterSheet {...defaultProps} />);
|
|
28
|
+
await user.click(screen.getByTestId("trigger"));
|
|
29
|
+
const sheet = screen.getByTestId("sort-filter-sheet");
|
|
30
|
+
// axe's aria-allowed-attr rejects aria-modal on plain divs; modal behavior
|
|
31
|
+
// (focus trap, Escape, sibling aria-hidden) comes from RAC's ModalOverlay.
|
|
32
|
+
expect(screen.getByTestId("sort-filter-sheet-modal")).not.toHaveAttribute("aria-modal");
|
|
33
|
+
expect(sheet).toHaveAttribute("role", "dialog");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("closes sheet when close button is clicked", async () => {
|
|
37
|
+
const user = userEvent.setup();
|
|
38
|
+
const onOpenChange = vi.fn();
|
|
39
|
+
render(<CphSortFilterSheet {...defaultProps} onOpenChange={onOpenChange} />);
|
|
40
|
+
await user.click(screen.getByTestId("trigger"));
|
|
41
|
+
await user.click(screen.getByRole("button", { name: "Close sort & filter" }));
|
|
42
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("closes sheet when Done button is clicked", async () => {
|
|
46
|
+
const user = userEvent.setup();
|
|
47
|
+
const onOpenChange = vi.fn();
|
|
48
|
+
render(<CphSortFilterSheet {...defaultProps} onOpenChange={onOpenChange} />);
|
|
49
|
+
await user.click(screen.getByTestId("trigger"));
|
|
50
|
+
await user.click(screen.getByRole("button", { name: "Done" }));
|
|
51
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("closes sheet on Escape key", async () => {
|
|
55
|
+
const user = userEvent.setup();
|
|
56
|
+
const onOpenChange = vi.fn();
|
|
57
|
+
render(<CphSortFilterSheet {...defaultProps} onOpenChange={onOpenChange} />);
|
|
58
|
+
await user.click(screen.getByTestId("trigger"));
|
|
59
|
+
await user.keyboard("[Escape]");
|
|
60
|
+
expect(onOpenChange).toHaveBeenCalledWith(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("renders title in heading", async () => {
|
|
64
|
+
const user = userEvent.setup();
|
|
65
|
+
render(<CphSortFilterSheet {...defaultProps} title="Custom Title" />);
|
|
66
|
+
await user.click(screen.getByTestId("trigger"));
|
|
67
|
+
expect(screen.getByRole("heading", { name: "Custom Title" })).toBeInTheDocument();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("renders description when provided", async () => {
|
|
71
|
+
const user = userEvent.setup();
|
|
72
|
+
render(
|
|
73
|
+
<CphSortFilterSheet {...defaultProps} description="Manage sorting and filtering options" />,
|
|
74
|
+
);
|
|
75
|
+
await user.click(screen.getByTestId("trigger"));
|
|
76
|
+
expect(screen.getByText("Manage sorting and filtering options")).toBeInTheDocument();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("renders children content", async () => {
|
|
80
|
+
const user = userEvent.setup();
|
|
81
|
+
render(<CphSortFilterSheet {...defaultProps} />);
|
|
82
|
+
await user.click(screen.getByTestId("trigger"));
|
|
83
|
+
expect(screen.getByTestId("sheet-content")).toBeInTheDocument();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("applies cph-table__sheet class", async () => {
|
|
87
|
+
const user = userEvent.setup();
|
|
88
|
+
render(<CphSortFilterSheet {...defaultProps} />);
|
|
89
|
+
await user.click(screen.getByTestId("trigger"));
|
|
90
|
+
expect(screen.getByTestId("sort-filter-sheet")).toHaveClass("cph-table__sheet");
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("forwards additional props to dialog", async () => {
|
|
94
|
+
const user = userEvent.setup();
|
|
95
|
+
render(<CphSortFilterSheet {...defaultProps} id="custom-sheet" />);
|
|
96
|
+
await user.click(screen.getByTestId("trigger"));
|
|
97
|
+
expect(screen.getByTestId("sort-filter-sheet")).toHaveAttribute("id", "custom-sheet");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("uses controlled isOpen prop", async () => {
|
|
101
|
+
render(<CphSortFilterSheet {...defaultProps} isOpen={true} />);
|
|
102
|
+
expect(screen.getByTestId("sort-filter-sheet")).toBeInTheDocument();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("uses uncontrolled defaultOpen prop", async () => {
|
|
106
|
+
render(<CphSortFilterSheet {...defaultProps} defaultOpen={true} />);
|
|
107
|
+
expect(screen.getByTestId("sort-filter-sheet")).toBeInTheDocument();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("does not render sheet when closed", () => {
|
|
111
|
+
render(<CphSortFilterSheet {...defaultProps} isOpen={false} />);
|
|
112
|
+
expect(screen.queryByTestId("sort-filter-sheet")).not.toBeInTheDocument();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("forwards consumer data attributes and style to the dialog", () => {
|
|
116
|
+
render(
|
|
117
|
+
<CphSortFilterSheet
|
|
118
|
+
{...defaultProps}
|
|
119
|
+
isOpen={true}
|
|
120
|
+
data-track="sheet"
|
|
121
|
+
style={{ zIndex: 7 }}
|
|
122
|
+
aria-label="Custom sheet"
|
|
123
|
+
>
|
|
124
|
+
<div data-testid="sheet-content">Sheet content</div>
|
|
125
|
+
</CphSortFilterSheet>,
|
|
126
|
+
);
|
|
127
|
+
const sheet = screen.getByTestId("sort-filter-sheet");
|
|
128
|
+
expect(sheet).toHaveAttribute("data-track", "sheet");
|
|
129
|
+
expect(sheet).toHaveAttribute("aria-label", "Custom sheet");
|
|
130
|
+
expect(sheet).toHaveStyle({ zIndex: "7" });
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("generates unique title ids for multiple sheets", () => {
|
|
134
|
+
render(
|
|
135
|
+
<>
|
|
136
|
+
<CphSortFilterSheet isOpen={true} description="First sheet">
|
|
137
|
+
<div>First</div>
|
|
138
|
+
</CphSortFilterSheet>
|
|
139
|
+
<CphSortFilterSheet isOpen={true} description="Second sheet">
|
|
140
|
+
<div>Second</div>
|
|
141
|
+
</CphSortFilterSheet>
|
|
142
|
+
</>,
|
|
143
|
+
);
|
|
144
|
+
const sheets = screen.getAllByTestId("sort-filter-sheet");
|
|
145
|
+
expect(sheets).toHaveLength(2);
|
|
146
|
+
const labelledBy = sheets.map((sheet) => sheet.getAttribute("aria-labelledby"));
|
|
147
|
+
expect(labelledBy[0]).toMatch(/^cph-sort-filter-sheet-title-/);
|
|
148
|
+
expect(labelledBy[1]).toMatch(/^cph-sort-filter-sheet-title-/);
|
|
149
|
+
expect(new Set(labelledBy).size).toBe(2);
|
|
150
|
+
const describedBy = sheets.map((sheet) => sheet.getAttribute("aria-describedby"));
|
|
151
|
+
expect(new Set(describedBy).size).toBe(2);
|
|
152
|
+
for (const id of describedBy) {
|
|
153
|
+
expect(document.getElementById(id ?? "")).toHaveTextContent(/sheet$/i);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/** @packageDocumentation Mobile sort/filter sheet dialog. */
|
|
2
|
+
|
|
3
|
+
import { X } from "lucide-react";
|
|
4
|
+
import {
|
|
5
|
+
useId,
|
|
6
|
+
type CSSProperties,
|
|
7
|
+
type ReactNode,
|
|
8
|
+
type ReactElement,
|
|
9
|
+
type DOMAttributes,
|
|
10
|
+
} from "react";
|
|
11
|
+
import {
|
|
12
|
+
Button,
|
|
13
|
+
Dialog,
|
|
14
|
+
DialogTrigger,
|
|
15
|
+
Heading,
|
|
16
|
+
Modal,
|
|
17
|
+
ModalOverlay,
|
|
18
|
+
Pressable,
|
|
19
|
+
type FocusableElement,
|
|
20
|
+
} from "react-aria-components";
|
|
21
|
+
|
|
22
|
+
import { composeClassName } from "../../utils/composeClassName";
|
|
23
|
+
|
|
24
|
+
export interface CphSortFilterSheetProps {
|
|
25
|
+
/** Content to render inside the sheet. */
|
|
26
|
+
children: ReactNode;
|
|
27
|
+
/** Whether sheet is open (controlled). */
|
|
28
|
+
isOpen?: boolean;
|
|
29
|
+
/** Default open state (uncontrolled). */
|
|
30
|
+
defaultOpen?: boolean;
|
|
31
|
+
/** Callback when sheet open state changes. */
|
|
32
|
+
onOpenChange?: (isOpen: boolean) => void;
|
|
33
|
+
/** Title for the sheet. */
|
|
34
|
+
title?: string;
|
|
35
|
+
/** Description for the sheet. */
|
|
36
|
+
description?: string;
|
|
37
|
+
/** Trigger element to open the sheet. */
|
|
38
|
+
trigger?: ReactNode;
|
|
39
|
+
/** Accessible name used when no visible title is supplied. */
|
|
40
|
+
"aria-label"?: string;
|
|
41
|
+
/** DOM id forwarded to the dialog. */
|
|
42
|
+
id?: string;
|
|
43
|
+
/** Additional class names for the dialog. */
|
|
44
|
+
className?: string;
|
|
45
|
+
/** Inline styles forwarded to the dialog. */
|
|
46
|
+
style?: CSSProperties;
|
|
47
|
+
/** Extra data attributes forwarded to the dialog. */
|
|
48
|
+
[key: `data-${string}`]: string | undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function CphSortFilterSheet({
|
|
52
|
+
children,
|
|
53
|
+
isOpen,
|
|
54
|
+
defaultOpen,
|
|
55
|
+
onOpenChange,
|
|
56
|
+
title = "Sort & Filter",
|
|
57
|
+
description,
|
|
58
|
+
trigger,
|
|
59
|
+
"aria-label": ariaLabel,
|
|
60
|
+
id,
|
|
61
|
+
className,
|
|
62
|
+
style,
|
|
63
|
+
...dataAttrs
|
|
64
|
+
}: Readonly<CphSortFilterSheetProps>) {
|
|
65
|
+
const uid = useId().replace(/[^a-zA-Z0-9-_]/g, "");
|
|
66
|
+
const titleId = `cph-sort-filter-sheet-title-${uid}`;
|
|
67
|
+
const descriptionId = `cph-sort-filter-sheet-description-${uid}`;
|
|
68
|
+
const handleClose = () => onOpenChange?.(false);
|
|
69
|
+
|
|
70
|
+
// className is a plain string here, so composition always resolves to a string.
|
|
71
|
+
const dialogClassName = composeClassName("cph-table__sheet", className) as string;
|
|
72
|
+
|
|
73
|
+
return (
|
|
74
|
+
<DialogTrigger
|
|
75
|
+
{...(isOpen !== undefined ? { isOpen } : {})}
|
|
76
|
+
{...(defaultOpen !== undefined ? { defaultOpen } : {})}
|
|
77
|
+
{...(onOpenChange ? { onOpenChange } : {})}
|
|
78
|
+
>
|
|
79
|
+
{trigger ? (
|
|
80
|
+
<Pressable>{trigger as ReactElement<DOMAttributes<FocusableElement>, string>}</Pressable>
|
|
81
|
+
) : (
|
|
82
|
+
<Button aria-label="Open sort & filter">Sort & Filter</Button>
|
|
83
|
+
)}
|
|
84
|
+
<ModalOverlay
|
|
85
|
+
isDismissable
|
|
86
|
+
isKeyboardDismissDisabled={false}
|
|
87
|
+
className={composeClassName("cph-table__sheet-overlay", undefined)}
|
|
88
|
+
>
|
|
89
|
+
<Modal className={composeClassName("cph-table__sheet-modal", undefined)}>
|
|
90
|
+
<div data-testid="sort-filter-sheet-modal">
|
|
91
|
+
<Dialog
|
|
92
|
+
{...dataAttrs}
|
|
93
|
+
className={dialogClassName}
|
|
94
|
+
{...(style !== undefined ? { style } : {})}
|
|
95
|
+
{...(ariaLabel !== undefined ? { "aria-label": ariaLabel } : {})}
|
|
96
|
+
aria-labelledby={titleId}
|
|
97
|
+
{...(description ? { "aria-describedby": descriptionId } : {})}
|
|
98
|
+
{...(id !== undefined ? { id } : {})}
|
|
99
|
+
data-cph-table="sort-filter-sheet"
|
|
100
|
+
data-testid="sort-filter-sheet"
|
|
101
|
+
>
|
|
102
|
+
<div className="cph-table__sheet-heading">
|
|
103
|
+
<Heading id={titleId} slot="title">
|
|
104
|
+
{title}
|
|
105
|
+
</Heading>
|
|
106
|
+
<Button
|
|
107
|
+
className="cph-table__sheet-close"
|
|
108
|
+
onPress={handleClose}
|
|
109
|
+
aria-label="Close sort & filter"
|
|
110
|
+
>
|
|
111
|
+
<X size={20} />
|
|
112
|
+
</Button>
|
|
113
|
+
</div>
|
|
114
|
+
{description && (
|
|
115
|
+
<p id={descriptionId} className="cph-table__sheet-description">
|
|
116
|
+
{description}
|
|
117
|
+
</p>
|
|
118
|
+
)}
|
|
119
|
+
<div className="cph-table__sheet-content">{children}</div>
|
|
120
|
+
<div className="cph-table__sheet-actions">
|
|
121
|
+
<Button className="cph-table__sheet-action-btn" onPress={handleClose} slot="close">
|
|
122
|
+
Done
|
|
123
|
+
</Button>
|
|
124
|
+
</div>
|
|
125
|
+
</Dialog>
|
|
126
|
+
</div>
|
|
127
|
+
</Modal>
|
|
128
|
+
</ModalOverlay>
|
|
129
|
+
</DialogTrigger>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { CphStatusChip } from "./CphStatusChip";
|
|
5
|
+
|
|
6
|
+
const TONES = ["neutral", "sage", "orange", "error"] as const;
|
|
7
|
+
|
|
8
|
+
describe("CphStatusChip", () => {
|
|
9
|
+
it.each(TONES)("maps tone %s onto the matching CphBadge class", (tone) => {
|
|
10
|
+
render(<CphStatusChip tone={tone}>Alpha</CphStatusChip>);
|
|
11
|
+
expect(screen.getByText("Alpha")).toHaveClass(`cph-badge--${tone}`);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("defaults to the neutral tone", () => {
|
|
15
|
+
render(<CphStatusChip>Alpha</CphStatusChip>);
|
|
16
|
+
expect(screen.getByText("Alpha")).toHaveClass("cph-badge--neutral");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("keeps the label reachable by text so status is never colour-only", () => {
|
|
20
|
+
render(<CphStatusChip tone="error">Needs attention</CphStatusChip>);
|
|
21
|
+
// The spec forbids conveying status by colour alone; the text must survive
|
|
22
|
+
// independently of the dot.
|
|
23
|
+
expect(screen.getByText("Needs attention")).toBeInTheDocument();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("hides the decorative dot from assistive technology", () => {
|
|
27
|
+
const { container } = render(<CphStatusChip tone="sage">Indexed</CphStatusChip>);
|
|
28
|
+
const dot = container.querySelector(".cph-table__chip-dot");
|
|
29
|
+
expect(dot).not.toBeNull();
|
|
30
|
+
expect(dot).toHaveAttribute("aria-hidden", "true");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("accepts an extra className without losing the chip class", () => {
|
|
34
|
+
render(
|
|
35
|
+
<CphStatusChip tone="orange" className="uppercase">
|
|
36
|
+
In review
|
|
37
|
+
</CphStatusChip>,
|
|
38
|
+
);
|
|
39
|
+
const chip = screen.getByText("In review");
|
|
40
|
+
expect(chip).toHaveClass("cph-table__chip");
|
|
41
|
+
expect(chip).toHaveClass("uppercase");
|
|
42
|
+
});
|
|
43
|
+
});
|