Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: preserve focused route in tab on changing screens list
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jan 5, 2020
1 parent 543679f commit adbeb29
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
83 changes: 83 additions & 0 deletions packages/routers/__tests__/TabRouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,89 @@ it('gets state on route names change', () => {
});
});

it('preserves focused route on route names change', () => {
const router = TabRouter({});

expect(
router.getStateForRouteNamesChange(
{
index: 1,
key: 'tab-test',
routeKeyHistory: [],
routeNames: ['bar', 'baz', 'qux'],
routes: [
{ key: 'bar-test', name: 'bar' },
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
],
stale: false,
type: 'tab',
},
{
routeNames: ['qux', 'foo', 'fiz', 'baz'],
routeParamList: {
qux: { name: 'John' },
fiz: { fruit: 'apple' },
},
}
)
).toEqual({
index: 3,
key: 'tab-test',
routeKeyHistory: [],
routeNames: ['qux', 'foo', 'fiz', 'baz'],
routes: [
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
{ key: 'foo-test', name: 'foo' },
{ key: 'fiz-test', name: 'fiz', params: { fruit: 'apple' } },
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
],
stale: false,
type: 'tab',
});
});

it('falls back to first route if route is removed on route names change', () => {
const router = TabRouter({});

expect(
router.getStateForRouteNamesChange(
{
index: 1,
key: 'tab-test',
routeKeyHistory: [],
routeNames: ['bar', 'baz', 'qux'],
routes: [
{ key: 'bar-test', name: 'bar' },
{ key: 'baz-test', name: 'baz', params: { answer: 42 } },
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
],
stale: false,
type: 'tab',
},
{
routeNames: ['qux', 'foo', 'fiz'],
routeParamList: {
qux: { name: 'John' },
fiz: { fruit: 'apple' },
},
}
)
).toEqual({
index: 0,
key: 'tab-test',
routeKeyHistory: [],
routeNames: ['qux', 'foo', 'fiz'],
routes: [
{ key: 'qux-test', name: 'qux', params: { name: 'Jane' } },
{ key: 'foo-test', name: 'foo' },
{ key: 'fiz-test', name: 'fiz', params: { fruit: 'apple' } },
],
stale: false,
type: 'tab',
});
});

it('handles navigate action', () => {
const router = TabRouter({});
const options = {
Expand Down
7 changes: 6 additions & 1 deletion packages/routers/src/TabRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,16 @@ export default function TabRouter({
}
);

const index = Math.max(
0,
routeNames.indexOf(state.routes[state.index].name)
);

return {
...state,
routeNames,
routes,
index: Math.min(state.index, routes.length - 1),
index,
};
},

Expand Down

0 comments on commit adbeb29

Please sign in to comment.