mobx-route 0.0.94 → 0.0.96
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/package.json
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
|
|
2
|
+
import { AllPropertiesOptional, Maybe } from 'yummies/utils/types';
|
|
3
|
+
import { AnyRouteEntity, RouteNavigateParams, RouteParams } from '../../core/index.js';
|
|
4
|
+
type SwitchBaseProps = {
|
|
3
5
|
children: ReactNode;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
+
};
|
|
7
|
+
type SwitchPropsWithDefaultRoute<TRoute extends AnyRouteEntity> = {
|
|
8
|
+
default?: TRoute;
|
|
9
|
+
} & (AllPropertiesOptional<RouteParams<TRoute>> extends true ? {
|
|
10
|
+
params?: Maybe<RouteParams<TRoute>>;
|
|
11
|
+
} : {
|
|
12
|
+
params: RouteParams<TRoute>;
|
|
13
|
+
}) & SwitchBaseProps & RouteNavigateParams;
|
|
14
|
+
type SwitchPropsWithDefaultUrl = {
|
|
15
|
+
default?: string;
|
|
16
|
+
} & SwitchBaseProps & RouteNavigateParams;
|
|
17
|
+
export type SwitchProps<TRoute extends AnyRouteEntity> = SwitchPropsWithDefaultRoute<TRoute> | SwitchPropsWithDefaultUrl;
|
|
18
|
+
export declare const Switch: (<TRoute extends AnyRouteEntity>({ children, default: defaultNavigation, params, ...navigateParams }: SwitchProps<TRoute>) => ReactNode) & {
|
|
6
19
|
displayName: string;
|
|
7
20
|
};
|
|
21
|
+
export {};
|
|
8
22
|
//# sourceMappingURL=switch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/react/components/switch.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/react/components/switch.tsx"],"names":[],"mappings":"AAGA,OAAO,EAIL,SAAS,EAEV,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEnE,OAAO,EACL,cAAc,EAEd,mBAAmB,EACnB,WAAW,EACZ,MAAM,qBAAqB,CAAC;AAG7B,KAAK,eAAe,GAAG;IACrB,QAAQ,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF,KAAK,2BAA2B,CAAC,MAAM,SAAS,cAAc,IAAI;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,CAAC,qBAAqB,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,SAAS,IAAI,GACxD;IACE,MAAM,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;CACrC,GACD;IACE,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC,GACJ,eAAe,GACf,mBAAmB,CAAC;AAEtB,KAAK,yBAAyB,GAAG;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,GAAG,eAAe,GACjB,mBAAmB,CAAC;AAEtB,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,cAAc,IACjD,2BAA2B,CAAC,MAAM,CAAC,GACnC,yBAAyB,CAAC;AAsB9B,eAAO,MAAM,MAAM,IAAsB,MAAM,SAAS,cAAc,uEAMnE,WAAW,CAAC,MAAM,CAAC;;CAiCpB,CAAC"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import { buildSearchString } from 'mobx-location-history';
|
|
2
3
|
import { observer } from 'mobx-react-lite';
|
|
3
|
-
import { Fragment, isValidElement } from 'react';
|
|
4
|
+
import { Fragment, isValidElement, useEffect, } from 'react';
|
|
4
5
|
import { flatMapDeep } from 'yummies/data';
|
|
6
|
+
import { routeConfig, } from '../../core/index.js';
|
|
5
7
|
import { isRouteEntity } from '../../core/utils/is-route-entity.js';
|
|
6
8
|
const flattenChildren = (children) => flatMapDeep(children, (c) =>
|
|
7
9
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -12,15 +14,37 @@ const isValidRouteElement = (element) => {
|
|
|
12
14
|
// @ts-ignore
|
|
13
15
|
isRouteEntity(element.props?.route));
|
|
14
16
|
};
|
|
15
|
-
export const Switch = observer(({ children
|
|
17
|
+
export const Switch = observer(function ({ children, default: defaultNavigation,
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
params, ...navigateParams }) {
|
|
20
|
+
let resultElement = null;
|
|
16
21
|
let defaultElement = null;
|
|
17
22
|
for (const element of flattenChildren(children)) {
|
|
18
23
|
if (isValidRouteElement(element) && element.props.route.isOpened) {
|
|
19
|
-
|
|
24
|
+
resultElement = element;
|
|
25
|
+
break;
|
|
20
26
|
}
|
|
21
27
|
else {
|
|
22
28
|
defaultElement = element;
|
|
23
29
|
}
|
|
24
30
|
}
|
|
25
|
-
|
|
31
|
+
const isResultElementFound = !!resultElement;
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
if (!isResultElementFound && defaultNavigation) {
|
|
34
|
+
if (typeof defaultNavigation === 'string') {
|
|
35
|
+
const history = routeConfig.get().history;
|
|
36
|
+
const url = `${defaultNavigation}${buildSearchString(navigateParams.query || {})}`;
|
|
37
|
+
if (navigateParams.replace) {
|
|
38
|
+
history.replace(url, navigateParams.state);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
history.push(url, navigateParams.state);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else if (!defaultNavigation.isOpened) {
|
|
45
|
+
defaultNavigation.open(params, navigateParams);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}, [isResultElementFound, defaultNavigation]);
|
|
49
|
+
return resultElement ?? defaultElement;
|
|
26
50
|
});
|