mobx-route 0.0.94 → 0.0.95
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,18 @@
|
|
|
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
|
-
export
|
|
6
|
+
};
|
|
7
|
+
export type SwitchProps<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
|
+
export declare const Switch: (<TRoute extends AnyRouteEntity>({ children, default: defaultRoute, params, ...navigateParams }: SwitchProps<TRoute>) => ReactNode) & {
|
|
6
15
|
displayName: string;
|
|
7
16
|
};
|
|
17
|
+
export {};
|
|
8
18
|
//# sourceMappingURL=switch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/react/components/switch.tsx"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../../../src/react/components/switch.tsx"],"names":[],"mappings":"AAEA,OAAO,EAIL,SAAS,EAEV,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEnE,OAAO,EACL,cAAc,EACd,mBAAmB,EACnB,WAAW,EACZ,MAAM,qBAAqB,CAAC;AAG7B,KAAK,eAAe,GAAG;IACrB,QAAQ,EAAE,SAAS,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,cAAc,IAAI;IACvD,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;AAsBtB,eAAO,MAAM,MAAM,IAAsB,MAAM,SAAS,cAAc,kEAKnE,WAAW,CAAC,MAAM,CAAC;;CAsBpB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
2
|
import { observer } from 'mobx-react-lite';
|
|
3
|
-
import { Fragment, isValidElement } from 'react';
|
|
3
|
+
import { Fragment, isValidElement, useEffect, } from 'react';
|
|
4
4
|
import { flatMapDeep } from 'yummies/data';
|
|
5
5
|
import { isRouteEntity } from '../../core/utils/is-route-entity.js';
|
|
6
6
|
const flattenChildren = (children) => flatMapDeep(children, (c) =>
|
|
@@ -12,15 +12,23 @@ const isValidRouteElement = (element) => {
|
|
|
12
12
|
// @ts-ignore
|
|
13
13
|
isRouteEntity(element.props?.route));
|
|
14
14
|
};
|
|
15
|
-
export const Switch = observer(({ children })
|
|
15
|
+
export const Switch = observer(function ({ children, default: defaultRoute, params, ...navigateParams }) {
|
|
16
|
+
let resultElement = null;
|
|
16
17
|
let defaultElement = null;
|
|
17
18
|
for (const element of flattenChildren(children)) {
|
|
18
19
|
if (isValidRouteElement(element) && element.props.route.isOpened) {
|
|
19
|
-
|
|
20
|
+
resultElement = element;
|
|
21
|
+
break;
|
|
20
22
|
}
|
|
21
23
|
else {
|
|
22
24
|
defaultElement = element;
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
|
-
|
|
27
|
+
const isResultElementFound = !!resultElement;
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!isResultElementFound && defaultRoute && !defaultRoute.isOpened) {
|
|
30
|
+
defaultRoute?.open(params, navigateParams);
|
|
31
|
+
}
|
|
32
|
+
}, [isResultElementFound]);
|
|
33
|
+
return resultElement ?? defaultElement;
|
|
26
34
|
});
|