Skip to content

Commit

Permalink
refactor: use the enabled property for automatic linking
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 22, 2024
1 parent b0bec6f commit b19568c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
8 changes: 5 additions & 3 deletions packages/native/src/__tests__/createStaticNavigation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ it("throws if linking is enabled but there's no linking configuration", () => {
'Linking is enabled but no linking configuration was found for the screens.'
);

expect(() => {
render(<Navigation linking={{ enabled: false, prefixes: ['myapp://'] }} />);
}).not.toThrow();

expect(() => {
render(
<Navigation
linking={{ enabled: true, prefixes: ['myapp://'], config: 'auto' }}
/>
<Navigation linking={{ enabled: 'auto', prefixes: ['myapp://'] }} />
);
}).not.toThrow();
});
18 changes: 10 additions & 8 deletions packages/native/src/createStaticNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ type Props = Omit<
/**
* Options for deep linking.
*/
linking?: Omit<LinkingOptions<ParamListBase>, 'config'> & {
linking?: Omit<LinkingOptions<ParamListBase>, 'config' | 'enabled'> & {
/**
* Configure automatic linking for the screens in the tree.
* When 'auto' is specified, all leaf screens will automatically have a path.
* Whether deep link handling should be enabled.
* Defaults to `true` if any `linking` options are specified, `false` otherwise.
*
* When 'auto' is specified, all leaf screens will get a autogenerated path.
* The generated path will be a kebab-case version of the screen name.
* This can be overridden for specific screens by specifying `linking` for the screen.
*/
config?: 'auto';
enabled?: 'auto' | true | false;
};
};

Expand All @@ -46,20 +48,20 @@ export function createStaticNavigation(tree: StaticNavigation<any, any, any>) {
if (tree.config.screens) {
return createPathConfigForStaticNavigation(
tree,
linking?.config === 'auto',
linking?.enabled === 'auto',
true
);
}

return undefined;
}, [linking?.config]);
}, [linking?.enabled]);

if (linking?.enabled && screens == null) {
if (linking?.enabled === true && screens == null) {
throw new Error(
'Linking is enabled but no linking configuration was found for the screens.\n\n' +
'To solve this:\n' +
"- Specify a 'linking' property for the screens you want to link to.\n" +
"- Or set 'linking.config' to 'auto' to generate paths automatically.\n\n" +
"- Or set 'linking.enabled' to 'auto' to generate paths automatically.\n\n" +
'See usage guide: https://reactnavigation.org/docs/7.x/static-configuration#linking'
);
}
Expand Down

0 comments on commit b19568c

Please sign in to comment.