@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,174 @@
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 { CphMobileCards, CphMobileCard } from "./CphMobileCards";
6
+
7
+ describe("CphMobileCards", () => {
8
+ const defaultRows = [
9
+ {
10
+ id: "row-1",
11
+ content: <div data-testid="row-1-content">Row 1 Content</div>,
12
+ secondary: <div data-testid="row-1-secondary">Row 1 Secondary</div>,
13
+ isSelected: false,
14
+ },
15
+ {
16
+ id: "row-2",
17
+ content: <div data-testid="row-2-content">Row 2 Content</div>,
18
+ isSelected: true,
19
+ },
20
+ ];
21
+
22
+ it("renders mobile cards container with cph-table__mobile-cards class", () => {
23
+ render(<CphMobileCards rows={defaultRows} />);
24
+ expect(screen.getByTestId("mobile-cards")).toHaveClass("cph-table__mobile-cards");
25
+ expect(screen.getByTestId("mobile-cards")).toHaveAttribute("role", "list");
26
+ });
27
+
28
+ it("renders each row as a card", () => {
29
+ render(<CphMobileCards rows={defaultRows} />);
30
+ expect(screen.getByTestId("mobile-card-row-1")).toBeInTheDocument();
31
+ expect(screen.getByTestId("mobile-card-row-2")).toBeInTheDocument();
32
+ });
33
+
34
+ it("applies selected state to card", () => {
35
+ render(<CphMobileCards rows={defaultRows} />);
36
+ expect(screen.getByTestId("mobile-card-row-2")).toHaveAttribute("data-selected", "true");
37
+ expect(screen.getByTestId("mobile-card-row-1")).toHaveAttribute("data-selected", "false");
38
+ });
39
+
40
+ it("renders checkbox for each card when selection is supported", () => {
41
+ render(<CphMobileCards rows={defaultRows} onSelectionChange={() => undefined} />);
42
+ expect(screen.getByRole("checkbox", { name: "Select row row-1" })).toBeInTheDocument();
43
+ expect(screen.getByRole("checkbox", { name: "Select row row-2" })).toBeInTheDocument();
44
+ });
45
+
46
+ it("renders no selection checkbox when onSelectionChange is omitted", () => {
47
+ render(<CphMobileCards rows={defaultRows} />);
48
+ expect(screen.queryByRole("checkbox", { name: "Select row row-1" })).not.toBeInTheDocument();
49
+ });
50
+
51
+ it("exposes each card as a listitem of the list", () => {
52
+ render(<CphMobileCards rows={defaultRows} />);
53
+ const list = screen.getByRole("list", { name: "Table rows as cards" });
54
+ const items = screen.getAllByRole("listitem");
55
+ expect(items).toHaveLength(2);
56
+ for (const item of items) {
57
+ expect(list).toContainElement(item);
58
+ }
59
+ });
60
+
61
+ it("calls onSelectionChange when checkbox is toggled", async () => {
62
+ const user = userEvent.setup();
63
+ const onSelectionChange = vi.fn();
64
+ render(<CphMobileCards rows={defaultRows} onSelectionChange={onSelectionChange} />);
65
+ await user.click(screen.getByRole("checkbox", { name: "Select row row-1" }));
66
+ expect(onSelectionChange).toHaveBeenCalledWith("row-1", true);
67
+ });
68
+
69
+ it("renders row content", () => {
70
+ render(<CphMobileCards rows={defaultRows} />);
71
+ expect(screen.getByTestId("row-1-content")).toBeInTheDocument();
72
+ expect(screen.getByTestId("row-2-content")).toBeInTheDocument();
73
+ });
74
+
75
+ it("renders secondary content when provided", () => {
76
+ render(<CphMobileCards rows={defaultRows} />);
77
+ expect(screen.getByTestId("row-1-secondary")).toBeInTheDocument();
78
+ });
79
+
80
+ it("uses renderRow function when provided", () => {
81
+ const renderRow = vi.fn((row) => (
82
+ <span data-testid={`custom-${row.id}`}>Custom: {row.id}</span>
83
+ ));
84
+ render(<CphMobileCards rows={defaultRows} renderRow={renderRow} />);
85
+ expect(screen.getByTestId("custom-row-1")).toBeInTheDocument();
86
+ expect(screen.getByTestId("custom-row-2")).toBeInTheDocument();
87
+ expect(renderRow).toHaveBeenCalledTimes(2);
88
+ });
89
+
90
+ it("forwards additional props to root div", () => {
91
+ render(<CphMobileCards rows={defaultRows} id="custom-mobile-cards" />);
92
+ expect(screen.getByTestId("mobile-cards")).toHaveAttribute("id", "custom-mobile-cards");
93
+ });
94
+
95
+ it("renders children when provided", () => {
96
+ render(
97
+ <CphMobileCards rows={defaultRows}>
98
+ <div data-testid="extra-child">Extra</div>
99
+ </CphMobileCards>,
100
+ );
101
+ expect(screen.getByTestId("extra-child")).toBeInTheDocument();
102
+ });
103
+ });
104
+
105
+ describe("CphMobileCard", () => {
106
+ it("renders card with cph-table__mobile-card class", () => {
107
+ render(<CphMobileCard id="test-card">Content</CphMobileCard>);
108
+ expect(screen.getByTestId("mobile-card-test-card")).toHaveClass("cph-table__mobile-card");
109
+ });
110
+
111
+ it("applies selected state", () => {
112
+ render(
113
+ <CphMobileCard id="test-card" isSelected>
114
+ Content
115
+ </CphMobileCard>,
116
+ );
117
+ expect(screen.getByTestId("mobile-card-test-card")).toHaveAttribute("data-selected", "true");
118
+ });
119
+
120
+ it("renders checkbox with correct label when selection is supported", () => {
121
+ render(
122
+ <CphMobileCard id="test-card" onSelectionChange={() => undefined}>
123
+ Content
124
+ </CphMobileCard>,
125
+ );
126
+ expect(screen.getByRole("checkbox", { name: "Select row test-card" })).toBeInTheDocument();
127
+ });
128
+
129
+ it("renders no checkbox without onSelectionChange", () => {
130
+ render(<CphMobileCard id="test-card">Content</CphMobileCard>);
131
+ expect(
132
+ screen.queryByRole("checkbox", { name: "Select row test-card" }),
133
+ ).not.toBeInTheDocument();
134
+ });
135
+
136
+ it("calls onSelectionChange when checkbox changes", async () => {
137
+ const user = userEvent.setup();
138
+ const onSelectionChange = vi.fn();
139
+ render(
140
+ <CphMobileCard id="test-card" onSelectionChange={onSelectionChange}>
141
+ Content
142
+ </CphMobileCard>,
143
+ );
144
+ await user.click(screen.getByRole("checkbox", { name: "Select row test-card" }));
145
+ expect(onSelectionChange).toHaveBeenCalledWith("test-card", true);
146
+ });
147
+
148
+ it("renders content", () => {
149
+ render(
150
+ <CphMobileCard id="test-card">
151
+ <span data-testid="card-content">Card Content</span>
152
+ </CphMobileCard>,
153
+ );
154
+ expect(screen.getByTestId("card-content")).toBeInTheDocument();
155
+ });
156
+
157
+ it("renders secondary content when provided", () => {
158
+ render(
159
+ <CphMobileCard id="test-card" secondary={<span data-testid="card-secondary">Secondary</span>}>
160
+ Content
161
+ </CphMobileCard>,
162
+ );
163
+ expect(screen.getByTestId("card-secondary")).toBeInTheDocument();
164
+ });
165
+
166
+ it("renders actions when provided", () => {
167
+ render(
168
+ <CphMobileCard id="test-card" actions={<button data-testid="card-action">Action</button>}>
169
+ Content
170
+ </CphMobileCard>,
171
+ );
172
+ expect(screen.getByTestId("card-action")).toBeInTheDocument();
173
+ });
174
+ });
@@ -0,0 +1,117 @@
1
+ /** @packageDocumentation Mobile stacked card layout for table rows. */
2
+
3
+ import type { HTMLAttributes, ReactNode } from "react";
4
+ import { Checkbox } from "react-aria-components";
5
+
6
+ import { composeClassName } from "../../utils/composeClassName";
7
+
8
+ export interface CphMobileCardProps {
9
+ /** Unique identifier for the row. */
10
+ id: string;
11
+ /** Whether the row is selected. */
12
+ isSelected?: boolean;
13
+ /** Callback when selection changes. */
14
+ onSelectionChange?: (id: string, selected: boolean) => void;
15
+ /** Main content of the card. */
16
+ children: ReactNode;
17
+ /** Optional secondary content (e.g., metadata). */
18
+ secondary?: ReactNode;
19
+ /** Optional actions for the card. */
20
+ actions?: ReactNode;
21
+ }
22
+
23
+ export function CphMobileCard({
24
+ id,
25
+ isSelected = false,
26
+ onSelectionChange,
27
+ children,
28
+ secondary,
29
+ actions,
30
+ }: Readonly<CphMobileCardProps>) {
31
+ return (
32
+ <article
33
+ className="cph-table__mobile-card"
34
+ data-selected={isSelected}
35
+ data-cph-table="mobile-card"
36
+ data-testid={`mobile-card-${id}`}
37
+ role="listitem"
38
+ >
39
+ {(onSelectionChange !== undefined || actions) && (
40
+ <div className="cph-table__mobile-card-header">
41
+ {onSelectionChange !== undefined ? (
42
+ <Checkbox
43
+ isSelected={isSelected}
44
+ onChange={(selected) => onSelectionChange(id, selected)}
45
+ className="cph-table__mobile-select"
46
+ aria-label={`Select row ${id}`}
47
+ />
48
+ ) : null}
49
+ {actions && <div className="cph-table__mobile-card-actions">{actions}</div>}
50
+ </div>
51
+ )}
52
+ <div className="cph-table__mobile-card-content">{children}</div>
53
+ {secondary && <div className="cph-table__mobile-card-secondary">{secondary}</div>}
54
+ </article>
55
+ );
56
+ }
57
+
58
+ export interface CphMobileCardsProps extends HTMLAttributes<HTMLDivElement> {
59
+ /** Array of row data to render as cards. */
60
+ rows: Array<{
61
+ id: string;
62
+ isSelected?: boolean;
63
+ content: ReactNode;
64
+ secondary?: ReactNode;
65
+ actions?: ReactNode;
66
+ }>;
67
+ /** Callback when row selection changes. */
68
+ onSelectionChange?: (id: string, selected: boolean) => void;
69
+ /** Render function for row content. */
70
+ renderRow?: (row: {
71
+ id: string;
72
+ content: ReactNode;
73
+ secondary?: ReactNode;
74
+ actions?: ReactNode;
75
+ }) => ReactNode;
76
+ }
77
+
78
+ export function CphMobileCards({
79
+ rows,
80
+ onSelectionChange,
81
+ renderRow,
82
+ className,
83
+ children,
84
+ ...props
85
+ }: Readonly<CphMobileCardsProps>) {
86
+ // For native elements, composeClassName may return a function (render-props callback).
87
+ // We need to resolve it to a string for native DOM elements.
88
+ const rootClassName = (() => {
89
+ const composed = composeClassName("cph-table__mobile-cards", className);
90
+ return typeof composed === "function" ? composed({ defaultClassName: undefined }) : composed;
91
+ })();
92
+
93
+ return (
94
+ <div
95
+ {...props}
96
+ className={rootClassName}
97
+ data-cph-table="mobile-cards"
98
+ data-testid="mobile-cards"
99
+ role="list"
100
+ aria-label="Table rows as cards"
101
+ >
102
+ {rows.map((row) => (
103
+ <CphMobileCard
104
+ key={row.id}
105
+ id={row.id}
106
+ {...(row.isSelected === undefined ? {} : { isSelected: row.isSelected })}
107
+ {...(onSelectionChange ? { onSelectionChange } : {})}
108
+ secondary={row.secondary}
109
+ actions={row.actions}
110
+ >
111
+ {renderRow ? renderRow(row) : row.content}
112
+ </CphMobileCard>
113
+ ))}
114
+ {children}
115
+ </div>
116
+ );
117
+ }
@@ -0,0 +1,51 @@
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 { CphCursorPagination } from "./CphCursorPagination";
6
+
7
+ describe("CphCursorPagination", () => {
8
+ it("renders the navigation landmark with a label", () => {
9
+ render(<CphCursorPagination />);
10
+ expect(screen.getByRole("navigation", { name: "Table pagination" })).toBeInTheDocument();
11
+ });
12
+
13
+ it("applies cph-table__pagination class to the root", () => {
14
+ render(<CphCursorPagination />);
15
+ expect(screen.getByTestId("cursor-pagination")).toHaveClass("cph-table__pagination");
16
+ });
17
+
18
+ it("disables Next when there is no next page", () => {
19
+ render(<CphCursorPagination hasNextPage={false} hasPreviousPage={false} />);
20
+ expect(screen.getByRole("button", { name: "Next" })).toBeDisabled();
21
+ expect(screen.getByRole("button", { name: "Previous" })).toBeDisabled();
22
+ });
23
+
24
+ it("disables navigation while loading", () => {
25
+ render(<CphCursorPagination hasNextPage hasPreviousPage isLoading />);
26
+ expect(screen.getByRole("button", { name: "Next" })).toBeDisabled();
27
+ expect(screen.getByRole("button", { name: "Previous" })).toBeDisabled();
28
+ });
29
+
30
+ it("calls onNextPage with the end cursor when Next is clicked", async () => {
31
+ const user = userEvent.setup();
32
+ const onNextPage = vi.fn();
33
+ render(<CphCursorPagination hasNextPage endCursor="cursor-2" onNextPage={onNextPage} />);
34
+ await user.click(screen.getByRole("button", { name: "Next" }));
35
+ expect(onNextPage).toHaveBeenCalledWith("cursor-2");
36
+ });
37
+
38
+ it("calls onPreviousPage with the start cursor when Previous is clicked", async () => {
39
+ const user = userEvent.setup();
40
+ const onPreviousPage = vi.fn();
41
+ render(
42
+ <CphCursorPagination
43
+ hasPreviousPage
44
+ startCursor="cursor-1"
45
+ onPreviousPage={onPreviousPage}
46
+ />,
47
+ );
48
+ await user.click(screen.getByRole("button", { name: "Previous" }));
49
+ expect(onPreviousPage).toHaveBeenCalledWith("cursor-1");
50
+ });
51
+ });
@@ -0,0 +1,74 @@
1
+ /** @packageDocumentation Cursor-based pagination controls for tables. */
2
+
3
+ import type { HTMLAttributes } from "react";
4
+ import { Button } from "react-aria-components";
5
+
6
+ import { composeClassName } from "../../utils/composeClassName";
7
+
8
+ export interface CphCursorPaginationProps extends HTMLAttributes<HTMLDivElement> {
9
+ /** Whether a next page exists. */
10
+ hasNextPage?: boolean;
11
+ /** Whether a previous page exists. */
12
+ hasPreviousPage?: boolean;
13
+ /** Cursor marking the start of the next page. */
14
+ startCursor?: string | null;
15
+ /** Cursor marking the end of the next page. */
16
+ endCursor?: string | null;
17
+ /** Called with the next page's start cursor. */
18
+ onNextPage?: (cursor?: string | null) => void;
19
+ /** Called with the previous page's end cursor. */
20
+ onPreviousPage?: (cursor?: string | null) => void;
21
+ /** True while a page fetch is in flight; disables navigation. */
22
+ isLoading?: boolean;
23
+ }
24
+
25
+ /**
26
+ * Cursor-based pagination controls.
27
+ *
28
+ * Renders a `nav` landmark (labelled) with Previous/Next buttons. Callbacks
29
+ * receive the cursor that should be requested next.
30
+ */
31
+ export function CphCursorPagination({
32
+ hasNextPage,
33
+ hasPreviousPage,
34
+ startCursor,
35
+ endCursor,
36
+ onNextPage,
37
+ onPreviousPage,
38
+ isLoading,
39
+ className,
40
+ ...props
41
+ }: Readonly<CphCursorPaginationProps>) {
42
+ const rootClassName = (() => {
43
+ const composed = composeClassName("cph-table__pagination", className);
44
+ return typeof composed === "function" ? composed({ defaultClassName: undefined }) : composed;
45
+ })();
46
+
47
+ return (
48
+ <div
49
+ {...props}
50
+ className={rootClassName}
51
+ data-cph-table="cursor-pagination"
52
+ data-testid="cursor-pagination"
53
+ >
54
+ <nav aria-label="Table pagination">
55
+ <Button
56
+ className="cph-btn cph-btn--secondary cph-table__pagination-btn"
57
+ isDisabled={!hasPreviousPage || Boolean(isLoading)}
58
+ onPress={() => onPreviousPage?.(startCursor ?? null)}
59
+ aria-label="Previous"
60
+ >
61
+ Previous
62
+ </Button>
63
+ <Button
64
+ className="cph-btn cph-btn--secondary cph-table__pagination-btn"
65
+ isDisabled={!hasNextPage || Boolean(isLoading)}
66
+ onPress={() => onNextPage?.(endCursor ?? null)}
67
+ aria-label="Next"
68
+ >
69
+ Next
70
+ </Button>
71
+ </nav>
72
+ </div>
73
+ );
74
+ }
@@ -0,0 +1,98 @@
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 { parsePageSizeKey } from "../../table/pageSize";
6
+ import { CphPagination } from "./CphPagination";
7
+
8
+ describe("CphPagination", () => {
9
+ const defaultProps = {
10
+ currentPage: 2,
11
+ totalPages: 5,
12
+ onPageChange: vi.fn(),
13
+ pageSize: 12 as const,
14
+ onPageSizeChange: vi.fn(),
15
+ };
16
+
17
+ it("renders pagination nav landmark", () => {
18
+ render(<CphPagination {...defaultProps} />);
19
+ expect(screen.getByRole("navigation", { name: "Pagination" })).toBeInTheDocument();
20
+ });
21
+
22
+ it("shows current page and total pages", () => {
23
+ render(<CphPagination {...defaultProps} />);
24
+ expect(screen.getByText("Page 2 of 5")).toBeInTheDocument();
25
+ });
26
+
27
+ it("disables Previous button on first page", () => {
28
+ render(<CphPagination {...defaultProps} currentPage={1} />);
29
+ expect(screen.getByRole("button", { name: "Previous page" })).toBeDisabled();
30
+ });
31
+
32
+ it("disables Next button on last page", () => {
33
+ render(<CphPagination {...defaultProps} currentPage={5} />);
34
+ expect(screen.getByRole("button", { name: "Next page" })).toBeDisabled();
35
+ });
36
+
37
+ it("calls onPageChange when Previous is clicked", async () => {
38
+ const user = userEvent.setup();
39
+ render(<CphPagination {...defaultProps} />);
40
+ await user.click(screen.getByRole("button", { name: "Previous page" }));
41
+ expect(defaultProps.onPageChange).toHaveBeenCalledWith(1);
42
+ });
43
+
44
+ it("calls onPageChange when Next is clicked", async () => {
45
+ const user = userEvent.setup();
46
+ render(<CphPagination {...defaultProps} />);
47
+ await user.click(screen.getByRole("button", { name: "Next page" }));
48
+ expect(defaultProps.onPageChange).toHaveBeenCalledWith(3);
49
+ });
50
+
51
+ it("renders page size select with PAGE_SIZES options", () => {
52
+ render(<CphPagination {...defaultProps} />);
53
+ // React Aria Components 1.21.1 exposes Select's trigger as a button, not a combobox.
54
+ const select = screen.getByRole("button", { name: /rows per page/i });
55
+ expect(select).toBeInTheDocument();
56
+ expect(select).toHaveTextContent("12");
57
+ });
58
+
59
+ it("calls onPageSizeChange when page size changes", async () => {
60
+ const user = userEvent.setup();
61
+ render(<CphPagination {...defaultProps} />);
62
+ const select = screen.getByRole("button", { name: /rows per page/i });
63
+ await user.click(select);
64
+ await user.click(screen.getByRole("option", { name: "20" }));
65
+ expect(defaultProps.onPageSizeChange).toHaveBeenCalledWith(20);
66
+ });
67
+
68
+ it("applies cph-table__pagination class", () => {
69
+ render(<CphPagination {...defaultProps} />);
70
+ expect(screen.getByTestId("pagination")).toHaveClass("cph-table__pagination");
71
+ });
72
+
73
+ it("forwards additional props to root div", () => {
74
+ render(<CphPagination {...defaultProps} id="custom-pagination" />);
75
+ expect(screen.getByTestId("pagination")).toHaveAttribute("id", "custom-pagination");
76
+ });
77
+ });
78
+
79
+ describe("parsePageSizeKey", () => {
80
+ it("resolves string keys for supported sizes", () => {
81
+ expect(parsePageSizeKey("8")).toBe(8);
82
+ expect(parsePageSizeKey("12")).toBe(12);
83
+ expect(parsePageSizeKey("20")).toBe(20);
84
+ });
85
+
86
+ it("returns undefined for null", () => {
87
+ expect(parsePageSizeKey(null)).toBeUndefined();
88
+ });
89
+
90
+ it("returns undefined for non-string keys", () => {
91
+ expect(parsePageSizeKey(20)).toBeUndefined();
92
+ });
93
+
94
+ it("returns undefined for strings that are not page sizes", () => {
95
+ expect(parsePageSizeKey("bogus")).toBeUndefined();
96
+ expect(parsePageSizeKey("10")).toBeUndefined();
97
+ });
98
+ });
@@ -0,0 +1,94 @@
1
+ /** @packageDocumentation Table pagination controls. */
2
+
3
+ import type { HTMLAttributes, Key } from "react";
4
+ import { Button, ListBox, ListBoxItem, Select, SelectValue } from "react-aria-components";
5
+
6
+ import { PAGE_SIZES, parsePageSizeKey, type PageSize } from "../../table/pageSize";
7
+ import { composeClassName } from "../../utils/composeClassName";
8
+
9
+ export interface CphPaginationProps extends HTMLAttributes<HTMLDivElement> {
10
+ /** Current page number (1-based). */
11
+ currentPage: number;
12
+ /** Total number of pages. */
13
+ totalPages: number;
14
+ /** Callback when page changes. */
15
+ onPageChange: (page: number) => void;
16
+ /** Current page size. */
17
+ pageSize: PageSize;
18
+ /** Callback when page size changes. */
19
+ onPageSizeChange: (size: PageSize) => void;
20
+ }
21
+
22
+ export function CphPagination({
23
+ currentPage,
24
+ totalPages,
25
+ onPageChange,
26
+ pageSize,
27
+ onPageSizeChange,
28
+ className,
29
+ ...props
30
+ }: Readonly<CphPaginationProps>) {
31
+ const handlePageSizeChange = (value: Key | null) => {
32
+ const next = parsePageSizeKey(value);
33
+ if (next !== undefined) {
34
+ onPageSizeChange(next);
35
+ }
36
+ };
37
+
38
+ // For native elements, composeClassName may return a function (render-props callback).
39
+ // We need to resolve it to a string for native DOM elements.
40
+ const rootClassName = (() => {
41
+ const composed = composeClassName("cph-table__pagination", className);
42
+ return typeof composed === "function" ? composed({ defaultClassName: undefined }) : composed;
43
+ })();
44
+
45
+ return (
46
+ <div {...props} className={rootClassName} data-cph-table="pagination" data-testid="pagination">
47
+ <nav aria-label="Pagination">
48
+ <div style={{ display: "flex", alignItems: "center", gap: "var(--cph-space-4)" }}>
49
+ <Button
50
+ onPress={() => onPageChange(Math.max(1, currentPage - 1))}
51
+ isDisabled={currentPage <= 1}
52
+ className="cph-table__pagination-btn"
53
+ aria-label="Previous page"
54
+ >
55
+ Previous
56
+ </Button>
57
+ <span className="cph-table__pagination-page" aria-live="polite">
58
+ Page {currentPage} of {totalPages}
59
+ </span>
60
+ <Button
61
+ onPress={() => onPageChange(Math.min(totalPages, currentPage + 1))}
62
+ isDisabled={currentPage >= totalPages}
63
+ className="cph-table__pagination-btn"
64
+ aria-label="Next page"
65
+ >
66
+ Next
67
+ </Button>
68
+ </div>
69
+ <div style={{ display: "flex", alignItems: "center", gap: "var(--cph-space-2)" }}>
70
+ <span className="cph-table__pagination-label">Rows per page:</span>
71
+ <Select
72
+ selectedKey={String(pageSize)}
73
+ onSelectionChange={handlePageSizeChange}
74
+ className={composeClassName("cph-table__pagination-select", undefined)}
75
+ aria-label="Rows per page"
76
+ >
77
+ <Button className="cph-table__pagination-select-btn" slot="label">
78
+ <SelectValue>
79
+ {({ selectedText, defaultChildren }) => selectedText ?? defaultChildren}
80
+ </SelectValue>
81
+ </Button>
82
+ <ListBox>
83
+ {PAGE_SIZES.map((size) => (
84
+ <ListBoxItem key={size} id={String(size)} textValue={String(size)}>
85
+ {size}
86
+ </ListBoxItem>
87
+ ))}
88
+ </ListBox>
89
+ </Select>
90
+ </div>
91
+ </nav>
92
+ </div>
93
+ );
94
+ }