UNPKG

3.29 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
4
5function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
6
7import React from 'react';
8import PropTypes from 'prop-types';
9import warning from 'warning';
10import invariant from 'invariant';
11import matchPath from './matchPath';
12
13/**
14 * The public API for rendering the first <Route> that matches.
15 */
16
17var Switch = function (_React$Component) {
18 _inherits(Switch, _React$Component);
19
20 function Switch() {
21 _classCallCheck(this, Switch);
22
23 return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
24 }
25
26 Switch.prototype.componentWillMount = function componentWillMount() {
27 invariant(this.context.router, 'You should not use <Switch> outside a <Router>');
28 };
29
30 Switch.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
31 warning(!(nextProps.location && !this.props.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.');
32
33 warning(!(!nextProps.location && this.props.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.');
34 };
35
36 Switch.prototype.render = function render() {
37 var route = this.context.router.route;
38 var children = this.props.children;
39
40 var location = this.props.location || route.location;
41
42 var match = void 0,
43 child = void 0;
44 React.Children.forEach(children, function (element) {
45 if (!React.isValidElement(element)) return;
46
47 var _element$props = element.props,
48 pathProp = _element$props.path,
49 exact = _element$props.exact,
50 strict = _element$props.strict,
51 sensitive = _element$props.sensitive,
52 from = _element$props.from;
53
54 var path = pathProp || from;
55
56 if (match == null) {
57 child = element;
58 match = path ? matchPath(location.pathname, { path: path, exact: exact, strict: strict, sensitive: sensitive }) : route.match;
59 }
60 });
61
62 return match ? React.cloneElement(child, { location: location, computedMatch: match }) : null;
63 };
64
65 return Switch;
66}(React.Component);
67
68Switch.contextTypes = {
69 router: PropTypes.shape({
70 route: PropTypes.object.isRequired
71 }).isRequired
72};
73Switch.propTypes = {
74 children: PropTypes.node,
75 location: PropTypes.object
76};
77
78
79export default Switch;
\No newline at end of file