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.
 
 
 
 

157 lines
6.5 KiB

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