Don't forward back actions to inactive children of TabRouters (#2121)

This commit is contained in:
Patrick Monteith 2018-01-27 21:21:47 +00:00 committed by Brent Vatne
parent a62ad18b31
commit e0de8b4dce
2 changed files with 69 additions and 6 deletions

View File

@ -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) {

View File

@ -634,6 +634,69 @@ describe('TabRouter', () => {
});
});
test('Back actions are not propagated to inactive children', () => {
const ScreenA = () => <div />;
const ScreenB = () => <div />;
const ScreenC = () => <div />;
const InnerNavigator = () => <div />;
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 = () => <div />;
const ScreenB = () => <div />;
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 = () => <div />;
const ScreenA = () => <div />;