@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,17 @@
|
|
|
1
|
+
/** @packageDocumentation Table row component. */
|
|
2
|
+
|
|
3
|
+
import type { HTMLAttributes, ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
export interface CphTableRowProps extends HTMLAttributes<HTMLTableRowElement> {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
/** Row state: selected, hovered, etc. */
|
|
8
|
+
state?: "selected" | "hovered";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function CphTableRow({ children, state, ...props }: Readonly<CphTableRowProps>) {
|
|
12
|
+
return (
|
|
13
|
+
<tr {...props} data-cph-table="row" data-state={state ?? undefined}>
|
|
14
|
+
{children}
|
|
15
|
+
</tr>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { CphToolbar } from "./CphToolbar";
|
|
7
|
+
|
|
8
|
+
function ControlledToolbar({
|
|
9
|
+
onSearchChange,
|
|
10
|
+
onSearchSubmit,
|
|
11
|
+
}: {
|
|
12
|
+
onSearchChange?: (value: string) => void;
|
|
13
|
+
onSearchSubmit?: (value: string) => void;
|
|
14
|
+
}) {
|
|
15
|
+
const [value, setValue] = useState("");
|
|
16
|
+
return (
|
|
17
|
+
<CphToolbar
|
|
18
|
+
searchValue={value}
|
|
19
|
+
onSearchChange={(nextValue) => {
|
|
20
|
+
setValue(nextValue);
|
|
21
|
+
onSearchChange?.(nextValue);
|
|
22
|
+
}}
|
|
23
|
+
{...(onSearchSubmit ? { onSearchSubmit } : {})}
|
|
24
|
+
/>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("CphToolbar", () => {
|
|
29
|
+
const defaultFilter = {
|
|
30
|
+
key: "status",
|
|
31
|
+
label: "Status",
|
|
32
|
+
options: [
|
|
33
|
+
{ value: "all", label: "All" },
|
|
34
|
+
{ value: "active", label: "Active" },
|
|
35
|
+
{ value: "inactive", label: "Inactive" },
|
|
36
|
+
],
|
|
37
|
+
value: "all",
|
|
38
|
+
onChange: vi.fn(),
|
|
39
|
+
};
|
|
40
|
+
const defaultFilters = [defaultFilter];
|
|
41
|
+
|
|
42
|
+
it("renders toolbar with cph-table__toolbar class", () => {
|
|
43
|
+
render(<CphToolbar />);
|
|
44
|
+
expect(screen.getByTestId("toolbar")).toHaveClass("cph-table__toolbar");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("renders SearchField with accessible label", () => {
|
|
48
|
+
render(<CphToolbar />);
|
|
49
|
+
const searchField = screen.getByRole("searchbox", { name: "Search table" });
|
|
50
|
+
expect(searchField).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("renders search input with placeholder", () => {
|
|
54
|
+
render(<CphToolbar searchPlaceholder="Find records…" />);
|
|
55
|
+
expect(screen.getByPlaceholderText("Find records…")).toBeInTheDocument();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("calls onSearchChange when input changes", async () => {
|
|
59
|
+
const onSearchChange = vi.fn();
|
|
60
|
+
const user = userEvent.setup();
|
|
61
|
+
render(<ControlledToolbar onSearchChange={onSearchChange} />);
|
|
62
|
+
const input = screen.getByLabelText("Search table");
|
|
63
|
+
await user.type(input, "test query");
|
|
64
|
+
expect(onSearchChange).toHaveBeenCalledWith("test query");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("shows clear button when search has value", () => {
|
|
68
|
+
render(<CphToolbar searchValue="test" onSearchChange={vi.fn()} />);
|
|
69
|
+
expect(screen.getByRole("button", { name: "Clear search" })).toBeInTheDocument();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("hides clear button when search is empty", () => {
|
|
73
|
+
render(<CphToolbar searchValue="" onSearchChange={vi.fn()} />);
|
|
74
|
+
expect(screen.queryByRole("button", { name: "Clear search" })).not.toBeInTheDocument();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("clears search when clear button is clicked", async () => {
|
|
78
|
+
const onSearchChange = vi.fn();
|
|
79
|
+
const user = userEvent.setup();
|
|
80
|
+
render(<CphToolbar searchValue="test" onSearchChange={onSearchChange} />);
|
|
81
|
+
await user.click(screen.getByRole("button", { name: "Clear search" }));
|
|
82
|
+
expect(onSearchChange).toHaveBeenCalledWith("");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("calls onSearchSubmit when Enter is pressed", async () => {
|
|
86
|
+
const onSearchSubmit = vi.fn();
|
|
87
|
+
const user = userEvent.setup();
|
|
88
|
+
render(<ControlledToolbar onSearchSubmit={onSearchSubmit} />);
|
|
89
|
+
const input = screen.getByLabelText("Search table");
|
|
90
|
+
await user.type(input, "submit query");
|
|
91
|
+
await user.keyboard("[Enter]");
|
|
92
|
+
expect(onSearchSubmit).toHaveBeenCalledWith("submit query");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("renders filter selects when filters provided", () => {
|
|
96
|
+
render(<CphToolbar filters={defaultFilters} />);
|
|
97
|
+
expect(screen.getByRole("group", { name: "Table filters" })).toBeInTheDocument();
|
|
98
|
+
expect(screen.getByRole("button", { name: /status/i })).toBeInTheDocument();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("renders filter options in select", async () => {
|
|
102
|
+
const user = userEvent.setup();
|
|
103
|
+
render(<CphToolbar filters={defaultFilters} />);
|
|
104
|
+
const select = screen.getByRole("button", { name: /status/i });
|
|
105
|
+
await user.click(select);
|
|
106
|
+
expect(screen.getByRole("option", { name: "All" })).toBeInTheDocument();
|
|
107
|
+
expect(screen.getByRole("option", { name: "Active" })).toBeInTheDocument();
|
|
108
|
+
expect(screen.getByRole("option", { name: "Inactive" })).toBeInTheDocument();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("calls filter onChange when option selected", async () => {
|
|
112
|
+
const user = userEvent.setup();
|
|
113
|
+
const filterChange = vi.fn();
|
|
114
|
+
const filters = [{ ...defaultFilter, onChange: filterChange }];
|
|
115
|
+
render(<CphToolbar filters={filters} />);
|
|
116
|
+
const select = screen.getByRole("button", { name: /status/i });
|
|
117
|
+
await user.click(select);
|
|
118
|
+
await user.click(screen.getByRole("option", { name: "Active" }));
|
|
119
|
+
expect(filterChange).toHaveBeenCalledWith("active");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("renders actions slot when provided", () => {
|
|
123
|
+
render(<CphToolbar actions={<button data-testid="custom-action">Custom</button>} />);
|
|
124
|
+
expect(screen.getByTestId("custom-action")).toBeInTheDocument();
|
|
125
|
+
expect(screen.getByTestId("custom-action")).toHaveTextContent("Custom");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("renders children when provided", () => {
|
|
129
|
+
render(
|
|
130
|
+
<CphToolbar>
|
|
131
|
+
<span data-testid="child-content">Child</span>
|
|
132
|
+
</CphToolbar>,
|
|
133
|
+
);
|
|
134
|
+
expect(screen.getByTestId("child-content")).toBeInTheDocument();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("forwards additional props to root div", () => {
|
|
138
|
+
render(<CphToolbar id="custom-toolbar" />);
|
|
139
|
+
expect(screen.getByTestId("toolbar")).toHaveAttribute("id", "custom-toolbar");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("does not overflow at 375px viewport width", () => {
|
|
143
|
+
const { container } = render(<CphToolbar filters={defaultFilters} />);
|
|
144
|
+
const toolbar = container.querySelector('[data-testid="toolbar"]') as HTMLElement;
|
|
145
|
+
expect(toolbar).not.toBeNull();
|
|
146
|
+
// The toolbar uses flex layout with min-width: 12rem (192px) on search field
|
|
147
|
+
// which fits within 375px. The flex container should not force overflow.
|
|
148
|
+
expect(toolbar!.scrollWidth).toBeLessThanOrEqual(375);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/** @packageDocumentation Table toolbar with search and filter controls. */
|
|
2
|
+
|
|
3
|
+
import { Search, Filter, ChevronDown, X } from "lucide-react";
|
|
4
|
+
import type { HTMLAttributes, ReactNode, Key } from "react";
|
|
5
|
+
import {
|
|
6
|
+
Button,
|
|
7
|
+
Input,
|
|
8
|
+
SearchField,
|
|
9
|
+
Select,
|
|
10
|
+
SelectValue,
|
|
11
|
+
ListBox,
|
|
12
|
+
ListBoxItem,
|
|
13
|
+
} from "react-aria-components";
|
|
14
|
+
|
|
15
|
+
import { composeClassName } from "../../utils/composeClassName";
|
|
16
|
+
|
|
17
|
+
export interface CphToolbarProps extends HTMLAttributes<HTMLDivElement> {
|
|
18
|
+
/** Search input value. */
|
|
19
|
+
searchValue?: string;
|
|
20
|
+
/** Callback when search value changes. */
|
|
21
|
+
onSearchChange?: (value: string) => void;
|
|
22
|
+
/** Callback when search is submitted. */
|
|
23
|
+
onSearchSubmit?: (value: string) => void;
|
|
24
|
+
/** Filter options for the filter selects. */
|
|
25
|
+
filters?: Array<{
|
|
26
|
+
key: string;
|
|
27
|
+
label: string;
|
|
28
|
+
options: Array<{ value: string; label: string }>;
|
|
29
|
+
value?: string;
|
|
30
|
+
onChange?: (value: string) => void;
|
|
31
|
+
}>;
|
|
32
|
+
/** Additional actions to render in the toolbar. */
|
|
33
|
+
actions?: ReactNode;
|
|
34
|
+
/** Placeholder for search input. */
|
|
35
|
+
searchPlaceholder?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type CphToolbarFilter = NonNullable<CphToolbarProps["filters"]>[number];
|
|
39
|
+
|
|
40
|
+
const EMPTY_FILTERS: NonNullable<CphToolbarProps["filters"]> = [];
|
|
41
|
+
|
|
42
|
+
function createFilterChangeHandler(
|
|
43
|
+
filterOnChange: CphToolbarFilter["onChange"],
|
|
44
|
+
): (value: Key | null) => void {
|
|
45
|
+
return (value) => {
|
|
46
|
+
if (typeof value === "string" && filterOnChange) {
|
|
47
|
+
filterOnChange(value);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function CphToolbar({
|
|
53
|
+
searchValue = "",
|
|
54
|
+
onSearchChange,
|
|
55
|
+
onSearchSubmit,
|
|
56
|
+
filters = EMPTY_FILTERS,
|
|
57
|
+
actions,
|
|
58
|
+
searchPlaceholder = "Search…",
|
|
59
|
+
className,
|
|
60
|
+
children,
|
|
61
|
+
...props
|
|
62
|
+
}: Readonly<CphToolbarProps>) {
|
|
63
|
+
const handleSearchChange = (value: string) => {
|
|
64
|
+
onSearchChange?.(value);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// For native elements, composeClassName may return a function (render-props callback).
|
|
68
|
+
// We need to resolve it to a string for native DOM elements.
|
|
69
|
+
const rootClassName = (() => {
|
|
70
|
+
const composed = composeClassName("cph-table__toolbar", className);
|
|
71
|
+
return typeof composed === "function" ? composed({ defaultClassName: undefined }) : composed;
|
|
72
|
+
})();
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div {...props} className={rootClassName} data-cph-table="toolbar" data-testid="toolbar">
|
|
76
|
+
<SearchField
|
|
77
|
+
aria-label="Search table"
|
|
78
|
+
value={searchValue}
|
|
79
|
+
onChange={handleSearchChange}
|
|
80
|
+
{...(onSearchSubmit ? { onSubmit: onSearchSubmit } : {})}
|
|
81
|
+
className={composeClassName("cph-table__search", undefined)}
|
|
82
|
+
>
|
|
83
|
+
<Search size={16} aria-hidden="true" />
|
|
84
|
+
<Input placeholder={searchPlaceholder} className="cph-table__search-input" />
|
|
85
|
+
{searchValue && (
|
|
86
|
+
<Button
|
|
87
|
+
className="cph-table__search-clear"
|
|
88
|
+
onPress={() => handleSearchChange("")}
|
|
89
|
+
aria-label="Clear search"
|
|
90
|
+
>
|
|
91
|
+
<X size={14} />
|
|
92
|
+
</Button>
|
|
93
|
+
)}
|
|
94
|
+
</SearchField>
|
|
95
|
+
|
|
96
|
+
{filters.length > 0 && (
|
|
97
|
+
<div className="cph-table__filter" role="group" aria-label="Table filters">
|
|
98
|
+
{filters.map((filter) => (
|
|
99
|
+
<Select
|
|
100
|
+
key={filter.key}
|
|
101
|
+
{...(filter.value !== undefined ? { selectedKey: filter.value } : {})}
|
|
102
|
+
aria-label={filter.label}
|
|
103
|
+
{...(filter.onChange
|
|
104
|
+
? { onSelectionChange: createFilterChangeHandler(filter.onChange) }
|
|
105
|
+
: {})}
|
|
106
|
+
className={composeClassName("cph-table__filter-select", undefined)}
|
|
107
|
+
>
|
|
108
|
+
<Button className="cph-table__filter-btn">
|
|
109
|
+
<Filter size={14} aria-hidden="true" />
|
|
110
|
+
<SelectValue>
|
|
111
|
+
{({ selectedText, defaultChildren }) =>
|
|
112
|
+
selectedText || defaultChildren || filter.label
|
|
113
|
+
}
|
|
114
|
+
</SelectValue>
|
|
115
|
+
<ChevronDown size={14} aria-hidden="true" />
|
|
116
|
+
</Button>
|
|
117
|
+
<ListBox>
|
|
118
|
+
{filter.options.map((option) => (
|
|
119
|
+
<ListBoxItem key={option.value} id={option.value} textValue={option.label}>
|
|
120
|
+
{option.label}
|
|
121
|
+
</ListBoxItem>
|
|
122
|
+
))}
|
|
123
|
+
</ListBox>
|
|
124
|
+
</Select>
|
|
125
|
+
))}
|
|
126
|
+
</div>
|
|
127
|
+
)}
|
|
128
|
+
|
|
129
|
+
{actions && (
|
|
130
|
+
<div className="cph-table__actions" data-cph-table="toolbar-actions">
|
|
131
|
+
{actions}
|
|
132
|
+
</div>
|
|
133
|
+
)}
|
|
134
|
+
|
|
135
|
+
{children}
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
}
|