1- import { useRef , useState } from "react" ;
1+ import { type RefObject , useRef , useState } from "react" ;
22import { Keyboard , type TextInputInstance , View } from "react-native" ;
33import { useHardwareBack } from "../../lib/backHandler" ;
44import { useToolbarLayout } from "./useToolbarLayout" ;
@@ -10,60 +10,86 @@ import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
1010import { Menu } from "./Menu" ;
1111import { SearchField } from "./SearchField" ;
1212import { androidHeaderMenuActions , findHeaderMenuAction } from "./headerMenu.android" ;
13- import type { AppBarMenuItem , AppBarProps } from "./AppBar.types" ;
13+ import type { AppBarMenuItem , AppBarProps , AppBarSearch } from "./AppBar.types" ;
1414import { useHasWorkspaceAppBar , WorkspaceAppBar } from "./WorkspaceAppBar.android" ;
1515
1616function selectMenuItem ( items : ReadonlyArray < AppBarMenuItem > , id : string ) {
1717 const action = findHeaderMenuAction ( items , id ) ;
1818 if ( action && ! action . disabled ) action . onPress ( ) ;
1919}
2020
21+ interface SearchState {
22+ readonly searching : boolean ;
23+ readonly open : ( ) => void ;
24+ readonly close : ( ) => void ;
25+ readonly inputRef : RefObject < TextInputInstance | null > ;
26+ }
27+
2128export function AppBar ( props : AppBarProps ) {
29+ return props . search ? (
30+ < SearchableAppBar { ...props } search = { props . search } />
31+ ) : (
32+ < AppBarView { ...props } />
33+ ) ;
34+ }
35+
36+ // Only a bar with search registers for hardware back: its focus check needs a navigation
37+ // context, and sheet headers such as Arrange threads render outside one.
38+ function SearchableAppBar ( props : AppBarProps & { readonly search : AppBarSearch } ) {
2239 const { search } = props ;
23- const { paddingTop, paddingBottom } = useToolbarLayout ( props . sheet ) ;
24- const { themeVariables } = useAppearancePreferences ( ) ;
25- const inWorkspace = useHasWorkspaceAppBar ( ) ;
2640 const [ searchOpen , setSearchOpen ] = useState ( false ) ;
2741 const inputRef = useRef < TextInputInstance > ( null ) ;
28- const inline = search ? .mode === "inline" ;
29- const controlled = search ? .active !== undefined ;
42+ const inline = search . mode === "inline" ;
43+ const controlled = search . active !== undefined ;
3044 const searching =
31- search !== undefined &&
32- ( inline || ( controlled ? search . active === true : searchOpen || search . value . length > 0 ) ) ;
33- const openSearch = ( ) => {
45+ inline || ( controlled ? search . active === true : searchOpen || search . value . length > 0 ) ;
46+ const open = ( ) => {
3447 if ( ! controlled ) setSearchOpen ( true ) ;
35- search ? .onOpen ?.( ) ;
48+ search . onOpen ?.( ) ;
3649 inputRef . current ?. focus ( ) ;
3750 } ;
38- const closeSearch = ( ) => {
51+ const close = ( ) => {
3952 if ( ! controlled ) {
40- search ? .onChangeText ( "" ) ;
53+ search . onChangeText ( "" ) ;
4154 setSearchOpen ( false ) ;
4255 Keyboard . dismiss ( ) ;
4356 }
44- search ? .onClose ?.( ) ;
57+ search . onClose ?.( ) ;
4558 } ;
4659 useHardwareBack ( searching && ! inline && ! controlled , ( ) => {
47- closeSearch ( ) ;
60+ close ( ) ;
4861 return true ;
4962 } ) ;
63+ return < AppBarView { ...props } searchState = { { searching, open, close, inputRef } } /> ;
64+ }
65+
66+ function AppBarView ( props : AppBarProps & { readonly searchState ?: SearchState } ) {
67+ const { search, searchState } = props ;
68+ const { paddingTop, paddingBottom } = useToolbarLayout ( props . sheet ) ;
69+ const { themeVariables } = useAppearancePreferences ( ) ;
70+ const inWorkspace = useHasWorkspaceAppBar ( ) ;
71+ const inline = search ?. mode === "inline" ;
72+ const searching = searchState ?. searching === true ;
73+ const openSearch = ( ) => searchState ?. open ( ) ;
74+ const closeSearch = ( ) => searchState ?. close ( ) ;
5075
5176 const searchLabel = search ?. accessibilityLabel ?? search ?. placeholder ?? "" ;
52- const searchField = search ? (
53- < SearchField
54- inputRef = { inputRef }
55- accessibilityLabel = { searchLabel }
56- clearAccessibilityLabel = { search . clearAccessibilityLabel ?? "Clear search" }
57- placeholder = {
58- search . compactToolbar
59- ? ( search . compactPlaceholder ?? search . placeholder )
60- : search . placeholder
61- }
62- value = { search . value }
63- onChangeText = { search . onChangeText }
64- autoFocus = { ! inline }
65- />
66- ) : null ;
77+ const searchField =
78+ search && searchState ? (
79+ < SearchField
80+ inputRef = { searchState . inputRef }
81+ accessibilityLabel = { searchLabel }
82+ clearAccessibilityLabel = { search . clearAccessibilityLabel ?? "Clear search" }
83+ placeholder = {
84+ search . compactToolbar
85+ ? ( search . compactPlaceholder ?? search . placeholder )
86+ : search . placeholder
87+ }
88+ value = { search . value }
89+ onChangeText = { search . onChangeText }
90+ autoFocus = { ! inline }
91+ />
92+ ) : null ;
6793
6894 if ( inWorkspace ) {
6995 const menu = props . menus ?. [ 0 ] ;
0 commit comments