@homecode/ui 4.18.23 → 4.18.27
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/Router.js +6 -7
- package/dist/esm/src/components/Router/store.js +10 -6
- package/dist/esm/src/components/Tabs/Tabs.js +12 -16
- package/dist/esm/src/tools/queryParams.js +26 -1
- package/dist/esm/types/src/components/Router/Router.d.ts +7 -1
- package/dist/esm/types/src/tools/queryParams.d.ts +2 -0
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import { useContext, useEffect, useMemo } from 'react';
|
|
3
|
-
import {
|
|
3
|
+
import { useStore } from 'justorm/dist/esm/src/plugins/react';
|
|
4
4
|
import STORE from './store.js';
|
|
5
5
|
import Context from './context.js';
|
|
6
6
|
import { parsePath, replaceParamsInPath } from './Router.helpers.js';
|
|
@@ -8,15 +8,14 @@ export { Route } from './Route.js';
|
|
|
8
8
|
export { Redirect } from './Redirect.js';
|
|
9
9
|
export { Link } from './Link/Link.js';
|
|
10
10
|
|
|
11
|
-
const Router =
|
|
12
|
-
router: ['path']
|
|
13
|
-
}
|
|
14
|
-
const { children, single, basePath = '', store: { router }, } = props;
|
|
11
|
+
const Router = (props) => {
|
|
12
|
+
const { router } = useStore({ router: ['path'] });
|
|
13
|
+
const { children, single, basePath = '' } = props;
|
|
15
14
|
const ctx = useContext(Context);
|
|
16
15
|
const fullPath = ctx.basePath + basePath;
|
|
17
16
|
useEffect(() => {
|
|
18
17
|
const onPopState = () => {
|
|
19
|
-
STORE.go(window.location.pathname, { replace: true });
|
|
18
|
+
STORE.go(window.location.pathname, {}, { replace: true });
|
|
20
19
|
};
|
|
21
20
|
window.addEventListener('popstate', onPopState);
|
|
22
21
|
return () => window.removeEventListener('popstate', onPopState);
|
|
@@ -60,7 +59,7 @@ const Router = withStore({
|
|
|
60
59
|
return childs;
|
|
61
60
|
}, [children, router.path, fullPath, single]);
|
|
62
61
|
return jsx(Fragment, { children: matchedRoutes });
|
|
63
|
-
}
|
|
62
|
+
};
|
|
64
63
|
Router.displayName = 'Router';
|
|
65
64
|
const RouterStore = STORE;
|
|
66
65
|
const RouterContext = Context;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createStore } from 'justorm/react';
|
|
2
2
|
import { addUniq, spliceWhere } from '../../tools/array.js';
|
|
3
|
-
import { parseQueryParams } from '../../tools/queryParams.js';
|
|
3
|
+
import { parseQueryParams, applyQueryParams } from '../../tools/queryParams.js';
|
|
4
4
|
import { isBrowser } from '../../tools/env.js';
|
|
5
5
|
import 'nanoid';
|
|
6
6
|
import '../../tools/dom.js';
|
|
@@ -20,19 +20,23 @@ const STORE = createStore('router', {
|
|
|
20
20
|
un(cb) {
|
|
21
21
|
spliceWhere(LISTENERS, cb);
|
|
22
22
|
},
|
|
23
|
-
go(path,
|
|
23
|
+
go(path, query, params = {}) {
|
|
24
24
|
if (path === this.path)
|
|
25
25
|
return;
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
const { replace } = params;
|
|
27
|
+
const pathStr = applyQueryParams(path ?? this.path, query, this.query);
|
|
28
|
+
const action = replace ? 'replaceState' : 'pushState';
|
|
29
|
+
console.log('Router.go', query, pathStr);
|
|
30
|
+
history[action]({}, '', pathStr);
|
|
31
|
+
onRouteChange(pathStr);
|
|
28
32
|
},
|
|
29
33
|
back() {
|
|
30
34
|
history.back();
|
|
31
35
|
onRouteChange();
|
|
32
36
|
},
|
|
33
|
-
replaceState(href,
|
|
37
|
+
replaceState(href, params = {}) {
|
|
34
38
|
history.replaceState({}, '', href);
|
|
35
|
-
if (!quiet)
|
|
39
|
+
if (!params.quiet)
|
|
36
40
|
onRouteChange(href);
|
|
37
41
|
},
|
|
38
42
|
});
|
|
@@ -5,11 +5,9 @@ import { Button } from '../Button/Button.js';
|
|
|
5
5
|
import { ButtonGroup } from '../ButtonGroup/ButtonGroup.js';
|
|
6
6
|
import S from './Tabs.styl.js';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
return ['string', 'number'].includes(typeof id);
|
|
10
|
-
}
|
|
8
|
+
const isId = id => ['string', 'number'].includes(typeof id);
|
|
11
9
|
function Tabs(props) {
|
|
12
|
-
const { size = 'm', className, contentClassName, items, hideTabsIfSingle =
|
|
10
|
+
const { size = 'm', className, contentClassName, items, hideTabsIfSingle = false, onChange, renderAll, activeId: initialId, children, ...rest } = props;
|
|
13
11
|
const [activeId, setActiveId] = useState(isId(initialId) ? initialId : items[0].id);
|
|
14
12
|
useEffect(() => {
|
|
15
13
|
if (isId(initialId)) {
|
|
@@ -26,18 +24,16 @@ function Tabs(props) {
|
|
|
26
24
|
onChange?.(id);
|
|
27
25
|
}, []);
|
|
28
26
|
const tabsContent = [];
|
|
29
|
-
const tabsButtons = items.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
});
|
|
40
|
-
const tabs = (jsx(ButtonGroup, { className: className, ...rest, children: tabsButtons }));
|
|
27
|
+
const tabsButtons = items.map((params) => {
|
|
28
|
+
const { id, label, forceRender, content, contentClassName: currContentClassName, ...rest } = params;
|
|
29
|
+
const isActive = activeId === id;
|
|
30
|
+
const tabContent = typeof content === 'function' ? content() : content;
|
|
31
|
+
if (renderAll || forceRender || isActive) {
|
|
32
|
+
tabsContent.push(jsx("div", { className: cn(contentClassName, currContentClassName, !isActive && S.inactive), children: tabContent }, id));
|
|
33
|
+
}
|
|
34
|
+
return (createElement(Button, { ...rest, size: size, key: id, onClick: e => onTabClick(e, params), checked: isActive }, label));
|
|
35
|
+
});
|
|
36
|
+
const tabs = tabsButtons.length === 1 && hideTabsIfSingle ? null : (jsx(ButtonGroup, { className: className, ...rest, children: tabsButtons }));
|
|
41
37
|
if (typeof children === 'function') {
|
|
42
38
|
return children({
|
|
43
39
|
tabs,
|
|
@@ -10,6 +10,31 @@ function parseQueryParams(qs) {
|
|
|
10
10
|
return acc;
|
|
11
11
|
}, {});
|
|
12
12
|
}
|
|
13
|
+
function stringifyQueryParams(params) {
|
|
14
|
+
return Object.entries(params)
|
|
15
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
16
|
+
.join('&');
|
|
17
|
+
}
|
|
18
|
+
function applyQueryParams(path, queryParams, currParams) {
|
|
19
|
+
if (!queryParams) {
|
|
20
|
+
if (Object.keys(currParams).length === 0)
|
|
21
|
+
return path;
|
|
22
|
+
return `${path}?${stringifyQueryParams(currParams)}`;
|
|
23
|
+
}
|
|
24
|
+
if (typeof queryParams === 'string') {
|
|
25
|
+
return queryParams;
|
|
26
|
+
}
|
|
27
|
+
const query = { ...currParams };
|
|
28
|
+
Object.entries(queryParams).forEach(([key, value]) => {
|
|
29
|
+
if (value === false)
|
|
30
|
+
delete query[key];
|
|
31
|
+
else
|
|
32
|
+
query[key] = value;
|
|
33
|
+
});
|
|
34
|
+
if (Object.keys(query).length === 0)
|
|
35
|
+
return path;
|
|
36
|
+
return `${path}?${stringifyQueryParams(query)}`;
|
|
37
|
+
}
|
|
13
38
|
const setSSRQueryParams = (params) => {
|
|
14
39
|
Object.assign(SSRQueryParams, params);
|
|
15
40
|
};
|
|
@@ -17,4 +42,4 @@ const queryParams = isBrowser
|
|
|
17
42
|
? parseQueryParams()
|
|
18
43
|
: SSRQueryParams;
|
|
19
44
|
|
|
20
|
-
export { parseQueryParams, queryParams, setSSRQueryParams };
|
|
45
|
+
export { applyQueryParams, parseQueryParams, queryParams, setSSRQueryParams, stringifyQueryParams };
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
|
|
2
|
+
import STORE from './store';
|
|
3
|
+
import * as T from './Router.types';
|
|
4
|
+
export declare const Router: {
|
|
5
|
+
(props: T.Props): JSX.Element;
|
|
6
|
+
displayName: string;
|
|
7
|
+
};
|
|
3
8
|
export * from './Route';
|
|
4
9
|
export * from './Redirect';
|
|
5
10
|
export * from './Link/Link';
|
|
6
11
|
export declare const RouterStore: any;
|
|
7
12
|
export declare const RouterContext: import("react").Context<import("./context").ContextType>;
|
|
13
|
+
export type RouterStore = typeof STORE;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare function parseQueryParams(qs?: string): Record<string, string>;
|
|
2
|
+
export declare function stringifyQueryParams(params: Record<string, string>): string;
|
|
3
|
+
export declare function applyQueryParams(path: any, queryParams: any, currParams: any): any;
|
|
2
4
|
export declare const setSSRQueryParams: (params: any) => void;
|
|
3
5
|
export declare const queryParams: Record<string, string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@homecode/ui",
|
|
3
|
-
"version": "4.18.
|
|
3
|
+
"version": "4.18.27",
|
|
4
4
|
"description": "React UI components library",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"fastest-validator": "^1.16.0",
|
|
49
49
|
"favicons": "^7.1.3",
|
|
50
50
|
"favicons-webpack-plugin": "^6.0.0",
|
|
51
|
-
"justorm": "^
|
|
51
|
+
"justorm": "^3.0.0-beta-8",
|
|
52
52
|
"lodash.omit": "^4.5.0",
|
|
53
53
|
"lodash.pick": "^4.4.0",
|
|
54
54
|
"moment": "^2.29.4",
|