99 ParamListBase ,
1010 Router ,
1111 createNavigator ,
12+ TargetRoute ,
1213} from '../src/index' ;
1314
1415type Props = {
@@ -18,7 +19,7 @@ type Props = {
1819
1920type Action = {
2021 type : 'JUMP_TO' ;
21- payload : { name : string ; params ?: object } ;
22+ payload : { name ?: string ; key ? : string ; params ?: object } ;
2223} ;
2324
2425export type TabNavigationProp <
@@ -31,10 +32,10 @@ export type TabNavigationProp<
3132 * @param name Name of the route for the tab.
3233 * @param [params] Params object for the route.
3334 */
34- jumpTo < RouteName extends keyof ParamList > (
35+ jumpTo < RouteName extends Extract < keyof ParamList , string > > (
3536 ...args : ParamList [ RouteName ] extends void
36- ? [ RouteName ]
37- : [ RouteName , ParamList [ RouteName ] ]
37+ ? [ TargetRoute < RouteName > ]
38+ : [ TargetRoute < RouteName > , ParamList [ RouteName ] ]
3839 ) : void ;
3940} ;
4041
@@ -98,35 +99,44 @@ const TabRouter: Router<Action | CommonAction> = {
9899 } ,
99100
100101 getStateForAction ( state , action ) {
102+ let index = - 1 ;
101103 switch ( action . type ) {
102104 case 'JUMP_TO' :
103105 case 'NAVIGATE' :
104- if ( state . routeNames . includes ( action . payload . name ) ) {
105- const index = state . routes . findIndex (
106+ if ( action . payload . key ) {
107+ index = state . routes . findIndex (
108+ route => route . key === action . payload . key
109+ ) ;
110+ }
111+
112+ if ( action . payload . name ) {
113+ index = state . routes . findIndex (
106114 route => route . name === action . payload . name
107115 ) ;
116+ }
108117
109- return {
110- ...state ,
111- routes :
112- action . payload . params !== undefined
113- ? state . routes . map ( ( route , i ) =>
114- i === index
115- ? {
116- ...route ,
117- params : {
118- ...route . params ,
119- ...action . payload . params ,
120- } ,
121- }
122- : route
123- )
124- : state . routes ,
125- index,
126- } ;
118+ if ( index == - 1 ) {
119+ return null ;
127120 }
128121
129- return null ;
122+ return {
123+ ...state ,
124+ routes :
125+ action . payload . params !== undefined
126+ ? state . routes . map ( ( route , i ) =>
127+ i === index
128+ ? {
129+ ...route ,
130+ params : {
131+ ...route . params ,
132+ ...action . payload . params ,
133+ } ,
134+ }
135+ : route
136+ )
137+ : state . routes ,
138+ index,
139+ } ;
130140
131141 case 'REPLACE' : {
132142 return {
@@ -171,8 +181,20 @@ const TabRouter: Router<Action | CommonAction> = {
171181 } ,
172182
173183 actionCreators : {
174- jumpTo ( name : string , params ?: object ) {
175- return { type : 'JUMP_TO' , payload : { name, params } } ;
184+ jumpTo ( target : TargetRoute < string > , params ?: object ) {
185+ if ( typeof target === 'string' ) {
186+ return { type : 'JUMP_TO' , payload : { name : target , params } } ;
187+ } else {
188+ if (
189+ ( target . hasOwnProperty ( 'key' ) && target . hasOwnProperty ( 'name' ) ) ||
190+ ( ! target . hasOwnProperty ( 'key' ) && ! target . hasOwnProperty ( 'name' ) )
191+ ) {
192+ throw new Error (
193+ 'While calling jumpTo you need to specify either name or key'
194+ ) ;
195+ }
196+ return { type : 'JUMP_TO' , payload : { ...target , params } } ;
197+ }
176198 } ,
177199 } ,
178200} ;
0 commit comments