Skip to content

Commit

Permalink
Allow TabBarBottom accept onTabPress callback (#486)
Browse files Browse the repository at this point in the history
Pressing a tab bar often requires performing some kind of side effects
other than navigating: _Scroll-to-top_ functionality, for instance.
At the moment `TabView.TabBarTop`, which is the default on _Android_,
handles `onTabPress` callback as expected, but `TabView.TabBarBottom`
ignores it.

This PR addresses the problem so that `TabView.TabBarBottom` can accept
and handle the optional `onTabPress` callback. The callback is invoked
exactly once if present on every tab bar press, accepting as a sole
parameter a `NavigationRoute`, which is the route object the pressed
target represents.
  • Loading branch information
kimdhoe committed Mar 1, 2017
1 parent e44dee1 commit 3491bb5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/api/navigators/TabNavigator.md
Expand Up @@ -108,6 +108,7 @@ Several options get passed to the underlying router to modify navigation logic:
- `showLabel` - whether to show label for tab, default is true
- `style` - style object for the tab bar
- `labelStyle` - style object for the tab label
- `onTabPress` - optional callback invoked on tab press

Example:

Expand Down Expand Up @@ -135,6 +136,7 @@ tabBarOptions: {
- `indicatorStyle` - style object for the tab indicator (line at the bottom of the tab)
- `labelStyle` - style object for the tab label
- `style` - style object for the tab bar
- `onTabPress` - optional callback invoked on tab press

Example:

Expand Down
18 changes: 17 additions & 1 deletion src/views/TabView/TabBarBottom.js
Expand Up @@ -35,6 +35,7 @@ type Props = {
position: Animated.Value;
navigationState: NavigationState;
jumpToIndex: (index: number) => void;
onTabPress: (route: NavigationRoute) => void;
getLabel: (scene: TabScene) => ?(React.Element<*> | string);
renderIcon: (scene: TabScene) => React.Element<*>;
showLabel: boolean;
Expand All @@ -57,6 +58,18 @@ export default class TabBarBottom extends PureComponent<DefaultProps, Props, voi

props: Props;

_handleTabPress = (index: number) => {
const {
navigationState,
jumpToIndex,
onTabPress,
} = this.props;
jumpToIndex(index);
if (onTabPress) {
onTabPress(navigationState.routes[index]);
}
}

_renderLabel = (scene: TabScene) => {
const {
position,
Expand Down Expand Up @@ -144,7 +157,10 @@ export default class TabBarBottom extends PureComponent<DefaultProps, Props, voi
});
const justifyContent = this.props.showIcon ? 'flex-end' : 'center';
return (
<TouchableWithoutFeedback key={route.key} onPress={() => jumpToIndex(index)}>
<TouchableWithoutFeedback
key={route.key}
onPress={() => this._handleTabPress(index)}
>
<Animated.View style={[styles.tab, { backgroundColor, justifyContent }]}>
{this._renderIcon(scene)}
{this._renderLabel(scene)}
Expand Down

0 comments on commit 3491bb5

Please sign in to comment.