From 7e8b5106369a3500a11cb2129c75dd6a9fb5bfb4 Mon Sep 17 00:00:00 2001 From: Patrick Monteith Date: Sat, 27 Jan 2018 21:21:47 +0000 Subject: [PATCH] Don't forward back actions to inactive children of TabRouters (#2121) --- .../react-navigation/src/routers/TabRouter.js | 12 ++-- .../src/routers/__tests__/TabRouter-test.js | 63 +++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/packages/react-navigation/src/routers/TabRouter.js b/packages/react-navigation/src/routers/TabRouter.js index 4a3bac93..e14456b8 100644 --- a/packages/react-navigation/src/routers/TabRouter.js +++ b/packages/react-navigation/src/routers/TabRouter.js @@ -100,12 +100,12 @@ export default (routeConfigs, config = {}) => { let activeTabIndex = state.index; const isBackEligible = action.key == null || action.key === activeTabLastState.key; - if ( - action.type === NavigationActions.BACK && - isBackEligible && - shouldBackNavigateToInitialRoute - ) { - activeTabIndex = initialRouteIndex; + if (action.type === NavigationActions.BACK) { + if (isBackEligible && shouldBackNavigateToInitialRoute) { + activeTabIndex = initialRouteIndex; + } else { + return state; + } } let didNavigate = false; if (action.type === NavigationActions.NAVIGATE) { diff --git a/packages/react-navigation/src/routers/__tests__/TabRouter-test.js b/packages/react-navigation/src/routers/__tests__/TabRouter-test.js index 56771360..2b10ac69 100644 --- a/packages/react-navigation/src/routers/__tests__/TabRouter-test.js +++ b/packages/react-navigation/src/routers/__tests__/TabRouter-test.js @@ -634,6 +634,69 @@ describe('TabRouter', () => { }); }); + test('Back actions are not propagated to inactive children', () => { + const ScreenA = () =>
; + const ScreenB = () =>
; + const ScreenC = () =>
; + const InnerNavigator = () =>
; + InnerNavigator.router = TabRouter({ + a: { screen: ScreenA }, + b: { screen: ScreenB }, + }); + + const router = TabRouter( + { + inner: { screen: InnerNavigator }, + c: { screen: ScreenC }, + }, + { + backBehavior: 'none', + } + ); + + const state0 = router.getStateForAction(INIT_ACTION); + + const state1 = router.getStateForAction( + { type: NavigationActions.NAVIGATE, routeName: 'b' }, + state0 + ); + + const state2 = router.getStateForAction( + { type: NavigationActions.NAVIGATE, routeName: 'c' }, + state1 + ); + + const state3 = router.getStateForAction( + { type: NavigationActions.BACK }, + state2 + ); + + expect(state3).toEqual(state2); + }); + + test('Back behavior initialRoute works', () => { + const ScreenA = () =>
; + const ScreenB = () =>
; + const router = TabRouter({ + a: { screen: ScreenA }, + b: { screen: ScreenB }, + }); + + const state0 = router.getStateForAction(INIT_ACTION); + + const state1 = router.getStateForAction( + { type: NavigationActions.NAVIGATE, routeName: 'b' }, + state0 + ); + + const state2 = router.getStateForAction( + { type: NavigationActions.BACK }, + state1 + ); + + expect(state2).toEqual(state0); + }); + test('Inner actions are only unpacked if the current tab matches', () => { const PlainScreen = () =>
; const ScreenA = () =>
;