@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,172 @@
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 { CphTableFilterMultiSelect, type CphTableFilterOption } from "./CphTableFilterMultiSelect";
7
+
8
+ const OPTIONS: CphTableFilterOption[] = [
9
+ { value: "active", label: "Active" },
10
+ { value: "pending", label: "Pending" },
11
+ { value: "failed", label: "Failed" },
12
+ ];
13
+
14
+ const EMPTY_SELECTION: string[] = [];
15
+
16
+ function ControlledMultiSelect({
17
+ initial = EMPTY_SELECTION,
18
+ onValueChange,
19
+ clearable,
20
+ label,
21
+ placeholder,
22
+ }: {
23
+ initial?: string[];
24
+ onValueChange?: (value: string[]) => void;
25
+ clearable?: boolean;
26
+ label?: string;
27
+ placeholder?: string;
28
+ }) {
29
+ const [value, setValue] = useState<string[]>(initial);
30
+ return (
31
+ <CphTableFilterMultiSelect
32
+ value={value}
33
+ onValueChange={(next) => {
34
+ setValue(next);
35
+ onValueChange?.(next);
36
+ }}
37
+ options={OPTIONS}
38
+ {...(label === undefined ? {} : { label })}
39
+ {...(placeholder === undefined ? {} : { placeholder })}
40
+ {...(clearable === undefined ? {} : { clearable })}
41
+ />
42
+ );
43
+ }
44
+
45
+ describe("CphTableFilterMultiSelect", () => {
46
+ it("renders a trigger with the default aria-label and placeholder", () => {
47
+ render(<ControlledMultiSelect />);
48
+ const trigger = screen.getByRole("button", { name: "Filter by ..." });
49
+ expect(trigger).toHaveClass("cph-table__filter-multi__trigger");
50
+ expect(trigger).toHaveTextContent("All");
51
+ });
52
+
53
+ it("uses the provided label as the trigger aria-label", () => {
54
+ render(<ControlledMultiSelect label="Status" />);
55
+ expect(screen.getByRole("button", { name: "Status" })).toBeInTheDocument();
56
+ });
57
+
58
+ it("opens a listbox with checkbox-style options", async () => {
59
+ const user = userEvent.setup();
60
+ render(<ControlledMultiSelect />);
61
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
62
+ const listbox = screen.getByRole("listbox", { name: "Filter by ..." });
63
+ expect(listbox).toHaveAttribute("aria-multiselectable", "true");
64
+ expect(screen.getAllByRole("option")).toHaveLength(OPTIONS.length);
65
+ });
66
+
67
+ it("toggles options and reports the selection as string[]", async () => {
68
+ const user = userEvent.setup();
69
+ const onValueChange = vi.fn();
70
+ render(<ControlledMultiSelect onValueChange={onValueChange} />);
71
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
72
+ await user.click(screen.getByRole("option", { name: /Active/ }));
73
+ expect(onValueChange).toHaveBeenLastCalledWith(["active"]);
74
+ await user.click(screen.getByRole("option", { name: /Pending/ }));
75
+ expect(onValueChange).toHaveBeenLastCalledWith(["active", "pending"]);
76
+ await user.click(screen.getByRole("option", { name: /Active/ }));
77
+ expect(onValueChange).toHaveBeenLastCalledWith(["pending"]);
78
+ });
79
+
80
+ it("shows selected labels on the trigger", async () => {
81
+ const user = userEvent.setup();
82
+ render(<ControlledMultiSelect initial={["active", "failed"]} />);
83
+ expect(screen.getByRole("button", { name: "Filter by ..." })).toHaveTextContent(
84
+ "Active, Failed",
85
+ );
86
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
87
+ expect(screen.getByRole("option", { name: /Active/ })).toHaveAttribute("aria-selected", "true");
88
+ });
89
+
90
+ it("collapses 3+ selections into a count", () => {
91
+ render(<ControlledMultiSelect initial={["active", "pending", "failed"]} />);
92
+ expect(screen.getByRole("button", { name: "Filter by ..." })).toHaveTextContent("3 selected");
93
+ });
94
+
95
+ it("renders a custom placeholder when nothing is selected", () => {
96
+ render(<ControlledMultiSelect placeholder="Any status" />);
97
+ expect(screen.getByRole("button", { name: "Filter by ..." })).toHaveTextContent("Any status");
98
+ });
99
+
100
+ it("clears the selection via the clear button", async () => {
101
+ const user = userEvent.setup();
102
+ const onValueChange = vi.fn();
103
+ render(<ControlledMultiSelect initial={["active"]} clearable onValueChange={onValueChange} />);
104
+ await user.click(screen.getByRole("button", { name: "Clear Filter by ... filter" }));
105
+ expect(onValueChange).toHaveBeenLastCalledWith([]);
106
+ expect(screen.getByRole("button", { name: "Filter by ..." })).toHaveTextContent("All");
107
+ });
108
+
109
+ it("hides the clear button when clearable is absent or selection empty", () => {
110
+ const first = render(<ControlledMultiSelect initial={["active"]} />);
111
+ expect(screen.queryByTestId("filter-multi-clear")).not.toBeInTheDocument();
112
+ first.unmount();
113
+ render(<ControlledMultiSelect clearable />);
114
+ expect(screen.queryByTestId("filter-multi-clear")).not.toBeInTheDocument();
115
+ });
116
+
117
+ it("merges className onto the root", () => {
118
+ render(<ControlledMultiSelect />);
119
+ // Root wrapper carries the composed class even without extra input.
120
+ const trigger = screen.getByRole("button", { name: "Filter by ..." });
121
+ expect(trigger.closest(".cph-table__filter-multi")).toBeInTheDocument();
122
+ });
123
+
124
+ it("renders an empty listbox when options are empty", async () => {
125
+ const user = userEvent.setup();
126
+ render(<CphTableFilterMultiSelect value={[]} options={[]} label="Status" />);
127
+ await user.click(screen.getByRole("button", { name: "Status" }));
128
+ expect(screen.getByRole("listbox", { name: "Status" })).toBeInTheDocument();
129
+ expect(screen.queryByRole("option")).not.toBeInTheDocument();
130
+ });
131
+
132
+ it("marks unselected options with aria-selected=false", async () => {
133
+ const user = userEvent.setup();
134
+ render(<ControlledMultiSelect initial={["active"]} />);
135
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
136
+ expect(screen.getByRole("option", { name: /Active/ })).toHaveAttribute("aria-selected", "true");
137
+ expect(screen.getByRole("option", { name: /Pending/ })).toHaveAttribute(
138
+ "aria-selected",
139
+ "false",
140
+ );
141
+ });
142
+
143
+ it("keeps the selection across popover reopen (controlled value)", async () => {
144
+ const user = userEvent.setup();
145
+ render(<ControlledMultiSelect initial={["failed"]} />);
146
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
147
+ expect(screen.getByRole("option", { name: /Failed/ })).toHaveAttribute("aria-selected", "true");
148
+ await user.keyboard("{Escape}");
149
+ await user.click(screen.getByRole("button", { name: "Filter by ..." }));
150
+ expect(screen.getByRole("option", { name: /Failed/ })).toHaveAttribute("aria-selected", "true");
151
+ });
152
+
153
+ it("uses the provided label in the clear button aria-label", async () => {
154
+ const user = userEvent.setup();
155
+ const onValueChange = vi.fn();
156
+ render(
157
+ <ControlledMultiSelect
158
+ initial={["active"]}
159
+ label="Status"
160
+ clearable
161
+ onValueChange={onValueChange}
162
+ />,
163
+ );
164
+ await user.click(screen.getByRole("button", { name: "Clear Status filter" }));
165
+ expect(onValueChange).toHaveBeenLastCalledWith([]);
166
+ });
167
+
168
+ it("ignores unknown value keys when composing trigger text", () => {
169
+ render(<ControlledMultiSelect initial={["ghost"]} />);
170
+ expect(screen.getByRole("button", { name: "Filter by ..." })).toHaveTextContent("All");
171
+ });
172
+ });
@@ -0,0 +1,132 @@
1
+ import { Check, ChevronDown, X } from "lucide-react";
2
+ import type { ReactElement } from "react";
3
+ import {
4
+ Button,
5
+ DialogTrigger,
6
+ ListBox,
7
+ ListBoxItem,
8
+ Popover,
9
+ type Selection,
10
+ } from "react-aria-components";
11
+
12
+ export interface CphTableFilterOption {
13
+ value: string;
14
+ label: string;
15
+ }
16
+
17
+ export interface CphTableFilterMultiSelectProps {
18
+ /** Currently selected values (controlled). */
19
+ value: string[];
20
+ /** Called when the selection changes. */
21
+ onValueChange?: (value: string[]) => void;
22
+ /** Options rendered in the popover listbox. */
23
+ options: CphTableFilterOption[];
24
+ /** Visible label; also used to derive `aria-label` when the trigger needs one. */
25
+ label?: string;
26
+ /** Trigger text when nothing is selected. */
27
+ placeholder?: string;
28
+ /** Render a clear button that resets the selection to `[]`. */
29
+ clearable?: boolean;
30
+ className?: string;
31
+ }
32
+
33
+ function selectionToStrings(selection: Selection, options: CphTableFilterOption[]): string[] {
34
+ if (selection === "all") {
35
+ return options.map((option) => option.value);
36
+ }
37
+ return [...selection].map(String);
38
+ }
39
+
40
+ /**
41
+ * Multi-select filter control: a trigger button opening a popover listbox with
42
+ * checkbox-style items (`selectionMode="multiple"`).
43
+ *
44
+ * Selection state lives entirely in the controlled `value` prop; the listbox
45
+ * reports changes through `onValueChange` as a plain `string[]`.
46
+ */
47
+ export function CphTableFilterMultiSelect({
48
+ value,
49
+ onValueChange,
50
+ options,
51
+ label,
52
+ placeholder = "All",
53
+ clearable,
54
+ className = "",
55
+ }: Readonly<CphTableFilterMultiSelectProps>): ReactElement {
56
+ const ariaLabel = label ?? "Filter by ...";
57
+ const selectedLabels = options
58
+ .filter((option) => value.includes(option.value))
59
+ .map((option) => option.label);
60
+
61
+ const triggerText =
62
+ selectedLabels.length === 0
63
+ ? placeholder
64
+ : selectedLabels.length <= 2
65
+ ? selectedLabels.join(", ")
66
+ : `${selectedLabels.length} selected`;
67
+
68
+ const handleSelectionChange = (selection: Selection) => {
69
+ onValueChange?.(selectionToStrings(selection, options));
70
+ };
71
+
72
+ const handleClear = () => {
73
+ onValueChange?.([]);
74
+ };
75
+
76
+ return (
77
+ <div className={`cph-table__filter-multi ${className}`}>
78
+ <DialogTrigger>
79
+ <Button
80
+ className="cph-table__filter-multi__trigger"
81
+ aria-label={ariaLabel}
82
+ data-testid="filter-multi-trigger"
83
+ >
84
+ <span className="cph-table__filter-multi__trigger-text">{triggerText}</span>
85
+ <ChevronDown size={14} aria-hidden="true" />
86
+ </Button>
87
+ <Popover className="cph-table__filter-multi__popover">
88
+ <ListBox
89
+ aria-label={ariaLabel}
90
+ selectionMode="multiple"
91
+ selectedKeys={value}
92
+ onSelectionChange={handleSelectionChange}
93
+ className="cph-table__filter-multi__list"
94
+ >
95
+ {options.map((option) => (
96
+ <ListBoxItem
97
+ key={option.value}
98
+ id={option.value}
99
+ textValue={option.label}
100
+ className="cph-table__filter-multi__option"
101
+ >
102
+ {({ isSelected }) => (
103
+ <>
104
+ <span
105
+ className={`cph-table__filter-multi__checkbox${
106
+ isSelected ? " cph-table__filter-multi__checkbox--checked" : ""
107
+ }`}
108
+ aria-hidden="true"
109
+ >
110
+ {isSelected && <Check size={12} />}
111
+ </span>
112
+ <span className="cph-table__filter-multi__option-label">{option.label}</span>
113
+ </>
114
+ )}
115
+ </ListBoxItem>
116
+ ))}
117
+ </ListBox>
118
+ </Popover>
119
+ </DialogTrigger>
120
+ {clearable && value.length > 0 && (
121
+ <Button
122
+ className="cph-table__filter-multi__clear"
123
+ aria-label={`Clear ${ariaLabel} filter`}
124
+ onPress={handleClear}
125
+ data-testid="filter-multi-clear"
126
+ >
127
+ <X size={14} aria-hidden="true" />
128
+ </Button>
129
+ )}
130
+ </div>
131
+ );
132
+ }
@@ -0,0 +1,154 @@
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 { CphTableFilterRange, type CphTableFilterRangeValue } from "./CphTableFilterRange";
7
+
8
+ function ControlledRange({
9
+ initial,
10
+ onValueChange,
11
+ label,
12
+ min,
13
+ max,
14
+ step,
15
+ placeholder,
16
+ ariaLabel,
17
+ }: {
18
+ initial?: CphTableFilterRangeValue;
19
+ onValueChange?: (value: CphTableFilterRangeValue) => void;
20
+ label?: string;
21
+ min?: number;
22
+ max?: number;
23
+ step?: number;
24
+ placeholder?: { min?: string; max?: string };
25
+ ariaLabel?: string;
26
+ }) {
27
+ const [value, setValue] = useState<CphTableFilterRangeValue>(initial ?? {});
28
+ return (
29
+ <CphTableFilterRange
30
+ value={value}
31
+ onValueChange={(next) => {
32
+ setValue(next);
33
+ onValueChange?.(next);
34
+ }}
35
+ {...(label === undefined ? {} : { label })}
36
+ {...(min === undefined ? {} : { min })}
37
+ {...(max === undefined ? {} : { max })}
38
+ {...(step === undefined ? {} : { step })}
39
+ {...(placeholder === undefined ? {} : { placeholder })}
40
+ {...(ariaLabel === undefined ? {} : { "aria-label": ariaLabel })}
41
+ />
42
+ );
43
+ }
44
+
45
+ describe("CphTableFilterRange", () => {
46
+ it("renders two spinbutton inputs inside a labelled group", () => {
47
+ render(<ControlledRange label="Value" />);
48
+ expect(screen.getByRole("group", { name: "Value" })).toHaveClass("cph-table__filter-range");
49
+ expect(screen.getByRole("spinbutton", { name: "Value min" })).toBeInTheDocument();
50
+ expect(screen.getByRole("spinbutton", { name: "Value max" })).toBeInTheDocument();
51
+ });
52
+
53
+ it("falls back to a default accessible name without label or aria-label", () => {
54
+ render(<CphTableFilterRange />);
55
+ expect(screen.getByRole("group", { name: "Filter by range" })).toBeInTheDocument();
56
+ expect(screen.getByRole("spinbutton", { name: "Filter by range min" })).toBeInTheDocument();
57
+ });
58
+
59
+ it("prefers aria-label over label for the group name", () => {
60
+ render(<CphTableFilterRange label="Value" aria-label="Score bounds" />);
61
+ expect(screen.getByRole("group", { name: "Score bounds" })).toBeInTheDocument();
62
+ });
63
+
64
+ it("emits a partial value when only min is typed", async () => {
65
+ const user = userEvent.setup();
66
+ const onValueChange = vi.fn();
67
+ render(<ControlledRange onValueChange={onValueChange} />);
68
+ await user.type(screen.getByRole("spinbutton", { name: "Filter by range min" }), "10");
69
+ expect(onValueChange).toHaveBeenLastCalledWith({ min: 10 });
70
+ });
71
+
72
+ it("emits a partial value when only max is typed", async () => {
73
+ const user = userEvent.setup();
74
+ const onValueChange = vi.fn();
75
+ render(<ControlledRange onValueChange={onValueChange} />);
76
+ await user.type(screen.getByRole("spinbutton", { name: "Filter by range max" }), "100");
77
+ expect(onValueChange).toHaveBeenLastCalledWith({ max: 100 });
78
+ });
79
+
80
+ it("keeps the other bound when one side changes", async () => {
81
+ const user = userEvent.setup();
82
+ render(<ControlledRange initial={{ min: 5, max: 50 }} />);
83
+ const minInput = screen.getByRole("spinbutton", { name: "Filter by range min" });
84
+ expect(minInput).toHaveValue(5);
85
+ await user.clear(minInput);
86
+ await user.type(minInput, "7");
87
+ expect(minInput).toHaveValue(7);
88
+ expect(screen.getByRole("spinbutton", { name: "Filter by range max" })).toHaveValue(50);
89
+ });
90
+
91
+ it("omits the bound key when the input is cleared", async () => {
92
+ const user = userEvent.setup();
93
+ const onValueChange = vi.fn();
94
+ render(<ControlledRange initial={{ min: 5, max: 50 }} onValueChange={onValueChange} />);
95
+ await user.clear(screen.getByRole("spinbutton", { name: "Filter by range min" }));
96
+ expect(onValueChange).toHaveBeenLastCalledWith({ max: 50 });
97
+ });
98
+
99
+ it("applies min/max/step attributes to both inputs", () => {
100
+ render(<ControlledRange min={0} max={100} step={5} />);
101
+ for (const name of ["Filter by range min", "Filter by range max"]) {
102
+ const input = screen.getByRole("spinbutton", { name });
103
+ expect(input).toHaveAttribute("min", "0");
104
+ expect(input).toHaveAttribute("max", "100");
105
+ expect(input).toHaveAttribute("step", "5");
106
+ }
107
+ });
108
+
109
+ it("defaults step to 1", () => {
110
+ render(<ControlledRange />);
111
+ expect(screen.getByRole("spinbutton", { name: "Filter by range min" })).toHaveAttribute(
112
+ "step",
113
+ "1",
114
+ );
115
+ });
116
+
117
+ it("renders per-side placeholders", () => {
118
+ render(<ControlledRange placeholder={{ min: "from", max: "to" }} />);
119
+ expect(screen.getByPlaceholderText("from")).toBeInTheDocument();
120
+ expect(screen.getByPlaceholderText("to")).toBeInTheDocument();
121
+ });
122
+
123
+ it("renders the visible label element when provided", () => {
124
+ render(<ControlledRange label="Value" />);
125
+ expect(screen.getByText("Value")).toBeInTheDocument();
126
+ });
127
+
128
+ it("merges className onto the root", () => {
129
+ render(<CphTableFilterRange className="mt-4" />);
130
+ expect(screen.getByTestId("filter-range")).toHaveClass("cph-table__filter-range", "mt-4");
131
+ });
132
+
133
+ it("emits combined bounds when both sides are typed", async () => {
134
+ const user = userEvent.setup();
135
+ const onValueChange = vi.fn();
136
+ render(<ControlledRange onValueChange={onValueChange} />);
137
+ await user.type(screen.getByRole("spinbutton", { name: "Filter by range min" }), "10");
138
+ await user.type(screen.getByRole("spinbutton", { name: "Filter by range max" }), "90");
139
+ expect(onValueChange).toHaveBeenLastCalledWith({ min: 10, max: 90 });
140
+ });
141
+
142
+ it("renames the group and inputs when aria-label is provided", () => {
143
+ render(<CphTableFilterRange aria-label="Score bounds" />);
144
+ expect(screen.getByRole("group", { name: "Score bounds" })).toBeInTheDocument();
145
+ expect(screen.getByRole("spinbutton", { name: "Score bounds min" })).toBeInTheDocument();
146
+ expect(screen.getByRole("spinbutton", { name: "Score bounds max" })).toBeInTheDocument();
147
+ });
148
+
149
+ it("renders with no value prop at all", () => {
150
+ render(<CphTableFilterRange label="Value" />);
151
+ expect(screen.getByRole("spinbutton", { name: "Value min" })).toHaveValue(null);
152
+ expect(screen.getByRole("spinbutton", { name: "Value max" })).toHaveValue(null);
153
+ });
154
+ });
@@ -0,0 +1,109 @@
1
+ import type { ReactElement } from "react";
2
+ import { Input, Label } from "react-aria-components";
3
+
4
+ export interface CphTableFilterRangeValue {
5
+ min?: number;
6
+ max?: number;
7
+ }
8
+
9
+ export interface CphTableFilterRangeProps {
10
+ /** Controlled bounds (either side optional). */
11
+ value?: CphTableFilterRangeValue;
12
+ /** Called when either bound changes. Omitted keys mean "no bound". */
13
+ onValueChange?: (value: CphTableFilterRangeValue) => void;
14
+ /** Visible label; also the accessible name when no `aria-label` is given. */
15
+ label?: string;
16
+ /** Lower bound of the allowed range (native `min`). */
17
+ min?: number;
18
+ /** Upper bound of the allowed range (native `max`). */
19
+ max?: number;
20
+ /** Step increment (native `step`). Defaults to `1`. */
21
+ step?: number;
22
+ /** Placeholders for the min/max inputs. */
23
+ placeholder?: { min?: string; max?: string };
24
+ /** Overrides the accessible name derived from `label`. */
25
+ "aria-label"?: string;
26
+ className?: string;
27
+ }
28
+
29
+ function parseBound(raw: string): number | undefined {
30
+ if (raw.trim() === "") {
31
+ return undefined;
32
+ }
33
+ const parsed = Number(raw);
34
+ return Number.isNaN(parsed) ? undefined : parsed;
35
+ }
36
+
37
+ /**
38
+ * Numeric range filter: two spinbutton inputs (min / max) in a dense row.
39
+ *
40
+ * Both bounds are optional — an empty input means "no bound" and the key is
41
+ * omitted from the emitted value (never set to `undefined`, per
42
+ * `exactOptionalPropertyTypes`).
43
+ */
44
+ export function CphTableFilterRange({
45
+ value,
46
+ onValueChange,
47
+ label,
48
+ min,
49
+ max,
50
+ step = 1,
51
+ placeholder,
52
+ "aria-label": ariaLabel,
53
+ className = "",
54
+ }: Readonly<CphTableFilterRangeProps>): ReactElement {
55
+ const accessibleName = ariaLabel ?? label ?? "Filter by range";
56
+
57
+ const emit = (bound: "min" | "max", raw: string) => {
58
+ const next: CphTableFilterRangeValue = { ...value };
59
+ const parsed = parseBound(raw);
60
+ if (parsed === undefined) {
61
+ delete next[bound];
62
+ } else {
63
+ next[bound] = parsed;
64
+ }
65
+ onValueChange?.(next);
66
+ };
67
+
68
+ return (
69
+ <div
70
+ className={`cph-table__filter-range ${className}`}
71
+ role="group"
72
+ aria-label={accessibleName}
73
+ data-testid="filter-range"
74
+ >
75
+ {label && (
76
+ <Label className="cph-table__filter-range__label" data-testid="filter-range-label">
77
+ {label}
78
+ </Label>
79
+ )}
80
+ <Input
81
+ type="number"
82
+ className="cph-table__filter-range__input"
83
+ aria-label={label ? `${label} min` : `${accessibleName} min`}
84
+ data-testid="filter-range-min"
85
+ value={value?.min === undefined ? "" : String(value.min)}
86
+ onChange={(event) => emit("min", event.target.value)}
87
+ {...(min === undefined ? {} : { min })}
88
+ {...(max === undefined ? {} : { max })}
89
+ {...(step === undefined ? {} : { step })}
90
+ {...(placeholder?.min === undefined ? {} : { placeholder: placeholder.min })}
91
+ />
92
+ <span className="cph-table__filter-range__sep" aria-hidden="true">
93
+ –
94
+ </span>
95
+ <Input
96
+ type="number"
97
+ className="cph-table__filter-range__input"
98
+ aria-label={label ? `${label} max` : `${accessibleName} max`}
99
+ data-testid="filter-range-max"
100
+ value={value?.max === undefined ? "" : String(value.max)}
101
+ onChange={(event) => emit("max", event.target.value)}
102
+ {...(min === undefined ? {} : { min })}
103
+ {...(max === undefined ? {} : { max })}
104
+ {...(step === undefined ? {} : { step })}
105
+ {...(placeholder?.max === undefined ? {} : { placeholder: placeholder.max })}
106
+ />
107
+ </div>
108
+ );
109
+ }