@react-navigation/core 8.0.0-alpha.22 → 8.0.0-alpha.24
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/NavigationBuilderContext.js.map +1 -1
- package/lib/module/StaticNavigation.js +1 -1
- package/lib/module/StaticNavigation.js.map +1 -1
- package/lib/module/createNavigatorFactory.js.map +1 -1
- package/lib/module/getPathFromState.js +87 -57
- package/lib/module/getPathFromState.js.map +1 -1
- package/lib/module/getStateFromPath.js +77 -46
- package/lib/module/getStateFromPath.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/types.js +13 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/useEventEmitter.js +4 -1
- package/lib/module/useEventEmitter.js.map +1 -1
- package/lib/module/useNavigationBuilder.js +11 -14
- package/lib/module/useNavigationBuilder.js.map +1 -1
- package/lib/module/useOnAction.js +4 -1
- package/lib/module/useOnAction.js.map +1 -1
- package/lib/module/useOnPreventRemove.js +35 -24
- package/lib/module/useOnPreventRemove.js.map +1 -1
- package/lib/module/useRoute.js.map +1 -1
- package/lib/module/validatePathConfig.js +50 -37
- package/lib/module/validatePathConfig.js.map +1 -1
- package/lib/typescript/src/NavigationBuilderContext.d.ts +2 -2
- package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
- package/lib/typescript/src/StaticNavigation.d.ts +3 -3
- package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
- package/lib/typescript/src/createNavigatorFactory.d.ts +4 -2
- package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
- package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
- package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +66 -33
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/useEventEmitter.d.ts +1 -1
- package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationBuilder.d.ts +0 -7
- package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationHelpers.d.ts +0 -7
- package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
- package/lib/typescript/src/useOnAction.d.ts.map +1 -1
- package/lib/typescript/src/useOnPreventRemove.d.ts +2 -6
- package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
- package/lib/typescript/src/useRoute.d.ts +1 -1
- package/lib/typescript/src/useRoute.d.ts.map +1 -1
- package/lib/typescript/src/utilities.d.ts +4 -38
- package/lib/typescript/src/utilities.d.ts.map +1 -1
- package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/NavigationBuilderContext.tsx +5 -1
- package/src/StaticNavigation.tsx +7 -6
- package/src/createNavigatorFactory.tsx +23 -2
- package/src/getPathFromState.tsx +97 -66
- package/src/getStateFromPath.tsx +168 -142
- package/src/index.tsx +1 -0
- package/src/types.tsx +257 -54
- package/src/useEventEmitter.tsx +6 -1
- package/src/useNavigationBuilder.tsx +11 -15
- package/src/useOnAction.tsx +6 -2
- package/src/useOnPreventRemove.tsx +68 -36
- package/src/useRoute.tsx +3 -5
- package/src/utilities.tsx +8 -41
- package/src/validatePathConfig.tsx +85 -58
package/src/utilities.tsx
CHANGED
|
@@ -25,8 +25,6 @@ export type UnionToIntersection<U> = (
|
|
|
25
25
|
? I
|
|
26
26
|
: never;
|
|
27
27
|
|
|
28
|
-
export type UnknownToUndefined<T> = unknown extends T ? undefined : T;
|
|
29
|
-
|
|
30
28
|
/**
|
|
31
29
|
* Exclude undefined from a type.
|
|
32
30
|
* Similar to NonNullable but only excludes undefined, not null.
|
|
@@ -173,46 +171,15 @@ type QueryParamRequiredKeys<Params, Parse> = Exclude<
|
|
|
173
171
|
QueryParamOptionalKeys<Params, Parse>
|
|
174
172
|
>;
|
|
175
173
|
|
|
176
|
-
/**
|
|
177
|
-
* Infer the path string from a linking config.
|
|
178
|
-
*/
|
|
179
|
-
export type InferPath<T> = T extends { path: infer P extends string }
|
|
180
|
-
? P
|
|
181
|
-
: never;
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Infer the parse functions from a linking config.
|
|
185
|
-
*/
|
|
186
|
-
export type InferParse<T> = T extends { parse: infer P } ? P : {};
|
|
187
|
-
|
|
188
174
|
export type InferParamsFromLinking<T> = T extends {
|
|
189
175
|
path: infer P extends string;
|
|
176
|
+
parse: infer Parse;
|
|
190
177
|
}
|
|
191
|
-
? UndefinedIfAllOptional<
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
? UndefinedIfAllOptional<
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
: undefined;
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Infer the params type from a screen component or nested navigator.
|
|
202
|
-
*/
|
|
203
|
-
export type InferScreenParams<T> =
|
|
204
|
-
T extends React.ComponentType<{ route: { params: infer P } }>
|
|
205
|
-
? P
|
|
206
|
-
: T extends { config: { screens: infer Screens } }
|
|
207
|
-
? import('./types').NavigatorScreenParams<{
|
|
208
|
-
[K in keyof Screens]: Screens[K] extends React.ComponentType<{
|
|
209
|
-
route: { params: infer P };
|
|
210
|
-
}>
|
|
211
|
-
? P
|
|
212
|
-
: Screens[K] extends { screen: infer S }
|
|
213
|
-
? S extends React.ComponentType<{ route: { params: infer P } }>
|
|
214
|
-
? P
|
|
215
|
-
: undefined
|
|
216
|
-
: undefined;
|
|
217
|
-
}>
|
|
178
|
+
? UndefinedIfAllOptional<ExtractParamsType<ExtractParamStrings<P>, Parse>>
|
|
179
|
+
: T extends {
|
|
180
|
+
path: infer P extends string;
|
|
181
|
+
}
|
|
182
|
+
? UndefinedIfAllOptional<ExtractParamStrings<P>>
|
|
183
|
+
: T extends string
|
|
184
|
+
? UndefinedIfAllOptional<ExtractParamStrings<T>>
|
|
218
185
|
: undefined;
|
|
@@ -1,24 +1,39 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const ROOT_VALIDATION_LIST = [
|
|
2
|
+
'- path (string)',
|
|
3
|
+
'- initialRouteName (string)',
|
|
4
|
+
'- screens (object)',
|
|
5
|
+
].join('\n');
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
const NESTED_VALIDATION_LIST = [
|
|
8
|
+
ROOT_VALIDATION_LIST,
|
|
9
|
+
'- alias (array)',
|
|
10
|
+
'- exact (boolean)',
|
|
11
|
+
'- stringify (object)',
|
|
12
|
+
'- parse (object)',
|
|
13
|
+
'- shared (boolean)',
|
|
14
|
+
].join('\n');
|
|
15
|
+
|
|
16
|
+
const getValidationType = (key: string, root: boolean) => {
|
|
17
|
+
switch (key) {
|
|
18
|
+
case 'path':
|
|
19
|
+
case 'initialRouteName':
|
|
20
|
+
return 'string';
|
|
21
|
+
case 'screens':
|
|
22
|
+
return 'object';
|
|
23
|
+
case 'alias':
|
|
24
|
+
return root ? undefined : 'array';
|
|
25
|
+
case 'exact':
|
|
26
|
+
case 'shared':
|
|
27
|
+
return root ? undefined : 'boolean';
|
|
28
|
+
case 'stringify':
|
|
29
|
+
case 'parse':
|
|
30
|
+
return root ? undefined : 'object';
|
|
31
|
+
default:
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
21
35
|
|
|
36
|
+
export function validatePathConfig(config: unknown, root = true) {
|
|
22
37
|
if (typeof config !== 'object' || config === null) {
|
|
23
38
|
throw new Error(
|
|
24
39
|
`Expected the configuration to be an object, but got ${JSON.stringify(
|
|
@@ -27,58 +42,70 @@ export function validatePathConfig(config: unknown, root = true) {
|
|
|
27
42
|
);
|
|
28
43
|
}
|
|
29
44
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
const pathConfig = config as Record<string, unknown>;
|
|
46
|
+
let validationErrors: string[] | undefined;
|
|
47
|
+
|
|
48
|
+
for (const key in pathConfig) {
|
|
49
|
+
const type = getValidationType(key, root);
|
|
50
|
+
|
|
51
|
+
if (type === undefined) {
|
|
52
|
+
(validationErrors ??= []).push(`- ${key} (extraneous)`);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const value = pathConfig[key];
|
|
57
|
+
|
|
58
|
+
if (value === undefined) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (type === 'array') {
|
|
63
|
+
if (!Array.isArray(value)) {
|
|
64
|
+
(validationErrors ??= []).push(
|
|
65
|
+
`- ${key} (expected 'Array', got '${typeof value}')`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
} else if (typeof value !== type) {
|
|
69
|
+
(validationErrors ??= []).push(
|
|
70
|
+
`- ${key} (expected '${type}', got '${typeof value}')`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (validationErrors) {
|
|
57
76
|
throw new Error(
|
|
58
|
-
`Found invalid properties in the configuration:\n${
|
|
59
|
-
|
|
60
|
-
)}\n\nYou can only specify the following properties:\n${
|
|
61
|
-
|
|
62
|
-
|
|
77
|
+
`Found invalid properties in the configuration:\n${validationErrors.join(
|
|
78
|
+
'\n'
|
|
79
|
+
)}\n\nYou can only specify the following properties:\n${
|
|
80
|
+
root ? ROOT_VALIDATION_LIST : NESTED_VALIDATION_LIST
|
|
81
|
+
}\n\nIf you want to specify configuration for screens, you need to specify them under a 'screens' property.\n\nSee https://reactnavigation.org/docs/configuring-links for more details on how to specify a linking configuration.`
|
|
63
82
|
);
|
|
64
83
|
}
|
|
65
84
|
|
|
85
|
+
const path = pathConfig.path;
|
|
86
|
+
|
|
66
87
|
if (
|
|
67
88
|
root &&
|
|
68
|
-
'path' in
|
|
69
|
-
typeof
|
|
70
|
-
|
|
89
|
+
'path' in pathConfig &&
|
|
90
|
+
typeof path === 'string' &&
|
|
91
|
+
path.includes(':')
|
|
71
92
|
) {
|
|
72
93
|
throw new Error(
|
|
73
|
-
`Found invalid path '${
|
|
94
|
+
`Found invalid path '${path}'. The 'path' in the top-level configuration cannot contain patterns for params.`
|
|
74
95
|
);
|
|
75
96
|
}
|
|
76
97
|
|
|
77
|
-
|
|
78
|
-
|
|
98
|
+
const screens = pathConfig.screens;
|
|
99
|
+
|
|
100
|
+
if (screens && typeof screens === 'object') {
|
|
101
|
+
const screenConfig = screens as Record<string, unknown>;
|
|
102
|
+
|
|
103
|
+
for (const name in screenConfig) {
|
|
104
|
+
const value = screenConfig[name];
|
|
105
|
+
|
|
79
106
|
if (typeof value !== 'string') {
|
|
80
107
|
validatePathConfig(value, false);
|
|
81
108
|
}
|
|
82
|
-
}
|
|
109
|
+
}
|
|
83
110
|
}
|
|
84
111
|
}
|