From 3429d169de82fa0dc90d93bbf149b9e487fafa70 Mon Sep 17 00:00:00 2001 From: Yury Korolev Date: Sun, 26 Mar 2017 16:59:39 +0300 Subject: [PATCH] Apply PR #525 --- src/views/TabView/TabBarBottom.js | 10 +++++++++- src/views/TabView/TabBarTop.js | 19 +++++++++++++++++++ src/views/TabView/TabView.js | 12 ++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/views/TabView/TabBarBottom.js b/src/views/TabView/TabBarBottom.js index 818f843f83..bc6e976d77 100644 --- a/src/views/TabView/TabBarBottom.js +++ b/src/views/TabView/TabBarBottom.js @@ -36,6 +36,7 @@ type Props = { navigationState: NavigationState; jumpToIndex: (index: number) => void; getLabel: (scene: TabScene) => ?(React.Element<*> | string); + getTabPressCallback: (scene: TabScene) => Function; renderIcon: (scene: TabScene) => React.Element<*>; showLabel: boolean; style?: Style; @@ -57,6 +58,13 @@ export default class TabBarBottom extends PureComponent { + const { getTabPressCallback, jumpToIndex } = this.props + const onTabPress = getTabPressCallback(scene); + onTabPress(scene); + jumpToIndex(scene.index); + }; + _renderLabel = (scene: TabScene) => { const { position, @@ -147,7 +155,7 @@ export default class TabBarBottom extends PureComponent jumpToIndex(index)}> + this._onTabPress(scene)}> {this._renderIcon(scene)} {this._renderLabel(scene)} diff --git a/src/views/TabView/TabBarTop.js b/src/views/TabView/TabBarTop.js index f895c28810..ecf4760230 100644 --- a/src/views/TabView/TabBarTop.js +++ b/src/views/TabView/TabBarTop.js @@ -35,6 +35,7 @@ type Props = { position: Animated.Value; navigationState: NavigationState; getLabel: (scene: TabScene) => ?(React.Element<*> | string); + getTabPressCallback: (scene: TabScene) => Function; renderIcon: (scene: TabScene) => React.Element<*>; labelStyle?: Style; }; @@ -51,6 +52,23 @@ export default class TabBarTop extends PureComponent props: Props; + _onTabPress = (route: NavigationRoute) => { + const { navigationState, getTabPressCallback } = this.props; + const { routes } = navigationState; + const routesLength = routes.length; + let index = -1; + for (let i = 0; i < routesLength; i++) { + if (routes[i] === route) { + index = i; + break; + } + } + const focused = index === navigationState.index; + const scene = { route, index, focused }; + const onTabPress = getTabPressCallback(scene); + onTabPress(scene); + } + _renderLabel = (scene: TabScene) => { const { position, @@ -123,6 +141,7 @@ export default class TabBarTop extends PureComponent return ( diff --git a/src/views/TabView/TabView.js b/src/views/TabView/TabView.js index b95d336736..7bfb2733d2 100644 --- a/src/views/TabView/TabView.js +++ b/src/views/TabView/TabView.js @@ -107,6 +107,17 @@ class TabView extends PureComponent { return route.routeName; }; + _getTabPressCallback = ({ route }: TabScene): Function => { + const tabBar = this.props.router.getScreenConfig( + this.props.childNavigationProps[route.key], + 'tabBar' + ); + if (tabBar && typeof tabBar.onTabPress === 'function') { + return tabBar.onTabPress; + } + return () => {}; + }; + _renderIcon = ({ focused, route, tintColor }: TabScene) => { const tabBar = this.props.router.getScreenConfig( this.props.childNavigationProps[route.key], @@ -135,6 +146,7 @@ class TabView extends PureComponent { {...tabBarOptions} navigation={this.props.navigation} getLabel={this._getLabel} + getTabPressCallback={this._getTabPressCallback} renderIcon={this._renderIcon} animationEnabled={animationEnabled} />