Skip to content

Commit

Permalink
feat: added the possibility to merge search params into useNavigation…
Browse files Browse the repository at this point in the history
…List, enabling breadcrumb to navigate with parameters shared between routes

feat: added the possibility to merge search params into useNavigationList, enabling breadcrumb to navigate with parameters shared between routes
  • Loading branch information
Tiagoperes authored Nov 11, 2024
2 parents 1c31511 + 0fc53d8 commit 4097270
Showing 1 changed file with 10 additions and 1 deletion.
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

0 comments on commit 4097270

Please sign in to comment.