Skip to content

Commit 08dbe76

Browse files
committed
feat: Linking prop is customizable via configs
1 parent f27d9e0 commit 08dbe76

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/components/Navigation/Navigation.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import { LoadingState, NavigationProps, NavigatorProps } from '@bluebase/components';
2-
import { useComponent, useConfig, useTheme } from '@bluebase/core';
3-
import { LinkingOptions, NavigationContainer } from '@react-navigation/native';
2+
import { useBlueBase, useComponent, useConfig, useTheme } from '@bluebase/core';
3+
import { getPathFromState, getStateFromPath, LinkingOptions, NavigationContainer } from '@react-navigation/native';
44
import * as Linking from 'expo-linking';
55
import React from 'react';
66

77
import { useBlueBaseContextPack } from '../../useBlueBaseContextPack';
88
import { createLinkingConfigs } from './createLinkingConfigs';
99
import { usePersistentState } from './usePersistentState';
1010

11+
export type getStateFromPathConfigFn =
12+
(defaultState: any, path: string, options?: any) => any;
13+
14+
export type getPathFromStateConfigFn =
15+
(defaultPath: string, state: any, options?: any) => any;
16+
1117
/**
1218
* Navigation (V5)
1319
* This serves as an entry point where BlueBase passes routes and navigation
1420
* configs to this component.
1521
*/
1622
export const Navigation = (props: NavigationProps) => {
17-
const { navigator, ...rest } = props;
23+
const BB = useBlueBase();
1824
const { theme } = useTheme();
25+
const { navigator, ...rest } = props;
1926

2027
const contextPack = useBlueBaseContextPack();
2128
const Navigator = useComponent<NavigatorProps>('Navigator');
@@ -29,7 +36,23 @@ export const Navigation = (props: NavigationProps) => {
2936

3037
const linking: LinkingOptions<any> = {
3138
config: createLinkingConfigs(navigator.routes, contextPack),
32-
prefixes: [Linking.createURL('/'), ...prefixes]
39+
prefixes: [Linking.createURL('/'), ...prefixes],
40+
getStateFromPath: (path, options) => {
41+
const getStateFromPathConfig = BB.Configs.getValue('navigation.linking.getStateFromPath');
42+
const defaultState = getStateFromPath(path, options);
43+
44+
return getStateFromPathConfig
45+
? getStateFromPathConfig(defaultState, path, options)
46+
: defaultState;
47+
},
48+
getPathFromState: (state, options?) => {
49+
const getPathFromStateConfig = BB.Configs.getValue('navigation.linking.getPathFromState');
50+
const path = getPathFromState(state, options);
51+
52+
return getPathFromStateConfig
53+
? getPathFromStateConfig(path, state, options)
54+
: path;
55+
},
3356
};
3457

3558
return (

0 commit comments

Comments
 (0)