mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
feat: add a new backBehavior: firstRoute for TabRouter
This commit is contained in:
@@ -17,7 +17,12 @@ export type TabActionType = {
|
||||
target?: string;
|
||||
};
|
||||
|
||||
export type BackBehavior = 'initialRoute' | 'order' | 'history' | 'none';
|
||||
export type BackBehavior =
|
||||
| 'initialRoute'
|
||||
| 'firstRoute'
|
||||
| 'history'
|
||||
| 'order'
|
||||
| 'none';
|
||||
|
||||
export type TabRouterOptions = DefaultRouterOptions & {
|
||||
backBehavior?: BackBehavior;
|
||||
@@ -74,13 +79,21 @@ const getRouteHistory = (
|
||||
history.unshift({ type: TYPE_ROUTE, key: routes[i - 1].key });
|
||||
}
|
||||
break;
|
||||
case 'firstRoute':
|
||||
if (index !== 0) {
|
||||
history.unshift({
|
||||
type: TYPE_ROUTE,
|
||||
key: routes[0].key,
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'initialRoute':
|
||||
initialRouteIndex = routes.findIndex(
|
||||
(route) => route.name === initialRouteName
|
||||
);
|
||||
initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
|
||||
|
||||
if (initialRouteIndex !== index) {
|
||||
if (index !== initialRouteIndex) {
|
||||
history.unshift({
|
||||
type: TYPE_ROUTE,
|
||||
key: routes[initialRouteIndex].key,
|
||||
|
||||
@@ -323,8 +323,11 @@ it('restores correct history on rehydrating with backBehavior: history', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('restores correct history on rehydrating with backBehavior: initialRoute', () => {
|
||||
const router = TabRouter({ backBehavior: 'initialRoute' });
|
||||
it('restores correct history on rehydrating with backBehavior: firstRoute', () => {
|
||||
const router = TabRouter({
|
||||
backBehavior: 'firstRoute',
|
||||
initialRouteName: 'bar',
|
||||
});
|
||||
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
@@ -363,6 +366,49 @@ it('restores correct history on rehydrating with backBehavior: initialRoute', ()
|
||||
});
|
||||
});
|
||||
|
||||
it('restores correct history on rehydrating with backBehavior: initialRoute', () => {
|
||||
const router = TabRouter({
|
||||
backBehavior: 'initialRoute',
|
||||
initialRouteName: 'bar',
|
||||
});
|
||||
|
||||
const options = {
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routeParamList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getRehydratedState(
|
||||
{
|
||||
index: 2,
|
||||
routes: [
|
||||
{ key: 'foo-0', name: 'foo' },
|
||||
{ key: 'bar-0', name: 'bar' },
|
||||
{ key: 'baz-0', name: 'baz' },
|
||||
{ key: 'qux-0', name: 'qux' },
|
||||
],
|
||||
},
|
||||
options
|
||||
)
|
||||
).toEqual({
|
||||
key: 'tab-test',
|
||||
index: 2,
|
||||
routeNames: ['foo', 'bar', 'baz', 'qux'],
|
||||
routes: [
|
||||
{ key: 'foo-0', name: 'foo' },
|
||||
{ key: 'bar-0', name: 'bar' },
|
||||
{ key: 'baz-0', name: 'baz' },
|
||||
{ key: 'qux-0', name: 'qux' },
|
||||
],
|
||||
history: [
|
||||
{ key: 'bar-0', type: 'route' },
|
||||
{ key: 'baz-0', type: 'route' },
|
||||
],
|
||||
stale: false,
|
||||
type: 'tab',
|
||||
});
|
||||
});
|
||||
|
||||
it('restores correct history on rehydrating with backBehavior: none', () => {
|
||||
const router = TabRouter({ backBehavior: 'none' });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user