fix: adjust index when route names are changed

This commit is contained in:
satyajit.happy
2019-07-29 00:59:11 +02:00
parent 04483a4f1b
commit 9ff483a554
4 changed files with 24 additions and 11 deletions

View File

@@ -107,10 +107,15 @@ function StackRouter(options: DefaultRouterOptions) {
},
getStateForRouteNamesChange(state, { routeNames }) {
const routes = state.routes.filter(route =>
routeNames.includes(route.name)
);
return {
...state,
routeNames,
routes: state.routes.filter(route => routeNames.includes(route.name)),
routes,
index: Math.min(state.index, routes.length - 1),
};
},

View File

@@ -91,17 +91,20 @@ function TabRouter(options: DefaultRouterOptions) {
},
getStateForRouteNamesChange(state, { routeNames, routeParamList }) {
const routes = routeNames.map(
name =>
state.routes.find(r => r.name === name) || {
name,
key: `${name}-${shortid()}`,
params: routeParamList[name],
}
);
return {
...state,
routeNames,
routes: routeNames.map(
name =>
state.routes.find(r => r.name === name) || {
name,
key: `${name}-${shortid()}`,
params: routeParamList[name],
}
),
routes,
index: Math.min(state.index, routes.length - 1),
};
},

View File

@@ -46,10 +46,15 @@ export default function MockRouter(options: DefaultRouterOptions) {
},
getStateForRouteNamesChange(state, { routeNames }) {
const routes = state.routes.filter(route =>
routeNames.includes(route.name)
);
return {
...state,
routeNames,
routes: state.routes.filter(route => routeNames.includes(route.name)),
routes,
index: Math.min(state.index, routes.length - 1),
};
},

View File

@@ -494,7 +494,7 @@ it('handles change in route names', () => {
);
expect(onStateChange).toBeCalledWith({
index: 1,
index: 0,
key: '0',
routeNames: ['foo', 'baz', 'qux'],
routes: [{ key: 'foo', name: 'foo' }],