From 88dfd84cf5d3e218962c491fbc4e5a99dbd865c9 Mon Sep 17 00:00:00 2001 From: pietro909 Date: Sun, 19 Nov 2017 23:00:10 +0100 Subject: [PATCH] Add previous scene to tabBarOnPress - fix #2787 (#2790) * Add previous scene to tabBarOnPress - fix #2787 * Update docs for tabBarOnPress --- docs/api/navigators/TabNavigator.md | 8 +++++++- src/views/TabView/TabBarBottom.js | 14 +++++++++++--- src/views/TabView/TabBarTop.js | 20 ++++++++++++++------ src/views/TabView/TabView.js | 2 +- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/docs/api/navigators/TabNavigator.md b/docs/api/navigators/TabNavigator.md index b279b0b8..062122d3 100644 --- a/docs/api/navigators/TabNavigator.md +++ b/docs/api/navigators/TabNavigator.md @@ -177,7 +177,13 @@ Title string of a tab displayed in the tab bar or React Element or a function th #### `tabBarOnPress` -Callback to handle tap events; arguments are the `scene: { route, index }` that was tapped and a `jumpToIndex` method that can perform the navigation for you. +Callback to handle tap events; the argument is an object containing: + +* the `previousScene: { route, index }` which is the scene we are leaving +* the `scene: { route, index }` that was tapped +* the `jumpToIndex` method that can perform the navigation for you + +Useful for adding a custom logic before the transition to the next scene (the tapped one) starts. ### Navigator Props diff --git a/src/views/TabView/TabBarBottom.js b/src/views/TabView/TabBarBottom.js index 53c0a468..732bec5f 100644 --- a/src/views/TabView/TabBarBottom.js +++ b/src/views/TabView/TabBarBottom.js @@ -36,8 +36,13 @@ type Props = { jumpToIndex: (index: number) => void, getLabel: (scene: TabScene) => ?(React.Node | string), getOnPress: ( + previousScene: NavigationRoute, scene: TabScene - ) => (scene: TabScene, jumpToIndex: (index: number) => void) => void, + ) => ({ + previousScene: NavigationRoute, + scene: TabScene, + jumpToIndex: (index: number) => void, + }) => void, getTestIDProps: (scene: TabScene) => (scene: TabScene) => any, renderIcon: (scene: TabScene) => React.Node, style?: ViewStyleProp, @@ -209,6 +214,7 @@ class TabBarBottom extends React.PureComponent { isLandscape, } = this.props; const { routes } = navigation.state; + const previousScene = routes[navigation.state.index]; // Prepend '-1', so there are always at least 2 items in inputRange const inputRange = [-1, ...routes.map((x: *, i: number) => i)]; @@ -229,7 +235,7 @@ class TabBarBottom extends React.PureComponent { {routes.map((route: NavigationRoute, index: number) => { const focused = index === navigation.state.index; const scene = { route, index, focused }; - const onPress = getOnPress(scene); + const onPress = getOnPress(previousScene, scene); const outputRange = inputRange.map( (inputIndex: number) => inputIndex === index @@ -251,7 +257,9 @@ class TabBarBottom extends React.PureComponent { testID={testID} accessibilityLabel={accessibilityLabel} onPress={() => - onPress ? onPress(scene, jumpToIndex) : jumpToIndex(index)} + onPress + ? onPress({ previousScene, scene, jumpToIndex }) + : jumpToIndex(index)} > void, getLabel: (scene: TabScene) => ?(React.Node | string), getOnPress: ( + previousScene: NavigationRoute, scene: TabScene - ) => (scene: TabScene, jumpToIndex: (index: number) => void) => void, - renderIcon: (scene: TabScene) => React.Node, + ) => ({ + previousScene: NavigationRoute, + scene: TabScene, + jumpToIndex: (index: number) => void, + }) => void, + renderIcon: (scene: TabScene) => React.Element<*>, labelStyle?: TextStyleProp, iconStyle?: ViewStyleProp, }; @@ -115,13 +122,12 @@ export default class TabBarTop extends React.PureComponent { ); }; - _handleOnPress = (scene: TabScene) => { + _handleOnPress = (previousScene: NavigationRoute, scene: TabScene) => { const { getOnPress, jumpToIndex }: Props = this.props; - - const onPress = getOnPress(scene); + const onPress = getOnPress(previousScene, scene); if (onPress) { - onPress(scene, jumpToIndex); + onPress({ previousScene, scene, jumpToIndex }); } else { jumpToIndex(scene.index); } @@ -130,6 +136,8 @@ export default class TabBarTop extends React.PureComponent { render() { // TODO: Define full proptypes const props: any = this.props; + const { state } = props.navigation; + const previousScene = state.routes[state.index]; return ( { return route.routeName; }; - _getOnPress = ({ route }: TabScene) => { + _getOnPress = (previousScene: TabScene, { route }: TabScene) => { const options = this.props.router.getScreenOptions( this.props.childNavigationProps[route.key], this.props.screenProps || {}