fix: fix backBehavior with initialRoute (#8110)

This commit is contained in:
ainar
2020-04-29 13:37:15 +02:00
committed by GitHub
parent 70be3f6d86
commit 420f6926e1
2 changed files with 117 additions and 13 deletions

View File

@@ -59,21 +59,31 @@ export const TabActions = {
const getRouteHistory = (
routes: Route<string>[],
index: number,
backBehavior: BackBehavior
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
const history = [{ type: TYPE_ROUTE, key: routes[index].key }];
let initialRouteIndex;
switch (backBehavior) {
case 'initialRoute':
if (index !== 0) {
history.unshift({ type: TYPE_ROUTE, key: routes[0].key });
}
break;
case 'order':
for (let i = index; i > 0; i--) {
history.unshift({ type: TYPE_ROUTE, key: routes[i - 1].key });
}
break;
case 'initialRoute':
initialRouteIndex = routes.findIndex(
(route) => route.name === initialRouteName
);
initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
if (initialRouteIndex !== index) {
history.unshift({
type: TYPE_ROUTE,
key: routes[initialRouteIndex].key,
});
}
break;
case 'history':
// The history will fill up on navigation
break;
@@ -85,7 +95,8 @@ const getRouteHistory = (
const changeIndex = (
state: TabNavigationState,
index: number,
backBehavior: BackBehavior
backBehavior: BackBehavior,
initialRouteName: string | undefined
) => {
let history;
@@ -96,7 +107,12 @@ const changeIndex = (
.filter((it) => (it.type === 'route' ? it.key !== currentKey : false))
.concat({ type: TYPE_ROUTE, key: currentKey });
} else {
history = getRouteHistory(state.routes, index, backBehavior);
history = getRouteHistory(
state.routes,
index,
backBehavior,
initialRouteName
);
}
return {
@@ -130,7 +146,12 @@ export default function TabRouter({
params: routeParamList[name],
}));
const history = getRouteHistory(routes, index, backBehavior);
const history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
return {
stale: false,
@@ -189,7 +210,12 @@ export default function TabRouter({
);
if (!history?.length) {
history = getRouteHistory(routes, index, backBehavior);
history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
}
return {
@@ -223,7 +249,12 @@ export default function TabRouter({
);
if (!history.length) {
history = getRouteHistory(routes, index, backBehavior);
history = getRouteHistory(
routes,
index,
backBehavior,
initialRouteName
);
}
return {
@@ -242,7 +273,7 @@ export default function TabRouter({
return state;
}
return changeIndex(state, index, backBehavior);
return changeIndex(state, index, backBehavior, initialRouteName);
},
getStateForAction(state, action) {
@@ -284,7 +315,8 @@ export default function TabRouter({
: state.routes,
},
index,
backBehavior
backBehavior,
initialRouteName
);
}

View File

@@ -882,6 +882,78 @@ it('handles back action with backBehavior: initialRoute', () => {
).toEqual(null);
});
it('handles back action with backBehavior: initialRoute and initialRouteName', () => {
const router = TabRouter({
backBehavior: 'initialRoute',
initialRouteName: 'baz',
});
const options = {
routeNames: ['bar', 'baz', 'qux'],
routeParamList: {},
};
let state = router.getInitialState(options);
expect(
router.getStateForAction(state, CommonActions.goBack(), options)
).toEqual(null);
state = router.getStateForAction(
state,
TabActions.jumpTo('qux'),
options
) as TabNavigationState;
expect(
router.getStateForAction(state, CommonActions.goBack(), options)
).toEqual({
stale: false,
type: 'tab',
key: 'tab-test',
index: 1,
routeNames: ['bar', 'baz', 'qux'],
routes: [
{ key: 'bar-test', name: 'bar' },
{ key: 'baz-test', name: 'baz' },
{ key: 'qux-test', name: 'qux' },
],
history: [{ type: 'route', key: 'baz-test' }],
});
state = router.getStateForAction(
state,
TabActions.jumpTo('bar'),
options
) as TabNavigationState;
expect(
router.getStateForAction(state, CommonActions.goBack(), options)
).toEqual({
stale: false,
type: 'tab',
key: 'tab-test',
index: 1,
routeNames: ['bar', 'baz', 'qux'],
routes: [
{ key: 'bar-test', name: 'bar' },
{ key: 'baz-test', name: 'baz' },
{ key: 'qux-test', name: 'qux' },
],
history: [{ type: 'route', key: 'baz-test' }],
});
state = router.getStateForAction(
state,
TabActions.jumpTo('baz'),
options
) as TabNavigationState;
expect(
router.getStateForAction(state, CommonActions.goBack(), options)
).toEqual(null);
});
it('handles back action with backBehavior: none', () => {
const router = TabRouter({ backBehavior: 'none' });
const options = {