@react-navigation/routers 8.0.0-alpha.1 → 8.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/lib/module/BaseRouter.js +3 -2
  2. package/lib/module/BaseRouter.js.map +1 -1
  3. package/lib/module/CommonActions.js.map +1 -1
  4. package/lib/module/StackRouter.js +334 -149
  5. package/lib/module/StackRouter.js.map +1 -1
  6. package/lib/module/TabRouter.js +52 -11
  7. package/lib/module/TabRouter.js.map +1 -1
  8. package/lib/typescript/src/BaseRouter.d.ts +7 -7
  9. package/lib/typescript/src/BaseRouter.d.ts.map +1 -1
  10. package/lib/typescript/src/CommonActions.d.ts +35 -35
  11. package/lib/typescript/src/CommonActions.d.ts.map +1 -1
  12. package/lib/typescript/src/DrawerRouter.d.ts +5 -5
  13. package/lib/typescript/src/DrawerRouter.d.ts.map +1 -1
  14. package/lib/typescript/src/StackRouter.d.ts +41 -18
  15. package/lib/typescript/src/StackRouter.d.ts.map +1 -1
  16. package/lib/typescript/src/TabRouter.d.ts +6 -6
  17. package/lib/typescript/src/TabRouter.d.ts.map +1 -1
  18. package/lib/typescript/src/createParamsFromAction.d.ts +2 -2
  19. package/lib/typescript/src/createParamsFromAction.d.ts.map +1 -1
  20. package/lib/typescript/src/createRouteFromAction.d.ts +2 -2
  21. package/lib/typescript/src/createRouteFromAction.d.ts.map +1 -1
  22. package/lib/typescript/src/index.d.ts +9 -9
  23. package/lib/typescript/src/index.d.ts.map +1 -1
  24. package/lib/typescript/src/types.d.ts +17 -27
  25. package/lib/typescript/src/types.d.ts.map +1 -1
  26. package/package.json +7 -7
  27. package/src/BaseRouter.tsx +4 -4
  28. package/src/CommonActions.tsx +36 -34
  29. package/src/DrawerRouter.tsx +3 -3
  30. package/src/StackRouter.tsx +546 -187
  31. package/src/TabRouter.tsx +79 -18
  32. package/src/createParamsFromAction.tsx +1 -1
  33. package/src/createRouteFromAction.tsx +1 -1
  34. package/src/types.tsx +30 -39
package/src/TabRouter.tsx CHANGED
@@ -14,9 +14,9 @@ import type {
14
14
 
15
15
  export type TabActionType = {
16
16
  type: 'JUMP_TO';
17
- payload: { name: string; params?: object };
18
- source?: string;
19
- target?: string;
17
+ payload: { name: string; params?: object | undefined };
18
+ source?: string | undefined;
19
+ target?: string | undefined;
20
20
  };
21
21
 
22
22
  export type BackBehavior =
@@ -37,7 +37,7 @@ export type TabRouterOptions = DefaultRouterOptions & {
37
37
  * - `fullHistory` - return to last visited route; doesn't drop duplicate entries unlike `history` - matches behavior of web pages
38
38
  * - `none` - do not handle going back
39
39
  */
40
- backBehavior?: BackBehavior;
40
+ backBehavior?: BackBehavior | undefined;
41
41
  };
42
42
 
43
43
  export type TabNavigationState<ParamList extends ParamListBase> = Omit<
@@ -91,10 +91,16 @@ const getRouteHistory = (
91
91
  backBehavior: BackBehavior,
92
92
  initialRouteName: string | undefined
93
93
  ) => {
94
+ const route = routes[index];
95
+
96
+ if (route == null) {
97
+ throw new Error(`Couldn't find a route at index ${index}.`);
98
+ }
99
+
94
100
  const history = [
95
101
  {
96
102
  type: TYPE_ROUTE,
97
- key: routes[index].key,
103
+ key: route.key,
98
104
  },
99
105
  ];
100
106
 
@@ -103,17 +109,29 @@ const getRouteHistory = (
103
109
  switch (backBehavior) {
104
110
  case 'order':
105
111
  for (let i = index; i > 0; i--) {
112
+ const route = routes[i - 1];
113
+
114
+ if (route == null) {
115
+ throw new Error(`Couldn't find a route at index ${i - 1}.`);
116
+ }
117
+
106
118
  history.unshift({
107
119
  type: TYPE_ROUTE,
108
- key: routes[i - 1].key,
120
+ key: route.key,
109
121
  });
110
122
  }
111
123
  break;
112
124
  case 'firstRoute':
113
125
  if (index !== 0) {
126
+ const route = routes[0];
127
+
128
+ if (route == null) {
129
+ throw new Error("Couldn't find a route at index 0.");
130
+ }
131
+
114
132
  history.unshift({
115
133
  type: TYPE_ROUTE,
116
- key: routes[0].key,
134
+ key: route.key,
117
135
  });
118
136
  }
119
137
  break;
@@ -124,9 +142,17 @@ const getRouteHistory = (
124
142
  initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
125
143
 
126
144
  if (index !== initialRouteIndex) {
145
+ const route = routes[initialRouteIndex];
146
+
147
+ if (route == null) {
148
+ throw new Error(
149
+ `Couldn't find a route at index ${initialRouteIndex}.`
150
+ );
151
+ }
152
+
127
153
  history.unshift({
128
154
  type: TYPE_ROUTE,
129
- key: routes[initialRouteIndex].key,
155
+ key: route.key,
130
156
  });
131
157
  }
132
158
  break;
@@ -150,6 +176,10 @@ const changeIndex = (
150
176
  if (backBehavior === 'history' || backBehavior === 'fullHistory') {
151
177
  const currentRoute = state.routes[index];
152
178
 
179
+ if (currentRoute == null) {
180
+ throw new Error(`Couldn't find a route at index ${index}.`);
181
+ }
182
+
153
183
  if (backBehavior === 'history') {
154
184
  // Remove the existing key from the history to de-duplicate it
155
185
  history = history.filter((it) =>
@@ -266,8 +296,9 @@ export function TabRouter({
266
296
  } as Route<string>;
267
297
  });
268
298
 
299
+ const currentRoute = state.routes[state.index ?? 0];
269
300
  const index = Math.min(
270
- Math.max(routeNames.indexOf(state.routes[state?.index ?? 0]?.name), 0),
301
+ Math.max(currentRoute ? routeNames.indexOf(currentRoute.name) : 0, 0),
271
302
  routes.length - 1
272
303
  );
273
304
 
@@ -311,10 +342,13 @@ export function TabRouter({
311
342
  }
312
343
  );
313
344
 
314
- const index = Math.max(
315
- 0,
316
- routeNames.indexOf(state.routes[state.index].name)
317
- );
345
+ const currentRoute = state.routes[state.index];
346
+
347
+ if (currentRoute == null) {
348
+ throw new Error(`Couldn't find a route at index ${state.index}.`);
349
+ }
350
+
351
+ const index = Math.max(0, routeNames.indexOf(currentRoute.name));
318
352
 
319
353
  let history = state.history.filter(
320
354
  // Type will always be 'route' for tabs, but could be different in a router extending this (e.g. drawer)
@@ -414,10 +448,18 @@ export function TabRouter({
414
448
  initialRouteName
415
449
  );
416
450
 
451
+ const updatedRoute = updatedState.routes[updatedState.index];
452
+
453
+ if (updatedRoute == null) {
454
+ throw new Error(
455
+ `Couldn't find a route at index ${updatedState.index}.`
456
+ );
457
+ }
458
+
417
459
  return {
418
460
  ...updatedState,
419
461
  preloadedRouteKeys: updatedState.preloadedRouteKeys.filter(
420
- (key) => key !== state.routes[updatedState.index].key
462
+ (key) => key !== updatedRoute.key
421
463
  ),
422
464
  };
423
465
  }
@@ -425,6 +467,10 @@ export function TabRouter({
425
467
  case 'GO_BACK': {
426
468
  const focusedRoute = state.routes[state.index];
427
469
 
470
+ if (focusedRoute == null) {
471
+ throw new Error(`Couldn't find a route at index ${state.index}.`);
472
+ }
473
+
428
474
  if (state.history.length === 1 && !focusedRoute.history?.length) {
429
475
  return null;
430
476
  }
@@ -453,7 +499,12 @@ export function TabRouter({
453
499
  }
454
500
 
455
501
  const previousHistoryItem = state.history[state.history.length - 2];
456
- const previousKey = previousHistoryItem?.key;
502
+
503
+ if (previousHistoryItem == null) {
504
+ return null;
505
+ }
506
+
507
+ const previousKey = previousHistoryItem.key;
457
508
  const index = state.routes.findLastIndex(
458
509
  (route) => route.key === previousKey
459
510
  );
@@ -464,13 +515,19 @@ export function TabRouter({
464
515
 
465
516
  let routes = state.routes;
466
517
 
518
+ const previousRoute = routes[index];
519
+
520
+ if (previousRoute == null) {
521
+ throw new Error(`Couldn't find a route at index ${index}.`);
522
+ }
523
+
467
524
  if (
468
525
  backBehavior === 'fullHistory' &&
469
- routes[index].params !== previousHistoryItem.params
526
+ previousRoute.params !== previousHistoryItem.params
470
527
  ) {
471
528
  routes = [...state.routes];
472
529
  routes[index] = {
473
- ...routes[index],
530
+ ...previousRoute,
474
531
  params: previousHistoryItem.params,
475
532
  };
476
533
  }
@@ -479,7 +536,7 @@ export function TabRouter({
479
536
  ...state,
480
537
  routes,
481
538
  preloadedRouteKeys: state.preloadedRouteKeys.filter(
482
- (key) => key !== state.routes[index].key
539
+ (key) => key !== previousRoute.key
483
540
  ),
484
541
  history: state.history.slice(0, -1),
485
542
  index,
@@ -497,6 +554,10 @@ export function TabRouter({
497
554
 
498
555
  const route = state.routes[routeIndex];
499
556
 
557
+ if (route == null) {
558
+ throw new Error(`Couldn't find a route at index ${routeIndex}.`);
559
+ }
560
+
500
561
  const getId = routeGetIdList[route.name];
501
562
 
502
563
  const currentId = getId?.({ params: route.params });
@@ -4,7 +4,7 @@ type Options = {
4
4
  action: {
5
5
  payload: {
6
6
  name: string;
7
- params?: object;
7
+ params?: object | undefined;
8
8
  };
9
9
  };
10
10
  routeParamList: ParamListBase;
@@ -7,7 +7,7 @@ type Options = {
7
7
  action: {
8
8
  payload: {
9
9
  name: string;
10
- params?: object;
10
+ params?: object | undefined;
11
11
  };
12
12
  };
13
13
  routeParamList: ParamListBase;
package/src/types.tsx CHANGED
@@ -6,7 +6,7 @@ export type NavigationRoute<
6
6
  ParamList extends ParamListBase,
7
7
  RouteName extends keyof ParamList,
8
8
  > = Route<Extract<RouteName, string>, ParamList[RouteName]> & {
9
- state?: NavigationState | PartialState<NavigationState>;
9
+ state?: NavigationState | PartialState<NavigationState> | undefined;
10
10
  };
11
11
 
12
12
  export type NavigationState<ParamList extends ParamListBase = ParamListBase> =
@@ -26,7 +26,7 @@ export type NavigationState<ParamList extends ParamListBase = ParamListBase> =
26
26
  /**
27
27
  * Alternative entries for history.
28
28
  */
29
- history?: unknown[];
29
+ history?: unknown[] | undefined;
30
30
  /**
31
31
  * List of rendered routes.
32
32
  */
@@ -45,20 +45,22 @@ export type NavigationState<ParamList extends ParamListBase = ParamListBase> =
45
45
 
46
46
  export type InitialState = Readonly<
47
47
  Partial<Omit<NavigationState, 'stale' | 'routes'>> & {
48
- routes: (Omit<Route<string>, 'key'> & { state?: InitialState })[];
48
+ routes: (Omit<Route<string>, 'key'> & {
49
+ state?: InitialState | undefined;
50
+ })[];
49
51
  }
50
52
  >;
51
53
 
52
54
  export type PartialRoute<R extends Route<string>> = Omit<R, 'key'> & {
53
- key?: string;
54
- state?: PartialState<NavigationState>;
55
+ key?: string | undefined;
56
+ state?: PartialState<NavigationState> | undefined;
55
57
  };
56
58
 
57
59
  export type PartialState<State extends NavigationState> = Partial<
58
60
  Omit<State, 'stale' | 'routes'>
59
61
  > &
60
62
  Readonly<{
61
- stale?: true;
63
+ stale?: true | undefined;
62
64
  routes: PartialRoute<Route<State['routeNames'][number]>>[];
63
65
  }>;
64
66
 
@@ -79,26 +81,24 @@ export type Route<
79
81
  * Path associated with the route.
80
82
  * Usually present when the screen was opened from a deep link.
81
83
  */
82
- path?: string;
84
+ path?: string | undefined;
83
85
  /**
84
86
  * History of param changes for this route.
85
87
  */
86
- history?: { type: 'params'; params: object }[];
87
- } & Readonly<
88
- undefined extends Params
89
- ? {
90
- /**
91
- * Params for this route
92
- */
93
- params?: Readonly<Params>;
94
- }
95
- : {
96
- /**
97
- * Params for this route
98
- */
99
- params: Readonly<Params>;
100
- }
101
- >
88
+ history?: { type: 'params'; params: object }[] | undefined;
89
+ } & (undefined extends Params
90
+ ? {
91
+ /**
92
+ * Params for this route
93
+ */
94
+ params?: Readonly<Params> | undefined;
95
+ }
96
+ : {
97
+ /**
98
+ * Params for this route
99
+ */
100
+ params: Readonly<Params>;
101
+ })
102
102
  >;
103
103
 
104
104
  export type ParamListBase = Record<string, object | undefined>;
@@ -111,15 +111,15 @@ export type NavigationAction = Readonly<{
111
111
  /**
112
112
  * Additional data for the action
113
113
  */
114
- payload?: object;
114
+ payload?: object | undefined;
115
115
  /**
116
116
  * Key of the route which dispatched this action.
117
117
  */
118
- source?: string;
118
+ source?: string | undefined;
119
119
  /**
120
120
  * Key of the navigator which should handle this action.
121
121
  */
122
- target?: string;
122
+ target?: string | undefined;
123
123
  }>;
124
124
 
125
125
  export type ActionCreators<Action extends NavigationAction> = {
@@ -131,7 +131,7 @@ export type DefaultRouterOptions<RouteName extends string = string> = {
131
131
  * Name of the route to focus by on initial render.
132
132
  * If not specified, usually the first route is used.
133
133
  */
134
- initialRouteName?: RouteName;
134
+ initialRouteName?: RouteName | undefined;
135
135
  };
136
136
 
137
137
  export type RouterFactory<
@@ -145,7 +145,9 @@ export type RouterConfigOptions = {
145
145
  routeParamList: ParamListBase;
146
146
  routeGetIdList: Record<
147
147
  string,
148
- | ((options: { params?: Record<string, any> }) => string | undefined)
148
+ | ((options: {
149
+ params?: Record<string, any> | undefined;
150
+ }) => string | undefined)
149
151
  | undefined
150
152
  >;
151
153
  };
@@ -228,17 +230,6 @@ export type Router<
228
230
  */
229
231
  shouldActionChangeFocus(action: NavigationAction): boolean;
230
232
 
231
- /**
232
- * Get the list of routes from the navigation state.
233
- *
234
- * By default, `state.routes` is used.
235
- * But in some cases (like `preloadedRoutes` in stack),
236
- * the actual list of routes may be different.
237
- *
238
- * @param state Navigation state to get the routes from.
239
- */
240
- getRoutesFromState?(state: State): Route<string>[];
241
-
242
233
  /**
243
234
  * Action creators for the router.
244
235
  */