@terreno/ui 56.0.0-beta.0 → 57.0.0-beta.1
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 +21 -203
- 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 +149 -3
- package/dist/Common.js.map +1 -1
- package/dist/DateTimeField.js +5 -21
- package/dist/DateTimeField.js.map +1 -1
- package/dist/EditableCard.d.ts +3 -0
- package/dist/EditableCard.js +23 -0
- package/dist/EditableCard.js.map +1 -0
- 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/Page.d.ts +2 -1
- package/dist/Page.js +0 -2
- package/dist/Page.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/Theme.js +3 -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 +7 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/package.json +26 -25
- package/src/ActionSheet.test.tsx +202 -172
- package/src/Badge.test.tsx +22 -1
- package/src/Badge.tsx +43 -0
- package/src/Box.test.tsx +31 -23
- package/src/Common.ts +167 -5
- package/src/ConsentFormScreen.test.tsx +22 -0
- package/src/ConsentNavigator.test.tsx +6 -8
- package/src/DateTimeField.test.tsx +66 -18
- package/src/DateTimeField.tsx +5 -19
- package/src/DraggableList.test.tsx +377 -0
- package/src/EditableCard.test.tsx +69 -0
- package/src/EditableCard.tsx +73 -0
- 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/Page.tsx +2 -3
- package/src/PickerSelect.test.tsx +43 -12
- package/src/PickerSelect.tsx +10 -0
- package/src/SelectField.tsx +2 -0
- package/src/SidebarNavigation.test.tsx +339 -0
- package/src/Theme.tsx +3 -0
- package/src/WebDropdownMenu.test.tsx +214 -1
- package/src/WebDropdownMenu.tsx +43 -21
- package/src/__snapshots__/Card.test.tsx.snap +1 -0
- package/src/__snapshots__/EditableCard.test.tsx.snap +242 -0
- 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/bunSetup.ts +6 -2
- package/src/conflictFieldDiff.test.ts +43 -0
- package/src/icons/BarsFilterIcon.tsx +18 -0
- package/src/index.tsx +7 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import type {ReactTestRendererJSON} from "react-test-renderer";
|
|
3
|
+
|
|
4
|
+
import {EditableCard} from "./EditableCard";
|
|
5
|
+
import {Text} from "./Text";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
describe("EditableCard", () => {
|
|
9
|
+
it("renders correctly with all props", () => {
|
|
10
|
+
const {toJSON} = renderWithTheme(
|
|
11
|
+
<EditableCard
|
|
12
|
+
badge={{iconName: "check", secondary: true, status: "success", value: "Verified"}}
|
|
13
|
+
description="123 Main Street"
|
|
14
|
+
helperText="Last updated 2 days ago"
|
|
15
|
+
iconName="location-dot"
|
|
16
|
+
onEdit={() => {}}
|
|
17
|
+
title="Home Address"
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
expect(toJSON()).toMatchSnapshot();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("renders title, description and helper text", () => {
|
|
24
|
+
const {getByText} = renderWithTheme(
|
|
25
|
+
<EditableCard
|
|
26
|
+
description="123 Main Street"
|
|
27
|
+
helperText="Last updated 2 days ago"
|
|
28
|
+
title="Home Address"
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
expect(getByText("Home Address")).toBeTruthy();
|
|
32
|
+
expect(getByText("123 Main Street")).toBeTruthy();
|
|
33
|
+
expect(getByText("Last updated 2 days ago")).toBeTruthy();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("renders the badge", () => {
|
|
37
|
+
const {getByText} = renderWithTheme(
|
|
38
|
+
<EditableCard
|
|
39
|
+
badge={{secondary: true, status: "success", value: "Verified"}}
|
|
40
|
+
title="Home Address"
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
expect(getByText("Verified")).toBeTruthy();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("uses the attention surface when attention is set", () => {
|
|
47
|
+
const {toJSON} = renderWithTheme(
|
|
48
|
+
<EditableCard attention description="123 Main Street" title="Home Address" />
|
|
49
|
+
);
|
|
50
|
+
const rendered = toJSON() as ReactTestRendererJSON;
|
|
51
|
+
expect(rendered.props.style).toMatchObject({backgroundColor: "#F2F9FA"});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("renders children below the text content", () => {
|
|
55
|
+
const {getByText} = renderWithTheme(
|
|
56
|
+
<EditableCard title="Home Address">
|
|
57
|
+
<Text>Extra content</Text>
|
|
58
|
+
</EditableCard>
|
|
59
|
+
);
|
|
60
|
+
expect(getByText("Extra content")).toBeTruthy();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("renders with testID", () => {
|
|
64
|
+
const {getByTestId} = renderWithTheme(
|
|
65
|
+
<EditableCard testID="editable-card" title="Home Address" />
|
|
66
|
+
);
|
|
67
|
+
expect(getByTestId("editable-card")).toBeTruthy();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type React from "react";
|
|
2
|
+
|
|
3
|
+
import {Badge} from "./Badge";
|
|
4
|
+
import {Box} from "./Box";
|
|
5
|
+
import {Card} from "./Card";
|
|
6
|
+
import type {EditableCardProps} from "./Common";
|
|
7
|
+
import {Icon} from "./Icon";
|
|
8
|
+
import {IconButton} from "./IconButton";
|
|
9
|
+
import {Text} from "./Text";
|
|
10
|
+
|
|
11
|
+
export const EditableCard = ({
|
|
12
|
+
children,
|
|
13
|
+
attention = false,
|
|
14
|
+
badge,
|
|
15
|
+
color,
|
|
16
|
+
description,
|
|
17
|
+
editAccessibilityLabel = "Edit",
|
|
18
|
+
helperText,
|
|
19
|
+
iconName,
|
|
20
|
+
onEdit,
|
|
21
|
+
padding = 3,
|
|
22
|
+
title,
|
|
23
|
+
...rest
|
|
24
|
+
}: EditableCardProps): React.ReactElement => {
|
|
25
|
+
return (
|
|
26
|
+
<Card
|
|
27
|
+
borderLeft="default"
|
|
28
|
+
borderRight="default"
|
|
29
|
+
color={color ?? (attention ? "secondaryExtraLight" : "base")}
|
|
30
|
+
direction="row"
|
|
31
|
+
gap={2}
|
|
32
|
+
padding={padding}
|
|
33
|
+
rounding="md"
|
|
34
|
+
width="100%"
|
|
35
|
+
{...rest}
|
|
36
|
+
>
|
|
37
|
+
{Boolean(iconName) && (
|
|
38
|
+
<Box paddingY={1}>
|
|
39
|
+
<Icon iconName={iconName!} size="md" />
|
|
40
|
+
</Box>
|
|
41
|
+
)}
|
|
42
|
+
<Box direction="column" flex="grow" gap={1} minWidth={0}>
|
|
43
|
+
{(Boolean(title) || Boolean(badge)) && (
|
|
44
|
+
<Box alignItems="center" direction="row" gap={2}>
|
|
45
|
+
{Boolean(title) && (
|
|
46
|
+
<Box flex="shrink" minWidth={0}>
|
|
47
|
+
<Text bold size="md" truncate>
|
|
48
|
+
{title}
|
|
49
|
+
</Text>
|
|
50
|
+
</Box>
|
|
51
|
+
)}
|
|
52
|
+
{Boolean(badge) && <Badge {...badge!} />}
|
|
53
|
+
</Box>
|
|
54
|
+
)}
|
|
55
|
+
{Boolean(description) && <Text size="md">{description}</Text>}
|
|
56
|
+
{Boolean(helperText) && (
|
|
57
|
+
<Text color="secondaryLight" size="sm">
|
|
58
|
+
{helperText}
|
|
59
|
+
</Text>
|
|
60
|
+
)}
|
|
61
|
+
{children}
|
|
62
|
+
</Box>
|
|
63
|
+
{Boolean(onEdit) && (
|
|
64
|
+
<IconButton
|
|
65
|
+
accessibilityLabel={editAccessibilityLabel}
|
|
66
|
+
iconName="pencil"
|
|
67
|
+
onClick={onEdit!}
|
|
68
|
+
variant="ghost"
|
|
69
|
+
/>
|
|
70
|
+
)}
|
|
71
|
+
</Card>
|
|
72
|
+
);
|
|
73
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {act, fireEvent, waitFor} from "@testing-library/react-native";
|
|
3
|
+
|
|
4
|
+
import {Filter} from "./Filter";
|
|
5
|
+
import {Text} from "./Text";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
describe("Filter", () => {
|
|
9
|
+
it("renders correctly with default props", () => {
|
|
10
|
+
const {toJSON} = renderWithTheme(
|
|
11
|
+
<Filter>
|
|
12
|
+
<Text>Body</Text>
|
|
13
|
+
</Filter>
|
|
14
|
+
);
|
|
15
|
+
expect(toJSON()).toMatchSnapshot();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("renders the trigger label", () => {
|
|
19
|
+
const {getByText} = renderWithTheme(
|
|
20
|
+
<Filter label="Filters">
|
|
21
|
+
<Text>Body</Text>
|
|
22
|
+
</Filter>
|
|
23
|
+
);
|
|
24
|
+
expect(getByText("Filters")).toBeTruthy();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("keeps the panel closed by default and open with defaultOpen", () => {
|
|
28
|
+
const closed = renderWithTheme(
|
|
29
|
+
<Filter testID="f">
|
|
30
|
+
<Text>Body</Text>
|
|
31
|
+
</Filter>
|
|
32
|
+
);
|
|
33
|
+
expect(closed.queryByTestId("f.panel")).toBeNull();
|
|
34
|
+
|
|
35
|
+
const open = renderWithTheme(
|
|
36
|
+
<Filter defaultOpen testID="f">
|
|
37
|
+
<Text>Body</Text>
|
|
38
|
+
</Filter>
|
|
39
|
+
);
|
|
40
|
+
expect(open.queryByTestId("f.panel")).toBeTruthy();
|
|
41
|
+
expect(open.getByText("Body")).toBeTruthy();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("hides the footer when action buttons are disabled", () => {
|
|
45
|
+
const {queryByTestId} = renderWithTheme(
|
|
46
|
+
<Filter defaultOpen showActionButtons={false} testID="f">
|
|
47
|
+
<Text>Body</Text>
|
|
48
|
+
</Filter>
|
|
49
|
+
);
|
|
50
|
+
expect(queryByTestId("f.apply")).toBeNull();
|
|
51
|
+
expect(queryByTestId("f.clear")).toBeNull();
|
|
52
|
+
expect(queryByTestId("f.cancel")).toBeNull();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("closes on outside click and calls onCancel", () => {
|
|
56
|
+
const onCancel = mock();
|
|
57
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
58
|
+
<Filter defaultOpen onCancel={onCancel} testID="f">
|
|
59
|
+
<Text>Body</Text>
|
|
60
|
+
</Filter>
|
|
61
|
+
);
|
|
62
|
+
fireEvent.press(getByTestId("f.backdrop"));
|
|
63
|
+
expect(onCancel).toHaveBeenCalled();
|
|
64
|
+
expect(queryByTestId("f.panel")).toBeNull();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("calls onClear and keeps the panel open when Clear is pressed", () => {
|
|
68
|
+
const onClear = mock();
|
|
69
|
+
const {getByTestId} = renderWithTheme(
|
|
70
|
+
<Filter defaultOpen onClear={onClear} testID="f">
|
|
71
|
+
<Text>Body</Text>
|
|
72
|
+
</Filter>
|
|
73
|
+
);
|
|
74
|
+
fireEvent.press(getByTestId("f.clear"));
|
|
75
|
+
expect(onClear).toHaveBeenCalled();
|
|
76
|
+
expect(getByTestId("f.panel")).toBeTruthy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("calls onApply and closes when Apply is pressed", async () => {
|
|
80
|
+
const onApply = mock();
|
|
81
|
+
const {getByTestId, queryByTestId} = renderWithTheme(
|
|
82
|
+
<Filter defaultOpen onApply={onApply} testID="f">
|
|
83
|
+
<Text>Body</Text>
|
|
84
|
+
</Filter>
|
|
85
|
+
);
|
|
86
|
+
await act(async () => {
|
|
87
|
+
fireEvent.press(getByTestId("f.apply"));
|
|
88
|
+
});
|
|
89
|
+
await waitFor(() => {
|
|
90
|
+
expect(onApply).toHaveBeenCalled();
|
|
91
|
+
});
|
|
92
|
+
expect(queryByTestId("f.panel")).toBeNull();
|
|
93
|
+
});
|
|
94
|
+
});
|
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
|
+
};
|