@homecode/ui 5.20.10 → 5.20.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/src/components/Router/Link/Link.js +2 -8
- package/dist/esm/src/components/Router/Link/linkHref.js +13 -1
- package/dist/esm/src/tools/queryParams.js +11 -4
- package/dist/esm/types/src/components/Router/Link/linkHref.d.ts +5 -0
- package/dist/esm/types/src/tools/queryParams.d.ts +4 -0
- package/dist/esm/types/src/tools/queryParams.test.d.ts +1 -0
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import cn from 'classnames';
|
|
|
5
5
|
import { Icon } from '../../Icon/Icon.js';
|
|
6
6
|
import Context from '../context.js';
|
|
7
7
|
import S from './Link.styl.js';
|
|
8
|
-
import {
|
|
8
|
+
import { isExternalHref, isNativeDocumentHref } from './linkHref.js';
|
|
9
9
|
|
|
10
10
|
const isStartFromDoubleSlash = href => /^\/\//.test(href);
|
|
11
11
|
const isHttpHref = (href) => /^https?:\/\//i.test(href);
|
|
@@ -33,13 +33,7 @@ const Link = ({ className, exactClassName, children, href: hrefProp, isClear = f
|
|
|
33
33
|
const isExternal = useMemo(() => {
|
|
34
34
|
if (internalHref)
|
|
35
35
|
return false;
|
|
36
|
-
|
|
37
|
-
// is parsed as host `api`.
|
|
38
|
-
if (isRootRelativeHref(hrefProp))
|
|
39
|
-
return false;
|
|
40
|
-
if (isHttpHref(hrefProp))
|
|
41
|
-
return true;
|
|
42
|
-
return /\./.test(hrefProp);
|
|
36
|
+
return isExternalHref(hrefProp);
|
|
43
37
|
}, [hrefProp, internalHref]);
|
|
44
38
|
const nativeDocumentProp = isNativeDocumentHref(internalHref ?? hrefProp);
|
|
45
39
|
const rootPath = useMemo(() => {
|
|
@@ -14,6 +14,18 @@ function isNativeDocumentHref(href) {
|
|
|
14
14
|
if (path === '/api' || path.startsWith('/api/'))
|
|
15
15
|
return true;
|
|
16
16
|
return path.includes('.');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Opens in a new tab. A bare `example.com` is a host, but a dot only in the
|
|
20
|
+
* query (`//storage?file=a.mp4`) is not.
|
|
21
|
+
*/
|
|
22
|
+
function isExternalHref(href) {
|
|
23
|
+
if (isRootRelativeHref(href))
|
|
24
|
+
return false;
|
|
25
|
+
if (/^https?:\/\//i.test(href))
|
|
26
|
+
return true;
|
|
27
|
+
const path = href.split(/[?#]/)[0] ?? '';
|
|
28
|
+
return path.includes('.');
|
|
17
29
|
}
|
|
18
30
|
|
|
19
|
-
export { isNativeDocumentHref, isRootRelativeHref };
|
|
31
|
+
export { isExternalHref, isNativeDocumentHref, isRootRelativeHref };
|
|
@@ -12,15 +12,22 @@ function parseQueryParams(qs) {
|
|
|
12
12
|
}
|
|
13
13
|
function stringifyQueryParams(params) {
|
|
14
14
|
return Object.entries(params)
|
|
15
|
-
.map(([key, value]) => `${key}=${value}`)
|
|
15
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
16
16
|
.join('&');
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Path with `queryParams` applied. A query already in `path`
|
|
20
|
+
* (`go('/storage?folder=1')`) is kept; `queryParams` override its keys.
|
|
21
|
+
*/
|
|
18
22
|
function applyQueryParams(path, queryParams) {
|
|
19
|
-
const clearPath = path.replace(/\?.*/, '/');
|
|
20
23
|
if (typeof queryParams === 'string')
|
|
21
24
|
return queryParams;
|
|
22
|
-
const
|
|
23
|
-
|
|
25
|
+
const [clearPath, pathQuery = ''] = path.split('?');
|
|
26
|
+
const query = {
|
|
27
|
+
...Object.fromEntries(new URLSearchParams(pathQuery)),
|
|
28
|
+
...queryParams,
|
|
29
|
+
};
|
|
30
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
24
31
|
if (value === false)
|
|
25
32
|
delete query[key];
|
|
26
33
|
else
|
|
@@ -6,3 +6,8 @@ export declare function isRootRelativeHref(href: string): boolean;
|
|
|
6
6
|
* an in-app route.
|
|
7
7
|
*/
|
|
8
8
|
export declare function isNativeDocumentHref(href: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Opens in a new tab. A bare `example.com` is a host, but a dot only in the
|
|
11
|
+
* query (`//storage?file=a.mp4`) is not.
|
|
12
|
+
*/
|
|
13
|
+
export declare function isExternalHref(href: string): boolean;
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export declare function parseQueryParams(qs?: string): Record<string, string>;
|
|
2
2
|
export declare function stringifyQueryParams(params: Record<string, string>): string;
|
|
3
|
+
/**
|
|
4
|
+
* Path with `queryParams` applied. A query already in `path`
|
|
5
|
+
* (`go('/storage?folder=1')`) is kept; `queryParams` override its keys.
|
|
6
|
+
*/
|
|
3
7
|
export declare function applyQueryParams(path: any, queryParams: Record<string, any> | string): string;
|
|
4
8
|
export declare const setSSRQueryParams: (params: any) => void;
|
|
5
9
|
export declare const queryParams: Record<string, string>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|