1 | 'use strict';
|
2 |
|
3 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
4 |
|
5 | var React = _interopDefault(require('react'));
|
6 | var PropTypes = _interopDefault(require('prop-types'));
|
7 | var history = require('history');
|
8 | var warning = _interopDefault(require('tiny-warning'));
|
9 | var createContext = _interopDefault(require('mini-create-react-context'));
|
10 | var invariant = _interopDefault(require('tiny-invariant'));
|
11 | var pathToRegexp = _interopDefault(require('path-to-regexp'));
|
12 | var reactIs = require('react-is');
|
13 | var hoistStatics = _interopDefault(require('hoist-non-react-statics'));
|
14 |
|
15 | function _extends() {
|
16 | _extends = Object.assign || function (target) {
|
17 | for (var i = 1; i < arguments.length; i++) {
|
18 | var source = arguments[i];
|
19 |
|
20 | for (var key in source) {
|
21 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
22 | target[key] = source[key];
|
23 | }
|
24 | }
|
25 | }
|
26 |
|
27 | return target;
|
28 | };
|
29 |
|
30 | return _extends.apply(this, arguments);
|
31 | }
|
32 |
|
33 | function _inheritsLoose(subClass, superClass) {
|
34 | subClass.prototype = Object.create(superClass.prototype);
|
35 | subClass.prototype.constructor = subClass;
|
36 | subClass.__proto__ = superClass;
|
37 | }
|
38 |
|
39 | function _objectWithoutPropertiesLoose(source, excluded) {
|
40 | if (source == null) return {};
|
41 | var target = {};
|
42 | var sourceKeys = Object.keys(source);
|
43 | var key, i;
|
44 |
|
45 | for (i = 0; i < sourceKeys.length; i++) {
|
46 | key = sourceKeys[i];
|
47 | if (excluded.indexOf(key) >= 0) continue;
|
48 | target[key] = source[key];
|
49 | }
|
50 |
|
51 | return target;
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | var createNamedContext = function createNamedContext(name) {
|
57 | var context = createContext();
|
58 | context.displayName = name;
|
59 | return context;
|
60 | };
|
61 |
|
62 | var historyContext =
|
63 |
|
64 | createNamedContext("Router-History");
|
65 |
|
66 |
|
67 |
|
68 | var createNamedContext$1 = function createNamedContext(name) {
|
69 | var context = createContext();
|
70 | context.displayName = name;
|
71 | return context;
|
72 | };
|
73 |
|
74 | var context =
|
75 |
|
76 | createNamedContext$1("Router");
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | var Router =
|
83 |
|
84 | function (_React$Component) {
|
85 | _inheritsLoose(Router, _React$Component);
|
86 |
|
87 | Router.computeRootMatch = function computeRootMatch(pathname) {
|
88 | return {
|
89 | path: "/",
|
90 | url: "/",
|
91 | params: {},
|
92 | isExact: pathname === "/"
|
93 | };
|
94 | };
|
95 |
|
96 | function Router(props) {
|
97 | var _this;
|
98 |
|
99 | _this = _React$Component.call(this, props) || this;
|
100 | _this.state = {
|
101 | location: props.history.location
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | _this._isMounted = false;
|
109 | _this._pendingLocation = null;
|
110 |
|
111 | if (!props.staticContext) {
|
112 | _this.unlisten = props.history.listen(function (location) {
|
113 | if (_this._isMounted) {
|
114 | _this.setState({
|
115 | location: location
|
116 | });
|
117 | } else {
|
118 | _this._pendingLocation = location;
|
119 | }
|
120 | });
|
121 | }
|
122 |
|
123 | return _this;
|
124 | }
|
125 |
|
126 | var _proto = Router.prototype;
|
127 |
|
128 | _proto.componentDidMount = function componentDidMount() {
|
129 | this._isMounted = true;
|
130 |
|
131 | if (this._pendingLocation) {
|
132 | this.setState({
|
133 | location: this._pendingLocation
|
134 | });
|
135 | }
|
136 | };
|
137 |
|
138 | _proto.componentWillUnmount = function componentWillUnmount() {
|
139 | if (this.unlisten) this.unlisten();
|
140 | };
|
141 |
|
142 | _proto.render = function render() {
|
143 | return React.createElement(context.Provider, {
|
144 | value: {
|
145 | history: this.props.history,
|
146 | location: this.state.location,
|
147 | match: Router.computeRootMatch(this.state.location.pathname),
|
148 | staticContext: this.props.staticContext
|
149 | }
|
150 | }, React.createElement(historyContext.Provider, {
|
151 | children: this.props.children || null,
|
152 | value: this.props.history
|
153 | }));
|
154 | };
|
155 |
|
156 | return Router;
|
157 | }(React.Component);
|
158 |
|
159 | {
|
160 | Router.propTypes = {
|
161 | children: PropTypes.node,
|
162 | history: PropTypes.object.isRequired,
|
163 | staticContext: PropTypes.object
|
164 | };
|
165 |
|
166 | Router.prototype.componentDidUpdate = function (prevProps) {
|
167 | warning(prevProps.history === this.props.history, "You cannot change <Router history>") ;
|
168 | };
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | var MemoryRouter =
|
176 |
|
177 | function (_React$Component) {
|
178 | _inheritsLoose(MemoryRouter, _React$Component);
|
179 |
|
180 | function MemoryRouter() {
|
181 | var _this;
|
182 |
|
183 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
184 | args[_key] = arguments[_key];
|
185 | }
|
186 |
|
187 | _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
188 | _this.history = history.createMemoryHistory(_this.props);
|
189 | return _this;
|
190 | }
|
191 |
|
192 | var _proto = MemoryRouter.prototype;
|
193 |
|
194 | _proto.render = function render() {
|
195 | return React.createElement(Router, {
|
196 | history: this.history,
|
197 | children: this.props.children
|
198 | });
|
199 | };
|
200 |
|
201 | return MemoryRouter;
|
202 | }(React.Component);
|
203 |
|
204 | {
|
205 | MemoryRouter.propTypes = {
|
206 | initialEntries: PropTypes.array,
|
207 | initialIndex: PropTypes.number,
|
208 | getUserConfirmation: PropTypes.func,
|
209 | keyLength: PropTypes.number,
|
210 | children: PropTypes.node
|
211 | };
|
212 |
|
213 | MemoryRouter.prototype.componentDidMount = function () {
|
214 | warning(!this.props.history, "<MemoryRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { MemoryRouter as Router }`.") ;
|
215 | };
|
216 | }
|
217 |
|
218 | var Lifecycle =
|
219 |
|
220 | function (_React$Component) {
|
221 | _inheritsLoose(Lifecycle, _React$Component);
|
222 |
|
223 | function Lifecycle() {
|
224 | return _React$Component.apply(this, arguments) || this;
|
225 | }
|
226 |
|
227 | var _proto = Lifecycle.prototype;
|
228 |
|
229 | _proto.componentDidMount = function componentDidMount() {
|
230 | if (this.props.onMount) this.props.onMount.call(this, this);
|
231 | };
|
232 |
|
233 | _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
234 | if (this.props.onUpdate) this.props.onUpdate.call(this, this, prevProps);
|
235 | };
|
236 |
|
237 | _proto.componentWillUnmount = function componentWillUnmount() {
|
238 | if (this.props.onUnmount) this.props.onUnmount.call(this, this);
|
239 | };
|
240 |
|
241 | _proto.render = function render() {
|
242 | return null;
|
243 | };
|
244 |
|
245 | return Lifecycle;
|
246 | }(React.Component);
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | function Prompt(_ref) {
|
253 | var message = _ref.message,
|
254 | _ref$when = _ref.when,
|
255 | when = _ref$when === void 0 ? true : _ref$when;
|
256 | return React.createElement(context.Consumer, null, function (context) {
|
257 | !context ? invariant(false, "You should not use <Prompt> outside a <Router>") : void 0;
|
258 | if (!when || context.staticContext) return null;
|
259 | var method = context.history.block;
|
260 | return React.createElement(Lifecycle, {
|
261 | onMount: function onMount(self) {
|
262 | self.release = method(message);
|
263 | },
|
264 | onUpdate: function onUpdate(self, prevProps) {
|
265 | if (prevProps.message !== message) {
|
266 | self.release();
|
267 | self.release = method(message);
|
268 | }
|
269 | },
|
270 | onUnmount: function onUnmount(self) {
|
271 | self.release();
|
272 | },
|
273 | message: message
|
274 | });
|
275 | });
|
276 | }
|
277 |
|
278 | {
|
279 | var messageType = PropTypes.oneOfType([PropTypes.func, PropTypes.string]);
|
280 | Prompt.propTypes = {
|
281 | when: PropTypes.bool,
|
282 | message: messageType.isRequired
|
283 | };
|
284 | }
|
285 |
|
286 | var cache = {};
|
287 | var cacheLimit = 10000;
|
288 | var cacheCount = 0;
|
289 |
|
290 | function compilePath(path) {
|
291 | if (cache[path]) return cache[path];
|
292 | var generator = pathToRegexp.compile(path);
|
293 |
|
294 | if (cacheCount < cacheLimit) {
|
295 | cache[path] = generator;
|
296 | cacheCount++;
|
297 | }
|
298 |
|
299 | return generator;
|
300 | }
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | function generatePath(path, params) {
|
307 | if (path === void 0) {
|
308 | path = "/";
|
309 | }
|
310 |
|
311 | if (params === void 0) {
|
312 | params = {};
|
313 | }
|
314 |
|
315 | return path === "/" ? path : compilePath(path)(params, {
|
316 | pretty: true
|
317 | });
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | function Redirect(_ref) {
|
325 | var computedMatch = _ref.computedMatch,
|
326 | to = _ref.to,
|
327 | _ref$push = _ref.push,
|
328 | push = _ref$push === void 0 ? false : _ref$push;
|
329 | return React.createElement(context.Consumer, null, function (context) {
|
330 | !context ? invariant(false, "You should not use <Redirect> outside a <Router>") : void 0;
|
331 | var history$1 = context.history,
|
332 | staticContext = context.staticContext;
|
333 | var method = push ? history$1.push : history$1.replace;
|
334 | var location = history.createLocation(computedMatch ? typeof to === "string" ? generatePath(to, computedMatch.params) : _extends({}, to, {
|
335 | pathname: generatePath(to.pathname, computedMatch.params)
|
336 | }) : to);
|
337 |
|
338 |
|
339 | if (staticContext) {
|
340 | method(location);
|
341 | return null;
|
342 | }
|
343 |
|
344 | return React.createElement(Lifecycle, {
|
345 | onMount: function onMount() {
|
346 | method(location);
|
347 | },
|
348 | onUpdate: function onUpdate(self, prevProps) {
|
349 | var prevLocation = history.createLocation(prevProps.to);
|
350 |
|
351 | if (!history.locationsAreEqual(prevLocation, _extends({}, location, {
|
352 | key: prevLocation.key
|
353 | }))) {
|
354 | method(location);
|
355 | }
|
356 | },
|
357 | to: to
|
358 | });
|
359 | });
|
360 | }
|
361 |
|
362 | {
|
363 | Redirect.propTypes = {
|
364 | push: PropTypes.bool,
|
365 | from: PropTypes.string,
|
366 | to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]).isRequired
|
367 | };
|
368 | }
|
369 |
|
370 | var cache$1 = {};
|
371 | var cacheLimit$1 = 10000;
|
372 | var cacheCount$1 = 0;
|
373 |
|
374 | function compilePath$1(path, options) {
|
375 | var cacheKey = "" + options.end + options.strict + options.sensitive;
|
376 | var pathCache = cache$1[cacheKey] || (cache$1[cacheKey] = {});
|
377 | if (pathCache[path]) return pathCache[path];
|
378 | var keys = [];
|
379 | var regexp = pathToRegexp(path, keys, options);
|
380 | var result = {
|
381 | regexp: regexp,
|
382 | keys: keys
|
383 | };
|
384 |
|
385 | if (cacheCount$1 < cacheLimit$1) {
|
386 | pathCache[path] = result;
|
387 | cacheCount$1++;
|
388 | }
|
389 |
|
390 | return result;
|
391 | }
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | function matchPath(pathname, options) {
|
398 | if (options === void 0) {
|
399 | options = {};
|
400 | }
|
401 |
|
402 | if (typeof options === "string" || Array.isArray(options)) {
|
403 | options = {
|
404 | path: options
|
405 | };
|
406 | }
|
407 |
|
408 | var _options = options,
|
409 | path = _options.path,
|
410 | _options$exact = _options.exact,
|
411 | exact = _options$exact === void 0 ? false : _options$exact,
|
412 | _options$strict = _options.strict,
|
413 | strict = _options$strict === void 0 ? false : _options$strict,
|
414 | _options$sensitive = _options.sensitive,
|
415 | sensitive = _options$sensitive === void 0 ? false : _options$sensitive;
|
416 | var paths = [].concat(path);
|
417 | return paths.reduce(function (matched, path) {
|
418 | if (!path && path !== "") return null;
|
419 | if (matched) return matched;
|
420 |
|
421 | var _compilePath = compilePath$1(path, {
|
422 | end: exact,
|
423 | strict: strict,
|
424 | sensitive: sensitive
|
425 | }),
|
426 | regexp = _compilePath.regexp,
|
427 | keys = _compilePath.keys;
|
428 |
|
429 | var match = regexp.exec(pathname);
|
430 | if (!match) return null;
|
431 | var url = match[0],
|
432 | values = match.slice(1);
|
433 | var isExact = pathname === url;
|
434 | if (exact && !isExact) return null;
|
435 | return {
|
436 | path: path,
|
437 |
|
438 | url: path === "/" && url === "" ? "/" : url,
|
439 |
|
440 | isExact: isExact,
|
441 |
|
442 | params: keys.reduce(function (memo, key, index) {
|
443 | memo[key.name] = values[index];
|
444 | return memo;
|
445 | }, {})
|
446 | };
|
447 | }, null);
|
448 | }
|
449 |
|
450 | function isEmptyChildren(children) {
|
451 | return React.Children.count(children) === 0;
|
452 | }
|
453 |
|
454 | function evalChildrenDev(children, props, path) {
|
455 | var value = children(props);
|
456 | 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`") ;
|
457 | return value || null;
|
458 | }
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 | var Route =
|
465 |
|
466 | function (_React$Component) {
|
467 | _inheritsLoose(Route, _React$Component);
|
468 |
|
469 | function Route() {
|
470 | return _React$Component.apply(this, arguments) || this;
|
471 | }
|
472 |
|
473 | var _proto = Route.prototype;
|
474 |
|
475 | _proto.render = function render() {
|
476 | var _this = this;
|
477 |
|
478 | return React.createElement(context.Consumer, null, function (context$1) {
|
479 | !context$1 ? invariant(false, "You should not use <Route> outside a <Router>") : void 0;
|
480 | var location = _this.props.location || context$1.location;
|
481 | var match = _this.props.computedMatch ? _this.props.computedMatch
|
482 | : _this.props.path ? matchPath(location.pathname, _this.props) : context$1.match;
|
483 |
|
484 | var props = _extends({}, context$1, {
|
485 | location: location,
|
486 | match: match
|
487 | });
|
488 |
|
489 | var _this$props = _this.props,
|
490 | children = _this$props.children,
|
491 | component = _this$props.component,
|
492 | render = _this$props.render;
|
493 |
|
494 |
|
495 | if (Array.isArray(children) && children.length === 0) {
|
496 | children = null;
|
497 | }
|
498 |
|
499 | return React.createElement(context.Provider, {
|
500 | value: props
|
501 | }, props.match ? children ? typeof children === "function" ? evalChildrenDev(children, props, _this.props.path) : children : component ? React.createElement(component, props) : render ? render(props) : null : typeof children === "function" ? evalChildrenDev(children, props, _this.props.path) : null);
|
502 | });
|
503 | };
|
504 |
|
505 | return Route;
|
506 | }(React.Component);
|
507 |
|
508 | {
|
509 | Route.propTypes = {
|
510 | children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
|
511 | component: function component(props, propName) {
|
512 | if (props[propName] && !reactIs.isValidElementType(props[propName])) {
|
513 | return new Error("Invalid prop 'component' supplied to 'Route': the prop is not a valid React component");
|
514 | }
|
515 | },
|
516 | exact: PropTypes.bool,
|
517 | location: PropTypes.object,
|
518 | path: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
519 | render: PropTypes.func,
|
520 | sensitive: PropTypes.bool,
|
521 | strict: PropTypes.bool
|
522 | };
|
523 |
|
524 | Route.prototype.componentDidMount = function () {
|
525 | 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") ;
|
526 | 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") ;
|
527 | 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") ;
|
528 | };
|
529 |
|
530 | Route.prototype.componentDidUpdate = function (prevProps) {
|
531 | 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.') ;
|
532 | 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.') ;
|
533 | };
|
534 | }
|
535 |
|
536 | function addLeadingSlash(path) {
|
537 | return path.charAt(0) === "/" ? path : "/" + path;
|
538 | }
|
539 |
|
540 | function addBasename(basename, location) {
|
541 | if (!basename) return location;
|
542 | return _extends({}, location, {
|
543 | pathname: addLeadingSlash(basename) + location.pathname
|
544 | });
|
545 | }
|
546 |
|
547 | function stripBasename(basename, location) {
|
548 | if (!basename) return location;
|
549 | var base = addLeadingSlash(basename);
|
550 | if (location.pathname.indexOf(base) !== 0) return location;
|
551 | return _extends({}, location, {
|
552 | pathname: location.pathname.substr(base.length)
|
553 | });
|
554 | }
|
555 |
|
556 | function createURL(location) {
|
557 | return typeof location === "string" ? location : history.createPath(location);
|
558 | }
|
559 |
|
560 | function staticHandler(methodName) {
|
561 | return function () {
|
562 | invariant(false, "You cannot %s with <StaticRouter>", methodName) ;
|
563 | };
|
564 | }
|
565 |
|
566 | function noop() {}
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 | var StaticRouter =
|
576 |
|
577 | function (_React$Component) {
|
578 | _inheritsLoose(StaticRouter, _React$Component);
|
579 |
|
580 | function StaticRouter() {
|
581 | var _this;
|
582 |
|
583 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
584 | args[_key] = arguments[_key];
|
585 | }
|
586 |
|
587 | _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
588 |
|
589 | _this.handlePush = function (location) {
|
590 | return _this.navigateTo(location, "PUSH");
|
591 | };
|
592 |
|
593 | _this.handleReplace = function (location) {
|
594 | return _this.navigateTo(location, "REPLACE");
|
595 | };
|
596 |
|
597 | _this.handleListen = function () {
|
598 | return noop;
|
599 | };
|
600 |
|
601 | _this.handleBlock = function () {
|
602 | return noop;
|
603 | };
|
604 |
|
605 | return _this;
|
606 | }
|
607 |
|
608 | var _proto = StaticRouter.prototype;
|
609 |
|
610 | _proto.navigateTo = function navigateTo(location, action) {
|
611 | var _this$props = this.props,
|
612 | _this$props$basename = _this$props.basename,
|
613 | basename = _this$props$basename === void 0 ? "" : _this$props$basename,
|
614 | _this$props$context = _this$props.context,
|
615 | context = _this$props$context === void 0 ? {} : _this$props$context;
|
616 | context.action = action;
|
617 | context.location = addBasename(basename, history.createLocation(location));
|
618 | context.url = createURL(context.location);
|
619 | };
|
620 |
|
621 | _proto.render = function render() {
|
622 | var _this$props2 = this.props,
|
623 | _this$props2$basename = _this$props2.basename,
|
624 | basename = _this$props2$basename === void 0 ? "" : _this$props2$basename,
|
625 | _this$props2$context = _this$props2.context,
|
626 | context = _this$props2$context === void 0 ? {} : _this$props2$context,
|
627 | _this$props2$location = _this$props2.location,
|
628 | location = _this$props2$location === void 0 ? "/" : _this$props2$location,
|
629 | rest = _objectWithoutPropertiesLoose(_this$props2, ["basename", "context", "location"]);
|
630 |
|
631 | var history$1 = {
|
632 | createHref: function createHref(path) {
|
633 | return addLeadingSlash(basename + createURL(path));
|
634 | },
|
635 | action: "POP",
|
636 | location: stripBasename(basename, history.createLocation(location)),
|
637 | push: this.handlePush,
|
638 | replace: this.handleReplace,
|
639 | go: staticHandler("go"),
|
640 | goBack: staticHandler("goBack"),
|
641 | goForward: staticHandler("goForward"),
|
642 | listen: this.handleListen,
|
643 | block: this.handleBlock
|
644 | };
|
645 | return React.createElement(Router, _extends({}, rest, {
|
646 | history: history$1,
|
647 | staticContext: context
|
648 | }));
|
649 | };
|
650 |
|
651 | return StaticRouter;
|
652 | }(React.Component);
|
653 |
|
654 | {
|
655 | StaticRouter.propTypes = {
|
656 | basename: PropTypes.string,
|
657 | context: PropTypes.object,
|
658 | location: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
|
659 | };
|
660 |
|
661 | StaticRouter.prototype.componentDidMount = function () {
|
662 | warning(!this.props.history, "<StaticRouter> ignores the history prop. To use a custom history, " + "use `import { Router }` instead of `import { StaticRouter as Router }`.") ;
|
663 | };
|
664 | }
|
665 |
|
666 |
|
667 |
|
668 |
|
669 |
|
670 | var Switch =
|
671 |
|
672 | function (_React$Component) {
|
673 | _inheritsLoose(Switch, _React$Component);
|
674 |
|
675 | function Switch() {
|
676 | return _React$Component.apply(this, arguments) || this;
|
677 | }
|
678 |
|
679 | var _proto = Switch.prototype;
|
680 |
|
681 | _proto.render = function render() {
|
682 | var _this = this;
|
683 |
|
684 | return React.createElement(context.Consumer, null, function (context) {
|
685 | !context ? invariant(false, "You should not use <Switch> outside a <Router>") : void 0;
|
686 | var location = _this.props.location || context.location;
|
687 | var element, match;
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | React.Children.forEach(_this.props.children, function (child) {
|
693 | if (match == null && React.isValidElement(child)) {
|
694 | element = child;
|
695 | var path = child.props.path || child.props.from;
|
696 | match = path ? matchPath(location.pathname, _extends({}, child.props, {
|
697 | path: path
|
698 | })) : context.match;
|
699 | }
|
700 | });
|
701 | return match ? React.cloneElement(element, {
|
702 | location: location,
|
703 | computedMatch: match
|
704 | }) : null;
|
705 | });
|
706 | };
|
707 |
|
708 | return Switch;
|
709 | }(React.Component);
|
710 |
|
711 | {
|
712 | Switch.propTypes = {
|
713 | children: PropTypes.node,
|
714 | location: PropTypes.object
|
715 | };
|
716 |
|
717 | Switch.prototype.componentDidUpdate = function (prevProps) {
|
718 | 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.') ;
|
719 | 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.') ;
|
720 | };
|
721 | }
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 | function withRouter(Component) {
|
728 | var displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
|
729 |
|
730 | var C = function C(props) {
|
731 | var wrappedComponentRef = props.wrappedComponentRef,
|
732 | remainingProps = _objectWithoutPropertiesLoose(props, ["wrappedComponentRef"]);
|
733 |
|
734 | return React.createElement(context.Consumer, null, function (context) {
|
735 | !context ? invariant(false, "You should not use <" + displayName + " /> outside a <Router>") : void 0;
|
736 | return React.createElement(Component, _extends({}, remainingProps, context, {
|
737 | ref: wrappedComponentRef
|
738 | }));
|
739 | });
|
740 | };
|
741 |
|
742 | C.displayName = displayName;
|
743 | C.WrappedComponent = Component;
|
744 |
|
745 | {
|
746 | C.propTypes = {
|
747 | wrappedComponentRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func, PropTypes.object])
|
748 | };
|
749 | }
|
750 |
|
751 | return hoistStatics(C, Component);
|
752 | }
|
753 |
|
754 | var useContext = React.useContext;
|
755 | function useHistory() {
|
756 | {
|
757 | !(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useHistory()") : void 0;
|
758 | }
|
759 |
|
760 | return useContext(historyContext);
|
761 | }
|
762 | function useLocation() {
|
763 | {
|
764 | !(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useLocation()") : void 0;
|
765 | }
|
766 |
|
767 | return useContext(context).location;
|
768 | }
|
769 | function useParams() {
|
770 | {
|
771 | !(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useParams()") : void 0;
|
772 | }
|
773 |
|
774 | var match = useContext(context).match;
|
775 | return match ? match.params : {};
|
776 | }
|
777 | function useRouteMatch(path) {
|
778 | {
|
779 | !(typeof useContext === "function") ? invariant(false, "You must use React >= 16.8 in order to use useRouteMatch()") : void 0;
|
780 | }
|
781 |
|
782 | var location = useLocation();
|
783 | var match = useContext(context).match;
|
784 | return path ? matchPath(location.pathname, path) : match;
|
785 | }
|
786 |
|
787 | {
|
788 | if (typeof window !== "undefined") {
|
789 | var global = window;
|
790 | var key = "__react_router_build__";
|
791 | var buildNames = {
|
792 | cjs: "CommonJS",
|
793 | esm: "ES modules",
|
794 | umd: "UMD"
|
795 | };
|
796 |
|
797 | if (global[key] && global[key] !== "cjs") {
|
798 | var initialBuildName = buildNames[global[key]];
|
799 | var secondaryBuildName = buildNames["cjs"];
|
800 |
|
801 |
|
802 | 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.");
|
803 | }
|
804 |
|
805 | global[key] = "cjs";
|
806 | }
|
807 | }
|
808 |
|
809 | exports.MemoryRouter = MemoryRouter;
|
810 | exports.Prompt = Prompt;
|
811 | exports.Redirect = Redirect;
|
812 | exports.Route = Route;
|
813 | exports.Router = Router;
|
814 | exports.StaticRouter = StaticRouter;
|
815 | exports.Switch = Switch;
|
816 | exports.__HistoryContext = historyContext;
|
817 | exports.__RouterContext = context;
|
818 | exports.generatePath = generatePath;
|
819 | exports.matchPath = matchPath;
|
820 | exports.useHistory = useHistory;
|
821 | exports.useLocation = useLocation;
|
822 | exports.useParams = useParams;
|
823 | exports.useRouteMatch = useRouteMatch;
|
824 | exports.withRouter = withRouter;
|
825 |
|