@homecode/ui 4.18.24 → 4.18.28

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.
@@ -1,6 +1,6 @@
1
1
  import { jsx, Fragment } from 'react/jsx-runtime';
2
2
  import { useContext, useEffect, useMemo } from 'react';
3
- import { withStore } from 'justorm/react';
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 = withStore({
12
- router: ['path'],
13
- })((props) => {
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, { replace } = {}) {
23
+ go(path, query, params = {}) {
24
24
  if (path === this.path)
25
25
  return;
26
- history[replace ? 'replaceState' : 'pushState']({}, '', path);
27
- onRouteChange(path);
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, { quiet } = {}) {
37
+ replaceState(href, params = {}) {
34
38
  history.replaceState({}, '', href);
35
- if (!quiet)
39
+ if (!params.quiet)
36
40
  onRouteChange(href);
37
41
  },
38
42
  });
@@ -10,6 +10,32 @@ 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
+ const clearPath = path.replace(/\?.*/, '/');
20
+ if (!queryParams) {
21
+ if (Object.keys(currParams).length === 0)
22
+ return path;
23
+ return `${clearPath}?${stringifyQueryParams(currParams)}`;
24
+ }
25
+ if (typeof queryParams === 'string') {
26
+ return queryParams;
27
+ }
28
+ const query = { ...currParams };
29
+ Object.entries(queryParams).forEach(([key, value]) => {
30
+ if (value === false)
31
+ delete query[key];
32
+ else
33
+ query[key] = value;
34
+ });
35
+ if (Object.keys(query).length === 0)
36
+ return clearPath;
37
+ return `${clearPath}?${stringifyQueryParams(query)}`;
38
+ }
13
39
  const setSSRQueryParams = (params) => {
14
40
  Object.assign(SSRQueryParams, params);
15
41
  };
@@ -17,4 +43,4 @@ const queryParams = isBrowser
17
43
  ? parseQueryParams()
18
44
  : SSRQueryParams;
19
45
 
20
- export { parseQueryParams, queryParams, setSSRQueryParams };
46
+ export { applyQueryParams, parseQueryParams, queryParams, setSSRQueryParams, stringifyQueryParams };
@@ -1,7 +1,13 @@
1
1
  /// <reference types="react" />
2
- export declare const Router: any;
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.24",
3
+ "version": "4.18.28",
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": "^2.1.3",
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",