UNPKG

fullts

Version:

Full stack framework in TypeScript, based on TSRPC.

115 lines (114 loc) 4.5 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); } return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import * as React from 'react'; var FulltsRouteRender = /** @class */ (function (_super) { __extends(FulltsRouteRender, _super); function FulltsRouteRender(props, context) { var _this = _super.call(this, props, context) || this; _this.renderId = 0; _this.loadedComponentClass = {}; _this.state = {}; return _this; } FulltsRouteRender.prototype.componentWillMount = function () { if (this.isDynamicImport(this.props.route.component)) { this.loadComponentClass(this.props.route.component); } else { //not function this.setState({ componentClass: this.props.route.component }); } }; FulltsRouteRender.prototype.componentWillUpdate = function (nextProps, nextState) { if (this.props != nextProps) { if (this.isDynamicImport(nextProps.route.component)) { var cacheKey = nextProps.route.component.toString(); var cache = this.loadedComponentClass[cacheKey]; if (cache) { nextState.componentClass = cache; } else { nextState.componentClass = undefined; this.loadComponentClass(nextProps.route.component); } } else { nextState.componentClass = nextProps.route.component; } } }; FulltsRouteRender.prototype.loadComponentClass = function (func) { var _this = this; func().then(function (v) { _this.loadedComponentClass[func.toString()] = v.default; _this.setState({ componentClass: v.default }); }); }; FulltsRouteRender.prototype.isDynamicImport = function (component) { var result; try { result = component(); } catch (_a) { return false; } if (result && result.then) { return true; } else { return false; } }; FulltsRouteRender.prototype.render = function () { if (this.props.route.layout) { //Layout return (React.createElement(FulltsRouteRender, { app: this.props.app, route: { path: this.props.route.path, component: this.props.route.layout }, routeProps: this.props.routeProps, thisIsLayout: true }, this.state.componentClass ? React.createElement(this.state.componentClass, __assign({ key: this.props.app.config.alwaysRemount ? ++this.renderId : this.renderId }, this.props.routeProps), this.props.children) : null)); } else { //非Layout var key = void 0; if (this.props.thisIsLayout) { key = this.props.app.config.alwaysRemountLayout ? ++this.renderId : this.renderId; } else { key = this.props.app.config.alwaysRemount ? ++this.renderId : this.renderId; } return this.state.componentClass ? React.createElement(this.state.componentClass, __assign({ key: key }, this.props.routeProps), this.props.children) : null; } }; return FulltsRouteRender; }(React.Component)); export default FulltsRouteRender;