@react-navigation/core 6.0.2
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/LICENSE +21 -0
- package/README.md +38 -0
- package/lib/commonjs/BaseNavigationContainer.js +393 -0
- package/lib/commonjs/BaseNavigationContainer.js.map +1 -0
- package/lib/commonjs/CurrentRenderContext.js +21 -0
- package/lib/commonjs/CurrentRenderContext.js.map +1 -0
- package/lib/commonjs/EnsureSingleNavigator.js +53 -0
- package/lib/commonjs/EnsureSingleNavigator.js.map +1 -0
- package/lib/commonjs/Group.js +15 -0
- package/lib/commonjs/Group.js.map +1 -0
- package/lib/commonjs/NavigationBuilderContext.js +23 -0
- package/lib/commonjs/NavigationBuilderContext.js.map +1 -0
- package/lib/commonjs/NavigationContainerRefContext.js +20 -0
- package/lib/commonjs/NavigationContainerRefContext.js.map +1 -0
- package/lib/commonjs/NavigationContext.js +20 -0
- package/lib/commonjs/NavigationContext.js.map +1 -0
- package/lib/commonjs/NavigationHelpersContext.js +21 -0
- package/lib/commonjs/NavigationHelpersContext.js.map +1 -0
- package/lib/commonjs/NavigationRouteContext.js +20 -0
- package/lib/commonjs/NavigationRouteContext.js.map +1 -0
- package/lib/commonjs/NavigationStateContext.js +42 -0
- package/lib/commonjs/NavigationStateContext.js.map +1 -0
- package/lib/commonjs/SceneView.js +97 -0
- package/lib/commonjs/SceneView.js.map +1 -0
- package/lib/commonjs/Screen.js +15 -0
- package/lib/commonjs/Screen.js.map +1 -0
- package/lib/commonjs/StaticContainer.js +43 -0
- package/lib/commonjs/StaticContainer.js.map +1 -0
- package/lib/commonjs/UnhandledActionContext.js +17 -0
- package/lib/commonjs/UnhandledActionContext.js.map +1 -0
- package/lib/commonjs/checkDuplicateRouteNames.js +31 -0
- package/lib/commonjs/checkDuplicateRouteNames.js.map +1 -0
- package/lib/commonjs/checkSerializable.js +59 -0
- package/lib/commonjs/checkSerializable.js.map +1 -0
- package/lib/commonjs/createNavigationContainerRef.js +82 -0
- package/lib/commonjs/createNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/createNavigatorFactory.js +34 -0
- package/lib/commonjs/createNavigatorFactory.js.map +1 -0
- package/lib/commonjs/findFocusedRoute.js +22 -0
- package/lib/commonjs/findFocusedRoute.js.map +1 -0
- package/lib/commonjs/fromEntries.js +19 -0
- package/lib/commonjs/fromEntries.js.map +1 -0
- package/lib/commonjs/getActionFromState.js +99 -0
- package/lib/commonjs/getActionFromState.js.map +1 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js +23 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/commonjs/getPathFromState.js +238 -0
- package/lib/commonjs/getPathFromState.js.map +1 -0
- package/lib/commonjs/getStateFromPath.js +450 -0
- package/lib/commonjs/getStateFromPath.js.map +1 -0
- package/lib/commonjs/index.js +227 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/isArrayEqual.js +15 -0
- package/lib/commonjs/isArrayEqual.js.map +1 -0
- package/lib/commonjs/types.js +18 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/useChildListeners.js +38 -0
- package/lib/commonjs/useChildListeners.js.map +1 -0
- package/lib/commonjs/useComponent.js +36 -0
- package/lib/commonjs/useComponent.js.map +1 -0
- package/lib/commonjs/useCurrentRender.js +33 -0
- package/lib/commonjs/useCurrentRender.js.map +1 -0
- package/lib/commonjs/useDescriptors.js +141 -0
- package/lib/commonjs/useDescriptors.js.map +1 -0
- package/lib/commonjs/useEventEmitter.js +118 -0
- package/lib/commonjs/useEventEmitter.js.map +1 -0
- package/lib/commonjs/useFocusEffect.js +97 -0
- package/lib/commonjs/useFocusEffect.js.map +1 -0
- package/lib/commonjs/useFocusEvents.js +77 -0
- package/lib/commonjs/useFocusEvents.js.map +1 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js +57 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/commonjs/useIsFocused.js +47 -0
- package/lib/commonjs/useIsFocused.js.map +1 -0
- package/lib/commonjs/useKeyedChildListeners.js +35 -0
- package/lib/commonjs/useKeyedChildListeners.js.map +1 -0
- package/lib/commonjs/useNavigation.js +36 -0
- package/lib/commonjs/useNavigation.js.map +1 -0
- package/lib/commonjs/useNavigationBuilder.js +480 -0
- package/lib/commonjs/useNavigationBuilder.js.map +1 -0
- package/lib/commonjs/useNavigationCache.js +122 -0
- package/lib/commonjs/useNavigationCache.js.map +1 -0
- package/lib/commonjs/useNavigationContainerRef.js +27 -0
- package/lib/commonjs/useNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/useNavigationHelpers.js +77 -0
- package/lib/commonjs/useNavigationHelpers.js.map +1 -0
- package/lib/commonjs/useNavigationState.js +41 -0
- package/lib/commonjs/useNavigationState.js.map +1 -0
- package/lib/commonjs/useOnAction.js +118 -0
- package/lib/commonjs/useOnAction.js.map +1 -0
- package/lib/commonjs/useOnGetState.js +60 -0
- package/lib/commonjs/useOnGetState.js.map +1 -0
- package/lib/commonjs/useOnPreventRemove.js +89 -0
- package/lib/commonjs/useOnPreventRemove.js.map +1 -0
- package/lib/commonjs/useOnRouteFocus.js +45 -0
- package/lib/commonjs/useOnRouteFocus.js.map +1 -0
- package/lib/commonjs/useOptionsGetters.js +99 -0
- package/lib/commonjs/useOptionsGetters.js.map +1 -0
- package/lib/commonjs/useRegisterNavigator.js +40 -0
- package/lib/commonjs/useRegisterNavigator.js.map +1 -0
- package/lib/commonjs/useRoute.js +32 -0
- package/lib/commonjs/useRoute.js.map +1 -0
- package/lib/commonjs/useRouteCache.js +61 -0
- package/lib/commonjs/useRouteCache.js.map +1 -0
- package/lib/commonjs/useScheduleUpdate.js +44 -0
- package/lib/commonjs/useScheduleUpdate.js.map +1 -0
- package/lib/commonjs/useSyncState.js +76 -0
- package/lib/commonjs/useSyncState.js.map +1 -0
- package/lib/commonjs/validatePathConfig.js +31 -0
- package/lib/commonjs/validatePathConfig.js.map +1 -0
- package/lib/module/BaseNavigationContainer.js +360 -0
- package/lib/module/BaseNavigationContainer.js.map +1 -0
- package/lib/module/CurrentRenderContext.js +9 -0
- package/lib/module/CurrentRenderContext.js.map +1 -0
- package/lib/module/EnsureSingleNavigator.js +38 -0
- package/lib/module/EnsureSingleNavigator.js.map +1 -0
- package/lib/module/Group.js +8 -0
- package/lib/module/Group.js.map +1 -0
- package/lib/module/NavigationBuilderContext.js +11 -0
- package/lib/module/NavigationBuilderContext.js.map +1 -0
- package/lib/module/NavigationContainerRefContext.js +8 -0
- package/lib/module/NavigationContainerRefContext.js.map +1 -0
- package/lib/module/NavigationContext.js +8 -0
- package/lib/module/NavigationContext.js.map +1 -0
- package/lib/module/NavigationHelpersContext.js +9 -0
- package/lib/module/NavigationHelpersContext.js.map +1 -0
- package/lib/module/NavigationRouteContext.js +8 -0
- package/lib/module/NavigationRouteContext.js.map +1 -0
- package/lib/module/NavigationStateContext.js +27 -0
- package/lib/module/NavigationStateContext.js.map +1 -0
- package/lib/module/SceneView.js +80 -0
- package/lib/module/SceneView.js.map +1 -0
- package/lib/module/Screen.js +8 -0
- package/lib/module/Screen.js.map +1 -0
- package/lib/module/StaticContainer.js +30 -0
- package/lib/module/StaticContainer.js.map +1 -0
- package/lib/module/UnhandledActionContext.js +4 -0
- package/lib/module/UnhandledActionContext.js.map +1 -0
- package/lib/module/checkDuplicateRouteNames.js +24 -0
- package/lib/module/checkDuplicateRouteNames.js.map +1 -0
- package/lib/module/checkSerializable.js +52 -0
- package/lib/module/checkSerializable.js.map +1 -0
- package/lib/module/createNavigationContainerRef.js +71 -0
- package/lib/module/createNavigationContainerRef.js.map +1 -0
- package/lib/module/createNavigatorFactory.js +24 -0
- package/lib/module/createNavigatorFactory.js.map +1 -0
- package/lib/module/findFocusedRoute.js +15 -0
- package/lib/module/findFocusedRoute.js.map +1 -0
- package/lib/module/fromEntries.js +12 -0
- package/lib/module/fromEntries.js.map +1 -0
- package/lib/module/getActionFromState.js +92 -0
- package/lib/module/getActionFromState.js.map +1 -0
- package/lib/module/getFocusedRouteNameFromRoute.js +15 -0
- package/lib/module/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/module/getPathFromState.js +223 -0
- package/lib/module/getPathFromState.js.map +1 -0
- package/lib/module/getStateFromPath.js +434 -0
- package/lib/module/getStateFromPath.js.map +1 -0
- package/lib/module/index.js +24 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/isArrayEqual.js +8 -0
- package/lib/module/isArrayEqual.js.map +1 -0
- package/lib/module/types.js +9 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useChildListeners.js +27 -0
- package/lib/module/useChildListeners.js.map +1 -0
- package/lib/module/useComponent.js +24 -0
- package/lib/module/useComponent.js.map +1 -0
- package/lib/module/useCurrentRender.js +19 -0
- package/lib/module/useCurrentRender.js.map +1 -0
- package/lib/module/useDescriptors.js +122 -0
- package/lib/module/useDescriptors.js.map +1 -0
- package/lib/module/useEventEmitter.js +107 -0
- package/lib/module/useEventEmitter.js.map +1 -0
- package/lib/module/useFocusEffect.js +83 -0
- package/lib/module/useFocusEffect.js.map +1 -0
- package/lib/module/useFocusEvents.js +63 -0
- package/lib/module/useFocusEvents.js.map +1 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js +43 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/module/useIsFocused.js +34 -0
- package/lib/module/useIsFocused.js.map +1 -0
- package/lib/module/useKeyedChildListeners.js +24 -0
- package/lib/module/useKeyedChildListeners.js.map +1 -0
- package/lib/module/useNavigation.js +21 -0
- package/lib/module/useNavigation.js.map +1 -0
- package/lib/module/useNavigationBuilder.js +443 -0
- package/lib/module/useNavigationBuilder.js.map +1 -0
- package/lib/module/useNavigationCache.js +107 -0
- package/lib/module/useNavigationCache.js.map +1 -0
- package/lib/module/useNavigationContainerRef.js +12 -0
- package/lib/module/useNavigationContainerRef.js.map +1 -0
- package/lib/module/useNavigationHelpers.js +59 -0
- package/lib/module/useNavigationHelpers.js.map +1 -0
- package/lib/module/useNavigationState.js +27 -0
- package/lib/module/useNavigationState.js.map +1 -0
- package/lib/module/useOnAction.js +103 -0
- package/lib/module/useOnAction.js.map +1 -0
- package/lib/module/useOnGetState.js +43 -0
- package/lib/module/useOnGetState.js.map +1 -0
- package/lib/module/useOnPreventRemove.js +68 -0
- package/lib/module/useOnPreventRemove.js.map +1 -0
- package/lib/module/useOnRouteFocus.js +31 -0
- package/lib/module/useOnRouteFocus.js.map +1 -0
- package/lib/module/useOptionsGetters.js +83 -0
- package/lib/module/useOptionsGetters.js.map +1 -0
- package/lib/module/useRegisterNavigator.js +27 -0
- package/lib/module/useRegisterNavigator.js.map +1 -0
- package/lib/module/useRoute.js +18 -0
- package/lib/module/useRoute.js.map +1 -0
- package/lib/module/useRouteCache.js +47 -0
- package/lib/module/useRouteCache.js.map +1 -0
- package/lib/module/useScheduleUpdate.js +29 -0
- package/lib/module/useScheduleUpdate.js.map +1 -0
- package/lib/module/useSyncState.js +64 -0
- package/lib/module/useSyncState.js.map +1 -0
- package/lib/module/validatePathConfig.js +24 -0
- package/lib/module/validatePathConfig.js.map +1 -0
- package/lib/typescript/src/BaseNavigationContainer.d.ts +14 -0
- package/lib/typescript/src/CurrentRenderContext.d.ts +9 -0
- package/lib/typescript/src/EnsureSingleNavigator.d.ts +13 -0
- package/lib/typescript/src/Group.d.ts +6 -0
- package/lib/typescript/src/NavigationBuilderContext.d.ts +34 -0
- package/lib/typescript/src/NavigationContainerRefContext.d.ts +8 -0
- package/lib/typescript/src/NavigationContext.d.ts +24 -0
- package/lib/typescript/src/NavigationHelpersContext.d.ts +9 -0
- package/lib/typescript/src/NavigationRouteContext.d.ts +7 -0
- package/lib/typescript/src/NavigationStateContext.d.ts +45 -0
- package/lib/typescript/src/SceneView.d.ts +21 -0
- package/lib/typescript/src/Screen.d.ts +6 -0
- package/lib/typescript/src/StaticContainer.d.ts +7 -0
- package/lib/typescript/src/UnhandledActionContext.d.ts +4 -0
- package/lib/typescript/src/checkDuplicateRouteNames.d.ts +2 -0
- package/lib/typescript/src/checkSerializable.d.ts +9 -0
- package/lib/typescript/src/createNavigationContainerRef.d.ts +3 -0
- package/lib/typescript/src/createNavigatorFactory.d.ts +11 -0
- package/lib/typescript/src/findFocusedRoute.d.ts +22 -0
- package/lib/typescript/src/fromEntries.d.ts +1 -0
- package/lib/typescript/src/getActionFromState.d.ts +16 -0
- package/lib/typescript/src/getFocusedRouteNameFromRoute.d.ts +2 -0
- package/lib/typescript/src/getPathFromState.d.ts +38 -0
- package/lib/typescript/src/getStateFromPath.d.ts +32 -0
- package/lib/typescript/src/index.d.ts +23 -0
- package/lib/typescript/src/isArrayEqual.d.ts +5 -0
- package/lib/typescript/src/types.d.ts +485 -0
- package/lib/typescript/src/useChildListeners.d.ts +11 -0
- package/lib/typescript/src/useComponent.d.ts +2 -0
- package/lib/typescript/src/useCurrentRender.d.ts +13 -0
- package/lib/typescript/src/useDescriptors.d.ts +89 -0
- package/lib/typescript/src/useEventEmitter.d.ts +8 -0
- package/lib/typescript/src/useFocusEffect.d.ts +10 -0
- package/lib/typescript/src/useFocusEvents.d.ts +12 -0
- package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +12 -0
- package/lib/typescript/src/useIsFocused.d.ts +5 -0
- package/lib/typescript/src/useKeyedChildListeners.d.ts +11 -0
- package/lib/typescript/src/useNavigation.d.ts +7 -0
- package/lib/typescript/src/useNavigationBuilder.d.ts +223 -0
- package/lib/typescript/src/useNavigationCache.d.ts +19 -0
- package/lib/typescript/src/useNavigationContainerRef.d.ts +2 -0
- package/lib/typescript/src/useNavigationHelpers.d.ts +174 -0
- package/lib/typescript/src/useNavigationState.d.ts +9 -0
- package/lib/typescript/src/useOnAction.d.ts +25 -0
- package/lib/typescript/src/useOnGetState.d.ts +8 -0
- package/lib/typescript/src/useOnPreventRemove.d.ts +16 -0
- package/lib/typescript/src/useOnRouteFocus.d.ts +14 -0
- package/lib/typescript/src/useOptionsGetters.d.ts +12 -0
- package/lib/typescript/src/useRegisterNavigator.d.ts +5 -0
- package/lib/typescript/src/useRoute.d.ts +8 -0
- package/lib/typescript/src/useRouteCache.d.ts +13 -0
- package/lib/typescript/src/useScheduleUpdate.d.ts +13 -0
- package/lib/typescript/src/useSyncState.d.ts +4 -0
- package/lib/typescript/src/validatePathConfig.d.ts +1 -0
- package/package.json +73 -0
- package/src/BaseNavigationContainer.tsx +453 -0
- package/src/CurrentRenderContext.tsx +10 -0
- package/src/EnsureSingleNavigator.tsx +53 -0
- package/src/Group.tsx +14 -0
- package/src/NavigationBuilderContext.tsx +70 -0
- package/src/NavigationContainerRefContext.tsx +14 -0
- package/src/NavigationContext.tsx +12 -0
- package/src/NavigationHelpersContext.tsx +13 -0
- package/src/NavigationRouteContext.tsx +10 -0
- package/src/NavigationStateContext.tsx +39 -0
- package/src/SceneView.tsx +134 -0
- package/src/Screen.tsx +17 -0
- package/src/StaticContainer.tsx +29 -0
- package/src/UnhandledActionContext.tsx +9 -0
- package/src/checkDuplicateRouteNames.tsx +33 -0
- package/src/checkSerializable.tsx +74 -0
- package/src/createNavigationContainerRef.tsx +97 -0
- package/src/createNavigatorFactory.tsx +40 -0
- package/src/findFocusedRoute.tsx +13 -0
- package/src/fromEntries.tsx +13 -0
- package/src/getActionFromState.tsx +154 -0
- package/src/getFocusedRouteNameFromRoute.tsx +28 -0
- package/src/getPathFromState.tsx +297 -0
- package/src/getStateFromPath.tsx +575 -0
- package/src/index.tsx +23 -0
- package/src/isArrayEqual.tsx +7 -0
- package/src/types.tsx +645 -0
- package/src/useChildListeners.tsx +37 -0
- package/src/useComponent.tsx +30 -0
- package/src/useCurrentRender.tsx +35 -0
- package/src/useDescriptors.tsx +229 -0
- package/src/useEventEmitter.tsx +130 -0
- package/src/useFocusEffect.tsx +112 -0
- package/src/useFocusEvents.tsx +73 -0
- package/src/useFocusedListenersChildrenAdapter.tsx +47 -0
- package/src/useIsFocused.tsx +43 -0
- package/src/useKeyedChildListeners.tsx +40 -0
- package/src/useNavigation.tsx +26 -0
- package/src/useNavigationBuilder.tsx +640 -0
- package/src/useNavigationCache.tsx +159 -0
- package/src/useNavigationContainerRef.tsx +17 -0
- package/src/useNavigationHelpers.tsx +98 -0
- package/src/useNavigationState.tsx +41 -0
- package/src/useOnAction.tsx +166 -0
- package/src/useOnGetState.tsx +47 -0
- package/src/useOnPreventRemove.tsx +99 -0
- package/src/useOnRouteFocus.tsx +47 -0
- package/src/useOptionsGetters.tsx +98 -0
- package/src/useRegisterNavigator.tsx +29 -0
- package/src/useRoute.tsx +22 -0
- package/src/useRouteCache.tsx +55 -0
- package/src/useScheduleUpdate.tsx +32 -0
- package/src/useSyncState.tsx +74 -0
- package/src/validatePathConfig.tsx +32 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NavigationState,
|
|
3
|
+
PartialState,
|
|
4
|
+
Route,
|
|
5
|
+
} from '@react-navigation/routers';
|
|
6
|
+
import * as queryString from 'query-string';
|
|
7
|
+
|
|
8
|
+
import fromEntries from './fromEntries';
|
|
9
|
+
import type { PathConfig, PathConfigMap } from './types';
|
|
10
|
+
import validatePathConfig from './validatePathConfig';
|
|
11
|
+
|
|
12
|
+
type Options<ParamList> = {
|
|
13
|
+
initialRouteName?: string;
|
|
14
|
+
screens: PathConfigMap<ParamList>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type State = NavigationState | Omit<PartialState<NavigationState>, 'stale'>;
|
|
18
|
+
|
|
19
|
+
type StringifyConfig = Record<string, (value: any) => string>;
|
|
20
|
+
|
|
21
|
+
type ConfigItem = {
|
|
22
|
+
pattern?: string;
|
|
23
|
+
stringify?: StringifyConfig;
|
|
24
|
+
screens?: Record<string, ConfigItem>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const getActiveRoute = (state: State): { name: string; params?: object } => {
|
|
28
|
+
const route =
|
|
29
|
+
typeof state.index === 'number'
|
|
30
|
+
? state.routes[state.index]
|
|
31
|
+
: state.routes[state.routes.length - 1];
|
|
32
|
+
|
|
33
|
+
if (route.state) {
|
|
34
|
+
return getActiveRoute(route.state);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return route;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Utility to serialize a navigation state object to a path string.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```js
|
|
45
|
+
* getPathFromState(
|
|
46
|
+
* {
|
|
47
|
+
* routes: [
|
|
48
|
+
* {
|
|
49
|
+
* name: 'Chat',
|
|
50
|
+
* params: { author: 'Jane', id: 42 },
|
|
51
|
+
* },
|
|
52
|
+
* ],
|
|
53
|
+
* },
|
|
54
|
+
* {
|
|
55
|
+
* screens: {
|
|
56
|
+
* Chat: {
|
|
57
|
+
* path: 'chat/:author/:id',
|
|
58
|
+
* stringify: { author: author => author.toLowerCase() }
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* )
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param state Navigation state to serialize.
|
|
66
|
+
* @param options Extra options to fine-tune how to serialize the path.
|
|
67
|
+
* @returns Path representing the state, e.g. /foo/bar?count=42.
|
|
68
|
+
*/
|
|
69
|
+
export default function getPathFromState<ParamList extends {}>(
|
|
70
|
+
state: State,
|
|
71
|
+
options?: Options<ParamList>
|
|
72
|
+
): string {
|
|
73
|
+
if (state == null) {
|
|
74
|
+
throw Error(
|
|
75
|
+
"Got 'undefined' for the navigation state. You must pass a valid state object."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (options) {
|
|
80
|
+
validatePathConfig(options);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create a normalized configs object which will be easier to use
|
|
84
|
+
const configs: Record<string, ConfigItem> = options?.screens
|
|
85
|
+
? createNormalizedConfigs(options?.screens)
|
|
86
|
+
: {};
|
|
87
|
+
|
|
88
|
+
let path = '/';
|
|
89
|
+
let current: State | undefined = state;
|
|
90
|
+
|
|
91
|
+
const allParams: Record<string, any> = {};
|
|
92
|
+
|
|
93
|
+
while (current) {
|
|
94
|
+
let index = typeof current.index === 'number' ? current.index : 0;
|
|
95
|
+
let route = current.routes[index] as Route<string> & {
|
|
96
|
+
state?: State;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
let pattern: string | undefined;
|
|
100
|
+
|
|
101
|
+
let focusedParams: Record<string, any> | undefined;
|
|
102
|
+
let focusedRoute = getActiveRoute(state);
|
|
103
|
+
let currentOptions = configs;
|
|
104
|
+
|
|
105
|
+
// Keep all the route names that appeared during going deeper in config in case the pattern is resolved to undefined
|
|
106
|
+
let nestedRouteNames = [];
|
|
107
|
+
|
|
108
|
+
let hasNext = true;
|
|
109
|
+
|
|
110
|
+
while (route.name in currentOptions && hasNext) {
|
|
111
|
+
pattern = currentOptions[route.name].pattern;
|
|
112
|
+
|
|
113
|
+
nestedRouteNames.push(route.name);
|
|
114
|
+
|
|
115
|
+
if (route.params) {
|
|
116
|
+
const stringify = currentOptions[route.name]?.stringify;
|
|
117
|
+
|
|
118
|
+
const currentParams = fromEntries(
|
|
119
|
+
Object.entries(route.params).map(([key, value]) => [
|
|
120
|
+
key,
|
|
121
|
+
stringify?.[key] ? stringify[key](value) : String(value),
|
|
122
|
+
])
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
if (pattern) {
|
|
126
|
+
Object.assign(allParams, currentParams);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (focusedRoute === route) {
|
|
130
|
+
// If this is the focused route, keep the params for later use
|
|
131
|
+
// We save it here since it's been stringified already
|
|
132
|
+
focusedParams = { ...currentParams };
|
|
133
|
+
|
|
134
|
+
pattern
|
|
135
|
+
?.split('/')
|
|
136
|
+
.filter((p) => p.startsWith(':'))
|
|
137
|
+
// eslint-disable-next-line no-loop-func
|
|
138
|
+
.forEach((p) => {
|
|
139
|
+
const name = getParamName(p);
|
|
140
|
+
|
|
141
|
+
// Remove the params present in the pattern since we'll only use the rest for query string
|
|
142
|
+
if (focusedParams) {
|
|
143
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
144
|
+
delete focusedParams[name];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// If there is no `screens` property or no nested state, we return pattern
|
|
151
|
+
if (!currentOptions[route.name].screens || route.state === undefined) {
|
|
152
|
+
hasNext = false;
|
|
153
|
+
} else {
|
|
154
|
+
index =
|
|
155
|
+
typeof route.state.index === 'number'
|
|
156
|
+
? route.state.index
|
|
157
|
+
: route.state.routes.length - 1;
|
|
158
|
+
|
|
159
|
+
const nextRoute = route.state.routes[index];
|
|
160
|
+
const nestedConfig = currentOptions[route.name].screens;
|
|
161
|
+
|
|
162
|
+
// if there is config for next route name, we go deeper
|
|
163
|
+
if (nestedConfig && nextRoute.name in nestedConfig) {
|
|
164
|
+
route = nextRoute as Route<string> & { state?: State };
|
|
165
|
+
currentOptions = nestedConfig;
|
|
166
|
+
} else {
|
|
167
|
+
// If not, there is no sense in going deeper in config
|
|
168
|
+
hasNext = false;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (pattern === undefined) {
|
|
174
|
+
pattern = nestedRouteNames.join('/');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (currentOptions[route.name] !== undefined) {
|
|
178
|
+
path += pattern
|
|
179
|
+
.split('/')
|
|
180
|
+
.map((p) => {
|
|
181
|
+
const name = getParamName(p);
|
|
182
|
+
|
|
183
|
+
// We don't know what to show for wildcard patterns
|
|
184
|
+
// Showing the route name seems ok, though whatever we show here will be incorrect
|
|
185
|
+
// Since the page doesn't actually exist
|
|
186
|
+
if (p === '*') {
|
|
187
|
+
return route.name;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// If the path has a pattern for a param, put the param in the path
|
|
191
|
+
if (p.startsWith(':')) {
|
|
192
|
+
const value = allParams[name];
|
|
193
|
+
|
|
194
|
+
if (value === undefined && p.endsWith('?')) {
|
|
195
|
+
// Optional params without value assigned in route.params should be ignored
|
|
196
|
+
return '';
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return encodeURIComponent(value);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return encodeURIComponent(p);
|
|
203
|
+
})
|
|
204
|
+
.join('/');
|
|
205
|
+
} else {
|
|
206
|
+
path += encodeURIComponent(route.name);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!focusedParams) {
|
|
210
|
+
focusedParams = focusedRoute.params;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (route.state) {
|
|
214
|
+
path += '/';
|
|
215
|
+
} else if (focusedParams) {
|
|
216
|
+
for (let param in focusedParams) {
|
|
217
|
+
if (focusedParams[param] === 'undefined') {
|
|
218
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
219
|
+
delete focusedParams[param];
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const query = queryString.stringify(focusedParams, { sort: false });
|
|
224
|
+
|
|
225
|
+
if (query) {
|
|
226
|
+
path += `?${query}`;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
current = route.state;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Remove multiple as well as trailing slashes
|
|
234
|
+
path = path.replace(/\/+/g, '/');
|
|
235
|
+
path = path.length > 1 ? path.replace(/\/$/, '') : path;
|
|
236
|
+
|
|
237
|
+
return path;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const getParamName = (pattern: string) =>
|
|
241
|
+
pattern.replace(/^:/, '').replace(/\?$/, '');
|
|
242
|
+
|
|
243
|
+
const joinPaths = (...paths: string[]): string =>
|
|
244
|
+
([] as string[])
|
|
245
|
+
.concat(...paths.map((p) => p.split('/')))
|
|
246
|
+
.filter(Boolean)
|
|
247
|
+
.join('/');
|
|
248
|
+
|
|
249
|
+
const createConfigItem = (
|
|
250
|
+
config: PathConfig<object> | string,
|
|
251
|
+
parentPattern?: string
|
|
252
|
+
): ConfigItem => {
|
|
253
|
+
if (typeof config === 'string') {
|
|
254
|
+
// If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern
|
|
255
|
+
const pattern = parentPattern ? joinPaths(parentPattern, config) : config;
|
|
256
|
+
|
|
257
|
+
return { pattern };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// If an object is specified as the value (e.g. Foo: { ... }),
|
|
261
|
+
// It can have `path` property and `screens` prop which has nested configs
|
|
262
|
+
let pattern: string | undefined;
|
|
263
|
+
|
|
264
|
+
if (config.exact && config.path === undefined) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
"A 'path' needs to be specified when specifying 'exact: true'. If you don't want this screen in the URL, specify it as empty string, e.g. `path: ''`."
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
pattern =
|
|
271
|
+
config.exact !== true
|
|
272
|
+
? joinPaths(parentPattern || '', config.path || '')
|
|
273
|
+
: config.path || '';
|
|
274
|
+
|
|
275
|
+
const screens = config.screens
|
|
276
|
+
? createNormalizedConfigs(config.screens, pattern)
|
|
277
|
+
: undefined;
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
|
|
281
|
+
pattern: pattern?.split('/').filter(Boolean).join('/'),
|
|
282
|
+
stringify: config.stringify,
|
|
283
|
+
screens,
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
const createNormalizedConfigs = (
|
|
288
|
+
options: PathConfigMap<object>,
|
|
289
|
+
pattern?: string
|
|
290
|
+
): Record<string, ConfigItem> =>
|
|
291
|
+
fromEntries(
|
|
292
|
+
Object.entries(options).map(([name, c]) => {
|
|
293
|
+
const result = createConfigItem(c, pattern);
|
|
294
|
+
|
|
295
|
+
return [name, result];
|
|
296
|
+
})
|
|
297
|
+
);
|