@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,40 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import type { KeyedListenerMap } from './NavigationBuilderContext';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook which lets child navigators add getters to be called for obtaining rehydrated state.
|
|
7
|
+
*/
|
|
8
|
+
export default function useKeyedChildListeners() {
|
|
9
|
+
const { current: keyedListeners } = React.useRef<
|
|
10
|
+
{
|
|
11
|
+
[K in keyof KeyedListenerMap]: Record<
|
|
12
|
+
string,
|
|
13
|
+
KeyedListenerMap[K] | undefined
|
|
14
|
+
>;
|
|
15
|
+
}
|
|
16
|
+
>({
|
|
17
|
+
getState: {},
|
|
18
|
+
beforeRemove: {},
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const addKeyedListener = React.useCallback(
|
|
22
|
+
<T extends keyof KeyedListenerMap>(
|
|
23
|
+
type: T,
|
|
24
|
+
key: string,
|
|
25
|
+
listener: KeyedListenerMap[T]
|
|
26
|
+
) => {
|
|
27
|
+
keyedListeners[type][key] = listener;
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
keyedListeners[type][key] = undefined;
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
[keyedListeners]
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
keyedListeners,
|
|
38
|
+
addKeyedListener,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
import NavigationContainerRefContext from './NavigationContainerRefContext';
|
|
4
|
+
import NavigationContext from './NavigationContext';
|
|
5
|
+
import type { NavigationProp } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hook to access the navigation prop of the parent screen anywhere.
|
|
9
|
+
*
|
|
10
|
+
* @returns Navigation prop of the parent screen.
|
|
11
|
+
*/
|
|
12
|
+
export default function useNavigation<
|
|
13
|
+
T = NavigationProp<ReactNavigation.RootParamList>
|
|
14
|
+
>(): T {
|
|
15
|
+
const root = React.useContext(NavigationContainerRefContext);
|
|
16
|
+
const navigation = React.useContext(NavigationContext);
|
|
17
|
+
|
|
18
|
+
if (navigation === undefined && root === undefined) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"Couldn't find a navigation object. Is your component inside NavigationContainer?"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// FIXME: Figure out a better way to do this
|
|
25
|
+
return (navigation ?? root) as unknown as T;
|
|
26
|
+
}
|
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CommonActions,
|
|
3
|
+
DefaultRouterOptions,
|
|
4
|
+
NavigationAction,
|
|
5
|
+
NavigationState,
|
|
6
|
+
ParamListBase,
|
|
7
|
+
PartialState,
|
|
8
|
+
Route,
|
|
9
|
+
Router,
|
|
10
|
+
RouterConfigOptions,
|
|
11
|
+
RouterFactory,
|
|
12
|
+
} from '@react-navigation/routers';
|
|
13
|
+
import * as React from 'react';
|
|
14
|
+
import { isValidElementType } from 'react-is';
|
|
15
|
+
|
|
16
|
+
import Group from './Group';
|
|
17
|
+
import isArrayEqual from './isArrayEqual';
|
|
18
|
+
import NavigationHelpersContext from './NavigationHelpersContext';
|
|
19
|
+
import NavigationRouteContext from './NavigationRouteContext';
|
|
20
|
+
import NavigationStateContext from './NavigationStateContext';
|
|
21
|
+
import Screen from './Screen';
|
|
22
|
+
import {
|
|
23
|
+
DefaultNavigatorOptions,
|
|
24
|
+
EventMapBase,
|
|
25
|
+
EventMapCore,
|
|
26
|
+
NavigatorScreenParams,
|
|
27
|
+
PrivateValueStore,
|
|
28
|
+
RouteConfig,
|
|
29
|
+
} from './types';
|
|
30
|
+
import useChildListeners from './useChildListeners';
|
|
31
|
+
import useComponent from './useComponent';
|
|
32
|
+
import useCurrentRender from './useCurrentRender';
|
|
33
|
+
import useDescriptors, { ScreenConfigWithParent } from './useDescriptors';
|
|
34
|
+
import useEventEmitter from './useEventEmitter';
|
|
35
|
+
import useFocusedListenersChildrenAdapter from './useFocusedListenersChildrenAdapter';
|
|
36
|
+
import useFocusEvents from './useFocusEvents';
|
|
37
|
+
import useKeyedChildListeners from './useKeyedChildListeners';
|
|
38
|
+
import useNavigationHelpers from './useNavigationHelpers';
|
|
39
|
+
import useOnAction from './useOnAction';
|
|
40
|
+
import useOnGetState from './useOnGetState';
|
|
41
|
+
import useOnRouteFocus from './useOnRouteFocus';
|
|
42
|
+
import useRegisterNavigator from './useRegisterNavigator';
|
|
43
|
+
import useScheduleUpdate from './useScheduleUpdate';
|
|
44
|
+
|
|
45
|
+
// This is to make TypeScript compiler happy
|
|
46
|
+
// eslint-disable-next-line babel/no-unused-expressions
|
|
47
|
+
PrivateValueStore;
|
|
48
|
+
|
|
49
|
+
type NavigatorRoute<State extends NavigationState> = {
|
|
50
|
+
key: string;
|
|
51
|
+
params?: NavigatorScreenParams<ParamListBase, State>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Extract route config object from React children elements.
|
|
56
|
+
*
|
|
57
|
+
* @param children React Elements to extract the config from.
|
|
58
|
+
*/
|
|
59
|
+
const getRouteConfigsFromChildren = <
|
|
60
|
+
State extends NavigationState,
|
|
61
|
+
ScreenOptions extends {},
|
|
62
|
+
EventMap extends EventMapBase
|
|
63
|
+
>(
|
|
64
|
+
children: React.ReactNode,
|
|
65
|
+
options?: ScreenConfigWithParent<State, ScreenOptions, EventMap>[0]
|
|
66
|
+
) => {
|
|
67
|
+
const configs = React.Children.toArray(children).reduce<
|
|
68
|
+
ScreenConfigWithParent<State, ScreenOptions, EventMap>[]
|
|
69
|
+
>((acc, child) => {
|
|
70
|
+
if (React.isValidElement(child)) {
|
|
71
|
+
if (child.type === Screen) {
|
|
72
|
+
// We can only extract the config from `Screen` elements
|
|
73
|
+
// If something else was rendered, it's probably a bug
|
|
74
|
+
acc.push([
|
|
75
|
+
options,
|
|
76
|
+
child.props as RouteConfig<
|
|
77
|
+
ParamListBase,
|
|
78
|
+
string,
|
|
79
|
+
State,
|
|
80
|
+
ScreenOptions,
|
|
81
|
+
EventMap
|
|
82
|
+
>,
|
|
83
|
+
]);
|
|
84
|
+
return acc;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (child.type === React.Fragment || child.type === Group) {
|
|
88
|
+
// When we encounter a fragment or group, we need to dive into its children to extract the configs
|
|
89
|
+
// This is handy to conditionally define a group of screens
|
|
90
|
+
acc.push(
|
|
91
|
+
...getRouteConfigsFromChildren<State, ScreenOptions, EventMap>(
|
|
92
|
+
child.props.children,
|
|
93
|
+
child.type !== Group
|
|
94
|
+
? options
|
|
95
|
+
: options != null
|
|
96
|
+
? [...options, child.props.screenOptions]
|
|
97
|
+
: [child.props.screenOptions]
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
return acc;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
throw new Error(
|
|
105
|
+
`A navigator can only contain 'Screen', 'Group' or 'React.Fragment' as its direct children (found ${
|
|
106
|
+
React.isValidElement(child)
|
|
107
|
+
? `'${
|
|
108
|
+
typeof child.type === 'string' ? child.type : child.type?.name
|
|
109
|
+
}'${
|
|
110
|
+
child.props?.name ? ` for the screen '${child.props.name}'` : ''
|
|
111
|
+
}`
|
|
112
|
+
: typeof child === 'object'
|
|
113
|
+
? JSON.stringify(child)
|
|
114
|
+
: `'${String(child)}'`
|
|
115
|
+
}). To render this component in the navigator, pass it in the 'component' prop to 'Screen'.`
|
|
116
|
+
);
|
|
117
|
+
}, []);
|
|
118
|
+
|
|
119
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
120
|
+
configs.forEach((config) => {
|
|
121
|
+
const { name, children, component, getComponent } = config[1];
|
|
122
|
+
|
|
123
|
+
if (typeof name !== 'string' || !name) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Got an invalid name (${JSON.stringify(
|
|
126
|
+
name
|
|
127
|
+
)}) for the screen. It must be a non-empty string.`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (
|
|
132
|
+
children != null ||
|
|
133
|
+
component !== undefined ||
|
|
134
|
+
getComponent !== undefined
|
|
135
|
+
) {
|
|
136
|
+
if (children != null && component !== undefined) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Got both 'component' and 'children' props for the screen '${name}'. You must pass only one of them.`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (children != null && getComponent !== undefined) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Got both 'getComponent' and 'children' props for the screen '${name}'. You must pass only one of them.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (component !== undefined && getComponent !== undefined) {
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Got both 'component' and 'getComponent' props for the screen '${name}'. You must pass only one of them.`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (children != null && typeof children !== 'function') {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Got an invalid value for 'children' prop for the screen '${name}'. It must be a function returning a React Element.`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (component !== undefined && !isValidElementType(component)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`Got an invalid value for 'component' prop for the screen '${name}'. It must be a valid React Component.`
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (getComponent !== undefined && typeof getComponent !== 'function') {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Got an invalid value for 'getComponent' prop for the screen '${name}'. It must be a function returning a React Component.`
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (typeof component === 'function' && component.name === 'component') {
|
|
173
|
+
// Inline anonymous functions passed in the `component` prop will have the name of the prop
|
|
174
|
+
// It's relatively safe to assume that it's not a component since it should also have PascalCase name
|
|
175
|
+
// We won't catch all scenarios here, but this should catch a good chunk of incorrect use.
|
|
176
|
+
console.warn(
|
|
177
|
+
`Looks like you're passing an inline function for 'component' prop for the screen '${name}' (e.g. component={() => <SomeComponent />}). Passing an inline function will cause the component state to be lost on re-render and cause perf issues since it's re-created every render. You can pass the function as children to 'Screen' instead to achieve the desired behaviour.`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
throw new Error(
|
|
182
|
+
`Couldn't find a 'component', 'getComponent' or 'children' prop for the screen '${name}'. This can happen if you passed 'undefined'. You likely forgot to export your component from the file it's defined in, or mixed up default import and named import when importing.`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return configs;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Hook for building navigators.
|
|
193
|
+
*
|
|
194
|
+
* @param createRouter Factory method which returns router object.
|
|
195
|
+
* @param options Options object containing `children` and additional options for the router.
|
|
196
|
+
* @returns An object containing `state`, `navigation`, `descriptors` objects.
|
|
197
|
+
*/
|
|
198
|
+
export default function useNavigationBuilder<
|
|
199
|
+
State extends NavigationState,
|
|
200
|
+
RouterOptions extends DefaultRouterOptions,
|
|
201
|
+
ActionHelpers extends Record<string, () => void>,
|
|
202
|
+
ScreenOptions extends {},
|
|
203
|
+
EventMap extends Record<string, any>
|
|
204
|
+
>(
|
|
205
|
+
createRouter: RouterFactory<State, any, RouterOptions>,
|
|
206
|
+
options: DefaultNavigatorOptions<
|
|
207
|
+
ParamListBase,
|
|
208
|
+
State,
|
|
209
|
+
ScreenOptions,
|
|
210
|
+
EventMap
|
|
211
|
+
> &
|
|
212
|
+
RouterOptions
|
|
213
|
+
) {
|
|
214
|
+
const navigatorKey = useRegisterNavigator();
|
|
215
|
+
|
|
216
|
+
const route = React.useContext(NavigationRouteContext) as
|
|
217
|
+
| NavigatorRoute<State>
|
|
218
|
+
| undefined;
|
|
219
|
+
|
|
220
|
+
const { children, screenListeners, ...rest } = options;
|
|
221
|
+
const { current: router } = React.useRef<Router<State, any>>(
|
|
222
|
+
createRouter({
|
|
223
|
+
...(rest as unknown as RouterOptions),
|
|
224
|
+
...(route?.params &&
|
|
225
|
+
route.params.state == null &&
|
|
226
|
+
route.params.initial !== false &&
|
|
227
|
+
typeof route.params.screen === 'string'
|
|
228
|
+
? { initialRouteName: route.params.screen }
|
|
229
|
+
: null),
|
|
230
|
+
})
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const routeConfigs =
|
|
234
|
+
getRouteConfigsFromChildren<State, ScreenOptions, EventMap>(children);
|
|
235
|
+
|
|
236
|
+
const screens = routeConfigs.reduce<
|
|
237
|
+
Record<string, ScreenConfigWithParent<State, ScreenOptions, EventMap>>
|
|
238
|
+
>((acc, config) => {
|
|
239
|
+
if (config[1].name in acc) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
`A navigator cannot contain multiple 'Screen' components with the same name (found duplicate screen named '${config[1].name}')`
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
acc[config[1].name] = config;
|
|
246
|
+
return acc;
|
|
247
|
+
}, {});
|
|
248
|
+
|
|
249
|
+
const routeNames = routeConfigs.map((config) => config[1].name);
|
|
250
|
+
const routeParamList = routeNames.reduce<Record<string, object | undefined>>(
|
|
251
|
+
(acc, curr) => {
|
|
252
|
+
const { initialParams } = screens[curr][1];
|
|
253
|
+
acc[curr] = initialParams;
|
|
254
|
+
return acc;
|
|
255
|
+
},
|
|
256
|
+
{}
|
|
257
|
+
);
|
|
258
|
+
const routeGetIdList = routeNames.reduce<
|
|
259
|
+
RouterConfigOptions['routeGetIdList']
|
|
260
|
+
>(
|
|
261
|
+
(acc, curr) =>
|
|
262
|
+
Object.assign(acc, {
|
|
263
|
+
[curr]: screens[curr][1].getId,
|
|
264
|
+
}),
|
|
265
|
+
{}
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
if (!routeNames.length) {
|
|
269
|
+
throw new Error(
|
|
270
|
+
"Couldn't find any screens for the navigator. Have you defined any screens as its children?"
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const isStateValid = React.useCallback(
|
|
275
|
+
(state) => state.type === undefined || state.type === router.type,
|
|
276
|
+
[router.type]
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
const isStateInitialized = React.useCallback(
|
|
280
|
+
(state) =>
|
|
281
|
+
state !== undefined && state.stale === false && isStateValid(state),
|
|
282
|
+
[isStateValid]
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
const {
|
|
286
|
+
state: currentState,
|
|
287
|
+
getState: getCurrentState,
|
|
288
|
+
setState: setCurrentState,
|
|
289
|
+
setKey,
|
|
290
|
+
getKey,
|
|
291
|
+
getIsInitial,
|
|
292
|
+
} = React.useContext(NavigationStateContext);
|
|
293
|
+
|
|
294
|
+
const stateCleanedUp = React.useRef(false);
|
|
295
|
+
|
|
296
|
+
const cleanUpState = React.useCallback(() => {
|
|
297
|
+
setCurrentState(undefined);
|
|
298
|
+
stateCleanedUp.current = true;
|
|
299
|
+
}, [setCurrentState]);
|
|
300
|
+
|
|
301
|
+
const setState = React.useCallback(
|
|
302
|
+
(state: NavigationState | PartialState<NavigationState> | undefined) => {
|
|
303
|
+
if (stateCleanedUp.current) {
|
|
304
|
+
// State might have been already cleaned up due to unmount
|
|
305
|
+
// We do not want to expose API allowing to override this
|
|
306
|
+
// This would lead to old data preservation on main navigator unmount
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
setCurrentState(state);
|
|
310
|
+
},
|
|
311
|
+
[setCurrentState]
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const [initializedState, isFirstStateInitialization] = React.useMemo(() => {
|
|
315
|
+
const initialRouteParamList = routeNames.reduce<
|
|
316
|
+
Record<string, object | undefined>
|
|
317
|
+
>((acc, curr) => {
|
|
318
|
+
const { initialParams } = screens[curr][1];
|
|
319
|
+
const initialParamsFromParams =
|
|
320
|
+
route?.params?.state == null &&
|
|
321
|
+
route?.params?.initial !== false &&
|
|
322
|
+
route?.params?.screen === curr
|
|
323
|
+
? route.params.params
|
|
324
|
+
: undefined;
|
|
325
|
+
|
|
326
|
+
acc[curr] =
|
|
327
|
+
initialParams !== undefined || initialParamsFromParams !== undefined
|
|
328
|
+
? {
|
|
329
|
+
...initialParams,
|
|
330
|
+
...initialParamsFromParams,
|
|
331
|
+
}
|
|
332
|
+
: undefined;
|
|
333
|
+
|
|
334
|
+
return acc;
|
|
335
|
+
}, {});
|
|
336
|
+
|
|
337
|
+
// If the current state isn't initialized on first render, we initialize it
|
|
338
|
+
// We also need to re-initialize it if the state passed from parent was changed (maybe due to reset)
|
|
339
|
+
// Otherwise assume that the state was provided as initial state
|
|
340
|
+
// So we need to rehydrate it to make it usable
|
|
341
|
+
if (
|
|
342
|
+
(currentState === undefined || !isStateValid(currentState)) &&
|
|
343
|
+
route?.params?.state == null
|
|
344
|
+
) {
|
|
345
|
+
return [
|
|
346
|
+
router.getInitialState({
|
|
347
|
+
routeNames,
|
|
348
|
+
routeParamList: initialRouteParamList,
|
|
349
|
+
routeGetIdList,
|
|
350
|
+
}),
|
|
351
|
+
true,
|
|
352
|
+
];
|
|
353
|
+
} else {
|
|
354
|
+
return [
|
|
355
|
+
router.getRehydratedState(
|
|
356
|
+
route?.params?.state ?? (currentState as PartialState<State>),
|
|
357
|
+
{
|
|
358
|
+
routeNames,
|
|
359
|
+
routeParamList: initialRouteParamList,
|
|
360
|
+
routeGetIdList,
|
|
361
|
+
}
|
|
362
|
+
),
|
|
363
|
+
false,
|
|
364
|
+
];
|
|
365
|
+
}
|
|
366
|
+
// We explicitly don't include routeNames, route.params etc. in the dep list
|
|
367
|
+
// below. We want to avoid forcing a new state to be calculated in those cases
|
|
368
|
+
// Instead, we handle changes to these in the nextState code below. Note
|
|
369
|
+
// that some changes to routeConfigs are explicitly ignored, such as changes
|
|
370
|
+
// to initialParams
|
|
371
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
372
|
+
}, [currentState, router, isStateValid]);
|
|
373
|
+
|
|
374
|
+
let state =
|
|
375
|
+
// If the state isn't initialized, or stale, use the state we initialized instead
|
|
376
|
+
// The state won't update until there's a change needed in the state we have initalized locally
|
|
377
|
+
// So it'll be `undefined` or stale until the first navigation event happens
|
|
378
|
+
isStateInitialized(currentState)
|
|
379
|
+
? (currentState as State)
|
|
380
|
+
: (initializedState as State);
|
|
381
|
+
|
|
382
|
+
let nextState: State = state;
|
|
383
|
+
|
|
384
|
+
if (!isArrayEqual(state.routeNames, routeNames)) {
|
|
385
|
+
// When the list of route names change, the router should handle it to remove invalid routes
|
|
386
|
+
nextState = router.getStateForRouteNamesChange(state, {
|
|
387
|
+
routeNames,
|
|
388
|
+
routeParamList,
|
|
389
|
+
routeGetIdList,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const previousNestedParamsRef = React.useRef(route?.params);
|
|
394
|
+
|
|
395
|
+
React.useEffect(() => {
|
|
396
|
+
previousNestedParamsRef.current = route?.params;
|
|
397
|
+
}, [route?.params]);
|
|
398
|
+
|
|
399
|
+
if (route?.params) {
|
|
400
|
+
const previousParams = previousNestedParamsRef.current;
|
|
401
|
+
|
|
402
|
+
let action: CommonActions.Action | undefined;
|
|
403
|
+
|
|
404
|
+
if (
|
|
405
|
+
typeof route.params.state === 'object' &&
|
|
406
|
+
route.params.state != null &&
|
|
407
|
+
route.params !== previousParams
|
|
408
|
+
) {
|
|
409
|
+
// If the route was updated with new state, we should reset to it
|
|
410
|
+
action = CommonActions.reset(route.params.state);
|
|
411
|
+
} else if (
|
|
412
|
+
typeof route.params.screen === 'string' &&
|
|
413
|
+
((route.params.initial === false && isFirstStateInitialization) ||
|
|
414
|
+
route.params !== previousParams)
|
|
415
|
+
) {
|
|
416
|
+
// If the route was updated with new screen name and/or params, we should navigate there
|
|
417
|
+
action = CommonActions.navigate({
|
|
418
|
+
name: route.params.screen,
|
|
419
|
+
params: route.params.params,
|
|
420
|
+
path: route.params.path,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// The update should be limited to current navigator only, so we call the router manually
|
|
425
|
+
const updatedState = action
|
|
426
|
+
? router.getStateForAction(nextState, action, {
|
|
427
|
+
routeNames,
|
|
428
|
+
routeParamList,
|
|
429
|
+
routeGetIdList,
|
|
430
|
+
})
|
|
431
|
+
: null;
|
|
432
|
+
|
|
433
|
+
nextState =
|
|
434
|
+
updatedState !== null
|
|
435
|
+
? router.getRehydratedState(updatedState, {
|
|
436
|
+
routeNames,
|
|
437
|
+
routeParamList,
|
|
438
|
+
routeGetIdList,
|
|
439
|
+
})
|
|
440
|
+
: nextState;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const shouldUpdate = state !== nextState;
|
|
444
|
+
|
|
445
|
+
useScheduleUpdate(() => {
|
|
446
|
+
if (shouldUpdate) {
|
|
447
|
+
// If the state needs to be updated, we'll schedule an update
|
|
448
|
+
setState(nextState);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
// The up-to-date state will come in next render, but we don't need to wait for it
|
|
453
|
+
// We can't use the outdated state since the screens have changed, which will cause error due to mismatched config
|
|
454
|
+
// So we override the state object we return to use the latest state as soon as possible
|
|
455
|
+
state = nextState;
|
|
456
|
+
|
|
457
|
+
React.useEffect(() => {
|
|
458
|
+
setKey(navigatorKey);
|
|
459
|
+
|
|
460
|
+
if (!getIsInitial()) {
|
|
461
|
+
// If it's not initial render, we need to update the state
|
|
462
|
+
// This will make sure that our container gets notifier of state changes due to new mounts
|
|
463
|
+
// This is necessary for proper screen tracking, URL updates etc.
|
|
464
|
+
setState(nextState);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return () => {
|
|
468
|
+
// We need to clean up state for this navigator on unmount
|
|
469
|
+
// We do it in a timeout because we need to detect if another navigator mounted in the meantime
|
|
470
|
+
// For example, if another navigator has started rendering, we should skip cleanup
|
|
471
|
+
// Otherwise, our cleanup step will cleanup state for the other navigator and re-initialize it
|
|
472
|
+
setTimeout(() => {
|
|
473
|
+
if (getCurrentState() !== undefined && getKey() === navigatorKey) {
|
|
474
|
+
cleanUpState();
|
|
475
|
+
}
|
|
476
|
+
}, 0);
|
|
477
|
+
};
|
|
478
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
479
|
+
}, []);
|
|
480
|
+
|
|
481
|
+
// We initialize this ref here to avoid a new getState getting initialized
|
|
482
|
+
// whenever initializedState changes. We want getState to have access to the
|
|
483
|
+
// latest initializedState, but don't need it to change when that happens
|
|
484
|
+
const initializedStateRef = React.useRef<State>();
|
|
485
|
+
initializedStateRef.current = initializedState;
|
|
486
|
+
|
|
487
|
+
const getState = React.useCallback((): State => {
|
|
488
|
+
const currentState = getCurrentState();
|
|
489
|
+
|
|
490
|
+
return isStateInitialized(currentState)
|
|
491
|
+
? (currentState as State)
|
|
492
|
+
: (initializedStateRef.current as State);
|
|
493
|
+
}, [getCurrentState, isStateInitialized]);
|
|
494
|
+
|
|
495
|
+
const emitter = useEventEmitter<EventMapCore<State>>((e) => {
|
|
496
|
+
let routeNames = [];
|
|
497
|
+
|
|
498
|
+
let route: Route<string> | undefined;
|
|
499
|
+
|
|
500
|
+
if (e.target) {
|
|
501
|
+
route = state.routes.find((route) => route.key === e.target);
|
|
502
|
+
|
|
503
|
+
if (route?.name) {
|
|
504
|
+
routeNames.push(route.name);
|
|
505
|
+
}
|
|
506
|
+
} else {
|
|
507
|
+
route = state.routes[state.index];
|
|
508
|
+
routeNames.push(
|
|
509
|
+
...Object.keys(screens).filter((name) => route?.name === name)
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (route == null) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const navigation = descriptors[route.key].navigation;
|
|
518
|
+
|
|
519
|
+
const listeners = ([] as (((e: any) => void) | undefined)[])
|
|
520
|
+
.concat(
|
|
521
|
+
// Get an array of listeners for all screens + common listeners on navigator
|
|
522
|
+
...[
|
|
523
|
+
screenListeners,
|
|
524
|
+
...routeNames.map((name) => {
|
|
525
|
+
const { listeners } = screens[name][1];
|
|
526
|
+
return listeners;
|
|
527
|
+
}),
|
|
528
|
+
].map((listeners) => {
|
|
529
|
+
const map =
|
|
530
|
+
typeof listeners === 'function'
|
|
531
|
+
? listeners({ route: route as any, navigation })
|
|
532
|
+
: listeners;
|
|
533
|
+
|
|
534
|
+
return map
|
|
535
|
+
? Object.keys(map)
|
|
536
|
+
.filter((type) => type === e.type)
|
|
537
|
+
.map((type) => map?.[type])
|
|
538
|
+
: undefined;
|
|
539
|
+
})
|
|
540
|
+
)
|
|
541
|
+
// We don't want same listener to be called multiple times for same event
|
|
542
|
+
// So we remove any duplicate functions from the array
|
|
543
|
+
.filter((cb, i, self) => cb && self.lastIndexOf(cb) === i);
|
|
544
|
+
|
|
545
|
+
listeners.forEach((listener) => listener?.(e));
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
useFocusEvents({ state, emitter });
|
|
549
|
+
|
|
550
|
+
React.useEffect(() => {
|
|
551
|
+
emitter.emit({ type: 'state', data: { state } });
|
|
552
|
+
}, [emitter, state]);
|
|
553
|
+
|
|
554
|
+
const { listeners: childListeners, addListener } = useChildListeners();
|
|
555
|
+
|
|
556
|
+
const { keyedListeners, addKeyedListener } = useKeyedChildListeners();
|
|
557
|
+
|
|
558
|
+
const onAction = useOnAction({
|
|
559
|
+
router,
|
|
560
|
+
getState,
|
|
561
|
+
setState,
|
|
562
|
+
key: route?.key,
|
|
563
|
+
actionListeners: childListeners.action,
|
|
564
|
+
beforeRemoveListeners: keyedListeners.beforeRemove,
|
|
565
|
+
routerConfigOptions: {
|
|
566
|
+
routeNames,
|
|
567
|
+
routeParamList,
|
|
568
|
+
routeGetIdList,
|
|
569
|
+
},
|
|
570
|
+
emitter,
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
const onRouteFocus = useOnRouteFocus({
|
|
574
|
+
router,
|
|
575
|
+
key: route?.key,
|
|
576
|
+
getState,
|
|
577
|
+
setState,
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
const navigation = useNavigationHelpers<
|
|
581
|
+
State,
|
|
582
|
+
ActionHelpers,
|
|
583
|
+
NavigationAction,
|
|
584
|
+
EventMap
|
|
585
|
+
>({
|
|
586
|
+
onAction,
|
|
587
|
+
getState,
|
|
588
|
+
emitter,
|
|
589
|
+
router,
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
useFocusedListenersChildrenAdapter({
|
|
593
|
+
navigation,
|
|
594
|
+
focusedListeners: childListeners.focus,
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
useOnGetState({
|
|
598
|
+
getState,
|
|
599
|
+
getStateListeners: keyedListeners.getState,
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
const descriptors = useDescriptors<
|
|
603
|
+
State,
|
|
604
|
+
ActionHelpers,
|
|
605
|
+
ScreenOptions,
|
|
606
|
+
EventMap
|
|
607
|
+
>({
|
|
608
|
+
state,
|
|
609
|
+
screens,
|
|
610
|
+
navigation,
|
|
611
|
+
screenOptions: options.screenOptions,
|
|
612
|
+
defaultScreenOptions: options.defaultScreenOptions,
|
|
613
|
+
onAction,
|
|
614
|
+
getState,
|
|
615
|
+
setState,
|
|
616
|
+
onRouteFocus,
|
|
617
|
+
addListener,
|
|
618
|
+
addKeyedListener,
|
|
619
|
+
router,
|
|
620
|
+
// @ts-expect-error: this should have both core and custom events, but too much work right now
|
|
621
|
+
emitter,
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
useCurrentRender({
|
|
625
|
+
state,
|
|
626
|
+
navigation,
|
|
627
|
+
descriptors,
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
const NavigationContent = useComponent(NavigationHelpersContext.Provider, {
|
|
631
|
+
value: navigation,
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
return {
|
|
635
|
+
state,
|
|
636
|
+
navigation,
|
|
637
|
+
descriptors,
|
|
638
|
+
NavigationContent,
|
|
639
|
+
};
|
|
640
|
+
}
|