@breautek/router 4.1.3 → 5.0.0
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/router.js +17 -104
- package/dist/src/IOnNoRoute.d.ts +2 -1
- package/dist/src/Route.d.ts +7 -4
- package/dist/src/RouteMatcher.d.ts +3 -2
- package/dist/src/Router.d.ts +3 -7
- package/dist/src/View.d.ts +6 -1
- package/eslint.config.js +14 -0
- package/package.json +30 -25
- package/pnpm-workspace.yaml +7 -0
- package/src/HashStrategy.ts +1 -1
- package/src/IOnNoRoute.ts +2 -1
- package/src/Route.tsx +19 -17
- package/src/RouteMatcher.ts +12 -10
- package/src/Router.tsx +16 -22
- package/src/TransitionSlide.ts +1 -1
- package/src/URLParser.ts +3 -3
- package/src/URLStrategy.ts +1 -1
- package/src/View.tsx +10 -1
package/dist/router.js
CHANGED
|
@@ -375,19 +375,17 @@ class RouteMatcher {
|
|
|
375
375
|
}
|
|
376
376
|
let props = {
|
|
377
377
|
url: url,
|
|
378
|
-
|
|
379
|
-
|
|
378
|
+
__base: base + componentToRender.props.url,
|
|
379
|
+
__matcher: this,
|
|
380
380
|
component: componentToRender.props.component,
|
|
381
381
|
entryTransition: componentToRender.props.entryTransition,
|
|
382
382
|
exitTransition: componentToRender.props.exitTransition,
|
|
383
|
-
componentProps:
|
|
384
|
-
|
|
385
|
-
router: this.$strategy
|
|
386
|
-
}
|
|
383
|
+
componentProps: componentToRender.props.componentProps,
|
|
384
|
+
__componentProps: Object.assign(Object.assign({}, componentToRender.props.componentProps), { url: url, router: this.$strategy })
|
|
387
385
|
};
|
|
388
386
|
if (params) {
|
|
389
387
|
for (let i in params) {
|
|
390
|
-
props.
|
|
388
|
+
props.__componentProps[i] = params[i];
|
|
391
389
|
}
|
|
392
390
|
}
|
|
393
391
|
return React.cloneElement(componentToRender, props);
|
|
@@ -502,7 +500,7 @@ class Router extends React__namespace.Component {
|
|
|
502
500
|
// currentRoute must be rendered as an array; because, exiting and incoming is rendered as an array.
|
|
503
501
|
// if currentRoute is not rendered as an array, a bug happens where the exiting screen is reloaded
|
|
504
502
|
// calling the constructor again.
|
|
505
|
-
return React__namespace.createElement(Root, { router: this.getRouterStrategy(), url: this.state.url }, [currentRoute]);
|
|
503
|
+
return React__namespace.createElement(Root, Object.assign({}, this.props.componentProps, { router: this.getRouterStrategy(), url: this.state.url }), [currentRoute]);
|
|
506
504
|
}
|
|
507
505
|
else {
|
|
508
506
|
return currentRoute;
|
|
@@ -537,6 +535,8 @@ class Router extends React__namespace.Component {
|
|
|
537
535
|
this.$incomingRoute = null;
|
|
538
536
|
this.$exitingRoute = null;
|
|
539
537
|
this.setState({ shouldTransition: false });
|
|
538
|
+
}).catch((error) => {
|
|
539
|
+
console.error(error);
|
|
540
540
|
});
|
|
541
541
|
}
|
|
542
542
|
}
|
|
@@ -602,14 +602,6 @@ class Router extends React__namespace.Component {
|
|
|
602
602
|
return null;
|
|
603
603
|
}
|
|
604
604
|
}
|
|
605
|
-
/**
|
|
606
|
-
* @deprecated Use Router.getInstance() instead.
|
|
607
|
-
* @returns {RouterStrategy}
|
|
608
|
-
*/
|
|
609
|
-
let getRouter = () => {
|
|
610
|
-
console.warn('getRouter() is deprecated. use Router.getInstance() instead.');
|
|
611
|
-
return Router.getInstance();
|
|
612
|
-
};
|
|
613
605
|
|
|
614
606
|
/**
|
|
615
607
|
* @notice Using the URLStrategy requires some backend configuration
|
|
@@ -739,9 +731,8 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
739
731
|
};
|
|
740
732
|
|
|
741
733
|
function insertStyle(css) {
|
|
742
|
-
if (typeof window === 'undefined')
|
|
734
|
+
if (typeof window === 'undefined')
|
|
743
735
|
return;
|
|
744
|
-
}
|
|
745
736
|
const style = document.createElement('style');
|
|
746
737
|
style.setAttribute('type', 'text/css');
|
|
747
738
|
style.innerHTML = css;
|
|
@@ -758,6 +749,7 @@ class View extends React__namespace.Component {
|
|
|
758
749
|
constructor(props) {
|
|
759
750
|
super(props);
|
|
760
751
|
this.$node = null;
|
|
752
|
+
this.state = this._initState();
|
|
761
753
|
}
|
|
762
754
|
/**
|
|
763
755
|
* Return the CSS class on this view
|
|
@@ -777,6 +769,8 @@ class View extends React__namespace.Component {
|
|
|
777
769
|
componentDidMount() {
|
|
778
770
|
this.getTitle().then((title) => {
|
|
779
771
|
this.props.router.setTitle(title);
|
|
772
|
+
}).catch((e) => {
|
|
773
|
+
console.error(e);
|
|
780
774
|
});
|
|
781
775
|
document.body.classList.add(this.$getViewBodyClass());
|
|
782
776
|
let stylesheet = this.getViewStylesheet();
|
|
@@ -839,15 +833,15 @@ class Route extends React__namespace.Component {
|
|
|
839
833
|
}
|
|
840
834
|
$getComponentsToRender(component) {
|
|
841
835
|
let url = component.props.url;
|
|
842
|
-
let base = component.props.
|
|
836
|
+
let base = component.props.__base || '';
|
|
843
837
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
844
838
|
let ViewComponent = component.props.component;
|
|
845
839
|
let child;
|
|
846
|
-
let routeComponent = component.props.
|
|
840
|
+
let routeComponent = component.props.__matcher.match(url, this.$getChildren(component), base);
|
|
847
841
|
if (routeComponent) {
|
|
848
842
|
child = this.$getComponentsToRender(routeComponent);
|
|
849
843
|
}
|
|
850
|
-
return (React__namespace.createElement(ViewComponent, Object.assign({}, component.props.componentProps, { ref: (node) => {
|
|
844
|
+
return (React__namespace.createElement(ViewComponent, Object.assign({}, component.props.componentProps, component.props.__componentProps, { ref: (node) => {
|
|
851
845
|
if (node) {
|
|
852
846
|
if (node instanceof View) {
|
|
853
847
|
this.$node = node;
|
|
@@ -864,7 +858,6 @@ class Route extends React__namespace.Component {
|
|
|
864
858
|
$getChildren(component) {
|
|
865
859
|
let children = null;
|
|
866
860
|
if (!component) {
|
|
867
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
868
861
|
component = this;
|
|
869
862
|
}
|
|
870
863
|
if (!component.props.children) {
|
|
@@ -1017,88 +1010,9 @@ class TransitionSlide extends TransitionStrategy {
|
|
|
1017
1010
|
}
|
|
1018
1011
|
}
|
|
1019
1012
|
|
|
1020
|
-
var
|
|
1021
|
-
var version = "4.1.3";
|
|
1022
|
-
var description = "An alternate react router.";
|
|
1023
|
-
var main = "dist/router.js";
|
|
1024
|
-
var types = "dist/src/api.d.ts";
|
|
1025
|
-
var type = "commonjs";
|
|
1026
|
-
var publishConfig = {
|
|
1027
|
-
registry: "https://registry.npmjs.org"
|
|
1028
|
-
};
|
|
1029
|
-
var scripts = {
|
|
1030
|
-
test: "jest",
|
|
1031
|
-
lint: "eslint --ext .ts,.tsx '?(src|spec)/**/*.?(ts|tsx)' --cache",
|
|
1032
|
-
build: "rollup -c rollup.config.js --bundleConfigAsCjs",
|
|
1033
|
-
"publish-beta": "npm publish --tag beta",
|
|
1034
|
-
prepublishOnly: "npm run build",
|
|
1035
|
-
version: "npm run-script build",
|
|
1036
|
-
postversion: "git push && git push --tags"
|
|
1037
|
-
};
|
|
1038
|
-
var repository = {
|
|
1039
|
-
type: "git",
|
|
1040
|
-
url: "https://github.com/breautek/router"
|
|
1041
|
-
};
|
|
1042
|
-
var keywords = [
|
|
1043
|
-
"react",
|
|
1044
|
-
"router",
|
|
1045
|
-
"page",
|
|
1046
|
-
"screen",
|
|
1047
|
-
"navigation"
|
|
1048
|
-
];
|
|
1049
|
-
var author = "norman@normanbreau.com";
|
|
1050
|
-
var license = "MIT";
|
|
1051
|
-
var homepage = "https://github.com/breautek/router";
|
|
1052
|
-
var peerDependencies = {
|
|
1053
|
-
react: "16.x || 17.x || 18.x",
|
|
1054
|
-
"react-dom": "16.x || 17.x || 18.x"
|
|
1055
|
-
};
|
|
1056
|
-
var devDependencies = {
|
|
1057
|
-
"@rollup/plugin-commonjs": "28.0.1",
|
|
1058
|
-
"@rollup/plugin-json": "6.1.0",
|
|
1059
|
-
"@rollup/plugin-node-resolve": "15.3.0",
|
|
1060
|
-
"@testing-library/jest-dom": "6.6.3",
|
|
1061
|
-
"@testing-library/react": "16.0.1",
|
|
1062
|
-
"@types/jest": "29.5.14",
|
|
1063
|
-
"@types/react": "18.3.12",
|
|
1064
|
-
"@types/react-dom": "18.3.1",
|
|
1065
|
-
"@typescript-eslint/eslint-plugin": "7.18.0",
|
|
1066
|
-
"@typescript-eslint/parser": "7.18.0",
|
|
1067
|
-
ajv: "8.17.1",
|
|
1068
|
-
eslint: "8.57.1",
|
|
1069
|
-
"eslint-plugin-react": "7.37.2",
|
|
1070
|
-
glob: "11.0.0",
|
|
1071
|
-
"ignore-styles": "5.0.1",
|
|
1072
|
-
jest: "29.7.0",
|
|
1073
|
-
"jest-environment-jsdom": "29.7.0",
|
|
1074
|
-
"node-sass": "9.0.0",
|
|
1075
|
-
react: "18.3.1",
|
|
1076
|
-
"react-dom": "18.3.1",
|
|
1077
|
-
rollup: "4.24.4",
|
|
1078
|
-
"rollup-plugin-progress": "1.1.2",
|
|
1079
|
-
"rollup-plugin-sass": "1.14.0",
|
|
1080
|
-
"rollup-plugin-typescript2": "0.36.0",
|
|
1081
|
-
"ts-jest": "29.2.5",
|
|
1082
|
-
"ts-node": "10.9.2",
|
|
1083
|
-
typescript: "5.5.4"
|
|
1084
|
-
};
|
|
1013
|
+
var version = "5.0.0";
|
|
1085
1014
|
var packageInfo = {
|
|
1086
|
-
|
|
1087
|
-
version: version,
|
|
1088
|
-
description: description,
|
|
1089
|
-
main: main,
|
|
1090
|
-
types: types,
|
|
1091
|
-
type: type,
|
|
1092
|
-
publishConfig: publishConfig,
|
|
1093
|
-
scripts: scripts,
|
|
1094
|
-
repository: repository,
|
|
1095
|
-
keywords: keywords,
|
|
1096
|
-
author: author,
|
|
1097
|
-
license: license,
|
|
1098
|
-
homepage: homepage,
|
|
1099
|
-
peerDependencies: peerDependencies,
|
|
1100
|
-
devDependencies: devDependencies
|
|
1101
|
-
};
|
|
1015
|
+
version: version};
|
|
1102
1016
|
|
|
1103
1017
|
const VERSION = packageInfo.version;
|
|
1104
1018
|
|
|
@@ -1113,6 +1027,5 @@ exports.TransitionSlide = TransitionSlide;
|
|
|
1113
1027
|
exports.TransitionStrategy = TransitionStrategy;
|
|
1114
1028
|
exports.URLStrategy = URLStrategy;
|
|
1115
1029
|
exports.View = View;
|
|
1116
|
-
exports.getRouter = getRouter;
|
|
1117
1030
|
exports.version = VERSION;
|
|
1118
1031
|
//# sourceMappingURL=router.js.map
|
package/dist/src/IOnNoRoute.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { IRouteProps } from 'Route';
|
|
2
3
|
export interface IOnNoRoute {
|
|
3
|
-
(indexRoute: React.ReactElement
|
|
4
|
+
(indexRoute: React.ReactElement<IRouteProps>, routes: React.ReactElement<IRouteProps>[]): React.ReactElement<IRouteProps>;
|
|
4
5
|
}
|
package/dist/src/Route.d.ts
CHANGED
|
@@ -3,24 +3,27 @@ import { View } from './View';
|
|
|
3
3
|
import { RouteMatcher } from './RouteMatcher';
|
|
4
4
|
import { RouterStrategy } from './RouterStrategy';
|
|
5
5
|
import { TransitionStrategy } from './TransitionStrategy';
|
|
6
|
-
export interface IRouteProps<
|
|
6
|
+
export interface IRouteProps<TUserProps = any, TInternalProps extends IComponentProps = IComponentProps> {
|
|
7
7
|
url: string;
|
|
8
8
|
component: React.ComponentClass<any>;
|
|
9
9
|
index?: boolean;
|
|
10
10
|
entryTransition?: TransitionStrategy;
|
|
11
11
|
exitTransition?: TransitionStrategy;
|
|
12
|
+
children?: React.ReactElement<IRouteProps> | React.ReactElement<IRouteProps>[];
|
|
13
|
+
componentProps?: TUserProps;
|
|
14
|
+
ref?: React.Ref<Route>;
|
|
12
15
|
/**
|
|
13
16
|
* @internal
|
|
14
17
|
*/
|
|
15
|
-
|
|
18
|
+
__base?: string;
|
|
16
19
|
/**
|
|
17
20
|
* @internal
|
|
18
21
|
*/
|
|
19
|
-
|
|
22
|
+
__componentProps?: TInternalProps;
|
|
20
23
|
/**
|
|
21
24
|
* @internal
|
|
22
25
|
*/
|
|
23
|
-
|
|
26
|
+
__matcher?: RouteMatcher;
|
|
24
27
|
}
|
|
25
28
|
export interface IComponentProps {
|
|
26
29
|
url: string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IRouteProps } from './Route';
|
|
1
2
|
import { RouterStrategy } from './RouterStrategy';
|
|
2
3
|
/**
|
|
3
4
|
* Will be invoked when no routes are found. Default implementation is to simply return the `indexRoute`.
|
|
@@ -6,7 +7,7 @@ import { RouterStrategy } from './RouterStrategy';
|
|
|
6
7
|
* @returns {React.ReactElement} An `ReactElement` of a `Route`
|
|
7
8
|
*/
|
|
8
9
|
export interface IOnNoRouteFunction {
|
|
9
|
-
(indexRoute: React.ReactElement, routes:
|
|
10
|
+
(indexRoute: React.ReactElement, routes: React.ReactElement<IRouteProps>[]): React.ReactElement<IRouteProps>;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* This class is reponsible for determing which route to render
|
|
@@ -25,5 +26,5 @@ export declare class RouteMatcher {
|
|
|
25
26
|
* @param indexRoute
|
|
26
27
|
* @param onNoRoute
|
|
27
28
|
*/
|
|
28
|
-
match(url: string, children:
|
|
29
|
+
match(url: string, children: React.ReactElement<IRouteProps>[], base: string, indexRoute?: React.ReactElement<IRouteProps>, onNoRoute?: IOnNoRouteFunction): React.ReactElement<IRouteProps>;
|
|
29
30
|
}
|
package/dist/src/Router.d.ts
CHANGED
|
@@ -2,9 +2,10 @@ import * as React from 'react';
|
|
|
2
2
|
import { RouterStrategy } from './RouterStrategy';
|
|
3
3
|
import { IRouterStrategyClass } from './IRouterStrategyClass';
|
|
4
4
|
import { IOnNoRoute } from './IOnNoRoute';
|
|
5
|
-
export interface IRouterProps {
|
|
5
|
+
export interface IRouterProps<T = any> {
|
|
6
6
|
strategy?: IRouterStrategyClass;
|
|
7
7
|
component: React.ComponentClass<any>;
|
|
8
|
+
componentProps?: T;
|
|
8
9
|
children?: React.ReactNode;
|
|
9
10
|
onNoRoute?: IOnNoRoute;
|
|
10
11
|
}
|
|
@@ -13,7 +14,7 @@ export interface IRouterState {
|
|
|
13
14
|
url: string;
|
|
14
15
|
shouldTransition: boolean;
|
|
15
16
|
}
|
|
16
|
-
export declare class Router<TRouterProps extends IRouterProps = IRouterProps
|
|
17
|
+
export declare class Router<TUserProps = any, TRouterProps extends IRouterProps<TUserProps> = IRouterProps<TUserProps>> extends React.Component<TRouterProps, IRouterState> {
|
|
17
18
|
state: IRouterState;
|
|
18
19
|
private $lastRenderedRoute;
|
|
19
20
|
private $matcher;
|
|
@@ -82,8 +83,3 @@ export declare class Router<TRouterProps extends IRouterProps = IRouterProps> ex
|
|
|
82
83
|
*/
|
|
83
84
|
private $getIndexRoute;
|
|
84
85
|
}
|
|
85
|
-
/**
|
|
86
|
-
* @deprecated Use Router.getInstance() instead.
|
|
87
|
-
* @returns {RouterStrategy}
|
|
88
|
-
*/
|
|
89
|
-
export declare let getRouter: () => RouterStrategy;
|
package/dist/src/View.d.ts
CHANGED
|
@@ -8,12 +8,17 @@ export interface IViewProps {
|
|
|
8
8
|
entryTransition?: TransitionStrategy;
|
|
9
9
|
exitTransition?: TransitionStrategy;
|
|
10
10
|
}
|
|
11
|
-
export
|
|
11
|
+
export interface IViewState {
|
|
12
|
+
}
|
|
13
|
+
export interface IViewSnapshotState {
|
|
14
|
+
}
|
|
15
|
+
export declare abstract class View<TPageProps extends IViewProps = IViewProps, TPageState extends IViewState = IViewState, TPageSnapshotState extends IViewSnapshotState = IViewSnapshotState> extends React.Component<TPageProps, TPageState, TPageSnapshotState> {
|
|
12
16
|
private $node;
|
|
13
17
|
/**
|
|
14
18
|
* @param props See [IViewProps]
|
|
15
19
|
*/
|
|
16
20
|
constructor(props: TPageProps);
|
|
21
|
+
protected abstract _initState(): TPageState;
|
|
17
22
|
/**
|
|
18
23
|
* Return the CSS class on this view
|
|
19
24
|
*/
|
package/eslint.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@breautek/router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "An alternate react router.",
|
|
5
5
|
"main": "dist/router.js",
|
|
6
6
|
"types": "dist/src/api.d.ts",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
12
|
"test": "jest",
|
|
13
|
-
"lint": "eslint
|
|
13
|
+
"lint": "eslint ./src ./spec",
|
|
14
|
+
"lint:fix": "eslint --fix ./src ./spec",
|
|
14
15
|
"build": "rollup -c rollup.config.js --bundleConfigAsCjs",
|
|
15
16
|
"publish-beta": "npm publish --tag beta",
|
|
16
17
|
"prepublishOnly": "npm run build",
|
|
@@ -32,36 +33,40 @@
|
|
|
32
33
|
"license": "MIT",
|
|
33
34
|
"homepage": "https://github.com/breautek/router",
|
|
34
35
|
"peerDependencies": {
|
|
35
|
-
"react": "16.x || 17.x || 18.x",
|
|
36
|
-
"react-dom": "16.x || 17.x || 18.x"
|
|
36
|
+
"react": "16.x || 17.x || 18.x || 19.x",
|
|
37
|
+
"react-dom": "16.x || 17.x || 18.x || 19.x"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
|
-
"@
|
|
40
|
+
"@jest/types": "30.2.0",
|
|
41
|
+
"@rollup/plugin-commonjs": "29.0.0",
|
|
40
42
|
"@rollup/plugin-json": "6.1.0",
|
|
41
|
-
"@rollup/plugin-node-resolve": "
|
|
42
|
-
"@testing-library/
|
|
43
|
-
"@testing-library/
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"@types/
|
|
47
|
-
"@
|
|
48
|
-
"@
|
|
43
|
+
"@rollup/plugin-node-resolve": "16.0.3",
|
|
44
|
+
"@testing-library/dom": "10.4.1",
|
|
45
|
+
"@testing-library/jest-dom": "6.9.1",
|
|
46
|
+
"@testing-library/react": "16.3.2",
|
|
47
|
+
"@totalpave/eslint-plugin": "7.0.17",
|
|
48
|
+
"@types/jest": "30.0.0",
|
|
49
|
+
"@types/react": "19.2.10",
|
|
50
|
+
"@types/react-dom": "19.2.3",
|
|
49
51
|
"ajv": "8.17.1",
|
|
50
|
-
"
|
|
51
|
-
"eslint-plugin-react": "7.37.2",
|
|
52
|
-
"glob": "11.0.0",
|
|
52
|
+
"glob": "13.0.1",
|
|
53
53
|
"ignore-styles": "5.0.1",
|
|
54
|
-
"jest": "
|
|
55
|
-
"jest-environment-jsdom": "
|
|
56
|
-
"
|
|
57
|
-
"react": "
|
|
58
|
-
"
|
|
59
|
-
"rollup": "4.24.4",
|
|
54
|
+
"jest": "30.2.0",
|
|
55
|
+
"jest-environment-jsdom": "30.2.0",
|
|
56
|
+
"react": "19.2.4",
|
|
57
|
+
"react-dom": "19.2.4",
|
|
58
|
+
"rollup": "4.57.1",
|
|
60
59
|
"rollup-plugin-progress": "1.1.2",
|
|
61
|
-
"rollup-plugin-sass": "1.
|
|
60
|
+
"rollup-plugin-sass": "1.15.3",
|
|
62
61
|
"rollup-plugin-typescript2": "0.36.0",
|
|
63
|
-
"
|
|
62
|
+
"sass": "1.97.3",
|
|
63
|
+
"ts-jest": "29.4.6",
|
|
64
64
|
"ts-node": "10.9.2",
|
|
65
|
-
"typescript": "5.
|
|
65
|
+
"typescript": "5.9.3"
|
|
66
|
+
},
|
|
67
|
+
"dependencies": {
|
|
68
|
+
"@types/events": "3.0.3",
|
|
69
|
+
"events": "3.3.0",
|
|
70
|
+
"tslib": "2.8.1"
|
|
66
71
|
}
|
|
67
72
|
}
|
package/src/HashStrategy.ts
CHANGED
package/src/IOnNoRoute.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import * as React from 'react';
|
|
3
|
+
import { IRouteProps } from 'Route';
|
|
3
4
|
|
|
4
5
|
export interface IOnNoRoute {
|
|
5
|
-
(indexRoute: React.ReactElement
|
|
6
|
+
(indexRoute: React.ReactElement<IRouteProps>, routes: React.ReactElement<IRouteProps>[]): React.ReactElement<IRouteProps>;
|
|
6
7
|
}
|
package/src/Route.tsx
CHANGED
|
@@ -5,27 +5,30 @@ import { RouteMatcher } from './RouteMatcher';
|
|
|
5
5
|
import { RouterStrategy } from './RouterStrategy';
|
|
6
6
|
import { TransitionStrategy } from './TransitionStrategy';
|
|
7
7
|
|
|
8
|
-
export interface IRouteProps<
|
|
8
|
+
export interface IRouteProps<TUserProps = any, TInternalProps extends IComponentProps = IComponentProps> {
|
|
9
9
|
url: string;
|
|
10
10
|
component: React.ComponentClass<any>;
|
|
11
11
|
index?: boolean;
|
|
12
12
|
entryTransition?: TransitionStrategy;
|
|
13
13
|
exitTransition?: TransitionStrategy;
|
|
14
|
+
children?: React.ReactElement<IRouteProps> | React.ReactElement<IRouteProps>[];
|
|
15
|
+
componentProps?: TUserProps;
|
|
16
|
+
ref?: React.Ref<Route>;
|
|
14
17
|
|
|
15
18
|
/**
|
|
16
19
|
* @internal
|
|
17
20
|
*/
|
|
18
|
-
|
|
21
|
+
__base?: string;
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* @internal
|
|
22
25
|
*/
|
|
23
|
-
|
|
26
|
+
__componentProps?: TInternalProps;
|
|
24
27
|
|
|
25
28
|
/**
|
|
26
29
|
* @internal
|
|
27
30
|
*/
|
|
28
|
-
|
|
31
|
+
__matcher?: RouteMatcher;
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
export interface IComponentProps {
|
|
@@ -38,10 +41,7 @@ export interface IComponentProps {
|
|
|
38
41
|
[key: string]: any;
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
|
|
42
|
-
export interface IRouteState {
|
|
43
|
-
|
|
44
|
-
}
|
|
44
|
+
export interface IRouteState {}
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* This class represents a route that renders a {@link View} component
|
|
@@ -62,22 +62,25 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
62
62
|
return this.$node;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
private $getComponentsToRender(component: React.ReactElement | React.Component): React.ReactNode {
|
|
65
|
+
private $getComponentsToRender(component: React.ReactElement<IRouteProps> | React.Component<IRouteProps>): React.ReactNode {
|
|
66
66
|
let url: string = component.props.url;
|
|
67
|
-
let base: string = component.props.
|
|
67
|
+
let base: string = component.props.__base || '';
|
|
68
68
|
|
|
69
69
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
70
70
|
let ViewComponent: React.ElementType = component.props.component;
|
|
71
71
|
let child: React.ReactNode;
|
|
72
72
|
|
|
73
|
-
let routeComponent = component.props.
|
|
73
|
+
let routeComponent: React.ReactElement<IRouteProps> = component.props.__matcher.match(url, this.$getChildren(component), base);
|
|
74
74
|
if (routeComponent) {
|
|
75
75
|
child = this.$getComponentsToRender(routeComponent);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
return (
|
|
79
79
|
<ViewComponent
|
|
80
|
-
{...
|
|
80
|
+
{...{
|
|
81
|
+
...component.props.componentProps,
|
|
82
|
+
...component.props.__componentProps
|
|
83
|
+
}}
|
|
81
84
|
ref={(node: React.Component) => {
|
|
82
85
|
if (node) {
|
|
83
86
|
if (node instanceof View) {
|
|
@@ -97,11 +100,10 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
97
100
|
);
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
private $getChildren(component: React.Component | React.ReactElement): React.ReactElement[] {
|
|
101
|
-
let children: React.ReactElement[] = null;
|
|
103
|
+
private $getChildren(component: React.Component<IRouteProps> | React.ReactElement<IRouteProps>): React.ReactElement<IRouteProps>[] {
|
|
104
|
+
let children: React.ReactElement<IRouteProps>[] = null;
|
|
102
105
|
|
|
103
106
|
if (!component) {
|
|
104
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
105
107
|
component = this;
|
|
106
108
|
}
|
|
107
109
|
|
|
@@ -110,10 +112,10 @@ export class Route<TComponentProps extends IComponentProps = IComponentProps, TR
|
|
|
110
112
|
}
|
|
111
113
|
|
|
112
114
|
if (component.props.children instanceof Array) {
|
|
113
|
-
children = component.props.children as React.ReactElement[];
|
|
115
|
+
children = component.props.children as React.ReactElement<IRouteProps>[];
|
|
114
116
|
}
|
|
115
117
|
else {
|
|
116
|
-
children = [ component.props.children as React.ReactElement ];
|
|
118
|
+
children = [ component.props.children as React.ReactElement<IRouteProps> ];
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
return children;
|
package/src/RouteMatcher.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {RouterStrategy} from './RouterStrategy';
|
|
|
11
11
|
* @returns {React.ReactElement} An `ReactElement` of a `Route`
|
|
12
12
|
*/
|
|
13
13
|
export interface IOnNoRouteFunction {
|
|
14
|
-
(indexRoute: React.ReactElement, routes:
|
|
14
|
+
(indexRoute: React.ReactElement, routes: React.ReactElement<IRouteProps>[]): React.ReactElement<IRouteProps>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -25,7 +25,7 @@ export class RouteMatcher {
|
|
|
25
25
|
this.$strategy = routerStrategy;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
private $defaultNoRouteFunction(indexRoute: React.ReactElement
|
|
28
|
+
private $defaultNoRouteFunction(indexRoute: React.ReactElement<IRouteProps>, routes: React.ReactElement<IRouteProps>[]): React.ReactElement<IRouteProps> {
|
|
29
29
|
return indexRoute;
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -38,11 +38,11 @@ export class RouteMatcher {
|
|
|
38
38
|
* @param indexRoute
|
|
39
39
|
* @param onNoRoute
|
|
40
40
|
*/
|
|
41
|
-
public match(url: string, children:
|
|
42
|
-
let componentToRender: React.ReactElement = null;
|
|
41
|
+
public match(url: string, children: React.ReactElement<IRouteProps>[], base: string, indexRoute?: React.ReactElement<IRouteProps>, onNoRoute?: IOnNoRouteFunction): React.ReactElement<IRouteProps> {
|
|
42
|
+
let componentToRender: React.ReactElement<IRouteProps> = null;
|
|
43
43
|
let params: IURLParams = null;
|
|
44
44
|
for (let i: number = 0; i < children.length; i++) {
|
|
45
|
-
let route: React.ReactElement = children[i];
|
|
45
|
+
let route: React.ReactElement<IRouteProps> = children[i];
|
|
46
46
|
let shouldAllowPartialMatching: boolean = !!route.props.children;
|
|
47
47
|
let parser: URLParser = new URLParser(base + route.props.url, shouldAllowPartialMatching);
|
|
48
48
|
params = parser.parse(url);
|
|
@@ -63,14 +63,16 @@ export class RouteMatcher {
|
|
|
63
63
|
return null;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
let props: IRouteProps<IComponentProps> = {
|
|
66
|
+
let props: IRouteProps<any, IComponentProps> = {
|
|
67
67
|
url : url,
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
__base: base + componentToRender.props.url,
|
|
69
|
+
__matcher: this,
|
|
70
70
|
component: componentToRender.props.component,
|
|
71
71
|
entryTransition: componentToRender.props.entryTransition,
|
|
72
72
|
exitTransition: componentToRender.props.exitTransition,
|
|
73
|
-
componentProps:
|
|
73
|
+
componentProps: componentToRender.props.componentProps,
|
|
74
|
+
__componentProps: {
|
|
75
|
+
...componentToRender.props.componentProps,
|
|
74
76
|
url : url,
|
|
75
77
|
router: this.$strategy
|
|
76
78
|
}
|
|
@@ -78,7 +80,7 @@ export class RouteMatcher {
|
|
|
78
80
|
|
|
79
81
|
if (params) {
|
|
80
82
|
for (let i in params) {
|
|
81
|
-
props.
|
|
83
|
+
props.__componentProps[i] = params[i];
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
package/src/Router.tsx
CHANGED
|
@@ -6,11 +6,12 @@ import {RouteMatcher} from './RouteMatcher';
|
|
|
6
6
|
import { RouterStrategy } from './RouterStrategy';
|
|
7
7
|
import { IRouterStrategyClass } from './IRouterStrategyClass';
|
|
8
8
|
import {IOnNoRoute} from './IOnNoRoute';
|
|
9
|
-
import { Route } from "./Route";
|
|
9
|
+
import { IRouteProps, Route } from "./Route";
|
|
10
10
|
|
|
11
|
-
export interface IRouterProps {
|
|
11
|
+
export interface IRouterProps<T = any> {
|
|
12
12
|
strategy?: IRouterStrategyClass;
|
|
13
13
|
component: React.ComponentClass<any>;
|
|
14
|
+
componentProps?: T;
|
|
14
15
|
children?: React.ReactNode;
|
|
15
16
|
onNoRoute?: IOnNoRoute;
|
|
16
17
|
}
|
|
@@ -21,7 +22,7 @@ export interface IRouterState {
|
|
|
21
22
|
shouldTransition: boolean;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
export class Router<TRouterProps extends IRouterProps = IRouterProps
|
|
25
|
+
export class Router<TUserProps = any, TRouterProps extends IRouterProps<TUserProps> = IRouterProps<TUserProps>> extends React.Component<TRouterProps, IRouterState> {
|
|
25
26
|
public state: IRouterState;
|
|
26
27
|
|
|
27
28
|
private $lastRenderedRoute: any;
|
|
@@ -115,7 +116,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
115
116
|
* @ignore
|
|
116
117
|
*/
|
|
117
118
|
public render(): React.ReactNode {
|
|
118
|
-
let currentRoute: React.ReactElement = this.$matcher.match(this.state.url || '/', this.$getChildren(), '', this.$getIndexRoute(), this.props.onNoRoute);
|
|
119
|
+
let currentRoute: React.ReactElement<IRouteProps> = this.$matcher.match(this.state.url || '/', this.$getChildren(), '', this.$getIndexRoute(), this.props.onNoRoute);
|
|
119
120
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
120
121
|
let Root: React.ElementType = null;
|
|
121
122
|
if (this.props.component) {
|
|
@@ -141,7 +142,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
141
142
|
}
|
|
142
143
|
|
|
143
144
|
// Incoming will always be safe to render, hence no defensive checks
|
|
144
|
-
let incoming: React.ReactElement = React.cloneElement(currentRoute, {
|
|
145
|
+
let incoming: React.ReactElement<IRouteProps> = React.cloneElement(currentRoute, {
|
|
145
146
|
ref : (route: Route) => {
|
|
146
147
|
this.$incomingRoute = route;
|
|
147
148
|
}
|
|
@@ -161,7 +162,7 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
161
162
|
// currentRoute must be rendered as an array; because, exiting and incoming is rendered as an array.
|
|
162
163
|
// if currentRoute is not rendered as an array, a bug happens where the exiting screen is reloaded
|
|
163
164
|
// calling the constructor again.
|
|
164
|
-
return <Root router={this.getRouterStrategy()} url={this.state.url}>{[ currentRoute ]}</Root>;
|
|
165
|
+
return <Root {...this.props.componentProps} router={this.getRouterStrategy()} url={this.state.url}>{[ currentRoute ]}</Root>;
|
|
165
166
|
}
|
|
166
167
|
else {
|
|
167
168
|
return currentRoute;
|
|
@@ -198,6 +199,8 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
198
199
|
this.$incomingRoute = null;
|
|
199
200
|
this.$exitingRoute = null;
|
|
200
201
|
this.setState({shouldTransition: false});
|
|
202
|
+
}).catch((error: Error) => {
|
|
203
|
+
console.error(error);
|
|
201
204
|
});
|
|
202
205
|
}
|
|
203
206
|
}
|
|
@@ -245,14 +248,14 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
245
248
|
/**
|
|
246
249
|
* Gets the potential routes
|
|
247
250
|
*/
|
|
248
|
-
private $getChildren(): React.ReactElement[] {
|
|
249
|
-
let children: React.ReactElement[] = null;
|
|
251
|
+
private $getChildren(): React.ReactElement<IRouteProps>[] {
|
|
252
|
+
let children: React.ReactElement<IRouteProps>[] = null;
|
|
250
253
|
|
|
251
254
|
if (this.props.children instanceof Array) {
|
|
252
|
-
children = this.props.children as React.ReactElement[];
|
|
255
|
+
children = this.props.children as React.ReactElement<IRouteProps>[];
|
|
253
256
|
}
|
|
254
257
|
else {
|
|
255
|
-
children = [ this.props.children as React.ReactElement ];
|
|
258
|
+
children = [ this.props.children as React.ReactElement<IRouteProps> ];
|
|
256
259
|
}
|
|
257
260
|
|
|
258
261
|
return children;
|
|
@@ -261,10 +264,10 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
261
264
|
/**
|
|
262
265
|
* Finds the index route. Returns null if there are no indexed routes.
|
|
263
266
|
*/
|
|
264
|
-
private $getIndexRoute(): React.ReactElement {
|
|
265
|
-
let children: React.ReactElement[] = this.$getChildren();
|
|
267
|
+
private $getIndexRoute(): React.ReactElement<IRouteProps> {
|
|
268
|
+
let children: React.ReactElement<IRouteProps>[] = this.$getChildren();
|
|
266
269
|
for (let i: number = 0; i < children.length; i++) {
|
|
267
|
-
let child: React.ReactElement = children[i];
|
|
270
|
+
let child: React.ReactElement<IRouteProps> = children[i];
|
|
268
271
|
if (child.props.index) {
|
|
269
272
|
return child;
|
|
270
273
|
}
|
|
@@ -273,12 +276,3 @@ export class Router<TRouterProps extends IRouterProps = IRouterProps> extends Re
|
|
|
273
276
|
return null;
|
|
274
277
|
}
|
|
275
278
|
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* @deprecated Use Router.getInstance() instead.
|
|
279
|
-
* @returns {RouterStrategy}
|
|
280
|
-
*/
|
|
281
|
-
export let getRouter = (): RouterStrategy => {
|
|
282
|
-
console.warn('getRouter() is deprecated. use Router.getInstance() instead.');
|
|
283
|
-
return Router.getInstance();
|
|
284
|
-
}
|
package/src/TransitionSlide.ts
CHANGED
|
@@ -61,7 +61,7 @@ export class TransitionSlide extends TransitionStrategy {
|
|
|
61
61
|
exitingNode.style.transition = transitionString;
|
|
62
62
|
|
|
63
63
|
//Add transition listener
|
|
64
|
-
let onTransitionEnd = () => {
|
|
64
|
+
let onTransitionEnd = (): void => {
|
|
65
65
|
//cleanup
|
|
66
66
|
incomingNode.removeEventListener('transitionend', onTransitionEnd);
|
|
67
67
|
|
package/src/URLParser.ts
CHANGED
|
@@ -30,8 +30,8 @@ export class URLParser {
|
|
|
30
30
|
*/
|
|
31
31
|
public parse(url: string): IURLParams {
|
|
32
32
|
url = this.$stripURL(url);
|
|
33
|
-
let parts:
|
|
34
|
-
let patternParts:
|
|
33
|
+
let parts: string[] = this.$getParts(url);
|
|
34
|
+
let patternParts: string[] = this.$getParts(this.$pattern);
|
|
35
35
|
|
|
36
36
|
if ((!this.$allowPartialMatch && parts.length !== patternParts.length) || url === '') {
|
|
37
37
|
return null;
|
|
@@ -84,7 +84,7 @@ export class URLParser {
|
|
|
84
84
|
* @private
|
|
85
85
|
* @param {string} url URL to split
|
|
86
86
|
*/
|
|
87
|
-
private $getParts(url: string):
|
|
87
|
+
private $getParts(url: string): string[] {
|
|
88
88
|
url = this.$stripURL(url);
|
|
89
89
|
return url.split('/');
|
|
90
90
|
}
|
package/src/URLStrategy.ts
CHANGED
package/src/View.tsx
CHANGED
|
@@ -12,7 +12,11 @@ export interface IViewProps {
|
|
|
12
12
|
exitTransition?: TransitionStrategy;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export
|
|
15
|
+
export interface IViewState {}
|
|
16
|
+
|
|
17
|
+
export interface IViewSnapshotState {}
|
|
18
|
+
|
|
19
|
+
export abstract class View<TPageProps extends IViewProps = IViewProps, TPageState extends IViewState = IViewState, TPageSnapshotState extends IViewSnapshotState = IViewSnapshotState> extends React.Component<TPageProps, TPageState, TPageSnapshotState> {
|
|
16
20
|
private $node: HTMLDivElement;
|
|
17
21
|
|
|
18
22
|
/**
|
|
@@ -21,8 +25,11 @@ export abstract class View<TPageProps extends IViewProps = IViewProps> extends R
|
|
|
21
25
|
public constructor(props: TPageProps) {
|
|
22
26
|
super(props);
|
|
23
27
|
this.$node = null;
|
|
28
|
+
this.state = this._initState();
|
|
24
29
|
}
|
|
25
30
|
|
|
31
|
+
protected abstract _initState(): TPageState;
|
|
32
|
+
|
|
26
33
|
/**
|
|
27
34
|
* Return the CSS class on this view
|
|
28
35
|
*/
|
|
@@ -44,6 +51,8 @@ export abstract class View<TPageProps extends IViewProps = IViewProps> extends R
|
|
|
44
51
|
public componentDidMount(): void {
|
|
45
52
|
this.getTitle().then((title: string) => {
|
|
46
53
|
this.props.router.setTitle(title);
|
|
54
|
+
}).catch((e) => {
|
|
55
|
+
console.error(e);
|
|
47
56
|
});
|
|
48
57
|
|
|
49
58
|
document.body.classList.add(this.$getViewBodyClass());
|