Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added the possibility to merge search params into useNavigationList, enabling breadcrumb to navigate with parameters shared between routes #28

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/runtime/src/use-navigation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ function routeKeyToLabel(key: string) {
* @param labelFactory optional. A function to create a label for a route. If not provided, every route will have the last part of
* its key capitalized as its label. If the label factory returns undefined, the default rule for building labels is used. If it returns
* null, the route is removed from the list.
* @param shouldMergeSearchParams optional. A function that takes a route key and returns a boolean indicating whether the search
* parameters (query string) from the current location should be merged into the route's URL. If the function returns `true`, the
* search parameters will be merged into the `href` of the route. If it returns `false` or is not provided, the search parameters
* will not be merged.
* @returns the navigation list.
*/
export function useNavigationList(
labelFactory?: (key: string, params: Record<string, any>) => string | undefined | null,
shouldMergeSearchParams?: (key: string) => boolean
): NavigationItem[] {
const [data, setData] = useState<{ route?: AnyRoute, params?: Record<string, any> }>({
route: CitronNavigator.instance?.currentRoute,
Expand All @@ -45,7 +50,11 @@ export function useNavigationList(
? result
: [
...result,
{ key: route.$key, href: route.$link({}), label },
{
key: route.$key,
href: route.$link({}, { mergeSearchParameters: shouldMergeSearchParams?.(route.$key) }),
label
},
]
},
[] as NavigationItem[],
Expand Down