@@ -2,21 +2,13 @@ import invariant from '../utils/invariant';
22import getScreenForRouteName from './getScreenForRouteName' ;
33import createConfigGetter from './createConfigGetter' ;
44
5- import NavigationActions from '../NavigationActions' ;
6- import StackActions from './StackActions ' ;
5+ import * as NavigationActions from '../NavigationActions' ;
6+ import * as SwitchActions from './SwitchActions ' ;
77import validateRouteConfigMap from './validateRouteConfigMap' ;
88import { createPathParser } from './pathUtils' ;
99
1010const defaultActionCreators = ( ) => ( { } ) ;
1111
12- function childrenUpdateWithoutSwitchingIndex ( actionType ) {
13- return [
14- NavigationActions . SET_PARAMS ,
15- // Todo: make SwitchRouter not depend on StackActions..
16- StackActions . COMPLETE_TRANSITION ,
17- ] . includes ( actionType ) ;
18- }
19-
2012export default ( routeConfigs , config = { } ) => {
2113 // Fail fast on invalid route definitions
2214 validateRouteConfigMap ( routeConfigs ) ;
@@ -172,6 +164,43 @@ export default (routeConfigs, config = {}) => {
172164 }
173165 }
174166
167+ if (
168+ action . type === SwitchActions . JUMP_TO &&
169+ ( action . key == null || action . key === state . key )
170+ ) {
171+ const { params } = action ;
172+ const index = state . routes . findIndex (
173+ route => route . routeName === action . routeName
174+ ) ;
175+
176+ if ( index === - 1 ) {
177+ throw new Error (
178+ `There is no route named '${
179+ action . routeName
180+ } ' in the navigator with the key '${ action . key } '.\n` +
181+ `Must be one of: ${ state . routes
182+ . map ( route => `'${ route . routeName } '` )
183+ . join ( ',' ) } `
184+ ) ;
185+ }
186+
187+ return getNextState ( action , prevState , {
188+ ...state ,
189+ routes : state . routes . map ( ( route , i ) =>
190+ i === index
191+ ? {
192+ ...route ,
193+ params : {
194+ ...route . params ,
195+ ...params ,
196+ } ,
197+ }
198+ : route
199+ ) ,
200+ index,
201+ } ) ;
202+ }
203+
175204 // Let the current child handle it
176205 const activeChildLastState = state . routes [ state . index ] ;
177206 const activeChildRouter = childRouters [ order [ state . index ] ] ;
@@ -329,9 +358,7 @@ export default (routeConfigs, config = {}) => {
329358
330359 // Nested routers can be updated after switching children with actions such as SET_PARAMS
331360 // and COMPLETE_TRANSITION.
332- // NOTE: This may be problematic with custom routers because we whitelist the actions
333- // that can be handled by child routers without automatically changing index.
334- if ( childrenUpdateWithoutSwitchingIndex ( action . type ) ) {
361+ if ( action . preserveFocus ) {
335362 index = state . index ;
336363 }
337364
0 commit comments