UNPKG

router-guard

Version:

A higher-order component for react-router v4 that implements routing guards

227 lines (193 loc) 6.33 kB
/** * Bundle of router-guard * Generated: 2020-04-08 * Version: 2.0.0 * License: MIT * Author: 2631541504@qq.com */ import React from 'react'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _typeof2(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } function _typeof(obj) { if (typeof Symbol === "function" && _typeof2(Symbol.iterator) === "symbol") { _typeof = function _typeof(obj) { return _typeof2(obj); }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : _typeof2(obj); }; } return _typeof(obj); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } /** * The meta info of the route * */ var Status; (function (Status) { Status[Status["Pending"] = 0] = "Pending"; Status[Status["Resolved"] = 1] = "Resolved"; })(Status || (Status = {})); function getPathStr(location) { var _location$pathname = location.pathname, pathname = _location$pathname === void 0 ? '/' : _location$pathname, _location$search = location.search, search = _location$search === void 0 ? '' : _location$search, _location$hash = location.hash, hash = _location$hash === void 0 ? '' : _location$hash; return pathname + search + hash; } function pathEqual(path, location) { if (typeof path === 'string') return path === getPathStr(location); return getPathStr(path) === getPathStr(location); } /** * @param PageComponent * The component you truly want to render * * @param meta * * @param guard * The guard that decides which route to take * * default: RouterGuard.guard * * @param pendingPlaceholder * The placeholder shows until the guard call the function `next` * * default: RouterGuard.pendingPlaceholder * * @return If guard is null, return PageComponent directly * else return a wrapped guard component * */ function RouterGuard(PageComponent, meta) { var _temp; var guard = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : RouterGuard.guard; var pendingPlaceholder = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : RouterGuard.pendingPlaceholder; if (!guard) return PageComponent; return _temp = /*#__PURE__*/ function (_React$Component) { _inherits(_temp, _React$Component); function _temp(props) { var _this; _classCallCheck(this, _temp); _this = _possibleConstructorReturn(this, _getPrototypeOf(_temp).call(this, props)); _this.state = { status: Status.Pending }; _this.$isNextCalled = false; _this.$isMounted = false; var $guard = guard; $guard(Object.assign({}, props, { meta: meta || {} }), _this.next(function (state) { if (_this.$isMounted) _this.setState(state);else _this.state = state; })); return _this; } _createClass(_temp, [{ key: "componentDidMount", value: function componentDidMount() { this.$isMounted = true; } }, { key: "next", value: function next(setState) { var _this2 = this; var set = setState || this.setState.bind(this); return function (path, replace) { if (_this2.$isNextCalled) return; if (!path || pathEqual(path, _this2.props.location)) { set({ status: Status.Resolved }); } else if (replace) { _this2.props.history.replace(path); } else { _this2.props.history.push(path); } _this2.$isNextCalled = true; }; } }, { key: "render", value: function render() { var props = this.props, state = this.state; return state.status === Status.Pending ? pendingPlaceholder && pendingPlaceholder(props, meta) : PageComponent ? React.createElement(PageComponent, props) : React.createElement(React.Fragment, null); } }]); return _temp; }(React.Component), _temp; } RouterGuard.guard = null; RouterGuard.pendingPlaceholder = null; export { RouterGuard, Status };