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.
 
 
 
 

139 lines
5.8 KiB

  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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; }
  5. import warning from "warning";
  6. import invariant from "invariant";
  7. import React from "react";
  8. import PropTypes from "prop-types";
  9. import matchPath from "./matchPath";
  10. var isEmptyChildren = function isEmptyChildren(children) {
  11. return React.Children.count(children) === 0;
  12. };
  13. /**
  14. * The public API for matching a single path and rendering.
  15. */
  16. var Route = function (_React$Component) {
  17. _inherits(Route, _React$Component);
  18. function Route() {
  19. var _temp, _this, _ret;
  20. _classCallCheck(this, Route);
  21. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  22. args[_key] = arguments[_key];
  23. }
  24. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
  25. match: _this.computeMatch(_this.props, _this.context.router)
  26. }, _temp), _possibleConstructorReturn(_this, _ret);
  27. }
  28. Route.prototype.getChildContext = function getChildContext() {
  29. return {
  30. router: _extends({}, this.context.router, {
  31. route: {
  32. location: this.props.location || this.context.router.route.location,
  33. match: this.state.match
  34. }
  35. })
  36. };
  37. };
  38. Route.prototype.computeMatch = function computeMatch(_ref, router) {
  39. var computedMatch = _ref.computedMatch,
  40. location = _ref.location,
  41. path = _ref.path,
  42. strict = _ref.strict,
  43. exact = _ref.exact,
  44. sensitive = _ref.sensitive;
  45. if (computedMatch) return computedMatch; // <Switch> already computed the match for us
  46. invariant(router, "You should not use <Route> or withRouter() outside a <Router>");
  47. var route = router.route;
  48. var pathname = (location || route.location).pathname;
  49. return matchPath(pathname, { path: path, strict: strict, exact: exact, sensitive: sensitive }, route.match);
  50. };
  51. Route.prototype.componentWillMount = function componentWillMount() {
  52. 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");
  53. warning(!(this.props.component && this.props.children && !isEmptyChildren(this.props.children)), "You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored");
  54. warning(!(this.props.render && this.props.children && !isEmptyChildren(this.props.children)), "You should not use <Route render> and <Route children> in the same route; <Route children> will be ignored");
  55. };
  56. Route.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps, nextContext) {
  57. warning(!(nextProps.location && !this.props.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.');
  58. warning(!(!nextProps.location && this.props.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.');
  59. this.setState({
  60. match: this.computeMatch(nextProps, nextContext.router)
  61. });
  62. };
  63. Route.prototype.render = function render() {
  64. var match = this.state.match;
  65. var _props = this.props,
  66. children = _props.children,
  67. component = _props.component,
  68. render = _props.render;
  69. var _context$router = this.context.router,
  70. history = _context$router.history,
  71. route = _context$router.route,
  72. staticContext = _context$router.staticContext;
  73. var location = this.props.location || route.location;
  74. var props = { match: match, location: location, history: history, staticContext: staticContext };
  75. if (component) return match ? React.createElement(component, props) : null;
  76. if (render) return match ? render(props) : null;
  77. if (typeof children === "function") return children(props);
  78. if (children && !isEmptyChildren(children)) return React.Children.only(children);
  79. return null;
  80. };
  81. return Route;
  82. }(React.Component);
  83. Route.propTypes = {
  84. computedMatch: PropTypes.object, // private, from <Switch>
  85. path: PropTypes.string,
  86. exact: PropTypes.bool,
  87. strict: PropTypes.bool,
  88. sensitive: PropTypes.bool,
  89. component: PropTypes.func,
  90. render: PropTypes.func,
  91. children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
  92. location: PropTypes.object
  93. };
  94. Route.contextTypes = {
  95. router: PropTypes.shape({
  96. history: PropTypes.object.isRequired,
  97. route: PropTypes.object.isRequired,
  98. staticContext: PropTypes.object
  99. })
  100. };
  101. Route.childContextTypes = {
  102. router: PropTypes.object.isRequired
  103. };
  104. export default Route;