@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 @@
|
|
|
1
|
+
{"version":3,"sources":["findFocusedRoute.tsx"],"names":["findFocusedRoute","state","current","routes","index","route"],"mappings":";;;;;;;AAEe,SAASA,gBAAT,CAA0BC,KAA1B,EAA+C;AAAA;;AAC5D,MAAIC,OAAiC,GAAGD,KAAxC;;AAEA,SAAO,aAAAC,OAAO,UAAP,4CAASC,MAAT,mBAAgBD,OAAO,CAACE,KAAxB,2DAAiC,CAAjC,EAAoCH,KAApC,KAA6C,IAApD,EAA0D;AAAA;;AACxDC,IAAAA,OAAO,GAAGA,OAAO,CAACC,MAAR,oBAAeD,OAAO,CAACE,KAAvB,6DAAgC,CAAhC,EAAmCH,KAA7C;AACD;;AAED,QAAMI,KAAK,gBAAGH,OAAH,8CAAG,UAASC,MAAT,iCAAgBD,OAAhB,8CAAgB,UAASE,KAAzB,6DAAkC,CAAlC,CAAd;AAEA,SAAOC,KAAP;AACD","sourcesContent":["import type { InitialState } from '@react-navigation/routers';\n\nexport default function findFocusedRoute(state: InitialState) {\n let current: InitialState | undefined = state;\n\n while (current?.routes[current.index ?? 0].state != null) {\n current = current.routes[current.index ?? 0].state;\n }\n\n const route = current?.routes[current?.index ?? 0];\n\n return route;\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = fromEntries;
|
|
7
|
+
|
|
8
|
+
// Object.fromEntries is not available in older iOS versions
|
|
9
|
+
function fromEntries(entries) {
|
|
10
|
+
return entries.reduce((acc, [k, v]) => {
|
|
11
|
+
if (acc.hasOwnProperty(k)) {
|
|
12
|
+
throw new Error(`A value for key '${k}' already exists in the object.`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
acc[k] = v;
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=fromEntries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["fromEntries.tsx"],"names":["fromEntries","entries","reduce","acc","k","v","hasOwnProperty","Error"],"mappings":";;;;;;;AAAA;AACe,SAASA,WAAT,CACbC,OADa,EAEb;AACA,SAAOA,OAAO,CAACC,MAAR,CAAe,CAACC,GAAD,EAAM,CAACC,CAAD,EAAIC,CAAJ,CAAN,KAAiB;AACrC,QAAIF,GAAG,CAACG,cAAJ,CAAmBF,CAAnB,CAAJ,EAA2B;AACzB,YAAM,IAAIG,KAAJ,CAAW,oBAAmBH,CAAE,iCAAhC,CAAN;AACD;;AAEDD,IAAAA,GAAG,CAACC,CAAD,CAAH,GAASC,CAAT;AACA,WAAOF,GAAP;AACD,GAPM,EAOJ,EAPI,CAAP;AAQD","sourcesContent":["// Object.fromEntries is not available in older iOS versions\nexport default function fromEntries<K extends string, V>(\n entries: (readonly [K, V])[]\n) {\n return entries.reduce((acc, [k, v]) => {\n if (acc.hasOwnProperty(k)) {\n throw new Error(`A value for key '${k}' already exists in the object.`);\n }\n\n acc[k] = v;\n return acc;\n }, {} as Record<K, V>);\n}\n"]}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getActionFromState;
|
|
7
|
+
|
|
8
|
+
function getActionFromState(state, options) {
|
|
9
|
+
var _state$index, _normalizedConfig$scr;
|
|
10
|
+
|
|
11
|
+
// Create a normalized configs object which will be easier to use
|
|
12
|
+
const normalizedConfig = options ? createNormalizedConfigItem(options) : {};
|
|
13
|
+
const routes = state.index != null ? state.routes.slice(0, state.index + 1) : state.routes;
|
|
14
|
+
|
|
15
|
+
if (routes.length === 0) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!(routes.length === 1 && routes[0].key === undefined || routes.length === 2 && routes[0].key === undefined && routes[0].name === (normalizedConfig === null || normalizedConfig === void 0 ? void 0 : normalizedConfig.initialRouteName) && routes[1].key === undefined)) {
|
|
20
|
+
return {
|
|
21
|
+
type: 'RESET',
|
|
22
|
+
payload: state
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const route = state.routes[(_state$index = state.index) !== null && _state$index !== void 0 ? _state$index : state.routes.length - 1];
|
|
27
|
+
let current = route === null || route === void 0 ? void 0 : route.state;
|
|
28
|
+
let config = normalizedConfig === null || normalizedConfig === void 0 ? void 0 : (_normalizedConfig$scr = normalizedConfig.screens) === null || _normalizedConfig$scr === void 0 ? void 0 : _normalizedConfig$scr[route === null || route === void 0 ? void 0 : route.name];
|
|
29
|
+
let params = { ...route.params
|
|
30
|
+
};
|
|
31
|
+
let payload = route ? {
|
|
32
|
+
name: route.name,
|
|
33
|
+
path: route.path,
|
|
34
|
+
params
|
|
35
|
+
} : undefined;
|
|
36
|
+
|
|
37
|
+
while (current) {
|
|
38
|
+
var _config, _config2, _config2$screens;
|
|
39
|
+
|
|
40
|
+
if (current.routes.length === 0) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const routes = current.index != null ? current.routes.slice(0, current.index + 1) : current.routes;
|
|
45
|
+
const route = routes[routes.length - 1]; // Explicitly set to override existing value when merging params
|
|
46
|
+
|
|
47
|
+
Object.assign(params, {
|
|
48
|
+
initial: undefined,
|
|
49
|
+
screen: undefined,
|
|
50
|
+
params: undefined,
|
|
51
|
+
state: undefined
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (routes.length === 1 && routes[0].key === undefined) {
|
|
55
|
+
params.initial = true;
|
|
56
|
+
params.screen = route.name;
|
|
57
|
+
} else if (routes.length === 2 && routes[0].key === undefined && routes[0].name === ((_config = config) === null || _config === void 0 ? void 0 : _config.initialRouteName) && routes[1].key === undefined) {
|
|
58
|
+
params.initial = false;
|
|
59
|
+
params.screen = route.name;
|
|
60
|
+
} else {
|
|
61
|
+
params.state = current;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (route.state) {
|
|
66
|
+
params.params = { ...route.params
|
|
67
|
+
};
|
|
68
|
+
params = params.params;
|
|
69
|
+
} else {
|
|
70
|
+
params.path = route.path;
|
|
71
|
+
params.params = route.params;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
current = route.state;
|
|
75
|
+
config = (_config2 = config) === null || _config2 === void 0 ? void 0 : (_config2$screens = _config2.screens) === null || _config2$screens === void 0 ? void 0 : _config2$screens[route.name];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!payload) {
|
|
79
|
+
return;
|
|
80
|
+
} // Try to construct payload for a `NAVIGATE` action from the state
|
|
81
|
+
// This lets us preserve the navigation state and not lose it
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
type: 'NAVIGATE',
|
|
86
|
+
payload
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const createNormalizedConfigItem = config => typeof config === 'object' && config != null ? {
|
|
91
|
+
initialRouteName: config.initialRouteName,
|
|
92
|
+
screens: config.screens != null ? createNormalizedConfigs(config.screens) : undefined
|
|
93
|
+
} : {};
|
|
94
|
+
|
|
95
|
+
const createNormalizedConfigs = options => Object.entries(options).reduce((acc, [k, v]) => {
|
|
96
|
+
acc[k] = createNormalizedConfigItem(v);
|
|
97
|
+
return acc;
|
|
98
|
+
}, {});
|
|
99
|
+
//# sourceMappingURL=getActionFromState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getActionFromState.tsx"],"names":["getActionFromState","state","options","normalizedConfig","createNormalizedConfigItem","routes","index","slice","length","undefined","key","name","initialRouteName","type","payload","route","current","config","screens","params","path","Object","assign","initial","screen","createNormalizedConfigs","entries","reduce","acc","k","v"],"mappings":";;;;;;;AA8Be,SAASA,kBAAT,CACbC,KADa,EAEbC,OAFa,EAGuD;AAAA;;AACpE;AACA,QAAMC,gBAAgB,GAAGD,OAAO,GAC5BE,0BAA0B,CAACF,OAAD,CADE,GAE5B,EAFJ;AAIA,QAAMG,MAAM,GACVJ,KAAK,CAACK,KAAN,IAAe,IAAf,GAAsBL,KAAK,CAACI,MAAN,CAAaE,KAAb,CAAmB,CAAnB,EAAsBN,KAAK,CAACK,KAAN,GAAc,CAApC,CAAtB,GAA+DL,KAAK,CAACI,MADvE;;AAGA,MAAIA,MAAM,CAACG,MAAP,KAAkB,CAAtB,EAAyB;AACvB,WAAOC,SAAP;AACD;;AAED,MACE,EACGJ,MAAM,CAACG,MAAP,KAAkB,CAAlB,IAAuBH,MAAM,CAAC,CAAD,CAAN,CAAUK,GAAV,KAAkBD,SAA1C,IACCJ,MAAM,CAACG,MAAP,KAAkB,CAAlB,IACCH,MAAM,CAAC,CAAD,CAAN,CAAUK,GAAV,KAAkBD,SADnB,IAECJ,MAAM,CAAC,CAAD,CAAN,CAAUM,IAAV,MAAmBR,gBAAnB,aAAmBA,gBAAnB,uBAAmBA,gBAAgB,CAAES,gBAArC,CAFD,IAGCP,MAAM,CAAC,CAAD,CAAN,CAAUK,GAAV,KAAkBD,SALtB,CADF,EAQE;AACA,WAAO;AACLI,MA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O;AACLD,IAAAA,IAAI,EAAE,UADD;AAELC,IAAAA;AAFK,GAAP;AAID;;AAED,MAAMV,0BAA0B,GAAIa,MAAD,IACjC,OAAOA,MAAP,KAAkB,QAAlB,IAA8BA,MAAM,IAAI,IAAxC,GACI;AACEL,EAAAA,gBAAgB,EAAEK,MAAM,CAACL,gBAD3B;AAEEM,EAAAA,OAAO,EACLD,MAAM,CAACC,OAAP,IAAkB,IAAlB,GACIO,uBAAuB,CAACR,MAAM,CAACC,OAAR,CAD3B,GAEIT;AALR,CADJ,GAQI,EATN;;AAWA,MAAMgB,uBAAuB,GAAIvB,OAAD,IAC9BmB,MAAM,CAACK,OAAP,CAAexB,OAAf,EAAwByB,MAAxB,CAA2D,CAACC,GAAD,EAAM,CAACC,CAAD,EAAIC,CAAJ,CAAN,KAAiB;AAC1EF,EAAAA,GAAG,CAACC,CAAD,CAAH,GAASzB,0BAA0B,CAAC0B,CAAD,CAAnC;AACA,SAAOF,GAAP;AACD,CAHD,EAGG,EAHH,CADF","sourcesContent":["import type {\n CommonActions,\n NavigationState,\n ParamListBase,\n PartialRoute,\n PartialState,\n Route,\n} from '@react-navigation/routers';\n\nimport type { NavigatorScreenParams, PathConfig, PathConfigMap } from './types';\n\ntype ConfigItem = {\n initialRouteName?: string;\n screens?: Record<string, ConfigItem>;\n};\n\ntype Options = {\n initialRouteName?: string;\n screens: PathConfigMap<object>;\n};\n\ntype NavigateAction<State extends NavigationState> = {\n type: 'NAVIGATE';\n payload: {\n name: string;\n params?: NavigatorScreenParams<State>;\n path?: string;\n };\n};\n\nexport default function getActionFromState(\n state: PartialState<NavigationState>,\n options?: Options\n): NavigateAction<NavigationState> | CommonActions.Action | undefined {\n // Create a normalized configs object which will be easier to use\n const normalizedConfig = options\n ? createNormalizedConfigItem(options as PathConfig<object> | string)\n : {};\n\n const routes =\n state.index != null ? state.routes.slice(0, state.index + 1) : state.routes;\n\n if (routes.length === 0) {\n return undefined;\n }\n\n if (\n !(\n (routes.length === 1 && routes[0].key === undefined) ||\n (routes.length === 2 &&\n routes[0].key === undefined &&\n routes[0].name === normalizedConfig?.initialRouteName &&\n routes[1].key === undefined)\n )\n ) {\n return {\n type: 'RESET',\n payload: state,\n };\n }\n\n const route = state.routes[state.index ?? state.routes.length - 1];\n\n let current: PartialState<NavigationState> | undefined = route?.state;\n let config: ConfigItem | undefined = normalizedConfig?.screens?.[route?.name];\n let params = { ...route.params } as NavigatorScreenParams<\n ParamListBase,\n NavigationState\n >;\n\n let payload = route\n ? { name: route.name, path: route.path, params }\n : undefined;\n\n while (current) {\n if (current.routes.length === 0) {\n return undefined;\n }\n\n const routes =\n current.index != null\n ? current.routes.slice(0, current.index + 1)\n : current.routes;\n\n const route: Route<string> | PartialRoute<Route<string>> =\n routes[routes.length - 1];\n\n // Explicitly set to override existing value when merging params\n Object.assign(params, {\n initial: undefined,\n screen: undefined,\n params: undefined,\n state: undefined,\n });\n\n if (routes.length === 1 && routes[0].key === undefined) {\n params.initial = true;\n params.screen = route.name;\n } else if (\n routes.length === 2 &&\n routes[0].key === undefined &&\n routes[0].name === config?.initialRouteName &&\n routes[1].key === undefined\n ) {\n params.initial = false;\n params.screen = route.name;\n } else {\n params.state = current;\n break;\n }\n\n if (route.state) {\n params.params = { ...route.params };\n params = params.params as NavigatorScreenParams<\n ParamListBase,\n NavigationState\n >;\n } else {\n params.path = route.path;\n params.params = route.params;\n }\n\n current = route.state;\n config = config?.screens?.[route.name];\n }\n\n if (!payload) {\n return;\n }\n\n // Try to construct payload for a `NAVIGATE` action from the state\n // This lets us preserve the navigation state and not lose it\n return {\n type: 'NAVIGATE',\n payload,\n };\n}\n\nconst createNormalizedConfigItem = (config: PathConfig<object> | string) =>\n typeof config === 'object' && config != null\n ? {\n initialRouteName: config.initialRouteName,\n screens:\n config.screens != null\n ? createNormalizedConfigs(config.screens)\n : undefined,\n }\n : {};\n\nconst createNormalizedConfigs = (options: PathConfigMap<object>) =>\n Object.entries(options).reduce<Record<string, ConfigItem>>((acc, [k, v]) => {\n acc[k] = createNormalizedConfigItem(v);\n return acc;\n }, {});\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getFocusedRouteNameFromRoute;
|
|
7
|
+
|
|
8
|
+
var _useRouteCache = require("./useRouteCache");
|
|
9
|
+
|
|
10
|
+
function getFocusedRouteNameFromRoute(route) {
|
|
11
|
+
var _route$CHILD_STATE, _state$index;
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error: this isn't in type definitions coz we want this private
|
|
14
|
+
const state = (_route$CHILD_STATE = route[_useRouteCache.CHILD_STATE]) !== null && _route$CHILD_STATE !== void 0 ? _route$CHILD_STATE : route.state;
|
|
15
|
+
const params = route.params;
|
|
16
|
+
const routeName = state ? // Get the currently active route name in the nested navigator
|
|
17
|
+
state.routes[// If we have a partial state without index, for tab/drawer, first screen will be focused one, and last for stack
|
|
18
|
+
// The type property will only exist for rehydrated state and not for state from deep link
|
|
19
|
+
(_state$index = state.index) !== null && _state$index !== void 0 ? _state$index : typeof state.type === 'string' && state.type !== 'stack' ? 0 : state.routes.length - 1].name : // If state doesn't exist, we need to default to `screen` param if available
|
|
20
|
+
typeof (params === null || params === void 0 ? void 0 : params.screen) === 'string' ? params.screen : undefined;
|
|
21
|
+
return routeName;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=getFocusedRouteNameFromRoute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getFocusedRouteNameFromRoute.tsx"],"names":["getFocusedRouteNameFromRoute","route","state","CHILD_STATE","params","routeName","routes","index","type","length","name","screen","undefined"],"mappings":";;;;;;;AAEA;;AAEe,SAASA,4BAAT,CACbC,KADa,EAEO;AAAA;;AACpB;AACA,QAAMC,KAAK,yBAAGD,KAAK,CAACE,0BAAD,CAAR,mEAAyBF,KAAK,CAACC,KAA1C;AACA,QAAME,MAAM,GAAGH,KAAK,CAACG,MAArB;AAEA,QAAMC,SAAS,GAAGH,KAAK,GACnB;AACAA,EAAAA,KAAK,CAACI,MAAN,CACE;AACA;AAFF,kBAGEJ,KAAK,CAACK,KAHR,uDAIK,OAAOL,KAAK,CAACM,IAAb,KAAsB,QAAtB,IAAkCN,KAAK,CAACM,IAAN,KAAe,OAAjD,GACG,CADH,GAEGN,KAAK,CAACI,MAAN,CAAaG,MAAb,GAAsB,CAN9B,EAOEC,IATiB,GAUnB;AACF,UAAON,MAAP,aAAOA,MAAP,uBAAOA,MAAM,CAAEO,MAAf,MAA0B,QAA1B,GACEP,MAAM,CAACO,MADT,GAEEC,SAbJ;AAeA,SAAOP,SAAP;AACD","sourcesContent":["import type { Route } from '@react-navigation/routers';\n\nimport { CHILD_STATE } from './useRouteCache';\n\nexport default function getFocusedRouteNameFromRoute(\n route: Partial<Route<string>>\n): string | undefined {\n // @ts-expect-error: this isn't in type definitions coz we want this private\n const state = route[CHILD_STATE] ?? route.state;\n const params = route.params as { screen?: unknown } | undefined;\n\n const routeName = state\n ? // Get the currently active route name in the nested navigator\n state.routes[\n // If we have a partial state without index, for tab/drawer, first screen will be focused one, and last for stack\n // The type property will only exist for rehydrated state and not for state from deep link\n state.index ??\n (typeof state.type === 'string' && state.type !== 'stack'\n ? 0\n : state.routes.length - 1)\n ].name\n : // If state doesn't exist, we need to default to `screen` param if available\n typeof params?.screen === 'string'\n ? params.screen\n : undefined;\n\n return routeName;\n}\n"]}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getPathFromState;
|
|
7
|
+
|
|
8
|
+
var queryString = _interopRequireWildcard(require("query-string"));
|
|
9
|
+
|
|
10
|
+
var _fromEntries = _interopRequireDefault(require("./fromEntries"));
|
|
11
|
+
|
|
12
|
+
var _validatePathConfig = _interopRequireDefault(require("./validatePathConfig"));
|
|
13
|
+
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
15
|
+
|
|
16
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
17
|
+
|
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
19
|
+
|
|
20
|
+
const getActiveRoute = state => {
|
|
21
|
+
const route = typeof state.index === 'number' ? state.routes[state.index] : state.routes[state.routes.length - 1];
|
|
22
|
+
|
|
23
|
+
if (route.state) {
|
|
24
|
+
return getActiveRoute(route.state);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return route;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Utility to serialize a navigation state object to a path string.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```js
|
|
34
|
+
* getPathFromState(
|
|
35
|
+
* {
|
|
36
|
+
* routes: [
|
|
37
|
+
* {
|
|
38
|
+
* name: 'Chat',
|
|
39
|
+
* params: { author: 'Jane', id: 42 },
|
|
40
|
+
* },
|
|
41
|
+
* ],
|
|
42
|
+
* },
|
|
43
|
+
* {
|
|
44
|
+
* screens: {
|
|
45
|
+
* Chat: {
|
|
46
|
+
* path: 'chat/:author/:id',
|
|
47
|
+
* stringify: { author: author => author.toLowerCase() }
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* )
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @param state Navigation state to serialize.
|
|
55
|
+
* @param options Extra options to fine-tune how to serialize the path.
|
|
56
|
+
* @returns Path representing the state, e.g. /foo/bar?count=42.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
function getPathFromState(state, options) {
|
|
61
|
+
if (state == null) {
|
|
62
|
+
throw Error("Got 'undefined' for the navigation state. You must pass a valid state object.");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (options) {
|
|
66
|
+
(0, _validatePathConfig.default)(options);
|
|
67
|
+
} // Create a normalized configs object which will be easier to use
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
const configs = options !== null && options !== void 0 && options.screens ? createNormalizedConfigs(options === null || options === void 0 ? void 0 : options.screens) : {};
|
|
71
|
+
let path = '/';
|
|
72
|
+
let current = state;
|
|
73
|
+
const allParams = {};
|
|
74
|
+
|
|
75
|
+
while (current) {
|
|
76
|
+
let index = typeof current.index === 'number' ? current.index : 0;
|
|
77
|
+
let route = current.routes[index];
|
|
78
|
+
let pattern;
|
|
79
|
+
let focusedParams;
|
|
80
|
+
let focusedRoute = getActiveRoute(state);
|
|
81
|
+
let currentOptions = configs; // Keep all the route names that appeared during going deeper in config in case the pattern is resolved to undefined
|
|
82
|
+
|
|
83
|
+
let nestedRouteNames = [];
|
|
84
|
+
let hasNext = true;
|
|
85
|
+
|
|
86
|
+
while (route.name in currentOptions && hasNext) {
|
|
87
|
+
pattern = currentOptions[route.name].pattern;
|
|
88
|
+
nestedRouteNames.push(route.name);
|
|
89
|
+
|
|
90
|
+
if (route.params) {
|
|
91
|
+
var _currentOptions$route;
|
|
92
|
+
|
|
93
|
+
const stringify = (_currentOptions$route = currentOptions[route.name]) === null || _currentOptions$route === void 0 ? void 0 : _currentOptions$route.stringify;
|
|
94
|
+
const currentParams = (0, _fromEntries.default)(Object.entries(route.params).map(([key, value]) => [key, stringify !== null && stringify !== void 0 && stringify[key] ? stringify[key](value) : String(value)]));
|
|
95
|
+
|
|
96
|
+
if (pattern) {
|
|
97
|
+
Object.assign(allParams, currentParams);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (focusedRoute === route) {
|
|
101
|
+
var _pattern;
|
|
102
|
+
|
|
103
|
+
// If this is the focused route, keep the params for later use
|
|
104
|
+
// We save it here since it's been stringified already
|
|
105
|
+
focusedParams = { ...currentParams
|
|
106
|
+
};
|
|
107
|
+
(_pattern = pattern) === null || _pattern === void 0 ? void 0 : _pattern.split('/').filter(p => p.startsWith(':')) // eslint-disable-next-line no-loop-func
|
|
108
|
+
.forEach(p => {
|
|
109
|
+
const name = getParamName(p); // Remove the params present in the pattern since we'll only use the rest for query string
|
|
110
|
+
|
|
111
|
+
if (focusedParams) {
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
113
|
+
delete focusedParams[name];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
} // If there is no `screens` property or no nested state, we return pattern
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
if (!currentOptions[route.name].screens || route.state === undefined) {
|
|
121
|
+
hasNext = false;
|
|
122
|
+
} else {
|
|
123
|
+
index = typeof route.state.index === 'number' ? route.state.index : route.state.routes.length - 1;
|
|
124
|
+
const nextRoute = route.state.routes[index];
|
|
125
|
+
const nestedConfig = currentOptions[route.name].screens; // if there is config for next route name, we go deeper
|
|
126
|
+
|
|
127
|
+
if (nestedConfig && nextRoute.name in nestedConfig) {
|
|
128
|
+
route = nextRoute;
|
|
129
|
+
currentOptions = nestedConfig;
|
|
130
|
+
} else {
|
|
131
|
+
// If not, there is no sense in going deeper in config
|
|
132
|
+
hasNext = false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (pattern === undefined) {
|
|
138
|
+
pattern = nestedRouteNames.join('/');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (currentOptions[route.name] !== undefined) {
|
|
142
|
+
path += pattern.split('/').map(p => {
|
|
143
|
+
const name = getParamName(p); // We don't know what to show for wildcard patterns
|
|
144
|
+
// Showing the route name seems ok, though whatever we show here will be incorrect
|
|
145
|
+
// Since the page doesn't actually exist
|
|
146
|
+
|
|
147
|
+
if (p === '*') {
|
|
148
|
+
return route.name;
|
|
149
|
+
} // If the path has a pattern for a param, put the param in the path
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
if (p.startsWith(':')) {
|
|
153
|
+
const value = allParams[name];
|
|
154
|
+
|
|
155
|
+
if (value === undefined && p.endsWith('?')) {
|
|
156
|
+
// Optional params without value assigned in route.params should be ignored
|
|
157
|
+
return '';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return encodeURIComponent(value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return encodeURIComponent(p);
|
|
164
|
+
}).join('/');
|
|
165
|
+
} else {
|
|
166
|
+
path += encodeURIComponent(route.name);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!focusedParams) {
|
|
170
|
+
focusedParams = focusedRoute.params;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (route.state) {
|
|
174
|
+
path += '/';
|
|
175
|
+
} else if (focusedParams) {
|
|
176
|
+
for (let param in focusedParams) {
|
|
177
|
+
if (focusedParams[param] === 'undefined') {
|
|
178
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
179
|
+
delete focusedParams[param];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const query = queryString.stringify(focusedParams, {
|
|
184
|
+
sort: false
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
if (query) {
|
|
188
|
+
path += `?${query}`;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
current = route.state;
|
|
193
|
+
} // Remove multiple as well as trailing slashes
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
path = path.replace(/\/+/g, '/');
|
|
197
|
+
path = path.length > 1 ? path.replace(/\/$/, '') : path;
|
|
198
|
+
return path;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const getParamName = pattern => pattern.replace(/^:/, '').replace(/\?$/, '');
|
|
202
|
+
|
|
203
|
+
const joinPaths = (...paths) => [].concat(...paths.map(p => p.split('/'))).filter(Boolean).join('/');
|
|
204
|
+
|
|
205
|
+
const createConfigItem = (config, parentPattern) => {
|
|
206
|
+
var _pattern2;
|
|
207
|
+
|
|
208
|
+
if (typeof config === 'string') {
|
|
209
|
+
// If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern
|
|
210
|
+
const pattern = parentPattern ? joinPaths(parentPattern, config) : config;
|
|
211
|
+
return {
|
|
212
|
+
pattern
|
|
213
|
+
};
|
|
214
|
+
} // If an object is specified as the value (e.g. Foo: { ... }),
|
|
215
|
+
// It can have `path` property and `screens` prop which has nested configs
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
let pattern;
|
|
219
|
+
|
|
220
|
+
if (config.exact && config.path === undefined) {
|
|
221
|
+
throw new Error("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: ''`.");
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
pattern = config.exact !== true ? joinPaths(parentPattern || '', config.path || '') : config.path || '';
|
|
225
|
+
const screens = config.screens ? createNormalizedConfigs(config.screens, pattern) : undefined;
|
|
226
|
+
return {
|
|
227
|
+
// Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.
|
|
228
|
+
pattern: (_pattern2 = pattern) === null || _pattern2 === void 0 ? void 0 : _pattern2.split('/').filter(Boolean).join('/'),
|
|
229
|
+
stringify: config.stringify,
|
|
230
|
+
screens
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const createNormalizedConfigs = (options, pattern) => (0, _fromEntries.default)(Object.entries(options).map(([name, c]) => {
|
|
235
|
+
const result = createConfigItem(c, pattern);
|
|
236
|
+
return [name, result];
|
|
237
|
+
}));
|
|
238
|
+
//# sourceMappingURL=getPathFromState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["getPathFromState.tsx"],"names":["getActiveRoute","state","route","index","routes","length","getPathFromState","options","Error","configs","screens","createNormalizedConfigs","path","current","allParams","pattern","focusedParams","focusedRoute","currentOptions","nestedRouteNames","hasNext","name","push","params","stringify","currentParams","Object","entries","map","key","value","String","assign","split","filter","p","startsWith","forEach","getParamName","undefined","nextRoute","nestedConfig","join","endsWith","encodeURIComponent","param","query","queryString","sort","replace","joinPaths","paths","concat","Boolean","createConfigItem","config","parentPattern","exact","c","result"],"mappings":";;;;;;;AAKA;;AAEA;;AAEA;;;;;;;;AAiBA,MAAMA,cAAc,GAAIC,KAAD,IAAqD;AAC1E,QAAMC,KAAK,GACT,OAAOD,KAAK,CAACE,KAAb,KAAuB,QAAvB,GACIF,KAAK,CAACG,MAAN,CAAaH,KAAK,CAACE,KAAnB,CADJ,GAEIF,KAAK,CAACG,MAAN,CAAaH,KAAK,CAACG,MAAN,CAAaC,MAAb,GAAsB,CAAnC,CAHN;;AAKA,MAAIH,KAAK,CAACD,KAAV,EAAiB;AACf,WAAOD,cAAc,CAACE,KAAK,CAACD,KAAP,CAArB;AACD;;AAED,SAAOC,KAAP;AACD,CAXD;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACe,SAASI,gBAAT,CACbL,KAD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,gBAAgB,CAACI,CAAD,EAAI3C,OAAJ,CAA/B;AAEA,SAAO,CAACM,IAAD,EAAOsC,MAAP,CAAP;AACD,CAJD,CADF,CAJF","sourcesContent":["import type {\n NavigationState,\n PartialState,\n Route,\n} from '@react-navigation/routers';\nimport * as queryString from 'query-string';\n\nimport fromEntries from './fromEntries';\nimport type { PathConfig, PathConfigMap } from './types';\nimport validatePathConfig from './validatePathConfig';\n\ntype Options<ParamList> = {\n initialRouteName?: string;\n screens: PathConfigMap<ParamList>;\n};\n\ntype State = NavigationState | Omit<PartialState<NavigationState>, 'stale'>;\n\ntype StringifyConfig = Record<string, (value: any) => string>;\n\ntype ConfigItem = {\n pattern?: string;\n stringify?: StringifyConfig;\n screens?: Record<string, ConfigItem>;\n};\n\nconst getActiveRoute = (state: State): { name: string; params?: object } => {\n const route =\n typeof state.index === 'number'\n ? state.routes[state.index]\n : state.routes[state.routes.length - 1];\n\n if (route.state) {\n return getActiveRoute(route.state);\n }\n\n return route;\n};\n\n/**\n * Utility to serialize a navigation state object to a path string.\n *\n * @example\n * ```js\n * getPathFromState(\n * {\n * routes: [\n * {\n * name: 'Chat',\n * params: { author: 'Jane', id: 42 },\n * },\n * ],\n * },\n * {\n * screens: {\n * Chat: {\n * path: 'chat/:author/:id',\n * stringify: { author: author => author.toLowerCase() }\n * }\n * }\n * }\n * )\n * ```\n *\n * @param state Navigation state to serialize.\n * @param options Extra options to fine-tune how to serialize the path.\n * @returns Path representing the state, e.g. /foo/bar?count=42.\n */\nexport default function getPathFromState<ParamList extends {}>(\n state: State,\n options?: Options<ParamList>\n): string {\n if (state == null) {\n throw Error(\n \"Got 'undefined' for the navigation state. You must pass a valid state object.\"\n );\n }\n\n if (options) {\n validatePathConfig(options);\n }\n\n // Create a normalized configs object which will be easier to use\n const configs: Record<string, ConfigItem> = options?.screens\n ? createNormalizedConfigs(options?.screens)\n : {};\n\n let path = '/';\n let current: State | undefined = state;\n\n const allParams: Record<string, any> = {};\n\n while (current) {\n let index = typeof current.index === 'number' ? current.index : 0;\n let route = current.routes[index] as Route<string> & {\n state?: State;\n };\n\n let pattern: string | undefined;\n\n let focusedParams: Record<string, any> | undefined;\n let focusedRoute = getActiveRoute(state);\n let currentOptions = configs;\n\n // Keep all the route names that appeared during going deeper in config in case the pattern is resolved to undefined\n let nestedRouteNames = [];\n\n let hasNext = true;\n\n while (route.name in currentOptions && hasNext) {\n pattern = currentOptions[route.name].pattern;\n\n nestedRouteNames.push(route.name);\n\n if (route.params) {\n const stringify = currentOptions[route.name]?.stringify;\n\n const currentParams = fromEntries(\n Object.entries(route.params).map(([key, value]) => [\n key,\n stringify?.[key] ? stringify[key](value) : String(value),\n ])\n );\n\n if (pattern) {\n Object.assign(allParams, currentParams);\n }\n\n if (focusedRoute === route) {\n // If this is the focused route, keep the params for later use\n // We save it here since it's been stringified already\n focusedParams = { ...currentParams };\n\n pattern\n ?.split('/')\n .filter((p) => p.startsWith(':'))\n // eslint-disable-next-line no-loop-func\n .forEach((p) => {\n const name = getParamName(p);\n\n // Remove the params present in the pattern since we'll only use the rest for query string\n if (focusedParams) {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete focusedParams[name];\n }\n });\n }\n }\n\n // If there is no `screens` property or no nested state, we return pattern\n if (!currentOptions[route.name].screens || route.state === undefined) {\n hasNext = false;\n } else {\n index =\n typeof route.state.index === 'number'\n ? route.state.index\n : route.state.routes.length - 1;\n\n const nextRoute = route.state.routes[index];\n const nestedConfig = currentOptions[route.name].screens;\n\n // if there is config for next route name, we go deeper\n if (nestedConfig && nextRoute.name in nestedConfig) {\n route = nextRoute as Route<string> & { state?: State };\n currentOptions = nestedConfig;\n } else {\n // If not, there is no sense in going deeper in config\n hasNext = false;\n }\n }\n }\n\n if (pattern === undefined) {\n pattern = nestedRouteNames.join('/');\n }\n\n if (currentOptions[route.name] !== undefined) {\n path += pattern\n .split('/')\n .map((p) => {\n const name = getParamName(p);\n\n // We don't know what to show for wildcard patterns\n // Showing the route name seems ok, though whatever we show here will be incorrect\n // Since the page doesn't actually exist\n if (p === '*') {\n return route.name;\n }\n\n // If the path has a pattern for a param, put the param in the path\n if (p.startsWith(':')) {\n const value = allParams[name];\n\n if (value === undefined && p.endsWith('?')) {\n // Optional params without value assigned in route.params should be ignored\n return '';\n }\n\n return encodeURIComponent(value);\n }\n\n return encodeURIComponent(p);\n })\n .join('/');\n } else {\n path += encodeURIComponent(route.name);\n }\n\n if (!focusedParams) {\n focusedParams = focusedRoute.params;\n }\n\n if (route.state) {\n path += '/';\n } else if (focusedParams) {\n for (let param in focusedParams) {\n if (focusedParams[param] === 'undefined') {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete focusedParams[param];\n }\n }\n\n const query = queryString.stringify(focusedParams, { sort: false });\n\n if (query) {\n path += `?${query}`;\n }\n }\n\n current = route.state;\n }\n\n // Remove multiple as well as trailing slashes\n path = path.replace(/\\/+/g, '/');\n path = path.length > 1 ? path.replace(/\\/$/, '') : path;\n\n return path;\n}\n\nconst getParamName = (pattern: string) =>\n pattern.replace(/^:/, '').replace(/\\?$/, '');\n\nconst joinPaths = (...paths: string[]): string =>\n ([] as string[])\n .concat(...paths.map((p) => p.split('/')))\n .filter(Boolean)\n .join('/');\n\nconst createConfigItem = (\n config: PathConfig<object> | string,\n parentPattern?: string\n): ConfigItem => {\n if (typeof config === 'string') {\n // If a string is specified as the value of the key(e.g. Foo: '/path'), use it as the pattern\n const pattern = parentPattern ? joinPaths(parentPattern, config) : config;\n\n return { pattern };\n }\n\n // If an object is specified as the value (e.g. Foo: { ... }),\n // It can have `path` property and `screens` prop which has nested configs\n let pattern: string | undefined;\n\n if (config.exact && config.path === undefined) {\n throw new Error(\n \"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: ''`.\"\n );\n }\n\n pattern =\n config.exact !== true\n ? joinPaths(parentPattern || '', config.path || '')\n : config.path || '';\n\n const screens = config.screens\n ? createNormalizedConfigs(config.screens, pattern)\n : undefined;\n\n return {\n // Normalize pattern to remove any leading, trailing slashes, duplicate slashes etc.\n pattern: pattern?.split('/').filter(Boolean).join('/'),\n stringify: config.stringify,\n screens,\n };\n};\n\nconst createNormalizedConfigs = (\n options: PathConfigMap<object>,\n pattern?: string\n): Record<string, ConfigItem> =>\n fromEntries(\n Object.entries(options).map(([name, c]) => {\n const result = createConfigItem(c, pattern);\n\n return [name, result];\n })\n );\n"]}
|