react-router-4-compat
Version:
A wrapped version of react router 4 which compatible with react router 3
100 lines (78 loc) • 4.3 kB
JavaScript
;
exports.__esModule = true;
exports.default = void 0;
var _invariant = _interopRequireDefault(require("invariant"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _Route = _interopRequireDefault(require("react-router/Route"));
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _inheritsLoose(subClass, superClass) { subClass.prototype.__proto__ = superClass && superClass.prototype; subClass.__proto__ = superClass; }
var isStateless = function isStateless(Component) {
return !Component.prototype || !Component.prototype.render;
};
var withRouter4Compat = function withRouter4Compat(Component, _temp) {
var _ref = _temp === void 0 ? {} : _temp,
_ref$withRef = _ref.withRef,
withRef = _ref$withRef === void 0 ? false : _ref$withRef;
var C =
/*#__PURE__*/
function (_React$Component) {
function C() {
return _React$Component.apply(this, arguments) || this;
}
var _proto = C.prototype;
// noinspection JSUnusedGlobalSymbols
_proto.getWrappedInstance = function getWrappedInstance() {
(0, _invariant.default)(withRef, 'To access the wrapped instance, you need to specify ' + '`{ withRef: true}` as the second argument of the withRouter() call.');
return this.wrappedInstance;
};
_proto.render = function render() {
var _this = this;
var _this$props = this.props,
wrappedComponentRef = _this$props.wrappedComponentRef,
remainingProps = _objectWithoutProperties(_this$props, ["wrappedComponentRef"]);
var wrappedComponentRefCompat = wrappedComponentRef ? function (c) {
_this.wrappedInstance = c;
return wrappedComponentRef(c);
} : function (c) {
_this.wrappedInstance = c;
};
return _react.default.createElement(_Route.default, {
children: function children(routeComponentProps) {
var match = routeComponentProps.match,
location = routeComponentProps.location,
history = routeComponentProps.history;
return isStateless(Component) ? _react.default.createElement(Component, _extends({}, remainingProps, routeComponentProps, {
router: history,
routes: _this.context.routesCompat,
params: match.params,
location: location
})) : _react.default.createElement(Component, _extends({}, remainingProps, routeComponentProps, {
router: history,
routes: _this.context.routesCompat,
params: match.params,
location: location,
ref: wrappedComponentRefCompat
}));
}
});
};
_inheritsLoose(C, _React$Component);
return C;
}(_react.default.Component);
C.displayName = "withRouter(" + (Component.displayName || Component.name) + ")";
C.WrappedComponent = Component;
C.propTypes = {
wrappedComponentRef: _propTypes.default.func
};
C.contextTypes = {
routesCompat: _propTypes.default.array
};
return (0, _hoistNonReactStatics.default)(C, Component);
};
var _default = withRouter4Compat;
exports.default = _default;
module.exports = exports["default"];