@krmao/react-basic
Version:
### features
76 lines (75 loc) • 3.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var basic_env_1 = (0, tslib_1.__importDefault)(require("./basic-env"));
var prop_types_1 = (0, tslib_1.__importDefault)(require("prop-types"));
var BasicErrorBoundary = /** @class */ (function (_super) {
(0, tslib_1.__extends)(BasicErrorBoundary, _super);
function BasicErrorBoundary(props) {
var _this = _super.call(this, props) || this;
_this.state = { error: undefined, errorInfo: undefined };
return _this;
}
BasicErrorBoundary.getDerivedStateFromError = function (error) {
return { error: error };
};
BasicErrorBoundary.prototype.componentDidCatch = function (error, errorInfo) {
this.setState({
error: error,
errorInfo: errorInfo
});
};
BasicErrorBoundary.prototype.render = function () {
var _a, _b;
if (this.state.errorInfo) {
if (basic_env_1.default.getApiEnv() === basic_env_1.default.PROD) {
var ReplaceComplement = this.props.replaceComplement;
if (!!ReplaceComplement) {
return react_1.default.createElement(ReplaceComplement, null);
}
else {
return react_1.default.createElement("h2", null, "Something went wrong.");
}
}
else {
return (react_1.default.createElement("div", { style: {
width: "100%",
height: "100%",
padding: 20,
background: "#e7e7e7"
} },
react_1.default.createElement("h2", null, "Something went wrong."),
react_1.default.createElement("details", { open: false, style: {
overflowY: "scroll",
overflowX: "scroll",
maxHeight: 200,
padding: 20,
whiteSpace: "pre-wrap"
} }, (_a = this.state.error) === null || _a === void 0 ? void 0 :
_a.toString(),
react_1.default.createElement("br", null), (_b = this.state.errorInfo) === null || _b === void 0 ? void 0 :
_b.componentStack)));
}
}
return this.props.children;
};
return BasicErrorBoundary;
}(react_1.default.Component));
exports.default = BasicErrorBoundary;
BasicErrorBoundary.propTypes = {
/**
* 生产环境显示的降级 UI
*
* const GoogleMapAddress = () => (
* <MapAddressGoogle
* centerLatLng={centerLatLng}
* zoomLevel={14}
* onMapClickListener={(e) => {
* this._onMapClickListener(e);
* }}
* />
* );
*/
replaceComplement: prop_types_1.default.oneOfType([prop_types_1.default.func])
};