1 |
3a515b92
|
cagy
|
import _inheritsLoose from '@babel/runtime/helpers/esm/inheritsLoose';
|
2 |
|
|
import React from 'react';
|
3 |
|
|
import PropTypes from 'prop-types';
|
4 |
|
|
import { createMemoryHistory, createLocation, locationsAreEqual, createPath } from 'history';
|
5 |
|
|
import warning from 'tiny-warning';
|
6 |
|
|
import createContext from 'mini-create-react-context';
|
7 |
|
|
import invariant from 'tiny-invariant';
|
8 |
|
|
import _extends from '@babel/runtime/helpers/esm/extends';
|
9 |
|
|
import pathToRegexp from 'path-to-regexp';
|
10 |
|
|
import { isValidElementType } from 'react-is';
|
11 |
|
|
import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/esm/objectWithoutPropertiesLoose';
|
12 |
|
|
import hoistStatics from 'hoist-non-react-statics';
|
13 |
|
|
|
14 |
|
|
// TODO: Replace with React.createContext once we can assume React 16+
|
15 |
|
|
|
16 |
|
|
var createNamedContext = function createNamedContext(name) {
|
17 |
|
|
var context = createContext();
|
18 |
|
|
context.displayName = name;
|
19 |
|
|
return context;
|
20 |
|
|
};
|
21 |
|
|
|
22 |
|
|
var context =
|
23 |
|
|
/*#__PURE__*/
|
24 |
|
|
createNamedContext("Router");
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* The public API for putting history on context.
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
var Router =
|
31 |
|
|
/*#__PURE__*/
|
32 |
|
|
function (_React$Component) {
|
33 |
|
|
_inheritsLoose(Router, _React$Component);
|
34 |
|
|
|
35 |
|
|
Router.computeRootMatch = function computeRootMatch(pathname) {
|
36 |
|
|
return {
|
37 |
|
|
path: "/",
|
38 |
|
|
url: "/",
|
39 |
|
|
params: {},
|
40 |
|
|
isExact: pathname === "/"
|
41 |
|
|
};
|
42 |
|
|
};
|
43 |
|
|
|
44 |
|
|
function Router(props) {
|
45 |
|
|
var _this;
|
46 |
|
|
|
47 |
|
|
_this = _React$Component.call(this, props) || this;
|
48 |
|
|
_this.state = {
|
49 |
|
|
location: props.history.location
|
50 |
|
|
}; // This is a bit of a hack. We have to start listening for location
|
51 |
|
|
// changes here in the constructor in case there are any <Redirect>s
|
52 |
|
|
// on the initial render. If there are, they will replace/push when
|
53 |
|
|
// they mount and since cDM fires in children before parents, we may
|
54 |
|
|
// get a new location before the <Router> is mounted.
|
55 |
|
|
|
56 |
|
|
_this._isMounted = false;
|
57 |
|
|
_this._pendingLocation = null;
|
58 |
|
|
|
59 |
|
|
if (!props.staticContext) {
|
60 |
|
|
_this.unlisten = props.history.listen(function (location) {
|
61 |
|
|
if (_this._isMounted) {
|
62 |
|
|
_this.setState({
|
63 |
|
|
location: location
|
64 |
|
|
});
|
65 |
|
|
} else {
|
66 |
|
|
_this._pendingLocation = location;
|
67 |
|
|
}
|
68 |
|
|
});
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
return _this;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
var _proto = Router.prototype;
|
75 |
|
|
|
76 |
|
|
_proto.componentDidMount = function componentDidMount() {
|
77 |
|
|
this._isMounted = true;
|
78 |
|
|
|
79 |
|
|
if (this._pendingLocation) {
|
80 |
|
|
this.setState({
|
81 |
|
|
location: this._pendingLocation
|
82 |
|
|
});
|
83 |
|
|
}
|
84 |
|
|
};
|
85 |
|
|
|
86 |
|
|
_proto.componentWillUnmount = function componentWillUnmount() {
|
87 |
|
|
if (this.unlisten) this.unlisten();
|
88 |
|
|
};
|
89 |
|
|
|
90 |
|
|
_proto.render = function render() {
|
91 |
|
|
return React.createElement(context.Provider, {
|
92 |
|
|
children: this.props.children || null,
|
93 |
|
|
value: {
|
94 |
|
|
history: this.props.history,
|
95 |
|
|
location: this.state.location,
|
96 |
|
|
match: Router.computeRootMatch(this.state.location.pathname),
|
97 |
|
|
staticContext: this.props.staticContext
|
98 |
|
|
}
|
99 |
|
|
});
|
100 |
|
|
};
|
101 |
|
|
|
102 |
|
|
return Router;
|
103 |
|
|
}(React.Component);
|
104 |
|
|
|
105 |
|
|
if (process.env.NODE_ENV !== "production") {
|
106 |
|
|
Router.propTypes = {
|
107 |
|
|
children: PropTypes.node,
|
108 |
|
|
history: PropTypes.object.isRequired,
|
109 |
|
|
staticContext: PropTypes.object
|
110 |
|
|
};
|
111 |
|
|
|
112 |
|
|
Router.prototype.componentDidUpdate = function (prevProps) {
|
113 |
|
|
process.env.NODE_ENV !== "production" ? warning(prevProps.history === this.props.history, "You cannot change <Router history>") : void 0;
|
114 |
|
|
};
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
/**
|
118 |
|
|
* The public API for a <Router> that stores location in memory.
|
119 |
|
|
*/
|
120 |
|
|
|
121 |
|
|
var MemoryRouter =
|
122 |
|
|
/*#__PURE__*/
|
123 |
|
|
function (_React$Component) {
|
124 |
|
|
_inheritsLoose(MemoryRouter, _React$Component);
|
125 |
|
|
|
126 |
|
|
function MemoryRouter() {
|
127 |
|
|
var _this;
|
128 |
|
|
|
129 |
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
130 |
|
|
args[_key] = arguments[_key];
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
134 |
|
|
_this.history = createMemoryHistory(_this.props);
|
135 |
|
|
return _this;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
var _proto = MemoryRouter.prototype;
|
139 |
|
|
|
140 |
|
|
_proto.render = function render() {
|
141 |
|
|
return React.createElement(Router, {
|
142 |
|
|
history: this.history,
|
143 |
|
|
children: this.props.children
|
144 |
|
|
});
|
145 |
|
|
};
|
146 |
|
|
|
147 |
|
|
return MemoryRouter;
|
148 |
|
|
}(React.Component);
|
149 |
|
|
|
150 |
|
|
if (process.env.NODE_ENV !== "production") {
|
151 |
|
|
MemoryRouter.propTypes = {
|
152 |
|
|
initialEntries: PropTypes.array,
|
153 |
|
|
initialIndex: PropTypes.number,
|
154 |
|
|
getUserConfirmation: PropTypes.func,
|
155 |
|
|
keyLength: PropTypes.number,
|
156 |
|
|
children: PropTypes.node
|
157 |
|
|
};
|
158 |
|
|
|
159 |
|
|
MemoryRouter.prototype.componentDidMount = function () {
|
160 |
|
|
process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<MemoryRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { MemoryRouter as Router }`.") : void 0;
|
161 |
|
|
};
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
var Lifecycle =
|
165 |
|
|
/*#__PURE__*/
|
166 |
|
|
function (_React$Component) {
|
167 |
|
|
_inheritsLoose(Lifecycle, _React$Component);
|
168 |
|
|
|
169 |
|
|
function Lifecycle() {
|
170 |
|
|
return _React$Component.apply(this, arguments) || this;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
var _proto = Lifecycle.prototype;
|
174 |
|
|
|
175 |
|
|
_proto.componentDidMount = function componentDidMount() {
|
176 |
|
|
if (this.props.onMount) this.props.onMount.call(this, this);
|
177 |
|
|
};
|
178 |
|
|
|
179 |
|
|
_proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
180 |
|
|
if (this.props.onUpdate) this.props.onUpdate.call(this, this, prevProps);
|
181 |
|
|
};
|
182 |
|
|
|
183 |
|
|
_proto.componentWillUnmount = function componentWillUnmount() {
|
184 |
|
|
if (this.props.onUnmount) this.props.onUnmount.call(this, this);
|
185 |
|
|
};
|
186 |
|
|
|
187 |
|
|
_proto.render = function render() {
|
188 |
|
|
return null;
|
189 |
|
|
};
|
190 |
|
|
|
191 |
|
|
return Lifecycle;
|
192 |
|
|
}(React.Component);
|
193 |
|
|
|
194 |
|
|
/**
|
195 |
|
|
* The public API for prompting the user before navigating away from a screen.
|
196 |
|
|
*/
|
197 |
|
|
|
198 |
|
|
function Prompt(_ref) {
|
199 |
|
|
var message = _ref.message,
|
200 |
|
|
_ref$when = _ref.when,
|
201 |
|
|
when = _ref$when === void 0 ? true : _ref$when;
|
202 |
|
|
return React.createElement(context.Consumer, null, function (context) {
|
203 |
|
|
!context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Prompt> outside a <Router>") : invariant(false) : void 0;
|
204 |
|
|
if (!when || context.staticContext) return null;
|
205 |
|
|
var method = context.history.block;
|
206 |
|
|
return React.createElement(Lifecycle, {
|
207 |
|
|
onMount: function onMount(self) {
|
208 |
|
|
self.release = method(message);
|
209 |
|
|
},
|
210 |
|
|
onUpdate: function onUpdate(self, prevProps) {
|
211 |
|
|
if (prevProps.message !== message) {
|
212 |
|
|
self.release();
|
213 |
|
|
self.release = method(message);
|
214 |
|
|
}
|
215 |
|
|
},
|
216 |
|
|
onUnmount: function onUnmount(self) {
|
217 |
|
|
self.release();
|
218 |
|
|
},
|
219 |
|
|
message: message
|
220 |
|
|
});
|
221 |
|
|
});
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
if (process.env.NODE_ENV !== "production") {
|
225 |
|
|
var messageType = PropTypes.oneOfType([PropTypes.func, PropTypes.string]);
|
226 |
|
|
Prompt.propTypes = {
|
227 |
|
|
when: PropTypes.bool,
|
228 |
|
|
message: messageType.isRequired
|
229 |
|
|
};
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
var cache = {};
|
233 |
|
|
var cacheLimit = 10000;
|
234 |
|
|
var cacheCount = 0;
|
235 |
|
|
|
236 |
|
|
function compilePath(path) {
|
237 |
|
|
if (cache[path]) return cache[path];
|
238 |
|
|
var generator = pathToRegexp.compile(path);
|
239 |
|
|
|
240 |
|
|
if (cacheCount < cacheLimit) {
|
241 |
|
|
cache[path] = generator;
|
242 |
|
|
cacheCount++;
|
243 |
|
|
}
|
244 |
|
|
|
245 |
|
|
return generator;
|
246 |
|
|
}
|
247 |
|
|
/**
|
248 |
|
|
* Public API for generating a URL pathname from a path and parameters.
|
249 |
|
|
*/
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
function generatePath(path, params) {
|
253 |
|
|
if (path === void 0) {
|
254 |
|
|
path = "/";
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
if (params === void 0) {
|
258 |
|
|
params = {};
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
return path === "/" ? path : compilePath(path)(params, {
|
262 |
|
|
pretty: true
|
263 |
|
|
});
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* The public API for navigating programmatically with a component.
|
268 |
|
|
*/
|
269 |
|
|
|
270 |
|
|
function Redirect(_ref) {
|
271 |
|
|
var computedMatch = _ref.computedMatch,
|
272 |
|
|
to = _ref.to,
|
273 |
|
|
_ref$push = _ref.push,
|
274 |
|
|
push = _ref$push === void 0 ? false : _ref$push;
|
275 |
|
|
return React.createElement(context.Consumer, null, function (context) {
|
276 |
|
|
!context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Redirect> outside a <Router>") : invariant(false) : void 0;
|
277 |
|
|
var history = context.history,
|
278 |
|
|
staticContext = context.staticContext;
|
279 |
|
|
var method = push ? history.push : history.replace;
|
280 |
|
|
var location = createLocation(computedMatch ? typeof to === "string" ? generatePath(to, computedMatch.params) : _extends({}, to, {
|
281 |
|
|
pathname: generatePath(to.pathname, computedMatch.params)
|
282 |
|
|
}) : to); // When rendering in a static context,
|
283 |
|
|
// set the new location immediately.
|
284 |
|
|
|
285 |
|
|
if (staticContext) {
|
286 |
|
|
method(location);
|
287 |
|
|
return null;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
|
|
return React.createElement(Lifecycle, {
|
291 |
|
|
onMount: function onMount() {
|
292 |
|
|
method(location);
|
293 |
|
|
},
|
294 |
|
|
onUpdate: function onUpdate(self, prevProps) {
|
295 |
|
|
var prevLocation = createLocation(prevProps.to);
|
296 |
|
|
|
297 |
|
|
if (!locationsAreEqual(prevLocation, _extends({}, location, {
|
298 |
|
|
key: prevLocation.key
|
299 |
|
|
}))) {
|
300 |
|
|
method(location);
|
301 |
|
|
}
|
302 |
|
|
},
|
303 |
|
|
to: to
|
304 |
|
|
});
|
305 |
|
|
});
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
if (process.env.NODE_ENV !== "production") {
|
309 |
|
|
Redirect.propTypes = {
|
310 |
|
|
push: PropTypes.bool,
|
311 |
|
|
from: PropTypes.string,
|
312 |
|
|
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired
|
313 |
|
|
};
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
var cache$1 = {};
|
317 |
|
|
var cacheLimit$1 = 10000;
|
318 |
|
|
var cacheCount$1 = 0;
|
319 |
|
|
|
320 |
|
|
function compilePath$1(path, options) {
|
321 |
|
|
var cacheKey = "" + options.end + options.strict + options.sensitive;
|
322 |
|
|
var pathCache = cache$1[cacheKey] || (cache$1[cacheKey] = {});
|
323 |
|
|
if (pathCache[path]) return pathCache[path];
|
324 |
|
|
var keys = [];
|
325 |
|
|
var regexp = pathToRegexp(path, keys, options);
|
326 |
|
|
var result = {
|
327 |
|
|
regexp: regexp,
|
328 |
|
|
keys: keys
|
329 |
|
|
};
|
330 |
|
|
|
331 |
|
|
if (cacheCount$1 < cacheLimit$1) {
|
332 |
|
|
pathCache[path] = result;
|
333 |
|
|
cacheCount$1++;
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
return result;
|
337 |
|
|
}
|
338 |
|
|
/**
|
339 |
|
|
* Public API for matching a URL pathname to a path.
|
340 |
|
|
*/
|
341 |
|
|
|
342 |
|
|
|
343 |
|
|
function matchPath(pathname, options) {
|
344 |
|
|
if (options === void 0) {
|
345 |
|
|
options = {};
|
346 |
|
|
}
|
347 |
|
|
|
348 |
|
|
if (typeof options === "string" || Array.isArray(options)) {
|
349 |
|
|
options = {
|
350 |
|
|
path: options
|
351 |
|
|
};
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
var _options = options,
|
355 |
|
|
path = _options.path,
|
356 |
|
|
_options$exact = _options.exact,
|
357 |
|
|
exact = _options$exact === void 0 ? false : _options$exact,
|
358 |
|
|
_options$strict = _options.strict,
|
359 |
|
|
strict = _options$strict === void 0 ? false : _options$strict,
|
360 |
|
|
_options$sensitive = _options.sensitive,
|
361 |
|
|
sensitive = _options$sensitive === void 0 ? false : _options$sensitive;
|
362 |
|
|
var paths = [].concat(path);
|
363 |
|
|
return paths.reduce(function (matched, path) {
|
364 |
|
|
if (!path && path !== "") return null;
|
365 |
|
|
if (matched) return matched;
|
366 |
|
|
|
367 |
|
|
var _compilePath = compilePath$1(path, {
|
368 |
|
|
end: exact,
|
369 |
|
|
strict: strict,
|
370 |
|
|
sensitive: sensitive
|
371 |
|
|
}),
|
372 |
|
|
regexp = _compilePath.regexp,
|
373 |
|
|
keys = _compilePath.keys;
|
374 |
|
|
|
375 |
|
|
var match = regexp.exec(pathname);
|
376 |
|
|
if (!match) return null;
|
377 |
|
|
var url = match[0],
|
378 |
|
|
values = match.slice(1);
|
379 |
|
|
var isExact = pathname === url;
|
380 |
|
|
if (exact && !isExact) return null;
|
381 |
|
|
return {
|
382 |
|
|
path: path,
|
383 |
|
|
// the path used to match
|
384 |
|
|
url: path === "/" && url === "" ? "/" : url,
|
385 |
|
|
// the matched portion of the URL
|
386 |
|
|
isExact: isExact,
|
387 |
|
|
// whether or not we matched exactly
|
388 |
|
|
params: keys.reduce(function (memo, key, index) {
|
389 |
|
|
memo[key.name] = values[index];
|
390 |
|
|
return memo;
|
391 |
|
|
}, {})
|
392 |
|
|
};
|
393 |
|
|
}, null);
|
394 |
|
|
}
|
395 |
|
|
|
396 |
|
|
function isEmptyChildren(children) {
|
397 |
|
|
return React.Children.count(children) === 0;
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
function evalChildrenDev(children, props, path) {
|
401 |
|
|
var value = children(props);
|
402 |
|
|
process.env.NODE_ENV !== "production" ? warning(value !== undefined, "You returned `undefined` from the `children` function of " + ("<Route" + (path ? " path=\"" + path + "\"" : "") + ">, but you ") + "should have returned a React element or `null`") : void 0;
|
403 |
|
|
return value || null;
|
404 |
|
|
}
|
405 |
|
|
/**
|
406 |
|
|
* The public API for matching a single path and rendering.
|
407 |
|
|
*/
|
408 |
|
|
|
409 |
|
|
|
410 |
|
|
var Route =
|
411 |
|
|
/*#__PURE__*/
|
412 |
|
|
function (_React$Component) {
|
413 |
|
|
_inheritsLoose(Route, _React$Component);
|
414 |
|
|
|
415 |
|
|
function Route() {
|
416 |
|
|
return _React$Component.apply(this, arguments) || this;
|
417 |
|
|
}
|
418 |
|
|
|
419 |
|
|
var _proto = Route.prototype;
|
420 |
|
|
|
421 |
|
|
_proto.render = function render() {
|
422 |
|
|
var _this = this;
|
423 |
|
|
|
424 |
|
|
return React.createElement(context.Consumer, null, function (context$1) {
|
425 |
|
|
!context$1 ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Route> outside a <Router>") : invariant(false) : void 0;
|
426 |
|
|
var location = _this.props.location || context$1.location;
|
427 |
|
|
var match = _this.props.computedMatch ? _this.props.computedMatch // <Switch> already computed the match for us
|
428 |
|
|
: _this.props.path ? matchPath(location.pathname, _this.props) : context$1.match;
|
429 |
|
|
|
430 |
|
|
var props = _extends({}, context$1, {
|
431 |
|
|
location: location,
|
432 |
|
|
match: match
|
433 |
|
|
});
|
434 |
|
|
|
435 |
|
|
var _this$props = _this.props,
|
436 |
|
|
children = _this$props.children,
|
437 |
|
|
component = _this$props.component,
|
438 |
|
|
render = _this$props.render; // Preact uses an empty array as children by
|
439 |
|
|
// default, so use null if that's the case.
|
440 |
|
|
|
441 |
|
|
if (Array.isArray(children) && children.length === 0) {
|
442 |
|
|
children = null;
|
443 |
|
|
}
|
444 |
|
|
|
445 |
|
|
return React.createElement(context.Provider, {
|
446 |
|
|
value: props
|
447 |
|
|
}, props.match ? children ? typeof children === "function" ? process.env.NODE_ENV !== "production" ? evalChildrenDev(children, props, _this.props.path) : children(props) : children : component ? React.createElement(component, props) : render ? render(props) : null : typeof children === "function" ? process.env.NODE_ENV !== "production" ? evalChildrenDev(children, props, _this.props.path) : children(props) : null);
|
448 |
|
|
});
|
449 |
|
|
};
|
450 |
|
|
|
451 |
|
|
return Route;
|
452 |
|
|
}(React.Component);
|
453 |
|
|
|
454 |
|
|
if (process.env.NODE_ENV !== "production") {
|
455 |
|
|
Route.propTypes = {
|
456 |
|
|
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
|
457 |
|
|
component: function component(props, propName) {
|
458 |
|
|
if (props[propName] && !isValidElementType(props[propName])) {
|
459 |
|
|
return new Error("Invalid prop 'component' supplied to 'Route': the prop is not a valid React component");
|
460 |
|
|
}
|
461 |
|
|
},
|
462 |
|
|
exact: PropTypes.bool,
|
463 |
|
|
location: PropTypes.object,
|
464 |
|
|
path: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
465 |
|
|
render: PropTypes.func,
|
466 |
|
|
sensitive: PropTypes.bool,
|
467 |
|
|
strict: PropTypes.bool
|
468 |
|
|
};
|
469 |
|
|
|
470 |
|
|
Route.prototype.componentDidMount = function () {
|
471 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.component), "You should not use <Route component> and <Route children> in the same route; <Route component> will be ignored") : void 0;
|
472 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(this.props.children && !isEmptyChildren(this.props.children) && this.props.render), "You should not use <Route render> and <Route children> in the same route; <Route render> will be ignored") : void 0;
|
473 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(this.props.component && this.props.render), "You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored") : void 0;
|
474 |
|
|
};
|
475 |
|
|
|
476 |
|
|
Route.prototype.componentDidUpdate = function (prevProps) {
|
477 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(this.props.location && !prevProps.location), '<Route> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') : void 0;
|
478 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(!this.props.location && prevProps.location), '<Route> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') : void 0;
|
479 |
|
|
};
|
480 |
|
|
}
|
481 |
|
|
|
482 |
|
|
function addLeadingSlash(path) {
|
483 |
|
|
return path.charAt(0) === "/" ? path : "/" + path;
|
484 |
|
|
}
|
485 |
|
|
|
486 |
|
|
function addBasename(basename, location) {
|
487 |
|
|
if (!basename) return location;
|
488 |
|
|
return _extends({}, location, {
|
489 |
|
|
pathname: addLeadingSlash(basename) + location.pathname
|
490 |
|
|
});
|
491 |
|
|
}
|
492 |
|
|
|
493 |
|
|
function stripBasename(basename, location) {
|
494 |
|
|
if (!basename) return location;
|
495 |
|
|
var base = addLeadingSlash(basename);
|
496 |
|
|
if (location.pathname.indexOf(base) !== 0) return location;
|
497 |
|
|
return _extends({}, location, {
|
498 |
|
|
pathname: location.pathname.substr(base.length)
|
499 |
|
|
});
|
500 |
|
|
}
|
501 |
|
|
|
502 |
|
|
function createURL(location) {
|
503 |
|
|
return typeof location === "string" ? location : createPath(location);
|
504 |
|
|
}
|
505 |
|
|
|
506 |
|
|
function staticHandler(methodName) {
|
507 |
|
|
return function () {
|
508 |
|
|
process.env.NODE_ENV !== "production" ? invariant(false, "You cannot %s with <StaticRouter>", methodName) : invariant(false) ;
|
509 |
|
|
};
|
510 |
|
|
}
|
511 |
|
|
|
512 |
|
|
function noop() {}
|
513 |
|
|
/**
|
514 |
|
|
* The public top-level API for a "static" <Router>, so-called because it
|
515 |
|
|
* can't actually change the current location. Instead, it just records
|
516 |
|
|
* location changes in a context object. Useful mainly in testing and
|
517 |
|
|
* server-rendering scenarios.
|
518 |
|
|
*/
|
519 |
|
|
|
520 |
|
|
|
521 |
|
|
var StaticRouter =
|
522 |
|
|
/*#__PURE__*/
|
523 |
|
|
function (_React$Component) {
|
524 |
|
|
_inheritsLoose(StaticRouter, _React$Component);
|
525 |
|
|
|
526 |
|
|
function StaticRouter() {
|
527 |
|
|
var _this;
|
528 |
|
|
|
529 |
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
530 |
|
|
args[_key] = arguments[_key];
|
531 |
|
|
}
|
532 |
|
|
|
533 |
|
|
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
534 |
|
|
|
535 |
|
|
_this.handlePush = function (location) {
|
536 |
|
|
return _this.navigateTo(location, "PUSH");
|
537 |
|
|
};
|
538 |
|
|
|
539 |
|
|
_this.handleReplace = function (location) {
|
540 |
|
|
return _this.navigateTo(location, "REPLACE");
|
541 |
|
|
};
|
542 |
|
|
|
543 |
|
|
_this.handleListen = function () {
|
544 |
|
|
return noop;
|
545 |
|
|
};
|
546 |
|
|
|
547 |
|
|
_this.handleBlock = function () {
|
548 |
|
|
return noop;
|
549 |
|
|
};
|
550 |
|
|
|
551 |
|
|
return _this;
|
552 |
|
|
}
|
553 |
|
|
|
554 |
|
|
var _proto = StaticRouter.prototype;
|
555 |
|
|
|
556 |
|
|
_proto.navigateTo = function navigateTo(location, action) {
|
557 |
|
|
var _this$props = this.props,
|
558 |
|
|
_this$props$basename = _this$props.basename,
|
559 |
|
|
basename = _this$props$basename === void 0 ? "" : _this$props$basename,
|
560 |
|
|
_this$props$context = _this$props.context,
|
561 |
|
|
context = _this$props$context === void 0 ? {} : _this$props$context;
|
562 |
|
|
context.action = action;
|
563 |
|
|
context.location = addBasename(basename, createLocation(location));
|
564 |
|
|
context.url = createURL(context.location);
|
565 |
|
|
};
|
566 |
|
|
|
567 |
|
|
_proto.render = function render() {
|
568 |
|
|
var _this$props2 = this.props,
|
569 |
|
|
_this$props2$basename = _this$props2.basename,
|
570 |
|
|
basename = _this$props2$basename === void 0 ? "" : _this$props2$basename,
|
571 |
|
|
_this$props2$context = _this$props2.context,
|
572 |
|
|
context = _this$props2$context === void 0 ? {} : _this$props2$context,
|
573 |
|
|
_this$props2$location = _this$props2.location,
|
574 |
|
|
location = _this$props2$location === void 0 ? "/" : _this$props2$location,
|
575 |
|
|
rest = _objectWithoutPropertiesLoose(_this$props2, ["basename", "context", "location"]);
|
576 |
|
|
|
577 |
|
|
var history = {
|
578 |
|
|
createHref: function createHref(path) {
|
579 |
|
|
return addLeadingSlash(basename + createURL(path));
|
580 |
|
|
},
|
581 |
|
|
action: "POP",
|
582 |
|
|
location: stripBasename(basename, createLocation(location)),
|
583 |
|
|
push: this.handlePush,
|
584 |
|
|
replace: this.handleReplace,
|
585 |
|
|
go: staticHandler("go"),
|
586 |
|
|
goBack: staticHandler("goBack"),
|
587 |
|
|
goForward: staticHandler("goForward"),
|
588 |
|
|
listen: this.handleListen,
|
589 |
|
|
block: this.handleBlock
|
590 |
|
|
};
|
591 |
|
|
return React.createElement(Router, _extends({}, rest, {
|
592 |
|
|
history: history,
|
593 |
|
|
staticContext: context
|
594 |
|
|
}));
|
595 |
|
|
};
|
596 |
|
|
|
597 |
|
|
return StaticRouter;
|
598 |
|
|
}(React.Component);
|
599 |
|
|
|
600 |
|
|
if (process.env.NODE_ENV !== "production") {
|
601 |
|
|
StaticRouter.propTypes = {
|
602 |
|
|
basename: PropTypes.string,
|
603 |
|
|
context: PropTypes.object,
|
604 |
|
|
location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
|
605 |
|
|
};
|
606 |
|
|
|
607 |
|
|
StaticRouter.prototype.componentDidMount = function () {
|
608 |
|
|
process.env.NODE_ENV !== "production" ? warning(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.") : void 0;
|
609 |
|
|
};
|
610 |
|
|
}
|
611 |
|
|
|
612 |
|
|
/**
|
613 |
|
|
* The public API for rendering the first <Route> that matches.
|
614 |
|
|
*/
|
615 |
|
|
|
616 |
|
|
var Switch =
|
617 |
|
|
/*#__PURE__*/
|
618 |
|
|
function (_React$Component) {
|
619 |
|
|
_inheritsLoose(Switch, _React$Component);
|
620 |
|
|
|
621 |
|
|
function Switch() {
|
622 |
|
|
return _React$Component.apply(this, arguments) || this;
|
623 |
|
|
}
|
624 |
|
|
|
625 |
|
|
var _proto = Switch.prototype;
|
626 |
|
|
|
627 |
|
|
_proto.render = function render() {
|
628 |
|
|
var _this = this;
|
629 |
|
|
|
630 |
|
|
return React.createElement(context.Consumer, null, function (context) {
|
631 |
|
|
!context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <Switch> outside a <Router>") : invariant(false) : void 0;
|
632 |
|
|
var location = _this.props.location || context.location;
|
633 |
|
|
var element, match; // We use React.Children.forEach instead of React.Children.toArray().find()
|
634 |
|
|
// here because toArray adds keys to all child elements and we do not want
|
635 |
|
|
// to trigger an unmount/remount for two <Route>s that render the same
|
636 |
|
|
// component at different URLs.
|
637 |
|
|
|
638 |
|
|
React.Children.forEach(_this.props.children, function (child) {
|
639 |
|
|
if (match == null && React.isValidElement(child)) {
|
640 |
|
|
element = child;
|
641 |
|
|
var path = child.props.path || child.props.from;
|
642 |
|
|
match = path ? matchPath(location.pathname, _extends({}, child.props, {
|
643 |
|
|
path: path
|
644 |
|
|
})) : context.match;
|
645 |
|
|
}
|
646 |
|
|
});
|
647 |
|
|
return match ? React.cloneElement(element, {
|
648 |
|
|
location: location,
|
649 |
|
|
computedMatch: match
|
650 |
|
|
}) : null;
|
651 |
|
|
});
|
652 |
|
|
};
|
653 |
|
|
|
654 |
|
|
return Switch;
|
655 |
|
|
}(React.Component);
|
656 |
|
|
|
657 |
|
|
if (process.env.NODE_ENV !== "production") {
|
658 |
|
|
Switch.propTypes = {
|
659 |
|
|
children: PropTypes.node,
|
660 |
|
|
location: PropTypes.object
|
661 |
|
|
};
|
662 |
|
|
|
663 |
|
|
Switch.prototype.componentDidUpdate = function (prevProps) {
|
664 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(this.props.location && !prevProps.location), '<Switch> elements should not change from uncontrolled to controlled (or vice versa). You initially used no "location" prop and then provided one on a subsequent render.') : void 0;
|
665 |
|
|
process.env.NODE_ENV !== "production" ? warning(!(!this.props.location && prevProps.location), '<Switch> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.') : void 0;
|
666 |
|
|
};
|
667 |
|
|
}
|
668 |
|
|
|
669 |
|
|
/**
|
670 |
|
|
* A public higher-order component to access the imperative API
|
671 |
|
|
*/
|
672 |
|
|
|
673 |
|
|
function withRouter(Component) {
|
674 |
|
|
var displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
|
675 |
|
|
|
676 |
|
|
var C = function C(props) {
|
677 |
|
|
var wrappedComponentRef = props.wrappedComponentRef,
|
678 |
|
|
remainingProps = _objectWithoutPropertiesLoose(props, ["wrappedComponentRef"]);
|
679 |
|
|
|
680 |
|
|
return React.createElement(context.Consumer, null, function (context) {
|
681 |
|
|
!context ? process.env.NODE_ENV !== "production" ? invariant(false, "You should not use <" + displayName + " /> outside a <Router>") : invariant(false) : void 0;
|
682 |
|
|
return React.createElement(Component, _extends({}, remainingProps, context, {
|
683 |
|
|
ref: wrappedComponentRef
|
684 |
|
|
}));
|
685 |
|
|
});
|
686 |
|
|
};
|
687 |
|
|
|
688 |
|
|
C.displayName = displayName;
|
689 |
|
|
C.WrappedComponent = Component;
|
690 |
|
|
|
691 |
|
|
if (process.env.NODE_ENV !== "production") {
|
692 |
|
|
C.propTypes = {
|
693 |
|
|
wrappedComponentRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object])
|
694 |
|
|
};
|
695 |
|
|
}
|
696 |
|
|
|
697 |
|
|
return hoistStatics(C, Component);
|
698 |
|
|
}
|
699 |
|
|
|
700 |
|
|
var useContext = React.useContext;
|
701 |
|
|
function useHistory() {
|
702 |
|
|
if (process.env.NODE_ENV !== "production") {
|
703 |
|
|
!(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useHistory()") : invariant(false) : void 0;
|
704 |
|
|
}
|
705 |
|
|
|
706 |
|
|
return useContext(context).history;
|
707 |
|
|
}
|
708 |
|
|
function useLocation() {
|
709 |
|
|
if (process.env.NODE_ENV !== "production") {
|
710 |
|
|
!(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useLocation()") : invariant(false) : void 0;
|
711 |
|
|
}
|
712 |
|
|
|
713 |
|
|
return useContext(context).location;
|
714 |
|
|
}
|
715 |
|
|
function useParams() {
|
716 |
|
|
if (process.env.NODE_ENV !== "production") {
|
717 |
|
|
!(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useParams()") : invariant(false) : void 0;
|
718 |
|
|
}
|
719 |
|
|
|
720 |
|
|
var match = useContext(context).match;
|
721 |
|
|
return match ? match.params : {};
|
722 |
|
|
}
|
723 |
|
|
function useRouteMatch(path) {
|
724 |
|
|
if (process.env.NODE_ENV !== "production") {
|
725 |
|
|
!(typeof useContext === "function") ? process.env.NODE_ENV !== "production" ? invariant(false, "You must use React >= 16.8 in order to use useRouteMatch()") : invariant(false) : void 0;
|
726 |
|
|
}
|
727 |
|
|
|
728 |
|
|
return path ? matchPath(useLocation().pathname, path) : useContext(context).match;
|
729 |
|
|
}
|
730 |
|
|
|
731 |
|
|
if (process.env.NODE_ENV !== "production") {
|
732 |
|
|
if (typeof window !== "undefined") {
|
733 |
|
|
var global = window;
|
734 |
|
|
var key = "__react_router_build__";
|
735 |
|
|
var buildNames = {
|
736 |
|
|
cjs: "CommonJS",
|
737 |
|
|
esm: "ES modules",
|
738 |
|
|
umd: "UMD"
|
739 |
|
|
};
|
740 |
|
|
|
741 |
|
|
if (global[key] && global[key] !== "esm") {
|
742 |
|
|
var initialBuildName = buildNames[global[key]];
|
743 |
|
|
var secondaryBuildName = buildNames["esm"]; // TODO: Add link to article that explains in detail how to avoid
|
744 |
|
|
// loading 2 different builds.
|
745 |
|
|
|
746 |
|
|
throw new Error("You are loading the " + secondaryBuildName + " build of React Router " + ("on a page that is already running the " + initialBuildName + " ") + "build, so things won't work right.");
|
747 |
|
|
}
|
748 |
|
|
|
749 |
|
|
global[key] = "esm";
|
750 |
|
|
}
|
751 |
|
|
}
|
752 |
|
|
|
753 |
|
|
export { MemoryRouter, Prompt, Redirect, Route, Router, StaticRouter, Switch, context as __RouterContext, generatePath, matchPath, useHistory, useLocation, useParams, useRouteMatch, withRouter };
|
754 |
|
|
//# sourceMappingURL=react-router.js.map
|