Skip to content

Commit

Permalink
feat: add a new backBehavior: firstRoute for TabRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Dec 2, 2020
1 parent c361795 commit 7c1cd26
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
17 changes: 15 additions & 2 deletions packages/routers/src/TabRouter.tsx
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
50 changes: 48 additions & 2 deletions packages/routers/src/__tests__/TabRouter.test.tsx
Expand Up @@ -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'],
Expand Down Expand Up @@ -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' });

Expand Down

0 comments on commit 7c1cd26

Please sign in to comment.