-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrouter.tsx
More file actions
141 lines (116 loc) · 3.84 KB
/
Copy pathrouter.tsx
File metadata and controls
141 lines (116 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable consistent-return */
import React, { PropsWithChildren, FunctionComponent } from 'react';
import { PluginModule, injectable, inject, storeKey } from 'reactant-module';
import type { AnyAction, ReducersMapObject } from 'redux';
import {
connectRouter,
ConnectedRouter,
CALL_HISTORY_METHOD,
onLocationChanged,
routerActions,
} from 'connected-react-router';
import type { Location, LocationState, Action, History } from 'history';
export {
createHashHistory,
createBrowserHistory,
createMemoryHistory,
} from 'history';
export { LOCATION_CHANGE } from 'connected-react-router';
const RouterOptions = Symbol('RouterOptions');
export interface RouterState {
action: Action;
location: Location<LocationState>;
}
export interface IRouterOptions {
/**
* create history for router, use `createHashHistory`/`createBrowserHistory`/`createMemoryHistory`
*/
createHistory: () => History;
/**
* Auto provider injection.
*/
autoProvide?: boolean;
/**
* auto create history and handle middleware
*/
autoCreateHistory?: boolean;
}
// TODO: support ssr and router config
@injectable({
name: 'Router',
})
class ReactantRouter extends PluginModule {
autoProvide: boolean;
protected history!: History;
autoCreateHistory: boolean;
onLocationChanged = onLocationChanged;
routerActions = routerActions;
protected readonly stateKey = 'router';
constructor(@inject(RouterOptions) protected options: IRouterOptions) {
super();
const { autoProvide = true } = this.options || {};
this.autoProvide = autoProvide;
this.autoCreateHistory = this.options?.autoCreateHistory ?? true;
if (this.autoCreateHistory) {
this.history = this.options.createHistory();
this.middleware = (store) => (next) => (_action) => {
const action = _action as AnyAction;
if (action.type !== CALL_HISTORY_METHOD) {
return next(action);
}
// Just call `history` method and `history` will change routing, then trigger `history.listen`, and dispatch LOCATION_CHANGE action
const {
payload: { method, args = [] },
} = action;
const history: Record<string, Function> = this.history as any;
history[method](...args);
};
}
}
get store() {
return this[storeKey];
}
beforeCombineRootReducers(reducers: ReducersMapObject): ReducersMapObject {
if (Object.prototype.hasOwnProperty.call(reducers, this.stateKey)) {
throw new Error(
`The identifier '${this.stateKey}' has a duplicate name, please reset the option 'stateKey' of 'ReactantRouter' module.`
);
}
return Object.assign(reducers, {
[this.stateKey]: connectRouter(this.history ?? this.defaultHistory),
});
}
protected defaultHistory?: {
location: History['location'];
action: string;
};
ConnectedRouter: FunctionComponent = (props) => (
<ConnectedRouter history={this.history}>{props.children}</ConnectedRouter>
);
get router(): RouterState | undefined {
return this.store?.getState()[this.stateKey];
}
get currentPath() {
return this.router?.location.pathname;
}
push(path: string, state?: LocationState) {
this.store?.dispatch(this.routerActions.push(path, state) as AnyAction);
}
replace(path: string, state?: LocationState) {
this.store?.dispatch(this.routerActions.replace(path, state) as AnyAction);
}
go(n: number) {
this.store?.dispatch(this.routerActions.go(n) as AnyAction);
}
goBack() {
this.store?.dispatch(this.routerActions.goBack() as AnyAction);
}
goForward() {
this.store?.dispatch(this.routerActions.goForward() as AnyAction);
}
provider = (props: PropsWithChildren<any>) => {
if (!this.autoProvide) return props.children;
return <this.ConnectedRouter>{props.children}</this.ConnectedRouter>;
};
}
export { ReactantRouter as Router, RouterOptions };