Skip to content

Commit 821c36f

Browse files
committed
Document new headerSearchBarOptions
1 parent b72d18b commit 821c36f

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

versioned_docs/version-7.x/elements.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ function HomeScreen() {
7373
// codeblock-focus-start
7474
const MyStack = createNativeStackNavigator({
7575
screenOptions: {
76-
header: ({ options, route }) => (
77-
<Header {...options} title={getHeaderTitle(options, route.name)} />
76+
header: ({ options, route, back }) => (
77+
<Header
78+
{...options}
79+
back={back}
80+
title={getHeaderTitle(options, route.name)}
81+
/>
7882
),
7983
},
8084
screens: {
@@ -107,8 +111,12 @@ function MyStack() {
107111
return (
108112
<Stack.Navigator
109113
screenOptions={{
110-
header: ({ options, route }) => (
111-
<Header {...options} title={getHeaderTitle(options, route.name)} />
114+
header: ({ options, route, back }) => (
115+
<Header
116+
{...options}
117+
back={back}
118+
title={getHeaderTitle(options, route.name)}
119+
/>
112120
),
113121
}}
114122
>
@@ -326,6 +334,35 @@ const RootStack = createNativeStackNavigator({
326334
</TabItem>
327335
</Tabs>
328336

337+
#### `headerSearchBarOptions`
338+
339+
Options for the search bar in the header. When this is specified, the header will contain a button to show a search input.
340+
341+
It can contain the following properties:
342+
343+
- `autoCapitalize`: The auto-capitalization behavior. Possible values: `none`, `words`, `sentences`, `characters`.
344+
- `autoFocus`: Automatically focus search input on mount.
345+
- `cancelButtonText`: Text to be used instead of default `Cancel` button text (iOS only).
346+
- `inputType`: Type of the input. Possible values: `text`, `phone`, `number`, `email`.
347+
- `onBlur`: Callback that gets called when search input has lost focus.
348+
- `onChangeText`: Callback that gets called when the text changes.
349+
- `onClose`: Callback that gets called when search input is closed.
350+
- `onFocus`: Callback that gets called when search input has received focus.
351+
- `placeholder`: Text displayed when search input is empty.
352+
353+
```js
354+
React.useLayoutEffect(() => {
355+
navigation.setOptions({
356+
headerSearchBarOptions: {
357+
placeholder: 'Search',
358+
onChangeText: (text) => {
359+
// Do something
360+
},
361+
},
362+
});
363+
}, [navigation]);
364+
```
365+
329366
#### `headerShadowVisible`
330367

331368
Whether to hide the elevation shadow (Android) or the bottom border (iOS) on the header.

versioned_docs/version-7.x/native-stack-navigator.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ Example:
479479

480480
#### `headerSearchBarOptions`
481481

482-
Options to render a native search bar on iOS. Search bars are rarely static so normally it is controlled by passing an object to `headerSearchBarOptions` navigation option in the component's body. You also need to specify `contentInsetAdjustmentBehavior="automatic"` in your `ScrollView`, `FlatList` etc. If you don't have a `ScrollView`, specify `headerTransparent: false`.
482+
Options to render a native search bar on iOS. Search bars are rarely static so normally it is controlled by passing an object to `headerSearchBarOptions` navigation option in the component's body.
483483

484-
Only supported on iOS and Android.
484+
You also need to specify `contentInsetAdjustmentBehavior="automatic"` in your `ScrollView`, `FlatList` etc. If you don't have a `ScrollView`, specify `headerTransparent: false`.
485485

486486
Example:
487487

versioned_docs/version-7.x/upgrading-from-6.x.md

+19
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,25 @@ return (
796796
);
797797
```
798798

799+
#### All navigators with headers now support `headerSearchBarOptions`
800+
801+
The `Header` component from `@react-navigation/elements` now supports a `headerSearchBarOptions` prop. This means all navigators that use the `Header` component now support a search bar in the header as well on all platforms. Previously, this was only available in the Native Stack Navigator on iOS and Android.
802+
803+
```js
804+
React.useLayoutEffect(() => {
805+
navigation.setOptions({
806+
headerSearchBarOptions: {
807+
placeholder: 'Search',
808+
onChangeText: (text) => {
809+
// Do something
810+
},
811+
},
812+
});
813+
}, [navigation]);
814+
```
815+
816+
See [`headerSearchBarOptions`](elements.md#headersearchbaroptions) for usage.
817+
799818
### New components in elements library
800819

801820
The `@react-navigation/elements` package now includes new components that can be used in your app:

0 commit comments

Comments
 (0)