Skip to content

Commit 4417bfd

Browse files
fix(mobile): dispatch native toolbar actions to current handlers
1 parent 22dc1da commit 4417bfd

2 files changed

Lines changed: 129 additions & 2 deletions

File tree

‎apps/mobile/src/native/NativeHeaderToolbar.test.tsx‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2-
import { act, createContext, type ReactElement } from "react";
2+
import { act, createContext, useState, type ReactElement } from "react";
33
import { createRoot, type Root } from "react-dom/client";
44
import { afterEach, expect, it, vi } from "vite-plus/test";
55

@@ -61,6 +61,39 @@ function DocumentToolbar(props: { readonly path: string; readonly placement: Pla
6161
);
6262
}
6363

64+
function PairingForm(props: {
65+
readonly placement: Placement;
66+
readonly onPair: (host: string, code: string) => void;
67+
}) {
68+
const [host, setHost] = useState("");
69+
const [code, setCode] = useState("");
70+
const pair = () => props.onPair(host, code);
71+
return (
72+
<>
73+
<input
74+
aria-label="Host"
75+
value={host}
76+
onInput={(event) => setHost(event.currentTarget.value)}
77+
/>
78+
<input
79+
aria-label="Code"
80+
value={code}
81+
onInput={(event) => setCode(event.currentTarget.value)}
82+
/>
83+
<NativeHeaderToolbar placement={props.placement}>
84+
<NativeHeaderToolbar.Button label="Pair" disabled={!host} onPress={pair} />
85+
<NativeHeaderToolbar.Menu title="Pairing">
86+
<NativeHeaderToolbar.Menu title="Actions">
87+
<NativeHeaderToolbar.MenuAction disabled={!host} onPress={pair}>
88+
Pair
89+
</NativeHeaderToolbar.MenuAction>
90+
</NativeHeaderToolbar.Menu>
91+
</NativeHeaderToolbar.Menu>
92+
</NativeHeaderToolbar>
93+
</>
94+
);
95+
}
96+
6497
function renderDocument(path: string, placement: Placement) {
6598
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
6699
harness.params = { path: path.split("/") };
@@ -135,6 +168,53 @@ it.each(["left", "right", "bottom"] as const)(
135168
},
136169
);
137170

171+
it.each(["left", "right", "bottom"] as const)(
172+
"pairs with current form values through retained %s button and nested menu actions",
173+
(placement) => {
174+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
175+
const container = document.createElement("div");
176+
root = createRoot(container);
177+
const onPair = vi.fn();
178+
act(() => root!.render(<PairingForm placement={placement} onPair={onPair} />));
179+
180+
const edit = (label: string, value: string) => {
181+
const input = container.querySelector<HTMLInputElement>(`input[aria-label="${label}"]`)!;
182+
act(() => {
183+
input.value = value;
184+
input.dispatchEvent(new Event("input", { bubbles: true }));
185+
});
186+
};
187+
edit("Host", "h");
188+
189+
const button = harness.renderedItems[0];
190+
if (button?.type !== "button") throw new Error("Expected the native Pair button");
191+
expect(button.disabled).toBe(false);
192+
const menu = harness.renderedItems[1];
193+
if (menu?.type !== "menu") throw new Error("Expected the native pairing menu");
194+
const submenu = menu.menu.items[0];
195+
if (submenu?.type !== "submenu") throw new Error("Expected the nested actions menu");
196+
const action = submenu.items[0];
197+
if (action?.type !== "action") throw new Error("Expected the native Pair action");
198+
expect(action.disabled).toBe(false);
199+
const applied = harness.setOptionsCalls;
200+
const params = harness.params;
201+
202+
edit("Host", "https://my-server.example");
203+
edit("Code", "123456");
204+
205+
expect(harness.params).toBe(params);
206+
expect(harness.setOptionsCalls).toBe(applied);
207+
expect(harness.renderedItems[0]).toBe(button);
208+
expect(harness.renderedItems[1]).toBe(menu);
209+
button.onPress?.();
210+
action.onPress?.();
211+
expect(onPair.mock.calls).toEqual([
212+
["https://my-server.example", "123456"],
213+
["https://my-server.example", "123456"],
214+
]);
215+
},
216+
);
217+
138218
it("applies a custom header element rendered inside a navigation container", () => {
139219
renderConnecting("Connecting");
140220
expect(renderedCustomLabel()).toBe("Connecting");

‎apps/mobile/src/native/NativeHeaderToolbar.tsx‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import type {
88
import {
99
Children,
1010
isValidElement,
11+
useCallback,
1112
useLayoutEffect,
1213
useMemo,
14+
useRef,
1315
use,
1416
type ReactElement,
1517
type ReactNode,
@@ -226,6 +228,34 @@ function collectToolbarItems(children: ReactNode): NativeToolbarItem[] {
226228
return items;
227229
}
228230

231+
function stabilizeItemCallbacks<
232+
T extends NativeToolbarItem | NativeStackHeaderItemMenu["menu"]["items"][number],
233+
>(item: T, path: string, register: (path: string, handler: () => void) => () => void): T {
234+
if (item.type === "button" || item.type === "action") {
235+
return { ...item, onPress: register(path, item.onPress) };
236+
}
237+
if (item.type === "menu") {
238+
return {
239+
...item,
240+
menu: {
241+
...item.menu,
242+
items: item.menu.items.map((child, index) =>
243+
stabilizeItemCallbacks(child, `${path}.${index}`, register),
244+
),
245+
},
246+
};
247+
}
248+
if (item.type === "submenu") {
249+
return {
250+
...item,
251+
items: item.items.map((child, index) =>
252+
stabilizeItemCallbacks(child, `${path}.${index}`, register),
253+
),
254+
};
255+
}
256+
return item;
257+
}
258+
229259
function NativeHeaderToolbarRoot(props: {
230260
readonly placement?: "left" | "right" | "bottom";
231261
readonly children?: ReactNode;
@@ -234,7 +264,24 @@ function NativeHeaderToolbarRoot(props: {
234264
const route = useRoute();
235265
const tabActivity = use(NativeWorkspaceTabActivityContext);
236266
const isTabActive = tabActivity?.isActive;
237-
const items = useMemo(() => collectToolbarItems(props.children), [props.children]);
267+
const committedHandlers = useRef(new Map<string, () => void>());
268+
const dispatch = useCallback((path: string) => committedHandlers.current.get(path)?.(), []);
269+
const { items, handlers } = useMemo(() => {
270+
const handlers = new Map<string, () => void>();
271+
const register = (path: string, handler: () => void) => {
272+
handlers.set(path, handler);
273+
return () => dispatch(path);
274+
};
275+
const items = collectToolbarItems(props.children).map((item, index) =>
276+
stabilizeItemCallbacks(item, String(index), register),
277+
);
278+
return { items, handlers };
279+
}, [props.children, dispatch]);
280+
// Native actions outlive their render when screen options dedupe unchanged presentation.
281+
// Publish handlers on every commit, even when the native item factories never run again.
282+
useLayoutEffect(() => {
283+
committedHandlers.current = handlers;
284+
});
238285
// UIKit's search placement item belongs to the toolbar, never a header group.
239286
const headerItems = useMemo(
240287
() => items.filter((item): item is NativeStackHeaderItem => item.type !== "searchBarPlacement"),

0 commit comments

Comments
 (0)