|
| 1 | +import { |
| 2 | + NavigationContainer, |
| 3 | + ParamListBase, |
| 4 | + useNavigation, |
| 5 | +} from '@react-navigation/native'; |
| 6 | +import { |
| 7 | + createNativeStackNavigator, |
| 8 | + NativeStackNavigationProp, |
| 9 | +} from '@react-navigation/native-stack'; |
| 10 | +import React, { |
| 11 | + createContext, |
| 12 | + ReactNode, |
| 13 | + useContext, |
| 14 | + useLayoutEffect, |
| 15 | + useState, |
| 16 | +} from 'react'; |
| 17 | +import { Button, ScrollView, Text, View } from 'react-native'; |
| 18 | +import { SearchBarPlacement, SearchBarProps } from 'react-native-screens'; |
| 19 | +import { ListItem, SettingsPicker, SettingsSwitch } from '../shared'; |
| 20 | +import { CenteredLayoutView } from '../shared/CenteredLayoutView'; |
| 21 | +import { |
| 22 | + BottomTabsContainer, |
| 23 | + TabConfiguration, |
| 24 | +} from '../shared/gamma/containers/bottom-tabs/BottomTabsContainer'; |
| 25 | +import ConfigWrapperContext, { |
| 26 | + Configuration, |
| 27 | + DEFAULT_GLOBAL_CONFIGURATION, |
| 28 | +} from '../shared/gamma/containers/bottom-tabs/ConfigWrapperContext'; |
| 29 | + |
| 30 | +type NavigationProp<ParamList extends ParamListBase> = { |
| 31 | + navigation: NativeStackNavigationProp<ParamList>; |
| 32 | +}; |
| 33 | + |
| 34 | +type SearchBarConfig = { |
| 35 | + placement: SearchBarProps['placement']; |
| 36 | + allowToolbarIntegration: boolean; |
| 37 | + useSystemItem: boolean; |
| 38 | +}; |
| 39 | + |
| 40 | +const defaultSearchBarConfig: SearchBarConfig = { |
| 41 | + placement: 'integrated', |
| 42 | + allowToolbarIntegration: true, |
| 43 | + useSystemItem: true, |
| 44 | +}; |
| 45 | + |
| 46 | +const SearchBarConfigContext = createContext({ |
| 47 | + searchBarConfig: defaultSearchBarConfig, |
| 48 | + setSearchBarConfig: (_: SearchBarConfig) => {}, |
| 49 | +}); |
| 50 | + |
| 51 | +export function useSearchBarConfig() { |
| 52 | + return useContext(SearchBarConfigContext); |
| 53 | +} |
| 54 | + |
| 55 | +export const SearchBarConfigProvider: React.FC<{ |
| 56 | + children: ReactNode; |
| 57 | +}> = ({ children }) => { |
| 58 | + const [searchBarConfig, setSearchBarConfig] = useState( |
| 59 | + defaultSearchBarConfig, |
| 60 | + ); |
| 61 | + |
| 62 | + return ( |
| 63 | + <SearchBarConfigContext.Provider |
| 64 | + value={{ searchBarConfig, setSearchBarConfig }}> |
| 65 | + {children} |
| 66 | + </SearchBarConfigContext.Provider> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +type MainRouteParamList = { |
| 71 | + Home: undefined; |
| 72 | + Stack: undefined; |
| 73 | + StackAndTabs: undefined; |
| 74 | +}; |
| 75 | + |
| 76 | +type MainStackNavigationProp = NavigationProp<MainRouteParamList>; |
| 77 | + |
| 78 | +const MainStack = createNativeStackNavigator<MainRouteParamList>(); |
| 79 | + |
| 80 | +function Home({ navigation }: MainStackNavigationProp) { |
| 81 | + return ( |
| 82 | + <View |
| 83 | + style={{ |
| 84 | + flex: 1, |
| 85 | + justifyContent: 'center', |
| 86 | + alignItems: 'center', |
| 87 | + gap: 10, |
| 88 | + }}> |
| 89 | + <Text>Test Search Bar placement</Text> |
| 90 | + <Button title="Stack only" onPress={() => navigation.push('Stack')} /> |
| 91 | + <Button |
| 92 | + title="Bottom Tabs and Stack" |
| 93 | + onPress={() => navigation.push('StackAndTabs')} |
| 94 | + /> |
| 95 | + </View> |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +export default function App() { |
| 100 | + return ( |
| 101 | + <NavigationContainer> |
| 102 | + <SearchBarConfigProvider> |
| 103 | + <MainStack.Navigator> |
| 104 | + <MainStack.Screen name="Home" component={Home} /> |
| 105 | + <MainStack.Screen |
| 106 | + name="Stack" |
| 107 | + component={ExamplesStackComponent} |
| 108 | + options={{ headerShown: false }} |
| 109 | + /> |
| 110 | + <MainStack.Screen |
| 111 | + name="StackAndTabs" |
| 112 | + component={TabsStackComponent} |
| 113 | + options={{ headerShown: false }} |
| 114 | + /> |
| 115 | + </MainStack.Navigator> |
| 116 | + </SearchBarConfigProvider> |
| 117 | + </NavigationContainer> |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +type ExamplesRouteParamList = { |
| 122 | + Menu: undefined; |
| 123 | + Test: undefined; |
| 124 | +}; |
| 125 | + |
| 126 | +type ExamplesStackNavigationProp = NavigationProp<ExamplesRouteParamList>; |
| 127 | + |
| 128 | +const ExamplesStack = createNativeStackNavigator<ExamplesRouteParamList>(); |
| 129 | + |
| 130 | +function ExamplesStackComponent({ showMenu = true }: { showMenu?: boolean }) { |
| 131 | + return ( |
| 132 | + <ExamplesStack.Navigator> |
| 133 | + {showMenu && <ExamplesStack.Screen name="Menu" component={Menu} />} |
| 134 | + <ExamplesStack.Screen |
| 135 | + name="Test" |
| 136 | + component={Test} |
| 137 | + options={{ |
| 138 | + headerLargeTitle: true, |
| 139 | + headerTransparent: true, |
| 140 | + headerBackButtonDisplayMode: 'minimal', |
| 141 | + }} |
| 142 | + /> |
| 143 | + </ExamplesStack.Navigator> |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +function Test({ navigation }: ExamplesStackNavigationProp) { |
| 148 | + const [search, setSearch] = useState(''); |
| 149 | + const { searchBarConfig } = useSearchBarConfig(); |
| 150 | + |
| 151 | + const places = [ |
| 152 | + '🏝️ Desert Island', |
| 153 | + '🏞️ National Park', |
| 154 | + '⛰️ Mountain', |
| 155 | + '🏰 Castle', |
| 156 | + '🗽 Statue of Liberty', |
| 157 | + '🌉 Bridge at Night', |
| 158 | + '🏦 Bank', |
| 159 | + '🏛️ Classical Building', |
| 160 | + '🏟️ Stadium', |
| 161 | + '🏪 Convenience Store', |
| 162 | + '🏫 School', |
| 163 | + '⛲ Fountain', |
| 164 | + '🌄 Sunrise Over Mountains', |
| 165 | + '🌆 Cityscape at Dusk', |
| 166 | + '🎡 Ferris Wheel', |
| 167 | + ]; |
| 168 | + |
| 169 | + useLayoutEffect(() => { |
| 170 | + navigation.setOptions({ |
| 171 | + headerSearchBarOptions: { |
| 172 | + placement: searchBarConfig.placement ?? 'automatic', |
| 173 | + allowToolbarIntegration: searchBarConfig.allowToolbarIntegration, |
| 174 | + onChangeText: event => setSearch(event.nativeEvent.text), |
| 175 | + }, |
| 176 | + }); |
| 177 | + }, [navigation, search, searchBarConfig]); |
| 178 | + |
| 179 | + return ( |
| 180 | + <ScrollView |
| 181 | + contentInsetAdjustmentBehavior="automatic" |
| 182 | + keyboardDismissMode="on-drag"> |
| 183 | + {places |
| 184 | + .filter(item => item.toLowerCase().indexOf(search.toLowerCase()) !== -1) |
| 185 | + .map(place => ( |
| 186 | + <ListItem |
| 187 | + key={place} |
| 188 | + title={place} |
| 189 | + onPress={() => navigation.goBack()} |
| 190 | + /> |
| 191 | + ))} |
| 192 | + </ScrollView> |
| 193 | + ); |
| 194 | +} |
| 195 | + |
| 196 | +function TabsStackComponent() { |
| 197 | + const [config, setConfig] = React.useState<Configuration>( |
| 198 | + DEFAULT_GLOBAL_CONFIGURATION, |
| 199 | + ); |
| 200 | + const { searchBarConfig } = useSearchBarConfig(); |
| 201 | + |
| 202 | + const TAB_CONFIGS: TabConfiguration[] = [ |
| 203 | + { |
| 204 | + tabScreenProps: { |
| 205 | + tabKey: 'main', |
| 206 | + title: 'Main', |
| 207 | + icon: { |
| 208 | + sfSymbolName: 'house', |
| 209 | + }, |
| 210 | + }, |
| 211 | + component: () => Menu({ tabsMode: true }), |
| 212 | + }, |
| 213 | + { |
| 214 | + tabScreenProps: { |
| 215 | + tabKey: 'another', |
| 216 | + title: 'Another', |
| 217 | + icon: { |
| 218 | + sfSymbolName: 'ellipsis', |
| 219 | + }, |
| 220 | + }, |
| 221 | + component: AnotherTab, |
| 222 | + }, |
| 223 | + { |
| 224 | + tabScreenProps: { |
| 225 | + tabKey: 'examples', |
| 226 | + title: 'Search', |
| 227 | + icon: { |
| 228 | + sfSymbolName: 'magnifyingglass', |
| 229 | + }, |
| 230 | + systemItem: searchBarConfig.useSystemItem ? 'search' : undefined, |
| 231 | + }, |
| 232 | + component: () => ExamplesStackComponent({ showMenu: false }), |
| 233 | + }, |
| 234 | + ]; |
| 235 | + |
| 236 | + return ( |
| 237 | + <ConfigWrapperContext.Provider |
| 238 | + value={{ |
| 239 | + config, |
| 240 | + setConfig, |
| 241 | + }}> |
| 242 | + <BottomTabsContainer tabConfigs={TAB_CONFIGS} /> |
| 243 | + </ConfigWrapperContext.Provider> |
| 244 | + ); |
| 245 | +} |
| 246 | + |
| 247 | +function AnotherTab() { |
| 248 | + return ( |
| 249 | + <CenteredLayoutView> |
| 250 | + <Text>Another tab</Text> |
| 251 | + </CenteredLayoutView> |
| 252 | + ); |
| 253 | +} |
| 254 | + |
| 255 | +function Menu({ tabsMode = false }: { tabsMode?: boolean }) { |
| 256 | + const { searchBarConfig, setSearchBarConfig } = useSearchBarConfig(); |
| 257 | + const navigation = useNavigation<ExamplesStackNavigationProp['navigation']>(); |
| 258 | + |
| 259 | + return ( |
| 260 | + <CenteredLayoutView> |
| 261 | + <SettingsSwitch |
| 262 | + label="allowToolbarIntegration" |
| 263 | + value={searchBarConfig.allowToolbarIntegration} |
| 264 | + onValueChange={value => |
| 265 | + setSearchBarConfig({ |
| 266 | + ...searchBarConfig, |
| 267 | + allowToolbarIntegration: value, |
| 268 | + }) |
| 269 | + } |
| 270 | + /> |
| 271 | + <SettingsPicker<SearchBarPlacement> |
| 272 | + label="placement" |
| 273 | + value={searchBarConfig.placement ?? 'automatic'} |
| 274 | + onValueChange={value => |
| 275 | + setSearchBarConfig({ |
| 276 | + ...searchBarConfig, |
| 277 | + placement: value, |
| 278 | + }) |
| 279 | + } |
| 280 | + items={[ |
| 281 | + 'automatic', |
| 282 | + 'inline', |
| 283 | + 'stacked', |
| 284 | + 'integrated', |
| 285 | + 'integratedButton', |
| 286 | + 'integratedCentered', |
| 287 | + ]} |
| 288 | + /> |
| 289 | + {tabsMode && ( |
| 290 | + <SettingsSwitch |
| 291 | + label="use systemItem" |
| 292 | + value={searchBarConfig.useSystemItem} |
| 293 | + onValueChange={value => |
| 294 | + setSearchBarConfig({ |
| 295 | + ...searchBarConfig, |
| 296 | + useSystemItem: value, |
| 297 | + }) |
| 298 | + } |
| 299 | + /> |
| 300 | + )} |
| 301 | + {!tabsMode && ( |
| 302 | + <Button title="Go to screen" onPress={() => navigation.push('Test')} /> |
| 303 | + )} |
| 304 | + </CenteredLayoutView> |
| 305 | + ); |
| 306 | +} |
0 commit comments