@terreno/ui 56.0.0-beta.2 → 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/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/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/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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import {StyleSheet} from "react-native";
|
|
4
|
+
|
|
5
|
+
import {FilterSelectMenu} from "./FilterSelectMenu";
|
|
6
|
+
import {renderWithTheme} from "./test-utils";
|
|
7
|
+
|
|
8
|
+
const OPTIONS = [
|
|
9
|
+
{label: "All", value: "all"},
|
|
10
|
+
{label: "Today", value: "today"},
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
describe("FilterSelectMenu", () => {
|
|
14
|
+
const defaultProps = {
|
|
15
|
+
onChange: () => {},
|
|
16
|
+
options: OPTIONS,
|
|
17
|
+
title: "Due date",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
it("renders correctly with default props", () => {
|
|
21
|
+
const {toJSON} = renderWithTheme(<FilterSelectMenu {...defaultProps} />);
|
|
22
|
+
expect(toJSON()).toMatchSnapshot();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("renders the title", () => {
|
|
26
|
+
const {getByText} = renderWithTheme(<FilterSelectMenu {...defaultProps} />);
|
|
27
|
+
expect(getByText("Due date")).toBeTruthy();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("shows the changes badge only when enabled", () => {
|
|
31
|
+
const {queryByTestId, rerender} = renderWithTheme(
|
|
32
|
+
<FilterSelectMenu {...defaultProps} testID="filter" />
|
|
33
|
+
);
|
|
34
|
+
expect(queryByTestId("filter.badge")).toBeNull();
|
|
35
|
+
|
|
36
|
+
rerender(<FilterSelectMenu {...defaultProps} showChangesBadge testID="filter" />);
|
|
37
|
+
expect(queryByTestId("filter.badge")).toBeTruthy();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("renders the select control", () => {
|
|
41
|
+
const {getByTestId} = renderWithTheme(
|
|
42
|
+
<FilterSelectMenu {...defaultProps} onChange={mock()} testID="filter" value="all" />
|
|
43
|
+
);
|
|
44
|
+
expect(getByTestId("filter")).toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("clips the compact select control to its fixed width", () => {
|
|
48
|
+
const {getByTestId} = renderWithTheme(
|
|
49
|
+
<FilterSelectMenu {...defaultProps} testID="filter" value="all" />
|
|
50
|
+
);
|
|
51
|
+
const style = StyleSheet.flatten(getByTestId("filter.selectContainer").props.style);
|
|
52
|
+
|
|
53
|
+
assert.equal(style.width, 138);
|
|
54
|
+
assert.equal(style.overflow, "hidden");
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {type FC, useState} from "react";
|
|
2
|
+
import {Pressable, View} from "react-native";
|
|
3
|
+
|
|
4
|
+
import type {FilterSelectMenuProps} from "./Common";
|
|
5
|
+
import {FilterChangesBadge} from "./FilterChangesBadge";
|
|
6
|
+
import {SelectField} from "./SelectField";
|
|
7
|
+
import {Text} from "./Text";
|
|
8
|
+
import {useTheme} from "./Theme";
|
|
9
|
+
import {resolveTestID} from "./testing/resolveTestId";
|
|
10
|
+
|
|
11
|
+
const SELECT_WIDTH = 138;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A single-select filter row: a title on the left and a compact select control
|
|
15
|
+
* on the right. Only the select is the click zone. Hovering the select tints
|
|
16
|
+
* the row with the `surface-base-hover` token.
|
|
17
|
+
*/
|
|
18
|
+
export const FilterSelectMenu: FC<FilterSelectMenuProps> = ({
|
|
19
|
+
title,
|
|
20
|
+
options,
|
|
21
|
+
value,
|
|
22
|
+
onChange,
|
|
23
|
+
placeholder,
|
|
24
|
+
showChangesBadge = false,
|
|
25
|
+
disabled = false,
|
|
26
|
+
testID,
|
|
27
|
+
}) => {
|
|
28
|
+
const {theme} = useTheme();
|
|
29
|
+
const [isHovered, setIsHovered] = useState(false);
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<View
|
|
33
|
+
style={{
|
|
34
|
+
alignItems: "center",
|
|
35
|
+
backgroundColor: isHovered ? theme.surface.baseHover : theme.surface.base,
|
|
36
|
+
borderRadius: theme.radius.default,
|
|
37
|
+
flexDirection: "row",
|
|
38
|
+
gap: theme.spacing.sm,
|
|
39
|
+
paddingHorizontal: theme.spacing.sm,
|
|
40
|
+
paddingVertical: theme.primitives.spacing3,
|
|
41
|
+
}}
|
|
42
|
+
testID={testID}
|
|
43
|
+
>
|
|
44
|
+
<View style={{alignItems: "center", flex: 1, flexDirection: "row", gap: 6}}>
|
|
45
|
+
<Text size="md">{title}</Text>
|
|
46
|
+
{showChangesBadge && (
|
|
47
|
+
<FilterChangesBadge testID={testID ? resolveTestID(testID, "badge") : undefined} />
|
|
48
|
+
)}
|
|
49
|
+
</View>
|
|
50
|
+
<Pressable
|
|
51
|
+
onHoverIn={() => setIsHovered(true)}
|
|
52
|
+
onHoverOut={() => setIsHovered(false)}
|
|
53
|
+
style={{overflow: "hidden", width: SELECT_WIDTH}}
|
|
54
|
+
testID={testID ? resolveTestID(testID, "selectContainer") : undefined}
|
|
55
|
+
>
|
|
56
|
+
<SelectField
|
|
57
|
+
disabled={disabled}
|
|
58
|
+
// The compact filter select is fixed at SELECT_WIDTH. Search mode renders
|
|
59
|
+
// the trigger as an <input> whose intrinsic min-width exceeds that width,
|
|
60
|
+
// overflowing the row and pushing the chevron outside the control's border.
|
|
61
|
+
// Filter option lists are short, so a plain (non-search) dropdown is correct.
|
|
62
|
+
disableSearch
|
|
63
|
+
onChange={onChange}
|
|
64
|
+
options={options}
|
|
65
|
+
placeholder={placeholder}
|
|
66
|
+
// The Filter panel is a fixed portal on document.body (zIndex 9999).
|
|
67
|
+
// Render the menu in a matching body portal so it floats above the
|
|
68
|
+
// panel instead of a React Native Modal that would stack behind it.
|
|
69
|
+
renderMenuInBodyPortal
|
|
70
|
+
testID={testID ? resolveTestID(testID, "select") : undefined}
|
|
71
|
+
value={value}
|
|
72
|
+
/>
|
|
73
|
+
</Pressable>
|
|
74
|
+
</View>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
@@ -86,25 +86,27 @@ describe("IconRegistry", () => {
|
|
|
86
86
|
).not.toThrow();
|
|
87
87
|
});
|
|
88
88
|
|
|
89
|
-
it("useIconRegistry
|
|
89
|
+
it("useIconRegistry merges consumer icons over the built-in icons", () => {
|
|
90
90
|
const wrapper = ({children}: {children: React.ReactNode}) => (
|
|
91
91
|
<ThemeProvider>
|
|
92
92
|
<IconRegistryProvider icons={ICONS}>{children}</IconRegistryProvider>
|
|
93
93
|
</ThemeProvider>
|
|
94
94
|
);
|
|
95
95
|
const {result} = renderHook(() => useIconRegistry(), {wrapper});
|
|
96
|
-
expect(result.current).toBe(ICONS);
|
|
97
96
|
expect(result.current.customStar).toBe(CustomStar);
|
|
97
|
+
// Built-in icons remain available alongside consumer icons.
|
|
98
|
+
expect(result.current["bars-filter"]).toBeDefined();
|
|
98
99
|
});
|
|
99
100
|
|
|
100
|
-
it("useIconRegistry
|
|
101
|
+
it("useIconRegistry exposes only the built-in icons when none are provided", () => {
|
|
101
102
|
const wrapper = ({children}: {children: React.ReactNode}) => (
|
|
102
103
|
<ThemeProvider>
|
|
103
104
|
<IconRegistryProvider>{children}</IconRegistryProvider>
|
|
104
105
|
</ThemeProvider>
|
|
105
106
|
);
|
|
106
107
|
const {result} = renderHook(() => useIconRegistry(), {wrapper});
|
|
107
|
-
expect(
|
|
108
|
+
expect(result.current["bars-filter"]).toBeDefined();
|
|
109
|
+
expect(result.current.customStar).toBeUndefined();
|
|
108
110
|
});
|
|
109
111
|
|
|
110
112
|
it("useCustomIcon resolves registered names and ignores everything else", () => {
|
package/src/IconRegistry.tsx
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import {createContext, type FC, type ReactNode, useContext} from "react";
|
|
1
|
+
import {createContext, type FC, type ReactNode, useContext, useMemo} from "react";
|
|
2
2
|
|
|
3
3
|
import type {CustomIconComponent, IconName, IconRegistryMap} from "./Common";
|
|
4
|
+
import {BarsFilterIcon} from "./icons/BarsFilterIcon";
|
|
4
5
|
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
const
|
|
6
|
+
// Icons shipped with @terreno/ui and always available via `iconName`. Consumer
|
|
7
|
+
// icons passed to the provider override these on name collision.
|
|
8
|
+
const BUILT_IN_ICONS: IconRegistryMap = {
|
|
9
|
+
"bars-filter": ({color, size, testID}) => (
|
|
10
|
+
<BarsFilterIcon fill={color} height={size} testID={testID} width={size} />
|
|
11
|
+
),
|
|
12
|
+
};
|
|
8
13
|
|
|
9
|
-
const IconRegistryContext = createContext<IconRegistryMap>(
|
|
14
|
+
const IconRegistryContext = createContext<IconRegistryMap>(BUILT_IN_ICONS);
|
|
10
15
|
|
|
11
16
|
interface IconRegistryProviderProps {
|
|
12
17
|
/** Map of custom icon name to the component that renders it. */
|
|
@@ -15,10 +20,10 @@ interface IconRegistryProviderProps {
|
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
export const IconRegistryProvider: FC<IconRegistryProviderProps> = ({icons, children}) => {
|
|
23
|
+
// Built-in icons are always available; consumer icons override them by name.
|
|
24
|
+
const mergedIcons = useMemo(() => ({...BUILT_IN_ICONS, ...icons}), [icons]);
|
|
18
25
|
return (
|
|
19
|
-
<IconRegistryContext.Provider value={
|
|
20
|
-
{children}
|
|
21
|
-
</IconRegistryContext.Provider>
|
|
26
|
+
<IconRegistryContext.Provider value={mergedIcons}>{children}</IconRegistryContext.Provider>
|
|
22
27
|
);
|
|
23
28
|
};
|
|
24
29
|
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {describe, expect, it, mock} from "bun:test";
|
|
2
2
|
import {act, fireEvent} from "@testing-library/react-native";
|
|
3
|
+
import {assert} from "chai";
|
|
3
4
|
import type {ReactTestInstance} from "react-test-renderer";
|
|
4
5
|
|
|
5
6
|
import {RNPickerSelect} from "./PickerSelect";
|
|
6
7
|
import {renderWithTheme} from "./test-utils";
|
|
8
|
+
import {WebDropdownMenu} from "./WebDropdownMenu";
|
|
7
9
|
|
|
8
10
|
/** Minimal element stub used to fake `document.activeElement` on web. */
|
|
9
11
|
interface MockElement {
|
|
@@ -232,6 +234,24 @@ describe("PickerSelect", () => {
|
|
|
232
234
|
}
|
|
233
235
|
});
|
|
234
236
|
|
|
237
|
+
it("requests body-portal rendering independently of trigger focus", () => {
|
|
238
|
+
ensureDocument();
|
|
239
|
+
savedOS = PlatformModule.OS;
|
|
240
|
+
try {
|
|
241
|
+
PlatformModule.OS = "web";
|
|
242
|
+
const {root} = renderWithTheme(
|
|
243
|
+
<RNPickerSelect {...defaultProps} renderMenuInBodyPortal value="2" />
|
|
244
|
+
);
|
|
245
|
+
const menu = root.findByType(WebDropdownMenu);
|
|
246
|
+
|
|
247
|
+
assert.isTrue(menu.props.renderInBodyPortal);
|
|
248
|
+
assert.isFalse(menu.props.keepTriggerFocus);
|
|
249
|
+
} finally {
|
|
250
|
+
PlatformModule.OS = savedOS;
|
|
251
|
+
restoreDocument();
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
235
255
|
it("renders web dropdown and opens on focus", async () => {
|
|
236
256
|
ensureDocument();
|
|
237
257
|
savedOS = PlatformModule.OS;
|
package/src/PickerSelect.tsx
CHANGED
|
@@ -133,6 +133,14 @@ export interface RNPickerSelectProps {
|
|
|
133
133
|
* @default false
|
|
134
134
|
*/
|
|
135
135
|
disableSearch?: boolean;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Web only. When true, the dropdown menu renders in a fixed portal on
|
|
139
|
+
* `document.body` instead of a React Native `Modal`, so it floats above
|
|
140
|
+
* ancestor portals / high-zIndex overlays (e.g. the `Filter` panel).
|
|
141
|
+
* @default false
|
|
142
|
+
*/
|
|
143
|
+
renderMenuInBodyPortal?: boolean;
|
|
136
144
|
}
|
|
137
145
|
|
|
138
146
|
export const RNPickerSelect = ({
|
|
@@ -158,6 +166,7 @@ export const RNPickerSelect = ({
|
|
|
158
166
|
|
|
159
167
|
InputAccessoryView,
|
|
160
168
|
disableSearch = false,
|
|
169
|
+
renderMenuInBodyPortal = false,
|
|
161
170
|
}: RNPickerSelectProps) => {
|
|
162
171
|
const searchable = !disableSearch;
|
|
163
172
|
const [showPicker, setShowPicker] = useState<boolean>(false);
|
|
@@ -824,6 +833,7 @@ export const RNPickerSelect = ({
|
|
|
824
833
|
}}
|
|
825
834
|
options={menuOptions}
|
|
826
835
|
presentation={Platform.OS === "android" ? "centered" : "anchored"}
|
|
836
|
+
renderInBodyPortal={renderMenuInBodyPortal}
|
|
827
837
|
selectedIndex={menuSelectedIndex >= 0 ? menuSelectedIndex : undefined}
|
|
828
838
|
showEmptyStateWhenNoOptions={searchInTrigger && webSearchQuery.trim().length > 0}
|
|
829
839
|
testIDPrefix="web_dropdown"
|
package/src/SelectField.tsx
CHANGED
|
@@ -16,6 +16,7 @@ export const SelectField: FC<SelectFieldProps> = ({
|
|
|
16
16
|
requireValue = false,
|
|
17
17
|
placeholder = "Please select an option.",
|
|
18
18
|
disableSearch = false,
|
|
19
|
+
renderMenuInBodyPortal = false,
|
|
19
20
|
title,
|
|
20
21
|
value,
|
|
21
22
|
onChange,
|
|
@@ -42,6 +43,7 @@ export const SelectField: FC<SelectFieldProps> = ({
|
|
|
42
43
|
}
|
|
43
44
|
}}
|
|
44
45
|
placeholder={!requireValue ? clearOption : {}}
|
|
46
|
+
renderMenuInBodyPortal={renderMenuInBodyPortal}
|
|
45
47
|
textInputProps={{testID: fieldTestIDs.input}}
|
|
46
48
|
value={value ?? ""}
|
|
47
49
|
/>
|
package/src/Theme.tsx
CHANGED
|
@@ -5,7 +5,12 @@ import type {ComponentProps} from "react";
|
|
|
5
5
|
import {Dimensions} from "react-native";
|
|
6
6
|
|
|
7
7
|
import {renderWithTheme} from "./test-utils";
|
|
8
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
resolveDocumentBodyPortalTarget,
|
|
10
|
+
scheduleAfterPaint,
|
|
11
|
+
useWebDropdownAnchor,
|
|
12
|
+
WebDropdownMenu,
|
|
13
|
+
} from "./WebDropdownMenu";
|
|
9
14
|
|
|
10
15
|
describe("WebDropdownMenu", () => {
|
|
11
16
|
const anchor = {height: 40, width: 200, x: 16, y: 32};
|
|
@@ -506,6 +511,20 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
|
|
|
506
511
|
);
|
|
507
512
|
};
|
|
508
513
|
|
|
514
|
+
it("resolves document.body as the portal target when it is an HTMLElement", () => {
|
|
515
|
+
ensureWebDocument();
|
|
516
|
+
const body = new (globalThis as {HTMLElement: typeof HTMLElement}).HTMLElement();
|
|
517
|
+
Object.defineProperty((globalThis as {document: Document}).document, "body", {
|
|
518
|
+
configurable: true,
|
|
519
|
+
value: body,
|
|
520
|
+
});
|
|
521
|
+
try {
|
|
522
|
+
assert.strictEqual(resolveDocumentBodyPortalTarget(), body);
|
|
523
|
+
} finally {
|
|
524
|
+
restoreWebDocument();
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
|
|
509
528
|
it("renders fixed-position overlay instead of Modal when keepTriggerFocus is true on web", () => {
|
|
510
529
|
ensureWebDocument();
|
|
511
530
|
savedOS = PlatformModule.OS;
|
|
@@ -531,6 +550,25 @@ describe("WebDropdownMenu keepTriggerFocus portal overlay", () => {
|
|
|
531
550
|
}
|
|
532
551
|
});
|
|
533
552
|
|
|
553
|
+
it("renders in the body portal without requiring trigger focus retention", () => {
|
|
554
|
+
ensureWebDocument();
|
|
555
|
+
savedOS = PlatformModule.OS;
|
|
556
|
+
try {
|
|
557
|
+
PlatformModule.OS = "web";
|
|
558
|
+
const {getByTestId, queryByTestId, root} = renderKeepFocusMenu({
|
|
559
|
+
keepTriggerFocus: false,
|
|
560
|
+
renderInBodyPortal: true,
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
assert.isOk(getByTestId("web_dropdown_menu"));
|
|
564
|
+
assert.isNull(queryByTestId("web_dropdown_modal"));
|
|
565
|
+
assert.equal(root.findAll((node) => node.type === "Modal").length, 0);
|
|
566
|
+
} finally {
|
|
567
|
+
PlatformModule.OS = savedOS;
|
|
568
|
+
restoreWebDocument();
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
534
572
|
it("renders a placeholder view when keepTriggerFocus menu is closed on web", () => {
|
|
535
573
|
ensureWebDocument();
|
|
536
574
|
savedOS = PlatformModule.OS;
|
|
@@ -644,6 +682,53 @@ describe("useWebDropdownAnchor", () => {
|
|
|
644
682
|
expect(result.current.anchor).toEqual({height: 40, width: 100, x: 10, y: 20});
|
|
645
683
|
});
|
|
646
684
|
|
|
685
|
+
it("ignores an older measurement that completes after a newer request", () => {
|
|
686
|
+
const {result} = renderHook(() => useWebDropdownAnchor());
|
|
687
|
+
const callbacks: Array<(x: number, y: number, width: number, height: number) => void> = [];
|
|
688
|
+
const measureInWindow = mock(
|
|
689
|
+
(callback: (x: number, y: number, width: number, height: number) => void): void => {
|
|
690
|
+
callbacks.push(callback);
|
|
691
|
+
}
|
|
692
|
+
);
|
|
693
|
+
(result.current.triggerRef as {current: unknown}).current = {measureInWindow};
|
|
694
|
+
const onFirstMeasured = mock(() => {});
|
|
695
|
+
const onSecondMeasured = mock(() => {});
|
|
696
|
+
|
|
697
|
+
act(() => {
|
|
698
|
+
result.current.measure(onFirstMeasured);
|
|
699
|
+
result.current.measure(onSecondMeasured);
|
|
700
|
+
});
|
|
701
|
+
act(() => {
|
|
702
|
+
callbacks[1](20, 30, 120, 50);
|
|
703
|
+
callbacks[0](10, 15, 100, 40);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
assert.deepEqual(result.current.anchor, {height: 50, width: 120, x: 20, y: 30});
|
|
707
|
+
assert.equal(onFirstMeasured.mock.calls.length, 0);
|
|
708
|
+
assert.equal(onSecondMeasured.mock.calls.length, 1);
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it("ignores a pending measurement after cancellation", () => {
|
|
712
|
+
const {result} = renderHook(() => useWebDropdownAnchor());
|
|
713
|
+
let completeMeasurement = (_x: number, _y: number, _width: number, _height: number): void => {};
|
|
714
|
+
const measureInWindow = mock(
|
|
715
|
+
(callback: (x: number, y: number, width: number, height: number) => void): void => {
|
|
716
|
+
completeMeasurement = callback;
|
|
717
|
+
}
|
|
718
|
+
);
|
|
719
|
+
(result.current.triggerRef as {current: unknown}).current = {measureInWindow};
|
|
720
|
+
const onMeasured = mock(() => {});
|
|
721
|
+
|
|
722
|
+
act(() => {
|
|
723
|
+
result.current.measure(onMeasured);
|
|
724
|
+
result.current.cancelPendingMeasurement();
|
|
725
|
+
completeMeasurement(10, 20, 100, 40);
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
assert.deepEqual(result.current.anchor, {height: 0, width: 0, x: 0, y: 0});
|
|
729
|
+
assert.equal(onMeasured.mock.calls.length, 0);
|
|
730
|
+
});
|
|
731
|
+
|
|
647
732
|
it("exercises the Pressable style callback for hover/pressed states", () => {
|
|
648
733
|
const {root} = renderWithTheme(
|
|
649
734
|
<WebDropdownMenu
|
package/src/WebDropdownMenu.tsx
CHANGED
|
@@ -24,6 +24,13 @@ export const scheduleAfterPaint = (callback: () => void): void => {
|
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
export const resolveDocumentBodyPortalTarget = (): HTMLElement | null => {
|
|
28
|
+
if (typeof document === "undefined" || typeof HTMLElement === "undefined") {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return document.body instanceof HTMLElement ? document.body : null;
|
|
32
|
+
};
|
|
33
|
+
|
|
27
34
|
export interface WebDropdownMenuOption {
|
|
28
35
|
key?: string | number;
|
|
29
36
|
label: string;
|
|
@@ -85,6 +92,8 @@ export interface WebDropdownMenuProps {
|
|
|
85
92
|
* Web only.
|
|
86
93
|
*/
|
|
87
94
|
keepTriggerFocus?: boolean;
|
|
95
|
+
/** Web only. Renders the menu in a fixed portal attached to `document.body`. */
|
|
96
|
+
renderInBodyPortal?: boolean;
|
|
88
97
|
/**
|
|
89
98
|
* `anchored` positions the menu below/above the trigger (web-style).
|
|
90
99
|
* `centered` shows a centered dialog, matching Android's native picker modal.
|
|
@@ -124,6 +133,7 @@ export const WebDropdownMenu = ({
|
|
|
124
133
|
disableSearch = false,
|
|
125
134
|
showEmptyStateWhenNoOptions = false,
|
|
126
135
|
keepTriggerFocus = false,
|
|
136
|
+
renderInBodyPortal = false,
|
|
127
137
|
presentation = "anchored",
|
|
128
138
|
}: WebDropdownMenuProps): ReactElement => {
|
|
129
139
|
const searchable = !disableSearch;
|
|
@@ -264,6 +274,7 @@ export const WebDropdownMenu = ({
|
|
|
264
274
|
const menuLayoutStyle = isCenteredPresentation
|
|
265
275
|
? centeredMenuLayoutStyle
|
|
266
276
|
: anchoredMenuLayoutStyle;
|
|
277
|
+
const usesWebPortal = keepTriggerFocus || renderInBodyPortal;
|
|
267
278
|
|
|
268
279
|
const menuContent = (
|
|
269
280
|
<>
|
|
@@ -375,7 +386,7 @@ export const WebDropdownMenu = ({
|
|
|
375
386
|
|
|
376
387
|
// Close on Escape when using the portal overlay (Modal handles this itself).
|
|
377
388
|
useEffect(() => {
|
|
378
|
-
if (!visible || !
|
|
389
|
+
if (!visible || !usesWebPortal || Platform.OS !== "web" || typeof document === "undefined") {
|
|
379
390
|
return;
|
|
380
391
|
}
|
|
381
392
|
const closeOnEscape = (event: KeyboardEvent): void => {
|
|
@@ -390,9 +401,9 @@ export const WebDropdownMenu = ({
|
|
|
390
401
|
document.removeEventListener("keyup", closeOnEscape, false);
|
|
391
402
|
}
|
|
392
403
|
};
|
|
393
|
-
}, [
|
|
404
|
+
}, [onClose, usesWebPortal, visible]);
|
|
394
405
|
|
|
395
|
-
if (Platform.OS === "web" &&
|
|
406
|
+
if (Platform.OS === "web" && usesWebPortal) {
|
|
396
407
|
if (!visible) {
|
|
397
408
|
return <View testID={`${testIDPrefix}_modal`} />;
|
|
398
409
|
}
|
|
@@ -428,10 +439,7 @@ export const WebDropdownMenu = ({
|
|
|
428
439
|
</View>
|
|
429
440
|
);
|
|
430
441
|
|
|
431
|
-
const portalTarget =
|
|
432
|
-
typeof document !== "undefined" && document.body instanceof HTMLElement
|
|
433
|
-
? document.body
|
|
434
|
-
: null;
|
|
442
|
+
const portalTarget = resolveDocumentBodyPortalTarget();
|
|
435
443
|
|
|
436
444
|
if (portalTarget) {
|
|
437
445
|
return createWebPortal({children: overlay, container: portalTarget});
|
|
@@ -506,23 +514,37 @@ export const WebDropdownMenu = ({
|
|
|
506
514
|
export const useWebDropdownAnchor = (): {
|
|
507
515
|
triggerRef: React.RefObject<View | null>;
|
|
508
516
|
anchor: WebDropdownAnchor;
|
|
517
|
+
cancelPendingMeasurement: () => void;
|
|
509
518
|
measure: (onMeasured: (anchor: WebDropdownAnchor) => void) => void;
|
|
510
519
|
} => {
|
|
511
520
|
const triggerRef = useRef<View>(null);
|
|
512
521
|
const [anchor, setAnchor] = useState<WebDropdownAnchor>({height: 0, width: 0, x: 0, y: 0});
|
|
522
|
+
const measurementRequestRef = useRef(0);
|
|
523
|
+
|
|
524
|
+
const cancelPendingMeasurement = useCallback((): void => {
|
|
525
|
+
measurementRequestRef.current += 1;
|
|
526
|
+
}, []);
|
|
527
|
+
|
|
528
|
+
const measure = useCallback(
|
|
529
|
+
(onMeasured: (next: WebDropdownAnchor) => void): void => {
|
|
530
|
+
const requestId = measurementRequestRef.current + 1;
|
|
531
|
+
measurementRequestRef.current = requestId;
|
|
532
|
+
const node = triggerRef.current;
|
|
533
|
+
if (node && typeof node.measureInWindow === "function") {
|
|
534
|
+
node.measureInWindow((x, y, w, h) => {
|
|
535
|
+
if (requestId !== measurementRequestRef.current) {
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
const next = {height: h, width: w, x, y};
|
|
539
|
+
setAnchor(next);
|
|
540
|
+
onMeasured(next);
|
|
541
|
+
});
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
onMeasured(anchor);
|
|
545
|
+
},
|
|
546
|
+
[anchor]
|
|
547
|
+
);
|
|
513
548
|
|
|
514
|
-
|
|
515
|
-
const node = triggerRef.current;
|
|
516
|
-
if (node && typeof node.measureInWindow === "function") {
|
|
517
|
-
node.measureInWindow((x, y, w, h) => {
|
|
518
|
-
const next = {height: h, width: w, x, y};
|
|
519
|
-
setAnchor(next);
|
|
520
|
-
onMeasured(next);
|
|
521
|
-
});
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
onMeasured(anchor);
|
|
525
|
-
};
|
|
526
|
-
|
|
527
|
-
return {anchor, measure, triggerRef};
|
|
549
|
+
return {anchor, cancelPendingMeasurement, measure, triggerRef};
|
|
528
550
|
};
|