mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
fix: adjust index when route names are changed
This commit is contained in:
@@ -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),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
@@ -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' }],
|
||||
|
||||
Reference in New Issue
Block a user