|
1 | 1 | import { Toolbar } from "./Toolbar"; |
2 | 2 | import { AppActivityIndicator } from "./AppActivityIndicator"; |
3 | | -import type { HeaderBarButtonMailSearchToolbarItem } from "react-native-screens"; |
| 3 | +import type { HeaderBarButtonMailSearchToolbarItem, SearchBarCommands } from "react-native-screens"; |
4 | 4 | import { NavigationContext } from "@react-navigation/native"; |
5 | | -import { use, useId, useLayoutEffect } from "react"; |
| 5 | +import { use, useId, useLayoutEffect, useRef } from "react"; |
6 | 6 | import { View } from "react-native"; |
7 | 7 | import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios"; |
8 | 8 | import { ScreenHeaderButton } from "./ScreenHeaderButton"; |
@@ -78,6 +78,19 @@ export function AppBar(props: AppBarProps) { |
78 | 78 | const usesNativeMailSearchToolbar = useNativeMailSearchToolbar(); |
79 | 79 | const { search, menus } = props; |
80 | 80 | const menu = menus?.[0]; |
| 81 | + const searchBarRef = useRef<SearchBarCommands>(null); |
| 82 | + const nativeSearchText = useRef(search?.value ?? ""); |
| 83 | + const searchValue = search?.value; |
| 84 | + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the native search bar keeps its own text; a value the caller changes (a reset) has to be pushed into it |
| 85 | + useLayoutEffect(() => { |
| 86 | + if (searchValue === undefined || searchValue === nativeSearchText.current) return; |
| 87 | + nativeSearchText.current = searchValue; |
| 88 | + searchBarRef.current?.setText(searchValue); |
| 89 | + }, [searchValue]); |
| 90 | + const reportSearchText = (text: string) => { |
| 91 | + nativeSearchText.current = text; |
| 92 | + search?.onChangeText(text); |
| 93 | + }; |
81 | 94 | const compactSearch = |
82 | 95 | search !== undefined && |
83 | 96 | (search.compactToolbar ?? !layout.usesSplitView) && |
@@ -142,8 +155,9 @@ export function AppBar(props: AppBarProps) { |
142 | 155 | ? { obscureBackground: false } |
143 | 156 | : undefined), |
144 | 157 | placeholder: search.placeholder, |
145 | | - onChangeText: (event) => search.onChangeText(event.nativeEvent.text), |
146 | | - onCancelButtonPress: () => search.onChangeText(""), |
| 158 | + ref: searchBarRef, |
| 159 | + onChangeText: (event) => reportSearchText(event.nativeEvent.text), |
| 160 | + onCancelButtonPress: () => reportSearchText(""), |
147 | 161 | }, |
148 | 162 | } |
149 | 163 | : undefined), |
|
0 commit comments