Skip to content

Commit 6f14564

Browse files
committed
fix: link component
1 parent 76e6b1e commit 6f14564

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/core/route/route.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ export interface IRoute<
150150

151151
createUrl(
152152
...args: IsPartial<TInputParams> extends true
153-
? [params?: Maybe<TInputParams>, query?: AnyObject]
154-
: [params: TInputParams, query?: AnyObject]
153+
? [params?: Maybe<TInputParams>, query?: AnyObject, mergeQuery?: boolean]
154+
: [params: TInputParams, query?: AnyObject, mergeQuery?: boolean]
155155
): string;
156156

157157
destroy(): void;

src/core/utils/build-url.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { buildSearchString } from 'mobx-location-history';
2+
import { routeConfig } from '../config/config.js';
3+
import type { RouteNavigateParams } from '../route/route.types.js';
4+
5+
export const buildUrl = (path: string, navigateParams: RouteNavigateParams) => {
6+
const query =
7+
(navigateParams.mergeQuery ?? routeConfig.get().mergeQuery)
8+
? {
9+
...routeConfig.get().queryParams.data,
10+
...navigateParams.query,
11+
}
12+
: (navigateParams.query ?? {});
13+
14+
return `${path}${buildSearchString(query)}`;
15+
};

src/react/components/link.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { buildSearchString } from 'mobx-location-history';
21
import { observer } from 'mobx-react-lite';
32
import {
43
type AnchorHTMLAttributes,
@@ -15,6 +14,7 @@ import {
1514
type RouteNavigateParams,
1615
routeConfig,
1716
} from '../../core/index.js';
17+
import { buildUrl } from '../../core/utils/build-url.js';
1818

1919
interface LinkAnchorProps
2020
extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
@@ -79,15 +79,14 @@ export const Link = observer(
7979
if (outerHref) {
8080
href = outerHref;
8181
} else {
82-
const query =
83-
(mergeQuery ?? routeConfig.get().mergeQuery)
84-
? { ...routeConfig.get().queryParams.data, ...navigateParams.query }
85-
: (navigateParams.query ?? {});
86-
8782
if (typeof to === 'string') {
88-
href = `${to}${buildSearchString(query)}`;
83+
href = buildUrl(to, navigateParams);
8984
} else {
90-
href = (to as AnyRoute).createUrl(params, query);
85+
href = (to as AnyRoute).createUrl(
86+
params,
87+
navigateParams.query,
88+
navigateParams.mergeQuery,
89+
);
9190
}
9291
}
9392

0 commit comments

Comments
 (0)