@react-navigation/routers 7.6.0 → 7.6.1

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.
@@ -6,79 +6,94 @@ import type {
6
6
  PartialState,
7
7
  } from './types';
8
8
 
9
- /**
10
- * Base router object that can be used when writing custom routers.
11
- * This provides few helper methods to handle common actions such as `RESET`.
12
- */
13
- export const BaseRouter = {
14
- getStateForAction<State extends NavigationState>(
15
- state: State,
16
- action: CommonNavigationAction
17
- ): State | PartialState<State> | null {
18
- switch (action.type) {
19
- case 'SET_PARAMS':
20
- case 'REPLACE_PARAMS': {
21
- const index = action.source
22
- ? state.routes.findIndex((r) => r.key === action.source)
23
- : state.index;
9
+ function getStateForAction<State extends NavigationState>(
10
+ state: State,
11
+ action: CommonNavigationAction
12
+ ): State | PartialState<State> | null;
13
+ function getStateForAction(
14
+ state: NavigationState,
15
+ action: CommonNavigationAction
16
+ ) {
17
+ switch (action.type) {
18
+ case 'SET_PARAMS':
19
+ case 'REPLACE_PARAMS': {
20
+ const index = action.source
21
+ ? state.routes.findIndex((r) => r.key === action.source)
22
+ : state.index;
24
23
 
25
- if (index === -1) {
26
- return null;
27
- }
24
+ if (index === -1) {
25
+ return null;
26
+ }
28
27
 
29
- return {
30
- ...state,
31
- routes: state.routes.map((r, i) =>
32
- i === index
33
- ? {
34
- ...r,
35
- params:
36
- action.type === 'REPLACE_PARAMS'
37
- ? action.payload.params
38
- : { ...r.params, ...action.payload.params },
39
- }
40
- : r
41
- ),
42
- };
28
+ return {
29
+ ...state,
30
+ routes: state.routes.map((r, i) =>
31
+ i === index
32
+ ? {
33
+ ...r,
34
+ params:
35
+ action.type === 'REPLACE_PARAMS'
36
+ ? action.payload.params
37
+ : { ...r.params, ...action.payload.params },
38
+ }
39
+ : r
40
+ ),
41
+ };
42
+ }
43
+
44
+ case 'RESET': {
45
+ const nextState = action.payload;
46
+
47
+ if (nextState == null) {
48
+ return null;
43
49
  }
44
50
 
45
- case 'RESET': {
46
- const nextState = action.payload as State | PartialState<State>;
51
+ const routeNamesSet = new Set(state.routeNames);
47
52
 
48
- const routeNamesSet = new Set(state.routeNames);
53
+ if (
54
+ nextState.routes.length === 0 ||
55
+ nextState.routes.some(
56
+ (route: { name: string }) => !routeNamesSet.has(route.name)
57
+ )
58
+ ) {
59
+ return null;
60
+ }
49
61
 
62
+ if (nextState.stale === false) {
50
63
  if (
51
- nextState.routes.length === 0 ||
52
- nextState.routes.some(
53
- (route: { name: string }) => !routeNamesSet.has(route.name)
54
- )
64
+ state.routeNames.length !== nextState.routeNames.length ||
65
+ nextState.routeNames.some((name) => !routeNamesSet.has(name)) ||
66
+ !Number.isInteger(nextState.index) ||
67
+ nextState.index < 0 ||
68
+ nextState.index >= nextState.routes.length
55
69
  ) {
56
70
  return null;
57
71
  }
58
72
 
59
- if (nextState.stale === false) {
60
- if (
61
- state.routeNames.length !== nextState.routeNames.length ||
62
- nextState.routeNames.some((name) => !routeNamesSet.has(name))
63
- ) {
64
- return null;
65
- }
66
-
67
- return {
68
- ...nextState,
69
- routes: nextState.routes.map((route) =>
70
- route.key ? route : { ...route, key: `${route.name}-${nanoid()}` }
71
- ),
72
- };
73
- }
74
-
75
- return nextState;
73
+ return {
74
+ ...nextState,
75
+ routes: nextState.routes.map((route) =>
76
+ 'key' in route && route.key
77
+ ? route
78
+ : { ...route, key: `${route.name}-${nanoid()}` }
79
+ ),
80
+ };
76
81
  }
77
82
 
78
- default:
79
- return null;
83
+ return nextState;
80
84
  }
81
- },
85
+
86
+ default:
87
+ return null;
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Base router object that can be used when writing custom routers.
93
+ * This provides few helper methods to handle common actions such as `RESET`.
94
+ */
95
+ export const BaseRouter = {
96
+ getStateForAction,
82
97
 
83
98
  shouldActionChangeFocus(action: CommonNavigationAction) {
84
99
  return action.type === 'NAVIGATE' || action.type === 'NAVIGATE_DEPRECATED';
@@ -1,11 +1,11 @@
1
1
  import { nanoid } from 'nanoid/non-secure';
2
2
 
3
+ import { SwitchRouter } from './SwitchRouter';
3
4
  import {
4
5
  type TabActionHelpers,
5
6
  TabActions,
6
7
  type TabActionType,
7
8
  type TabNavigationState,
8
- TabRouter,
9
9
  type TabRouterOptions,
10
10
  } from './TabRouter';
11
11
  import type {
@@ -14,6 +14,7 @@ import type {
14
14
  PartialState,
15
15
  Router,
16
16
  } from './types';
17
+
17
18
  export type DrawerStatus = 'open' | 'closed';
18
19
 
19
20
  export type DrawerActionType =
@@ -44,7 +45,7 @@ export type DrawerNavigationState<ParamList extends ParamListBase> = Omit<
44
45
  * List of previously visited route keys and drawer open status.
45
46
  */
46
47
  history: (
47
- | { type: 'route'; key: string }
48
+ | { type: 'route'; key: string; params?: object | undefined }
48
49
  | { type: 'drawer'; status: DrawerStatus }
49
50
  )[];
50
51
  };
@@ -87,10 +88,7 @@ export function DrawerRouter({
87
88
  DrawerNavigationState<ParamListBase>,
88
89
  DrawerActionType | CommonNavigationAction
89
90
  > {
90
- const router = TabRouter(rest) as unknown as Router<
91
- DrawerNavigationState<ParamListBase>,
92
- TabActionType | CommonNavigationAction
93
- >;
91
+ const router = SwitchRouter<'drawer'>(rest);
94
92
 
95
93
  const isDrawerInHistory = (
96
94
  state:
@@ -179,11 +177,16 @@ export function DrawerRouter({
179
177
  return partialState;
180
178
  }
181
179
 
182
- let state = router.getRehydratedState(partialState, {
183
- routeNames,
184
- routeParamList,
185
- routeGetIdList,
186
- });
180
+ let state: DrawerNavigationState<ParamListBase> = {
181
+ ...router.getRehydratedState(partialState, {
182
+ routeNames,
183
+ routeParamList,
184
+ routeGetIdList,
185
+ }),
186
+ default: defaultStatus,
187
+ type: 'drawer',
188
+ key: `drawer-${nanoid()}`,
189
+ };
187
190
 
188
191
  if (isDrawerInHistory(partialState)) {
189
192
  // Re-sync the drawer entry in history to correct it if it was wrong
@@ -191,12 +194,7 @@ export function DrawerRouter({
191
194
  state = addDrawerToHistory(state);
192
195
  }
193
196
 
194
- return {
195
- ...state,
196
- default: defaultStatus,
197
- type: 'drawer',
198
- key: `drawer-${nanoid()}`,
199
- };
197
+ return state;
200
198
  },
201
199
 
202
200
  getStateForRouteFocus(state, key) {
@@ -225,8 +223,12 @@ export function DrawerRouter({
225
223
  case 'NAVIGATE_DEPRECATED': {
226
224
  const result = router.getStateForAction(state, action, options);
227
225
 
228
- if (result != null && result.index !== state.index) {
229
- return closeDrawer(result as DrawerNavigationState<ParamListBase>);
226
+ if (
227
+ result != null &&
228
+ result.stale === false &&
229
+ result.index !== state.index
230
+ ) {
231
+ return closeDrawer(result);
230
232
  }
231
233
 
232
234
  return result;