-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
StaticRouter.js
121 lines (98 loc) · 3.01 KB
/
StaticRouter.js
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
import React from "react";
import PropTypes from "prop-types";
import { createLocation, createPath } from "history";
import invariant from "invariant";
import warning from "warning";
import Router from "./Router";
function addLeadingSlash(path) {
return path.charAt(0) === "/" ? path : "/" + path;
}
function addBasename(basename, location) {
if (!basename) return location;
return {
...location,
pathname: addLeadingSlash(basename) + location.pathname
};
}
function stripBasename(basename, location) {
if (!basename) return location;
const base = addLeadingSlash(basename);
if (location.pathname.indexOf(base) !== 0) return location;
return {
...location,
pathname: location.pathname.substr(base.length)
};
}
function createURL(location) {
return typeof location === "string" ? location : createPath(location);
}
function staticHandler(methodName) {
return () => {
invariant(false, "You cannot %s with <StaticRouter>", methodName);
};
}
function noop() {}
/**
* The public top-level API for a "static" <Router>, so-called because it
* can't actually change the current location. Instead, it just records
* location changes in a context object. Useful mainly in testing and
* server-rendering scenarios.
*/
class StaticRouter extends React.Component {
static defaultProps = {
basename: "",
location: "/"
};
createHref = path => addLeadingSlash(this.props.basename + createURL(path));
handlePush = location => {
const { basename, context } = this.props;
context.action = "PUSH";
context.location = addBasename(basename, createLocation(location));
context.url = createURL(context.location);
};
handleReplace = location => {
const { basename, context } = this.props;
context.action = "REPLACE";
context.location = addBasename(basename, createLocation(location));
context.url = createURL(context.location);
};
handleListen = () => noop;
handleBlock = () => noop;
render() {
const { basename, context, location, ...rest } = this.props;
const history = {
createHref: this.createHref,
action: "POP",
location: stripBasename(basename, createLocation(location)),
push: this.handlePush,
replace: this.handleReplace,
go: staticHandler("go"),
goBack: staticHandler("goBack"),
goForward: staticHandler("goForward"),
listen: this.handleListen,
block: this.handleBlock
};
return (
<Router
{...rest}
history={history}
staticContext={this.props.context || {}}
/>
);
}
}
if (__DEV__) {
StaticRouter.propTypes = {
basename: PropTypes.string,
context: PropTypes.object,
location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
};
StaticRouter.prototype.componentDidMount = function() {
warning(
!this.props.history,
"<StaticRouter> ignores the history prop. To use a custom history, " +
"use `import { Router }` instead of `import { StaticRouter as Router }`."
);
};
}
export default StaticRouter;