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