@amazon-devices/react-navigation__routers 8.0.9000000000 → 8.0.9000000002

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.
@@ -1,328 +0,0 @@
1
- "use strict";
2
-
3
- import { nanoid } from 'nanoid/non-secure';
4
- import { BaseRouter } from "./BaseRouter.js";
5
- import { createParamsFromAction } from "./createParamsFromAction.js";
6
- const TYPE_ROUTE = 'route';
7
- export const TabActions = {
8
- jumpTo(name, params) {
9
- return {
10
- type: 'JUMP_TO',
11
- payload: {
12
- name,
13
- params
14
- }
15
- };
16
- }
17
- };
18
- const getRouteHistory = (routes, index, backBehavior, initialRouteName) => {
19
- const history = [{
20
- type: TYPE_ROUTE,
21
- key: routes[index].key
22
- }];
23
- let initialRouteIndex;
24
- switch (backBehavior) {
25
- case 'order':
26
- for (let i = index; i > 0; i--) {
27
- history.unshift({
28
- type: TYPE_ROUTE,
29
- key: routes[i - 1].key
30
- });
31
- }
32
- break;
33
- case 'firstRoute':
34
- if (index !== 0) {
35
- history.unshift({
36
- type: TYPE_ROUTE,
37
- key: routes[0].key
38
- });
39
- }
40
- break;
41
- case 'initialRoute':
42
- initialRouteIndex = routes.findIndex(route => route.name === initialRouteName);
43
- initialRouteIndex = initialRouteIndex === -1 ? 0 : initialRouteIndex;
44
- if (index !== initialRouteIndex) {
45
- history.unshift({
46
- type: TYPE_ROUTE,
47
- key: routes[initialRouteIndex].key
48
- });
49
- }
50
- break;
51
- case 'history':
52
- case 'fullHistory':
53
- // The history will fill up on navigation
54
- break;
55
- }
56
- return history;
57
- };
58
- const changeIndex = (state, index, backBehavior, initialRouteName) => {
59
- let history = state.history;
60
- if (backBehavior === 'history' || backBehavior === 'fullHistory') {
61
- const currentRoute = state.routes[index];
62
- if (backBehavior === 'history') {
63
- // Remove the existing key from the history to de-duplicate it
64
- history = history.filter(it => it.type === 'route' ? it.key !== currentRoute.key : false);
65
- } else if (backBehavior === 'fullHistory') {
66
- const lastHistoryRouteItemIndex = history.findLastIndex(item => item.type === 'route');
67
- if (currentRoute.key === history[lastHistoryRouteItemIndex]?.key) {
68
- // For full-history, only remove if it matches the last route
69
- // Useful for drawer, if current route was in history, then drawer state changed
70
- // Then we only need to move the route to the front
71
- history = [...history.slice(0, lastHistoryRouteItemIndex), ...history.slice(lastHistoryRouteItemIndex + 1)];
72
- }
73
- }
74
- history = history.concat({
75
- type: TYPE_ROUTE,
76
- key: currentRoute.key,
77
- params: backBehavior === 'fullHistory' ? currentRoute.params : undefined
78
- });
79
- } else {
80
- history = getRouteHistory(state.routes, index, backBehavior, initialRouteName);
81
- }
82
- return {
83
- ...state,
84
- index,
85
- history
86
- };
87
- };
88
- export function TabRouter({
89
- initialRouteName,
90
- backBehavior = 'firstRoute'
91
- }) {
92
- const router = {
93
- ...BaseRouter,
94
- type: 'tab',
95
- getInitialState({
96
- routeNames,
97
- routeParamList
98
- }) {
99
- const index = initialRouteName !== undefined && routeNames.includes(initialRouteName) ? routeNames.indexOf(initialRouteName) : 0;
100
- const routes = routeNames.map(name => ({
101
- name,
102
- key: `${name}-${nanoid()}`,
103
- params: routeParamList[name]
104
- }));
105
- const history = getRouteHistory(routes, index, backBehavior, initialRouteName);
106
- return {
107
- stale: false,
108
- type: 'tab',
109
- key: `tab-${nanoid()}`,
110
- index,
111
- routeNames,
112
- history,
113
- routes,
114
- preloadedRouteKeys: []
115
- };
116
- },
117
- getRehydratedState(partialState, {
118
- routeNames,
119
- routeParamList
120
- }) {
121
- const state = partialState;
122
- if (state.stale === false) {
123
- return state;
124
- }
125
- const routes = routeNames.map(name => {
126
- const route = state.routes.find(r => r.name === name);
127
- return {
128
- ...route,
129
- name,
130
- key: route && route.name === name && route.key ? route.key : `${name}-${nanoid()}`,
131
- params: routeParamList[name] !== undefined ? {
132
- ...routeParamList[name],
133
- ...(route ? route.params : undefined)
134
- } : route ? route.params : undefined
135
- };
136
- });
137
- const index = Math.min(Math.max(routeNames.indexOf(state.routes[state?.index ?? 0]?.name), 0), routes.length - 1);
138
- const routeKeys = routes.map(route => route.key);
139
- const history = state.history?.filter(it => routeKeys.includes(it.key)) ?? [];
140
- return changeIndex({
141
- stale: false,
142
- type: 'tab',
143
- key: `tab-${nanoid()}`,
144
- index,
145
- routeNames,
146
- history,
147
- routes,
148
- preloadedRouteKeys: state.preloadedRouteKeys?.filter(key => routeKeys.includes(key)) ?? []
149
- }, index, backBehavior, initialRouteName);
150
- },
151
- getStateForRouteNamesChange(state, {
152
- routeNames,
153
- routeParamList,
154
- routeKeyChanges
155
- }) {
156
- const routes = routeNames.map(name => state.routes.find(r => r.name === name && !routeKeyChanges.includes(r.name)) || {
157
- name,
158
- key: `${name}-${nanoid()}`,
159
- params: routeParamList[name]
160
- });
161
- const index = Math.max(0, routeNames.indexOf(state.routes[state.index].name));
162
- let history = state.history.filter(
163
- // Type will always be 'route' for tabs, but could be different in a router extending this (e.g. drawer)
164
- it => it.type !== 'route' || routes.find(r => r.key === it.key));
165
- if (!history.length) {
166
- history = getRouteHistory(routes, index, backBehavior, initialRouteName);
167
- }
168
- return {
169
- ...state,
170
- history,
171
- routeNames,
172
- routes,
173
- index
174
- };
175
- },
176
- getStateForRouteFocus(state, key) {
177
- const index = state.routes.findIndex(r => r.key === key);
178
- if (index === -1 || index === state.index) {
179
- return state;
180
- }
181
- return changeIndex(state, index, backBehavior, initialRouteName);
182
- },
183
- getStateForAction(state, action, {
184
- routeParamList,
185
- routeGetIdList
186
- }) {
187
- switch (action.type) {
188
- case 'JUMP_TO':
189
- case 'NAVIGATE':
190
- case 'NAVIGATE_DEPRECATED':
191
- {
192
- const index = state.routes.findIndex(route => route.name === action.payload.name);
193
- if (index === -1) {
194
- return null;
195
- }
196
- const updatedState = changeIndex({
197
- ...state,
198
- routes: state.routes.map(route => {
199
- if (route.name !== action.payload.name) {
200
- return route;
201
- }
202
- const getId = routeGetIdList[route.name];
203
- const currentId = getId?.({
204
- params: route.params
205
- });
206
- const nextId = getId?.({
207
- params: action.payload.params
208
- });
209
- const key = currentId === nextId ? route.key : `${route.name}-${nanoid()}`;
210
- let params;
211
- if ((action.type === 'NAVIGATE' || action.type === 'NAVIGATE_DEPRECATED') && action.payload.merge && currentId === nextId) {
212
- params = action.payload.params !== undefined || routeParamList[route.name] !== undefined ? {
213
- ...routeParamList[route.name],
214
- ...route.params,
215
- ...action.payload.params
216
- } : route.params;
217
- } else {
218
- params = createParamsFromAction({
219
- action,
220
- routeParamList
221
- });
222
- }
223
- const path = action.type === 'NAVIGATE' && action.payload.path != null ? action.payload.path : route.path;
224
- return params !== route.params || path !== route.path ? {
225
- ...route,
226
- key,
227
- path,
228
- params
229
- } : route;
230
- })
231
- }, index, backBehavior, initialRouteName);
232
- return {
233
- ...updatedState,
234
- preloadedRouteKeys: updatedState.preloadedRouteKeys.filter(key => key !== state.routes[updatedState.index].key)
235
- };
236
- }
237
- case 'SET_PARAMS':
238
- case 'REPLACE_PARAMS':
239
- {
240
- const nextState = BaseRouter.getStateForAction(state, action);
241
- if (nextState !== null) {
242
- const index = nextState.index;
243
- if (index != null) {
244
- const focusedRoute = nextState.routes[index];
245
- const historyItemIndex = state.history.findLastIndex(item => item.key === focusedRoute.key);
246
- let updatedHistory = state.history;
247
- if (historyItemIndex !== -1) {
248
- updatedHistory = [...state.history];
249
- updatedHistory[historyItemIndex] = {
250
- ...updatedHistory[historyItemIndex],
251
- params: focusedRoute.params
252
- };
253
- }
254
- return {
255
- ...nextState,
256
- history: updatedHistory
257
- };
258
- }
259
- }
260
- return nextState;
261
- }
262
- case 'GO_BACK':
263
- {
264
- if (state.history.length === 1) {
265
- return null;
266
- }
267
- const previousHistoryItem = state.history[state.history.length - 2];
268
- const previousKey = previousHistoryItem?.key;
269
- const index = state.routes.findLastIndex(route => route.key === previousKey);
270
- if (index === -1) {
271
- return null;
272
- }
273
- let routes = state.routes;
274
- if (backBehavior === 'fullHistory' && routes[index].params !== previousHistoryItem.params) {
275
- routes = [...state.routes];
276
- routes[index] = {
277
- ...routes[index],
278
- params: previousHistoryItem.params
279
- };
280
- }
281
- return {
282
- ...state,
283
- routes,
284
- preloadedRouteKeys: state.preloadedRouteKeys.filter(key => key !== state.routes[index].key),
285
- history: state.history.slice(0, -1),
286
- index
287
- };
288
- }
289
- case 'PRELOAD':
290
- {
291
- const routeIndex = state.routes.findIndex(route => route.name === action.payload.name);
292
- if (routeIndex === -1) {
293
- return null;
294
- }
295
- const route = state.routes[routeIndex];
296
- const getId = routeGetIdList[route.name];
297
- const currentId = getId?.({
298
- params: route.params
299
- });
300
- const nextId = getId?.({
301
- params: action.payload.params
302
- });
303
- const key = currentId === nextId ? route.key : `${route.name}-${nanoid()}`;
304
- const params = createParamsFromAction({
305
- action,
306
- routeParamList
307
- });
308
- const newRoute = params !== route.params ? {
309
- ...route,
310
- key,
311
- params
312
- } : route;
313
- return {
314
- ...state,
315
- preloadedRouteKeys: state.preloadedRouteKeys.filter(key => key !== route.key).concat(newRoute.key),
316
- routes: state.routes.map((route, index) => index === routeIndex ? newRoute : route),
317
- history: key === route.key ? state.history : state.history.filter(record => record.key !== route.key)
318
- };
319
- }
320
- default:
321
- return BaseRouter.getStateForAction(state, action);
322
- }
323
- },
324
- actionCreators: TabActions
325
- };
326
- return router;
327
- }
328
- //# sourceMappingURL=TabRouter.js.map
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- export function createParamsFromAction({
4
- action,
5
- routeParamList
6
- }) {
7
- const {
8
- name,
9
- params
10
- } = action.payload;
11
- return routeParamList[name] !== undefined ? {
12
- ...routeParamList[name],
13
- ...params
14
- } : params;
15
- }
16
- //# sourceMappingURL=createParamsFromAction.js.map
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- import { nanoid } from 'nanoid/non-secure';
4
- import { createParamsFromAction } from "./createParamsFromAction.js";
5
- export function createRouteFromAction({
6
- action,
7
- routeParamList
8
- }) {
9
- const {
10
- name
11
- } = action.payload;
12
- return {
13
- key: `${name}-${nanoid()}`,
14
- name,
15
- params: createParamsFromAction({
16
- action,
17
- routeParamList
18
- })
19
- };
20
- }
21
- //# sourceMappingURL=createRouteFromAction.js.map
@@ -1,4 +0,0 @@
1
- "use strict";
2
-
3
- export {};
4
- //# sourceMappingURL=types.js.map
@@ -1,86 +0,0 @@
1
- import { nanoid } from 'nanoid/non-secure';
2
-
3
- import type {
4
- CommonNavigationAction,
5
- NavigationState,
6
- PartialState,
7
- } from './types';
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;
24
-
25
- if (index === -1) {
26
- return null;
27
- }
28
-
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
- };
43
- }
44
-
45
- case 'RESET': {
46
- const nextState = action.payload as State | PartialState<State>;
47
-
48
- if (
49
- nextState.routes.length === 0 ||
50
- nextState.routes.some(
51
- (route: { name: string }) => !state.routeNames.includes(route.name)
52
- )
53
- ) {
54
- return null;
55
- }
56
-
57
- if (nextState.stale === false) {
58
- if (
59
- state.routeNames.length !== nextState.routeNames.length ||
60
- nextState.routeNames.some(
61
- (name) => !state.routeNames.includes(name)
62
- )
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;
76
- }
77
-
78
- default:
79
- return null;
80
- }
81
- },
82
-
83
- shouldActionChangeFocus(action: CommonNavigationAction) {
84
- return action.type === 'NAVIGATE' || action.type === 'NAVIGATE_DEPRECATED';
85
- },
86
- };
@@ -1,180 +0,0 @@
1
- import type { NavigationState, PartialState, Route } from './types';
2
-
3
- type ResetState =
4
- | PartialState<NavigationState>
5
- | NavigationState
6
- | (Omit<NavigationState, 'routes'> & {
7
- routes: Omit<Route<string>, 'key'>[];
8
- });
9
-
10
- type GoBackAction = {
11
- type: 'GO_BACK';
12
- source?: string;
13
- target?: string;
14
- };
15
-
16
- type NavigateAction = {
17
- type: 'NAVIGATE';
18
- payload: {
19
- name: string;
20
- params?: object;
21
- path?: string;
22
- merge?: boolean;
23
- pop?: boolean;
24
- };
25
- source?: string;
26
- target?: string;
27
- };
28
-
29
- type NavigateDeprecatedAction = {
30
- type: 'NAVIGATE_DEPRECATED';
31
- payload: {
32
- name: string;
33
- params?: object;
34
- merge?: boolean;
35
- };
36
- source?: string;
37
- target?: string;
38
- };
39
-
40
- type ResetAction = {
41
- type: 'RESET';
42
- payload: ResetState | undefined;
43
- source?: string;
44
- target?: string;
45
- };
46
-
47
- type SetParamsAction = {
48
- type: 'SET_PARAMS';
49
- payload: { params?: object };
50
- source?: string;
51
- target?: string;
52
- };
53
-
54
- type ReplaceParamsAction = {
55
- type: 'REPLACE_PARAMS';
56
- payload: { params?: object };
57
- source?: string;
58
- target?: string;
59
- };
60
-
61
- type PreloadAction = {
62
- type: 'PRELOAD';
63
- payload: {
64
- name: string;
65
- params?: object;
66
- };
67
- source?: string;
68
- target?: string;
69
- };
70
-
71
- export type Action =
72
- | GoBackAction
73
- | NavigateAction
74
- | NavigateDeprecatedAction
75
- | ResetAction
76
- | SetParamsAction
77
- | ReplaceParamsAction
78
- | PreloadAction;
79
-
80
- export function goBack(): Action {
81
- return { type: 'GO_BACK' };
82
- }
83
-
84
- export function navigate(
85
- name: string,
86
- params?: object,
87
- options?: {
88
- merge?: boolean;
89
- pop?: boolean;
90
- }
91
- ): Action;
92
-
93
- export function navigate(options: {
94
- name: string;
95
- params?: object;
96
- path?: string;
97
- merge?: boolean;
98
- pop?: boolean;
99
- }): Action;
100
-
101
- export function navigate(...args: any): Action {
102
- if (typeof args[0] === 'string') {
103
- const [name, params, options] = args;
104
-
105
- if (typeof options === 'boolean') {
106
- console.warn(
107
- `Passing a boolean as the third argument to 'navigate' is deprecated. Pass '{ merge: true }' instead.`
108
- );
109
- }
110
-
111
- return {
112
- type: 'NAVIGATE',
113
- payload: {
114
- name,
115
- params,
116
- merge: typeof options === 'boolean' ? options : options?.merge,
117
- pop: options?.pop,
118
- },
119
- };
120
- } else {
121
- const payload = args[0] || {};
122
-
123
- if (!('name' in payload)) {
124
- throw new Error(
125
- 'You need to specify a name when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.'
126
- );
127
- }
128
-
129
- return { type: 'NAVIGATE', payload };
130
- }
131
- }
132
-
133
- export function navigateDeprecated(
134
- ...args:
135
- | [name: string]
136
- | [name: string, params: object | undefined]
137
- | [options: { name: string; params?: object }]
138
- ): Action {
139
- if (typeof args[0] === 'string') {
140
- return {
141
- type: 'NAVIGATE_DEPRECATED',
142
- payload: { name: args[0], params: args[1] },
143
- };
144
- } else {
145
- const payload = args[0] || {};
146
-
147
- if (!('name' in payload)) {
148
- throw new Error(
149
- 'You need to specify a name when calling navigateDeprecated with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigatelegacy for usage.'
150
- );
151
- }
152
-
153
- return { type: 'NAVIGATE_DEPRECATED', payload };
154
- }
155
- }
156
-
157
- export function reset(state: ResetState | undefined) {
158
- return { type: 'RESET', payload: state } as const satisfies ResetAction;
159
- }
160
-
161
- export function setParams(params: object) {
162
- return {
163
- type: 'SET_PARAMS',
164
- payload: { params },
165
- } as const satisfies SetParamsAction;
166
- }
167
-
168
- export function replaceParams(params: object) {
169
- return {
170
- type: 'REPLACE_PARAMS',
171
- payload: { params },
172
- } as const satisfies ReplaceParamsAction;
173
- }
174
-
175
- export function preload(name: string, params?: object) {
176
- return {
177
- type: 'PRELOAD',
178
- payload: { name, params },
179
- } as const satisfies PreloadAction;
180
- }