@terreno/ui 56.0.0-beta.2 → 57.0.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/dist/Badge.d.ts +1 -1
- package/dist/Badge.js +37 -1
- package/dist/Badge.js.map +1 -1
- package/dist/Common.d.ts +110 -3
- package/dist/Common.js.map +1 -1
- package/dist/Filter.d.ts +13 -0
- package/dist/Filter.js +120 -0
- package/dist/Filter.js.map +1 -0
- package/dist/FilterAccordion.d.ts +8 -0
- package/dist/FilterAccordion.js +59 -0
- package/dist/FilterAccordion.js.map +1 -0
- package/dist/FilterBoolean.d.ts +8 -0
- package/dist/FilterBoolean.js +74 -0
- package/dist/FilterBoolean.js.map +1 -0
- package/dist/FilterChangesBadge.d.ts +8 -0
- package/dist/FilterChangesBadge.js +11 -0
- package/dist/FilterChangesBadge.js.map +1 -0
- package/dist/FilterSelectMenu.d.ts +8 -0
- package/dist/FilterSelectMenu.js +37 -0
- package/dist/FilterSelectMenu.js.map +1 -0
- package/dist/IconRegistry.js +11 -6
- package/dist/IconRegistry.js.map +1 -1
- package/dist/PickerSelect.d.ts +8 -1
- package/dist/PickerSelect.js +2 -2
- package/dist/PickerSelect.js.map +1 -1
- package/dist/SelectField.js +2 -2
- package/dist/SelectField.js.map +1 -1
- package/dist/TapToEdit.js +2 -5
- package/dist/TapToEdit.js.map +1 -1
- package/dist/Theme.js +2 -0
- package/dist/Theme.js.map +1 -1
- package/dist/WebDropdownMenu.d.ts +5 -1
- package/dist/WebDropdownMenu.js +24 -10
- package/dist/WebDropdownMenu.js.map +1 -1
- package/dist/icons/BarsFilterIcon.d.ts +9 -0
- package/dist/icons/BarsFilterIcon.js +12 -0
- package/dist/icons/BarsFilterIcon.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/package.json +23 -23
- package/src/Badge.test.tsx +22 -1
- package/src/Badge.tsx +43 -0
- package/src/Common.ts +120 -5
- package/src/DateTimeField.test.tsx +16 -18
- package/src/Filter.test.tsx +94 -0
- package/src/Filter.tsx +257 -0
- package/src/FilterAccordion.test.tsx +58 -0
- package/src/FilterAccordion.tsx +113 -0
- package/src/FilterBoolean.test.tsx +65 -0
- package/src/FilterBoolean.tsx +130 -0
- package/src/FilterChangesBadge.test.tsx +16 -0
- package/src/FilterChangesBadge.tsx +13 -0
- package/src/FilterSelectMenu.test.tsx +56 -0
- package/src/FilterSelectMenu.tsx +76 -0
- package/src/IconRegistry.test.tsx +6 -4
- package/src/IconRegistry.tsx +13 -8
- package/src/PickerSelect.test.tsx +20 -0
- package/src/PickerSelect.tsx +10 -0
- package/src/SelectField.tsx +2 -0
- package/src/TapToEdit.tsx +6 -5
- package/src/Theme.tsx +2 -0
- package/src/WebDropdownMenu.test.tsx +86 -1
- package/src/WebDropdownMenu.tsx +43 -21
- package/src/__snapshots__/Filter.test.tsx.snap +161 -0
- package/src/__snapshots__/FilterAccordion.test.tsx.snap +83 -0
- package/src/__snapshots__/FilterBoolean.test.tsx.snap +109 -0
- package/src/__snapshots__/FilterChangesBadge.test.tsx.snap +18 -0
- package/src/__snapshots__/FilterSelectMenu.test.tsx.snap +359 -0
- package/src/conflictFieldDiff.test.ts +43 -0
- package/src/icons/BarsFilterIcon.tsx +18 -0
- package/src/index.tsx +6 -0
package/src/Filter.tsx
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import {type FC, type ReactNode, useEffect, useState} from "react";
|
|
2
|
+
import {Platform, Pressable, View, type ViewStyle} from "react-native";
|
|
3
|
+
|
|
4
|
+
import {Button} from "./Button";
|
|
5
|
+
import type {FilterProps} from "./Common";
|
|
6
|
+
import {createWebPortal} from "./createWebPortal";
|
|
7
|
+
import {Text} from "./Text";
|
|
8
|
+
import {useTheme} from "./Theme";
|
|
9
|
+
import {resolveTestID} from "./testing/resolveTestId";
|
|
10
|
+
import {useWebDropdownAnchor} from "./WebDropdownMenu";
|
|
11
|
+
|
|
12
|
+
const DEFAULT_WIDTH = 320;
|
|
13
|
+
const TRIGGER_OFFSET = 44;
|
|
14
|
+
const PANEL_GAP = 4;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Compositional filter dropdown. Renders a trigger button that opens a panel
|
|
18
|
+
* containing composed filter controls (`FilterSelectMenu`, `FilterBoolean`,
|
|
19
|
+
* `FilterAccordion`, or any custom content) plus an optional Apply / Clear /
|
|
20
|
+
* Cancel footer. Clicking outside the panel closes it. Desktop web only.
|
|
21
|
+
*
|
|
22
|
+
* On web the panel is rendered in a portal on `document.body` and anchored to
|
|
23
|
+
* the trigger, so it floats above all page content instead of being trapped in
|
|
24
|
+
* a React Native Web stacking context (which would let content bleed through).
|
|
25
|
+
*/
|
|
26
|
+
export const Filter: FC<FilterProps> = ({
|
|
27
|
+
children,
|
|
28
|
+
label = "Filter",
|
|
29
|
+
iconName = "bars-filter",
|
|
30
|
+
isOpen,
|
|
31
|
+
defaultOpen = false,
|
|
32
|
+
onOpenChange,
|
|
33
|
+
showActionButtons = true,
|
|
34
|
+
showApplyButton = true,
|
|
35
|
+
showClearButton = true,
|
|
36
|
+
showCancelButton = true,
|
|
37
|
+
applyButtonText = "Apply",
|
|
38
|
+
clearButtonText = "Clear",
|
|
39
|
+
cancelButtonText = "Cancel",
|
|
40
|
+
onApply,
|
|
41
|
+
onClear,
|
|
42
|
+
onCancel,
|
|
43
|
+
variant = "primary",
|
|
44
|
+
width = DEFAULT_WIDTH,
|
|
45
|
+
testID,
|
|
46
|
+
}) => {
|
|
47
|
+
const {theme} = useTheme();
|
|
48
|
+
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
49
|
+
const [anchorReady, setAnchorReady] = useState(false);
|
|
50
|
+
const {anchor, cancelPendingMeasurement, measure, triggerRef} = useWebDropdownAnchor();
|
|
51
|
+
|
|
52
|
+
const isControlled = isOpen !== undefined;
|
|
53
|
+
const open = isControlled ? isOpen : internalOpen;
|
|
54
|
+
|
|
55
|
+
// Measure-then-show: measure the trigger when the panel opens and only reveal
|
|
56
|
+
// the portaled panel once the anchor is known, so it never flashes at the
|
|
57
|
+
// default (0, 0) position before jumping into place.
|
|
58
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: only re-measure on open transitions; `measure` is recreated each render.
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
setAnchorReady(false);
|
|
61
|
+
if (!open) {
|
|
62
|
+
cancelPendingMeasurement();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
measure((): void => setAnchorReady(true));
|
|
67
|
+
return cancelPendingMeasurement;
|
|
68
|
+
}, [open]);
|
|
69
|
+
|
|
70
|
+
// Return focus to the trigger after any in-panel dismissal so keyboard users
|
|
71
|
+
// can continue from the control that opened the popup (web only).
|
|
72
|
+
const restoreTriggerFocus = (): void => {
|
|
73
|
+
if (Platform.OS !== "web" || !triggerRef.current) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const node = triggerRef.current as unknown as {
|
|
77
|
+
querySelector?: (selector: string) => {focus?: () => void} | null;
|
|
78
|
+
};
|
|
79
|
+
node.querySelector?.('[role="button"]')?.focus?.();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const setOpen = (next: boolean): void => {
|
|
83
|
+
if (!isControlled) {
|
|
84
|
+
setInternalOpen(next);
|
|
85
|
+
}
|
|
86
|
+
onOpenChange?.(next);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const handleApply = (): void => {
|
|
90
|
+
onApply?.();
|
|
91
|
+
setOpen(false);
|
|
92
|
+
restoreTriggerFocus();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const handleClear = (): void => {
|
|
96
|
+
// Clearing resets the filter values but keeps the panel open so the user can
|
|
97
|
+
// immediately pick new filters without reopening it.
|
|
98
|
+
onClear?.();
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const handleCancel = (): void => {
|
|
102
|
+
onCancel?.();
|
|
103
|
+
setOpen(false);
|
|
104
|
+
restoreTriggerFocus();
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const showFooter = showActionButtons && (showApplyButton || showClearButton || showCancelButton);
|
|
108
|
+
|
|
109
|
+
const panelSurfaceStyle: ViewStyle = {
|
|
110
|
+
backgroundColor: theme.surface.base,
|
|
111
|
+
borderColor: theme.border.default,
|
|
112
|
+
borderRadius: theme.radius.default,
|
|
113
|
+
borderWidth: 1,
|
|
114
|
+
shadowColor: theme.primitives.neutral900,
|
|
115
|
+
shadowOffset: {height: 4, width: 0},
|
|
116
|
+
shadowOpacity: 0.15,
|
|
117
|
+
shadowRadius: 12,
|
|
118
|
+
width,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const panelBody: ReactNode = (
|
|
122
|
+
<>
|
|
123
|
+
<View style={{padding: theme.spacing.sm}}>{children}</View>
|
|
124
|
+
{showFooter && (
|
|
125
|
+
<View
|
|
126
|
+
style={{
|
|
127
|
+
alignItems: "center",
|
|
128
|
+
borderTopColor: theme.border.default,
|
|
129
|
+
borderTopWidth: 1,
|
|
130
|
+
flexDirection: "row",
|
|
131
|
+
justifyContent: "space-between",
|
|
132
|
+
padding: theme.spacing.md,
|
|
133
|
+
}}
|
|
134
|
+
>
|
|
135
|
+
{showClearButton ? (
|
|
136
|
+
<Pressable
|
|
137
|
+
onPress={handleClear}
|
|
138
|
+
testID={testID ? resolveTestID(testID, "clear") : undefined}
|
|
139
|
+
>
|
|
140
|
+
<Text color="link" underline>
|
|
141
|
+
{clearButtonText}
|
|
142
|
+
</Text>
|
|
143
|
+
</Pressable>
|
|
144
|
+
) : (
|
|
145
|
+
<View />
|
|
146
|
+
)}
|
|
147
|
+
<View
|
|
148
|
+
style={{
|
|
149
|
+
alignItems: "center",
|
|
150
|
+
flexDirection: "row",
|
|
151
|
+
gap: theme.spacing.sm,
|
|
152
|
+
justifyContent: "flex-end",
|
|
153
|
+
}}
|
|
154
|
+
>
|
|
155
|
+
{showCancelButton && (
|
|
156
|
+
<Button
|
|
157
|
+
onClick={handleCancel}
|
|
158
|
+
testID={testID ? resolveTestID(testID, "cancel") : undefined}
|
|
159
|
+
text={cancelButtonText}
|
|
160
|
+
variant="outline"
|
|
161
|
+
/>
|
|
162
|
+
)}
|
|
163
|
+
{showApplyButton && (
|
|
164
|
+
<Button
|
|
165
|
+
onClick={handleApply}
|
|
166
|
+
testID={testID ? resolveTestID(testID, "apply") : undefined}
|
|
167
|
+
text={applyButtonText}
|
|
168
|
+
variant={variant}
|
|
169
|
+
/>
|
|
170
|
+
)}
|
|
171
|
+
</View>
|
|
172
|
+
</View>
|
|
173
|
+
)}
|
|
174
|
+
</>
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const renderOverlay = (): ReactNode => {
|
|
178
|
+
// Web: portal to document.body with fixed positioning so the panel escapes
|
|
179
|
+
// every ancestor stacking context and floats above all page content.
|
|
180
|
+
if (Platform.OS === "web" && typeof document !== "undefined") {
|
|
181
|
+
// Wait for the trigger measurement so the panel appears already anchored.
|
|
182
|
+
if (!anchorReady) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const overlay = (
|
|
186
|
+
<View
|
|
187
|
+
// box-none lets clicks pass to the backdrop / panel but not the empty overlay.
|
|
188
|
+
pointerEvents="box-none"
|
|
189
|
+
style={{inset: 0, position: "fixed", zIndex: 9999} as unknown as ViewStyle}
|
|
190
|
+
>
|
|
191
|
+
<Pressable
|
|
192
|
+
aria-role="button"
|
|
193
|
+
onPress={handleCancel}
|
|
194
|
+
style={{inset: 0, position: "fixed", zIndex: 1} as unknown as ViewStyle}
|
|
195
|
+
testID={testID ? resolveTestID(testID, "backdrop") : undefined}
|
|
196
|
+
/>
|
|
197
|
+
<View
|
|
198
|
+
style={
|
|
199
|
+
{
|
|
200
|
+
...panelSurfaceStyle,
|
|
201
|
+
left: anchor.x,
|
|
202
|
+
position: "fixed",
|
|
203
|
+
top: anchor.y + anchor.height + PANEL_GAP,
|
|
204
|
+
zIndex: 2,
|
|
205
|
+
} as unknown as ViewStyle
|
|
206
|
+
}
|
|
207
|
+
testID={testID ? resolveTestID(testID, "panel") : undefined}
|
|
208
|
+
>
|
|
209
|
+
{panelBody}
|
|
210
|
+
</View>
|
|
211
|
+
</View>
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const target = document.body instanceof HTMLElement ? document.body : null;
|
|
215
|
+
return target ? createWebPortal({children: overlay, container: target}) : overlay;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Native fallback: anchored inline beneath the trigger.
|
|
219
|
+
return (
|
|
220
|
+
<>
|
|
221
|
+
<Pressable
|
|
222
|
+
aria-role="button"
|
|
223
|
+
onPress={handleCancel}
|
|
224
|
+
style={{bottom: 0, left: 0, position: "absolute", right: 0, top: 0, zIndex: 10}}
|
|
225
|
+
testID={testID ? resolveTestID(testID, "backdrop") : undefined}
|
|
226
|
+
/>
|
|
227
|
+
<View
|
|
228
|
+
style={{
|
|
229
|
+
...panelSurfaceStyle,
|
|
230
|
+
left: 0,
|
|
231
|
+
position: "absolute",
|
|
232
|
+
top: TRIGGER_OFFSET,
|
|
233
|
+
zIndex: 11,
|
|
234
|
+
}}
|
|
235
|
+
testID={testID ? resolveTestID(testID, "panel") : undefined}
|
|
236
|
+
>
|
|
237
|
+
{panelBody}
|
|
238
|
+
</View>
|
|
239
|
+
</>
|
|
240
|
+
);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<View style={{position: "relative"}} testID={testID}>
|
|
245
|
+
<View ref={triggerRef}>
|
|
246
|
+
<Button
|
|
247
|
+
iconName={iconName}
|
|
248
|
+
onClick={() => setOpen(!open)}
|
|
249
|
+
testID={testID ? resolveTestID(testID, "trigger") : undefined}
|
|
250
|
+
text={label}
|
|
251
|
+
variant={variant}
|
|
252
|
+
/>
|
|
253
|
+
</View>
|
|
254
|
+
{Boolean(open) && renderOverlay()}
|
|
255
|
+
</View>
|
|
256
|
+
);
|
|
257
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import {FilterAccordion} from "./FilterAccordion";
|
|
5
|
+
import {Text} from "./Text";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
describe("FilterAccordion", () => {
|
|
9
|
+
const defaultProps = {
|
|
10
|
+
title: "Status",
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
it("renders correctly with default props", () => {
|
|
14
|
+
const {toJSON} = renderWithTheme(<FilterAccordion {...defaultProps} />);
|
|
15
|
+
expect(toJSON()).toMatchSnapshot();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("renders the title", () => {
|
|
19
|
+
const {getByText} = renderWithTheme(<FilterAccordion {...defaultProps} />);
|
|
20
|
+
expect(getByText("Status")).toBeTruthy();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("hides content until expanded", () => {
|
|
24
|
+
const {queryByText, getByTestId} = renderWithTheme(
|
|
25
|
+
<FilterAccordion {...defaultProps} testID="acc">
|
|
26
|
+
<Text>Content</Text>
|
|
27
|
+
</FilterAccordion>
|
|
28
|
+
);
|
|
29
|
+
expect(queryByText("Content")).toBeNull();
|
|
30
|
+
fireEvent.press(getByTestId("acc.header"));
|
|
31
|
+
expect(queryByText("Content")).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("respects defaultExpanded", () => {
|
|
35
|
+
const {getByText} = renderWithTheme(
|
|
36
|
+
<FilterAccordion {...defaultProps} defaultExpanded>
|
|
37
|
+
<Text>Content</Text>
|
|
38
|
+
</FilterAccordion>
|
|
39
|
+
);
|
|
40
|
+
expect(getByText("Content")).toBeTruthy();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("exposes the expanded state to assistive technology", () => {
|
|
44
|
+
const {getByTestId} = renderWithTheme(
|
|
45
|
+
<FilterAccordion {...defaultProps} expanded testID="acc" />
|
|
46
|
+
);
|
|
47
|
+
expect(getByTestId("acc.header")).toHaveProp("accessibilityState", {expanded: true});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("calls onToggle with the next state when controlled", () => {
|
|
51
|
+
const onToggle = mock();
|
|
52
|
+
const {getByTestId} = renderWithTheme(
|
|
53
|
+
<FilterAccordion {...defaultProps} expanded={false} onToggle={onToggle} testID="acc" />
|
|
54
|
+
);
|
|
55
|
+
fireEvent.press(getByTestId("acc.header"));
|
|
56
|
+
expect(onToggle).toHaveBeenCalledWith(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {type FC, useState} from "react";
|
|
2
|
+
import {Pressable, View} from "react-native";
|
|
3
|
+
|
|
4
|
+
import type {FilterAccordionProps} from "./Common";
|
|
5
|
+
import {FilterChangesBadge} from "./FilterChangesBadge";
|
|
6
|
+
import {Icon} from "./Icon";
|
|
7
|
+
import {Text} from "./Text";
|
|
8
|
+
import {useTheme} from "./Theme";
|
|
9
|
+
import {resolveTestID} from "./testing/resolveTestId";
|
|
10
|
+
|
|
11
|
+
// react-native's Pressable does not type the web `onKeyDown` event, but React
|
|
12
|
+
// Native Web forwards it. A disclosure button needs its own spacebar handler
|
|
13
|
+
// (per ARIA, Space activates it) since role="button" only activates on Enter.
|
|
14
|
+
interface WebKeyDownEvent {
|
|
15
|
+
key: string;
|
|
16
|
+
preventDefault: () => void;
|
|
17
|
+
repeat?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface WebKeyDownProps {
|
|
21
|
+
onKeyDown?: (event: WebKeyDownEvent) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An expandable/collapsible filter row. The header (title, optional changes
|
|
26
|
+
* badge, and chevron) is the full click zone. When expanded, the row switches
|
|
27
|
+
* to the alternate surface and reveals `children`. Empty by default.
|
|
28
|
+
*/
|
|
29
|
+
export const FilterAccordion: FC<FilterAccordionProps> = ({
|
|
30
|
+
title,
|
|
31
|
+
children,
|
|
32
|
+
expanded,
|
|
33
|
+
defaultExpanded = false,
|
|
34
|
+
onToggle,
|
|
35
|
+
showChangesBadge = false,
|
|
36
|
+
testID,
|
|
37
|
+
}) => {
|
|
38
|
+
const {theme} = useTheme();
|
|
39
|
+
const [isHovered, setIsHovered] = useState(false);
|
|
40
|
+
const [internalExpanded, setInternalExpanded] = useState(defaultExpanded);
|
|
41
|
+
|
|
42
|
+
const isControlled = expanded !== undefined;
|
|
43
|
+
const isExpanded = isControlled ? expanded : internalExpanded;
|
|
44
|
+
|
|
45
|
+
const handleToggle = (): void => {
|
|
46
|
+
const next = !isExpanded;
|
|
47
|
+
if (!isControlled) {
|
|
48
|
+
setInternalExpanded(next);
|
|
49
|
+
}
|
|
50
|
+
onToggle?.(next);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
let backgroundColor = theme.surface.base;
|
|
54
|
+
if (isExpanded) {
|
|
55
|
+
backgroundColor = theme.surface.baseAlternate;
|
|
56
|
+
} else if (isHovered) {
|
|
57
|
+
backgroundColor = theme.surface.baseHover;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const webKeyDownProps: WebKeyDownProps = {
|
|
61
|
+
onKeyDown: (event) => {
|
|
62
|
+
if (event.key !== " " && event.key !== "Spacebar") {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
// A native button activates once per press, so ignore held-key auto-repeat.
|
|
67
|
+
if (event.repeat) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
handleToggle();
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<View
|
|
76
|
+
style={{
|
|
77
|
+
backgroundColor,
|
|
78
|
+
borderRadius: theme.radius.default,
|
|
79
|
+
gap: theme.spacing.md,
|
|
80
|
+
paddingHorizontal: theme.spacing.sm,
|
|
81
|
+
paddingVertical: theme.primitives.spacing3,
|
|
82
|
+
}}
|
|
83
|
+
testID={testID}
|
|
84
|
+
>
|
|
85
|
+
<Pressable
|
|
86
|
+
{...webKeyDownProps}
|
|
87
|
+
accessibilityRole="button"
|
|
88
|
+
accessibilityState={{expanded: isExpanded}}
|
|
89
|
+
aria-expanded={isExpanded}
|
|
90
|
+
onHoverIn={() => setIsHovered(true)}
|
|
91
|
+
onHoverOut={() => setIsHovered(false)}
|
|
92
|
+
onPress={handleToggle}
|
|
93
|
+
style={{
|
|
94
|
+
alignItems: "center",
|
|
95
|
+
flexDirection: "row",
|
|
96
|
+
justifyContent: "space-between",
|
|
97
|
+
}}
|
|
98
|
+
testID={testID ? resolveTestID(testID, "header") : undefined}
|
|
99
|
+
>
|
|
100
|
+
<View style={{alignItems: "center", flex: 1, flexDirection: "row", gap: 6}}>
|
|
101
|
+
<Text size="md">{title}</Text>
|
|
102
|
+
{showChangesBadge && (
|
|
103
|
+
<FilterChangesBadge testID={testID ? resolveTestID(testID, "badge") : undefined} />
|
|
104
|
+
)}
|
|
105
|
+
</View>
|
|
106
|
+
<Icon color="primary" iconName={isExpanded ? "chevron-up" : "chevron-down"} size="sm" />
|
|
107
|
+
</Pressable>
|
|
108
|
+
{Boolean(isExpanded && children) && (
|
|
109
|
+
<View testID={testID ? resolveTestID(testID, "content") : undefined}>{children}</View>
|
|
110
|
+
)}
|
|
111
|
+
</View>
|
|
112
|
+
);
|
|
113
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {fireEvent} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import {FilterBoolean} from "./FilterBoolean";
|
|
5
|
+
import {renderWithTheme} from "./test-utils";
|
|
6
|
+
|
|
7
|
+
describe("FilterBoolean", () => {
|
|
8
|
+
const defaultProps = {
|
|
9
|
+
onChange: () => {},
|
|
10
|
+
title: "Urgent only",
|
|
11
|
+
value: false,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
it("renders correctly with default props", () => {
|
|
15
|
+
const {toJSON} = renderWithTheme(<FilterBoolean {...defaultProps} />);
|
|
16
|
+
expect(toJSON()).toMatchSnapshot();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("renders the title", () => {
|
|
20
|
+
const {getByText} = renderWithTheme(<FilterBoolean {...defaultProps} />);
|
|
21
|
+
expect(getByText("Urgent only")).toBeTruthy();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("toggles when the entire row is pressed", () => {
|
|
25
|
+
const onChange = mock();
|
|
26
|
+
const {getByTestId} = renderWithTheme(
|
|
27
|
+
<FilterBoolean {...defaultProps} onChange={onChange} testID="toggle" value={false} />
|
|
28
|
+
);
|
|
29
|
+
fireEvent.press(getByTestId("toggle"));
|
|
30
|
+
expect(onChange).toHaveBeenCalledWith(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("does not toggle when disabled", () => {
|
|
34
|
+
const onChange = mock();
|
|
35
|
+
const {getByTestId} = renderWithTheme(
|
|
36
|
+
<FilterBoolean {...defaultProps} disabled onChange={onChange} testID="toggle" />
|
|
37
|
+
);
|
|
38
|
+
fireEvent.press(getByTestId("toggle"));
|
|
39
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("exposes the checked state to assistive technology", () => {
|
|
43
|
+
const on = renderWithTheme(<FilterBoolean {...defaultProps} testID="toggle" value />);
|
|
44
|
+
expect(on.getByTestId("toggle")).toHaveProp("accessibilityState", {
|
|
45
|
+
checked: true,
|
|
46
|
+
disabled: false,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const off = renderWithTheme(<FilterBoolean {...defaultProps} testID="toggle" value={false} />);
|
|
50
|
+
expect(off.getByTestId("toggle")).toHaveProp("accessibilityState", {
|
|
51
|
+
checked: false,
|
|
52
|
+
disabled: false,
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("shows the changes badge only when enabled", () => {
|
|
57
|
+
const {queryByTestId, rerender} = renderWithTheme(
|
|
58
|
+
<FilterBoolean {...defaultProps} testID="toggle" />
|
|
59
|
+
);
|
|
60
|
+
expect(queryByTestId("toggle.badge")).toBeNull();
|
|
61
|
+
|
|
62
|
+
rerender(<FilterBoolean {...defaultProps} showChangesBadge testID="toggle" />);
|
|
63
|
+
expect(queryByTestId("toggle.badge")).toBeTruthy();
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import {type FC, useState} from "react";
|
|
2
|
+
import {Pressable, View} from "react-native";
|
|
3
|
+
|
|
4
|
+
import type {FilterBooleanProps} from "./Common";
|
|
5
|
+
import {FilterChangesBadge} from "./FilterChangesBadge";
|
|
6
|
+
import {Text} from "./Text";
|
|
7
|
+
import {useTheme} from "./Theme";
|
|
8
|
+
import {resolveTestID} from "./testing/resolveTestId";
|
|
9
|
+
|
|
10
|
+
// react-native's Pressable does not type the web `onKeyDown` event, but React
|
|
11
|
+
// Native Web forwards it. A switch needs its own spacebar handler (per ARIA,
|
|
12
|
+
// Space toggles it) since role="switch" only activates on Enter on the web.
|
|
13
|
+
interface WebKeyDownEvent {
|
|
14
|
+
key: string;
|
|
15
|
+
preventDefault: () => void;
|
|
16
|
+
repeat?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface WebKeyDownProps {
|
|
20
|
+
onKeyDown?: (event: WebKeyDownEvent) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const SWITCH_WIDTH = 36;
|
|
24
|
+
const SWITCH_HEIGHT = 20;
|
|
25
|
+
const KNOB_SIZE = 16;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A boolean filter row: a title on the left and a toggle switch on the right.
|
|
29
|
+
* The entire row is the click zone, matching the design spec. Hovering tints
|
|
30
|
+
* the row with the `surface-base-hover` token.
|
|
31
|
+
*/
|
|
32
|
+
export const FilterBoolean: FC<FilterBooleanProps> = ({
|
|
33
|
+
title,
|
|
34
|
+
value,
|
|
35
|
+
onChange,
|
|
36
|
+
showChangesBadge = false,
|
|
37
|
+
disabled = false,
|
|
38
|
+
focused = false,
|
|
39
|
+
testID,
|
|
40
|
+
}) => {
|
|
41
|
+
const {theme} = useTheme();
|
|
42
|
+
const [isHovered, setIsHovered] = useState(false);
|
|
43
|
+
|
|
44
|
+
const trackColor = disabled
|
|
45
|
+
? theme.surface.disabled
|
|
46
|
+
: value
|
|
47
|
+
? theme.surface.secondaryDark
|
|
48
|
+
: theme.surface.base;
|
|
49
|
+
const borderColor = focused
|
|
50
|
+
? theme.border.focus
|
|
51
|
+
: disabled
|
|
52
|
+
? theme.surface.disabled
|
|
53
|
+
: theme.surface.secondaryDark;
|
|
54
|
+
|
|
55
|
+
const handlePress = (): void => {
|
|
56
|
+
if (!disabled) {
|
|
57
|
+
onChange(!value);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const webKeyDownProps: WebKeyDownProps = {
|
|
62
|
+
onKeyDown: (event) => {
|
|
63
|
+
if (event.key !== " " && event.key !== "Spacebar") {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
// A native switch activates once per press, so ignore held-key auto-repeat.
|
|
68
|
+
if (event.repeat) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
handlePress();
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<Pressable
|
|
77
|
+
{...webKeyDownProps}
|
|
78
|
+
accessibilityRole="switch"
|
|
79
|
+
accessibilityState={{checked: value, disabled}}
|
|
80
|
+
aria-checked={value}
|
|
81
|
+
aria-disabled={disabled}
|
|
82
|
+
disabled={disabled}
|
|
83
|
+
onHoverIn={() => setIsHovered(true)}
|
|
84
|
+
onHoverOut={() => setIsHovered(false)}
|
|
85
|
+
onPress={handlePress}
|
|
86
|
+
style={{
|
|
87
|
+
alignItems: "center",
|
|
88
|
+
backgroundColor: isHovered && !disabled ? theme.surface.baseHover : theme.surface.base,
|
|
89
|
+
borderRadius: theme.radius.default,
|
|
90
|
+
flexDirection: "row",
|
|
91
|
+
justifyContent: "space-between",
|
|
92
|
+
paddingHorizontal: theme.spacing.sm,
|
|
93
|
+
paddingVertical: theme.primitives.spacing3,
|
|
94
|
+
}}
|
|
95
|
+
testID={testID}
|
|
96
|
+
>
|
|
97
|
+
<View style={{alignItems: "center", flexDirection: "row", gap: 6}}>
|
|
98
|
+
<Text size="md">{title}</Text>
|
|
99
|
+
{showChangesBadge && (
|
|
100
|
+
<FilterChangesBadge testID={testID ? resolveTestID(testID, "badge") : undefined} />
|
|
101
|
+
)}
|
|
102
|
+
</View>
|
|
103
|
+
<View
|
|
104
|
+
style={{
|
|
105
|
+
alignItems: value ? "flex-end" : "flex-start",
|
|
106
|
+
backgroundColor: trackColor,
|
|
107
|
+
borderColor,
|
|
108
|
+
borderRadius: SWITCH_HEIGHT,
|
|
109
|
+
borderWidth: focused ? 2 : 1,
|
|
110
|
+
height: SWITCH_HEIGHT,
|
|
111
|
+
justifyContent: "center",
|
|
112
|
+
paddingHorizontal: 1,
|
|
113
|
+
width: SWITCH_WIDTH,
|
|
114
|
+
}}
|
|
115
|
+
testID={testID ? resolveTestID(testID, "switch") : undefined}
|
|
116
|
+
>
|
|
117
|
+
<View
|
|
118
|
+
style={{
|
|
119
|
+
backgroundColor: theme.surface.base,
|
|
120
|
+
borderColor: disabled ? theme.surface.disabled : theme.surface.secondaryDark,
|
|
121
|
+
borderRadius: KNOB_SIZE / 2,
|
|
122
|
+
borderWidth: value ? 0 : 1,
|
|
123
|
+
height: KNOB_SIZE,
|
|
124
|
+
width: KNOB_SIZE,
|
|
125
|
+
}}
|
|
126
|
+
/>
|
|
127
|
+
</View>
|
|
128
|
+
</Pressable>
|
|
129
|
+
);
|
|
130
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {FilterChangesBadge} from "./FilterChangesBadge";
|
|
4
|
+
import {renderWithTheme} from "./test-utils";
|
|
5
|
+
|
|
6
|
+
describe("FilterChangesBadge", () => {
|
|
7
|
+
it("renders correctly", () => {
|
|
8
|
+
const {toJSON} = renderWithTheme(<FilterChangesBadge />);
|
|
9
|
+
expect(toJSON()).toMatchSnapshot();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("renders with the primary surface color and dot dimensions", () => {
|
|
13
|
+
const {getByTestId} = renderWithTheme(<FilterChangesBadge testID="badge" />);
|
|
14
|
+
expect(getByTestId("badge")).toHaveStyle({height: 8, width: 8});
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
|
|
3
|
+
import {Badge} from "./Badge";
|
|
4
|
+
import type {FilterChangesBadgeProps} from "./Common";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Small blue dot rendered next to a filter control's title when its selection
|
|
8
|
+
* differs from the default. Implemented via the design system `Badge` using the
|
|
9
|
+
* `variant=status, status=active, color=bold` spec.
|
|
10
|
+
*/
|
|
11
|
+
export const FilterChangesBadge = ({testID}: FilterChangesBadgeProps): React.ReactElement => {
|
|
12
|
+
return <Badge color="bold" status="active" testID={testID} variant="status" />;
|
|
13
|
+
};
|