@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,129 @@
|
|
|
1
|
+
/** @packageDocumentation Table header component rendering TanStack header groups. */
|
|
2
|
+
|
|
3
|
+
import { FlexRender, type Table, type RowData } from "@tanstack/react-table";
|
|
4
|
+
import { type ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
import type { CphColumnMeta } from "../../table/column";
|
|
7
|
+
import type { CphTableFeatures } from "../../table/features";
|
|
8
|
+
import { HeaderCell, type HeaderCellHeader, type HeaderCellColumn } from "./HeaderCell";
|
|
9
|
+
|
|
10
|
+
/** Props for the optional select-all checkbox rendered in a header cell. */
|
|
11
|
+
export interface CphTableHeadSelectAll {
|
|
12
|
+
/** The column id the select-all checkbox belongs to. */
|
|
13
|
+
readonly columnId: string;
|
|
14
|
+
/** Whether every visible row is selected. */
|
|
15
|
+
readonly checked: boolean;
|
|
16
|
+
/** Whether the select-all checkbox should render indeterminate. */
|
|
17
|
+
readonly indeterminate?: boolean;
|
|
18
|
+
/** Called with the next checked state when the checkbox is toggled. */
|
|
19
|
+
readonly onChange: (checked: boolean) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CphTableHeadProps<TData extends RowData> {
|
|
23
|
+
/** The TanStack table instance. */
|
|
24
|
+
table: Table<CphTableFeatures, TData>;
|
|
25
|
+
/** Optional additional content (e.g., custom header rows). */
|
|
26
|
+
children?: ReactNode;
|
|
27
|
+
/**
|
|
28
|
+
* Select-all checkbox state for the column with `columnId`.
|
|
29
|
+
*
|
|
30
|
+
* When omitted, or when the resolved header is not the addressed column,
|
|
31
|
+
* no select-all checkbox is rendered.
|
|
32
|
+
*/
|
|
33
|
+
selectAll?: CphTableHeadSelectAll;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Renders all header groups from the table instance.
|
|
38
|
+
*
|
|
39
|
+
* Each header group becomes a `<tr>` containing {@link HeaderCell} components
|
|
40
|
+
* for each visible leaf header in that group. The header cells receive their
|
|
41
|
+
* sorting, pinning, and selection state from the TanStack header instance.
|
|
42
|
+
*/
|
|
43
|
+
export function CphTableHead<TData extends RowData>({
|
|
44
|
+
table,
|
|
45
|
+
children,
|
|
46
|
+
selectAll,
|
|
47
|
+
}: Readonly<CphTableHeadProps<TData>>) {
|
|
48
|
+
const headerGroups = table.getHeaderGroups();
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<thead data-cph-table="thead">
|
|
52
|
+
{headerGroups.map((headerGroup) => (
|
|
53
|
+
<tr key={headerGroup.id} data-cph-table="header-group">
|
|
54
|
+
{headerGroup.headers.map((header) => {
|
|
55
|
+
if (header.isPlaceholder) {
|
|
56
|
+
return <th key={header.placeholderId ?? header.id} data-cph-table="placeholder" />;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const column = header.column;
|
|
60
|
+
const canSort = column.getCanSort();
|
|
61
|
+
const isSorted = column.getIsSorted();
|
|
62
|
+
const sortDirection = isSorted
|
|
63
|
+
? column.getSortIndex() === 0
|
|
64
|
+
? column.getIsSorted() === "asc"
|
|
65
|
+
? "asc"
|
|
66
|
+
: "desc"
|
|
67
|
+
: undefined
|
|
68
|
+
: undefined;
|
|
69
|
+
const pinned = column.getIsPinned();
|
|
70
|
+
const canPin = column.getCanPin();
|
|
71
|
+
const isLastPinned = pinned === "start" && column.getIsLastColumn("start");
|
|
72
|
+
const columnMeta = (column.columnDef.meta ?? undefined) as CphColumnMeta | undefined;
|
|
73
|
+
|
|
74
|
+
// Resolve the select-all checkbox for this header, if any.
|
|
75
|
+
const resolvedSelectAll =
|
|
76
|
+
selectAll && selectAll.columnId === column.id && !header.isPlaceholder
|
|
77
|
+
? selectAll
|
|
78
|
+
: undefined;
|
|
79
|
+
|
|
80
|
+
// Narrow header/column to minimal interfaces for HeaderCell
|
|
81
|
+
const headerCellHeader: HeaderCellHeader = {
|
|
82
|
+
id: header.id,
|
|
83
|
+
isPlaceholder: header.isPlaceholder,
|
|
84
|
+
...(header.placeholderId === undefined
|
|
85
|
+
? {}
|
|
86
|
+
: { placeholderId: header.placeholderId }),
|
|
87
|
+
};
|
|
88
|
+
const headerCellColumn: HeaderCellColumn = {
|
|
89
|
+
getCanSort: column.getCanSort.bind(column),
|
|
90
|
+
getIsSorted: column.getIsSorted.bind(column),
|
|
91
|
+
getSortIndex: column.getSortIndex.bind(column),
|
|
92
|
+
getIsPinned: column.getIsPinned.bind(column),
|
|
93
|
+
getCanPin: column.getCanPin.bind(column),
|
|
94
|
+
getIsLastColumn: column.getIsLastColumn.bind(column),
|
|
95
|
+
toggleSorting: column.toggleSorting.bind(column),
|
|
96
|
+
pin: column.pin.bind(column),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<HeaderCell
|
|
101
|
+
key={header.id}
|
|
102
|
+
header={headerCellHeader}
|
|
103
|
+
column={headerCellColumn}
|
|
104
|
+
canSort={canSort}
|
|
105
|
+
sortDirection={sortDirection}
|
|
106
|
+
pinned={pinned}
|
|
107
|
+
canPin={canPin}
|
|
108
|
+
isLastPinned={isLastPinned}
|
|
109
|
+
{...(resolvedSelectAll?.checked === undefined
|
|
110
|
+
? {}
|
|
111
|
+
: { selectAll: resolvedSelectAll.checked })}
|
|
112
|
+
{...(resolvedSelectAll?.indeterminate === undefined
|
|
113
|
+
? {}
|
|
114
|
+
: { selectAllIndeterminate: resolvedSelectAll.indeterminate })}
|
|
115
|
+
{...(resolvedSelectAll?.onChange === undefined
|
|
116
|
+
? {}
|
|
117
|
+
: { onSelectAllChange: resolvedSelectAll.onChange })}
|
|
118
|
+
{...(columnMeta?.align === undefined ? {} : { align: columnMeta.align })}
|
|
119
|
+
>
|
|
120
|
+
<FlexRender header={header} />
|
|
121
|
+
</HeaderCell>
|
|
122
|
+
);
|
|
123
|
+
})}
|
|
124
|
+
</tr>
|
|
125
|
+
))}
|
|
126
|
+
{children}
|
|
127
|
+
</thead>
|
|
128
|
+
);
|
|
129
|
+
}
|