119 lines
4.2 KiB
JavaScript
119 lines
4.2 KiB
JavaScript
"use strict";
|
|
|
|
exports.__esModule = true;
|
|
|
|
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; };
|
|
|
|
var _warning = require("warning");
|
|
|
|
var _warning2 = _interopRequireDefault(_warning);
|
|
|
|
var _invariant = require("invariant");
|
|
|
|
var _invariant2 = _interopRequireDefault(_invariant);
|
|
|
|
var _react = require("react");
|
|
|
|
var _react2 = _interopRequireDefault(_react);
|
|
|
|
var _propTypes = require("prop-types");
|
|
|
|
var _propTypes2 = _interopRequireDefault(_propTypes);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
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; }
|
|
|
|
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; }
|
|
|
|
/**
|
|
* The public API for putting history on context.
|
|
*/
|
|
var Router = function (_React$Component) {
|
|
_inherits(Router, _React$Component);
|
|
|
|
function Router() {
|
|
var _temp, _this, _ret;
|
|
|
|
_classCallCheck(this, Router);
|
|
|
|
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = {
|
|
match: _this.computeMatch(_this.props.history.location.pathname)
|
|
}, _temp), _possibleConstructorReturn(_this, _ret);
|
|
}
|
|
|
|
Router.prototype.getChildContext = function getChildContext() {
|
|
return {
|
|
router: _extends({}, this.context.router, {
|
|
history: this.props.history,
|
|
route: {
|
|
location: this.props.history.location,
|
|
match: this.state.match
|
|
}
|
|
})
|
|
};
|
|
};
|
|
|
|
Router.prototype.computeMatch = function computeMatch(pathname) {
|
|
return {
|
|
path: "/",
|
|
url: "/",
|
|
params: {},
|
|
isExact: pathname === "/"
|
|
};
|
|
};
|
|
|
|
Router.prototype.componentWillMount = function componentWillMount() {
|
|
var _this2 = this;
|
|
|
|
var _props = this.props,
|
|
children = _props.children,
|
|
history = _props.history;
|
|
|
|
|
|
(0, _invariant2.default)(children == null || _react2.default.Children.count(children) === 1, "A <Router> may have only one child element");
|
|
|
|
// Do this here so we can setState when a <Redirect> changes the
|
|
// location in componentWillMount. This happens e.g. when doing
|
|
// server rendering using a <StaticRouter>.
|
|
this.unlisten = history.listen(function () {
|
|
_this2.setState({
|
|
match: _this2.computeMatch(history.location.pathname)
|
|
});
|
|
});
|
|
};
|
|
|
|
Router.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
|
(0, _warning2.default)(this.props.history === nextProps.history, "You cannot change <Router history>");
|
|
};
|
|
|
|
Router.prototype.componentWillUnmount = function componentWillUnmount() {
|
|
this.unlisten();
|
|
};
|
|
|
|
Router.prototype.render = function render() {
|
|
var children = this.props.children;
|
|
|
|
return children ? _react2.default.Children.only(children) : null;
|
|
};
|
|
|
|
return Router;
|
|
}(_react2.default.Component);
|
|
|
|
Router.propTypes = {
|
|
history: _propTypes2.default.object.isRequired,
|
|
children: _propTypes2.default.node
|
|
};
|
|
Router.contextTypes = {
|
|
router: _propTypes2.default.object
|
|
};
|
|
Router.childContextTypes = {
|
|
router: _propTypes2.default.object.isRequired
|
|
};
|
|
exports.default = Router; |