@react-navigation/routers 7.5.6 → 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.
- package/lib/module/BaseRouter.js +50 -45
- package/lib/module/BaseRouter.js.map +1 -1
- package/lib/module/CommonActions.js +6 -0
- package/lib/module/CommonActions.js.map +1 -1
- package/lib/module/DrawerRouter.js +15 -14
- package/lib/module/DrawerRouter.js.map +1 -1
- package/lib/module/SwitchRouter.js +352 -0
- package/lib/module/SwitchRouter.js.map +1 -0
- package/lib/module/TabRouter.js +14 -300
- package/lib/module/TabRouter.js.map +1 -1
- package/lib/typescript/src/BaseRouter.d.ts +3 -1
- package/lib/typescript/src/BaseRouter.d.ts.map +1 -1
- package/lib/typescript/src/CommonActions.d.ts +3 -0
- package/lib/typescript/src/CommonActions.d.ts.map +1 -1
- package/lib/typescript/src/DrawerRouter.d.ts +1 -0
- package/lib/typescript/src/DrawerRouter.d.ts.map +1 -1
- package/lib/typescript/src/SwitchRouter.d.ts +78 -0
- package/lib/typescript/src/SwitchRouter.d.ts.map +1 -0
- package/lib/typescript/src/TabRouter.d.ts +4 -24
- package/lib/typescript/src/TabRouter.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/BaseRouter.tsx +74 -59
- package/src/CommonActions.tsx +7 -0
- package/src/DrawerRouter.tsx +21 -19
- package/src/SwitchRouter.tsx +580 -0
- package/src/TabRouter.tsx +24 -473
package/src/BaseRouter.tsx
CHANGED
|
@@ -6,79 +6,94 @@ import type {
|
|
|
6
6
|
PartialState,
|
|
7
7
|
} from './types';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
if (index === -1) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
46
|
-
const nextState = action.payload as State | PartialState<State>;
|
|
51
|
+
const routeNamesSet = new Set(state.routeNames);
|
|
47
52
|
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
nextState.
|
|
53
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
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';
|
package/src/CommonActions.tsx
CHANGED
|
@@ -90,6 +90,9 @@ export function navigate(
|
|
|
90
90
|
}
|
|
91
91
|
): Action;
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* @deprecated Use `navigate(name, params, options)` instead.
|
|
95
|
+
*/
|
|
93
96
|
export function navigate(options: {
|
|
94
97
|
name: string;
|
|
95
98
|
params?: object;
|
|
@@ -126,6 +129,10 @@ export function navigate(...args: any): Action {
|
|
|
126
129
|
);
|
|
127
130
|
}
|
|
128
131
|
|
|
132
|
+
console.warn(
|
|
133
|
+
`Passing an object as the argument to 'navigate' is deprecated. Use 'navigate(name, params, options)' instead.`
|
|
134
|
+
);
|
|
135
|
+
|
|
129
136
|
return { type: 'NAVIGATE', payload };
|
|
130
137
|
}
|
|
131
138
|
}
|
package/src/DrawerRouter.tsx
CHANGED
|
@@ -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 =
|
|
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 =
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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 (
|
|
229
|
-
|
|
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;
|