Skip to content

Commit 8cdd610

Browse files
committed
Document new API for react-native-tab-view
1 parent 14af773 commit 8cdd610

File tree

2 files changed

+107
-50
lines changed

2 files changed

+107
-50
lines changed

versioned_docs/version-7.x/tab-view.md

+75-48
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ Position of the tab bar in the tab view. Possible values are `'top'` and `'botto
250250

251251
Function which takes an object with the current route and returns a boolean to indicate whether to lazily render the scenes.
252252

253-
By default all scenes are rendered to provide a smoother swipe experience. But you might want to defer the rendering of unfocused scenes until the user sees them. To enable lazy rendering for a particular scene, return `true` from `getLazy` for that `route`:
253+
By default all scenes are rendered to provide a smoother swipe experience. But you might want to defer the rendering of unfocused scenes until the user sees them. To enable lazy rendering for a particular scene, return `true` from `lazy` for that `route`:
254254

255255
```js
256256
<TabView
@@ -358,72 +358,103 @@ return (
358358

359359
#### TabBar Props
360360

361-
##### `getLabelText`
361+
##### Options
362362

363-
Function which takes an object with the current route and returns the label text for the tab. Uses `route.title` by default.
363+
Options describe how each tab should be rendered. There are 2 ways to specify options:
364+
365+
- `commonOptions`: Options that apply to all tabs.
366+
- `options`: Options that apply to specific tabs. It has the route key as the key and the object with options.
367+
368+
Example:
364369

365370
```js
366371
<TabBar
367-
getLabelText={({ route }) => route.title}
368-
...
372+
commonOptions={{
373+
icon: ({ route, focused, color }) => (
374+
<Icon name={route.icon} color={color} />
375+
),
376+
}}
377+
options={{
378+
albums: {
379+
labelText: 'Albums',
380+
},
381+
profile: {
382+
labelText: 'Profile',
383+
},
384+
}}
369385
/>
370386
```
371387

372-
##### `getAccessible`
388+
The following options are available:
373389

374-
Function which takes an object with the current route and returns a boolean to indicate whether to mark a tab as `accessible`. Defaults to `true`.
390+
###### `accessibilityLabel`
375391

376-
##### `getAccessibilityLabel`
392+
Accessibility label for the tab button. Uses `route.accessibilityLabel` by default if specified, otherwise uses the route title.
377393

378-
Function which takes an object with the current route and returns a accessibility label for the tab button. Uses `route.accessibilityLabel` by default if specified, otherwise uses the route title.
394+
###### `accessible`
379395

380-
```js
381-
<TabBar
382-
getAccessibilityLabel={({ route }) => route.accessibilityLabel}
383-
...
384-
/>
385-
```
396+
Whether to mark the tab as `accessible`. Defaults to `true`.
397+
398+
###### `testID`
399+
400+
Test ID for the tab button. Uses `route.testID` by default.
401+
402+
###### `labelText`
403+
404+
Label text for the tab button. Uses `route.title` by default.
405+
406+
###### `labelAllowFontScaling`
407+
408+
Whether label font should scale to respect Text Size accessibility settings. Defaults to `true`.
409+
410+
###### `href`
411+
412+
URL to use for the anchor tag for the tab button on the Web.
386413

387-
##### `getTestID`
414+
###### `label`
388415

389-
Function which takes an object with the current route and returns a test id for the tab button to locate this tab button in tests. Uses `route.testID` by default.
416+
A function that returns a custom React Element to be used as a label. The function receives an object with the following properties:
417+
418+
- `route` - The route object for the tab.
419+
- `labelText` - The label text for the tab specified in the `labelText` option or the `route title`.
420+
- `focused` - Whether the label is for the focused state.
421+
- `color` - The color of the label.
422+
- `allowFontScaling` - Whether label font should scale to respect Text Size accessibility settings.
423+
- `style` - The style object for the label.
390424

391425
```js
392-
<TabBar
393-
getTestID={({ route }) => route.testID}
394-
...
395-
/>
426+
label: ({ route, labelText, focused, color }) => (
427+
<Text style={{ color, margin: 8 }}>{labelText ?? route.name}</Text>
428+
);
396429
```
397430
398-
##### `renderIcon`
431+
###### `icon`
399432
400-
Function which takes an object with the current route, focused status and color and returns a custom React Element to be used as a icon.
433+
A function that returns a custom React Element to be used as an icon. The function receives an object with the following properties:
434+
435+
- `route` - The route object for the tab.
436+
- `focused` - Whether the icon is for the focused state.
437+
- `color` - The color of the icon.
438+
- `size` - The size of the icon.
401439
402440
```js
403-
<TabBar
404-
renderIcon={({ route, focused, color }) => (
405-
<Icon
406-
name={focused ? 'albums' : 'albums-outlined'}
407-
color={color}
408-
/>
409-
)}
410-
...
411-
/>
441+
icon: ({ route, focused, color }) => (
442+
<Icon name={focused ? 'albums' : 'albums-outlined'} color={color} />
443+
);
412444
```
413445
414-
##### `renderLabel`
446+
###### `badge`
447+
448+
A function that returns a custom React Element to be used as a badge. The function receives an object with the following properties:
415449
416-
Function which takes an object with the current route, focused status and color and returns a custom React Element to be used as a label.
450+
- `route` - The route object for the tab.
417451
418452
```js
419-
<TabBar
420-
renderLabel={({ route, focused, color }) => (
421-
<Text style={{ color, margin: 8 }}>
422-
{route.title}
423-
</Text>
424-
)}
425-
...
426-
/>
453+
badge: ({ route }) => (
454+
<View
455+
style={{ backgroundColor: 'red', width: 20, height: 20, borderRadius: 10 }}
456+
/>
457+
);
427458
```
428459
429460
##### `renderTabBarItem`
@@ -434,10 +465,6 @@ Function which takes a `TabBarItemProps` object and returns a custom React Eleme
434465
435466
Function which takes an object with the current route and returns a custom React Element to be used as a tab indicator.
436467
437-
##### `renderBadge`
438-
439-
Function which takes an object with the current route and returns a custom React Element to be used as a badge.
440-
441468
##### `onTabPress`
442469
443470
Function to execute on tab press. It receives the scene for the pressed tab, useful for things like scroll to top.
@@ -515,11 +542,11 @@ Style to apply to the tab bar container.
515542
516543
##### `gap`
517544
518-
Define a spacing between tabs.
545+
Spacing between the tab items.
519546
520547
##### `testID`
521548
522-
Test id for the tabBar. Can be used for scrolling the tab bar in tests
549+
Test ID for the tab bar. Can be used for scrolling the tab bar in tests
523550
524551
## Optimization Tips
525552

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

+32-2
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,39 @@ Previously, the UI elements in React Navigation such as the header on platforms
387387

388388
#### React Native Tab View now has a new API to specify various options
389389

390-
TODO
390+
The API for the `TabBar` component in `react-native-tab-view` has been revamped. Previously, it took the following props:
391+
392+
- `getLabelText`
393+
- `getAccessible`
394+
- `getAccessibilityLabel`
395+
- `getTestID`
396+
- `renderIcon`
397+
- `renderLabel`
398+
- `renderBadge`
399+
400+
These props have been replaced with `commonOptions` and `options` props:
401+
402+
```js
403+
<TabBar
404+
commonOptions={{
405+
icon: ({ route, focused, color }) => (
406+
<Icon name={route.icon} color={color} />
407+
),
408+
}}
409+
options={{
410+
albums: {
411+
labelText: 'Albums',
412+
},
413+
profile: {
414+
labelText: 'Profile',
415+
},
416+
}}
417+
/>
418+
```
419+
420+
The new API will make it easier for us to improve re-rendering performance of the tab bar items in the library.
391421

392-
See [React Native Tab View](tab-view.md) for usage.
422+
See [React Native Tab View](tab-view.md#options) for usage.
393423

394424
#### Custom navigators now require more type information
395425

0 commit comments

Comments
 (0)