You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2. function _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; }
  3. function _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; }
  4. import React from "react";
  5. import PropTypes from "prop-types";
  6. import warning from "warning";
  7. import invariant from "invariant";
  8. import matchPath from "./matchPath";
  9. /**
  10. * The public API for rendering the first <Route> that matches.
  11. */
  12. var Switch = function (_React$Component) {
  13. _inherits(Switch, _React$Component);
  14. function Switch() {
  15. _classCallCheck(this, Switch);
  16. return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  17. }
  18. Switch.prototype.componentWillMount = function componentWillMount() {
  19. invariant(this.context.router, "You should not use <Switch> outside a <Router>");
  20. };
  21. Switch.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
  22. 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.');
  23. 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.');
  24. };
  25. Switch.prototype.render = function render() {
  26. var route = this.context.router.route;
  27. var children = this.props.children;
  28. var location = this.props.location || route.location;
  29. var match = void 0,
  30. child = void 0;
  31. React.Children.forEach(children, function (element) {
  32. if (match == null && React.isValidElement(element)) {
  33. var _element$props = element.props,
  34. pathProp = _element$props.path,
  35. exact = _element$props.exact,
  36. strict = _element$props.strict,
  37. sensitive = _element$props.sensitive,
  38. from = _element$props.from;
  39. var path = pathProp || from;
  40. child = element;
  41. match = matchPath(location.pathname, { path: path, exact: exact, strict: strict, sensitive: sensitive }, route.match);
  42. }
  43. });
  44. return match ? React.cloneElement(child, { location: location, computedMatch: match }) : null;
  45. };
  46. return Switch;
  47. }(React.Component);
  48. Switch.contextTypes = {
  49. router: PropTypes.shape({
  50. route: PropTypes.object.isRequired
  51. }).isRequired
  52. };
  53. Switch.propTypes = {
  54. children: PropTypes.node,
  55. location: PropTypes.object
  56. };
  57. export default Switch;