UNPKG

@alicloud/cloud-charts

Version:

![](https://img.shields.io/npm/v/@alicloud/cloud-charts?color=%23ff8200)

155 lines (121 loc) 5.02 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports["default"] = void 0; var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose")); var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var React = _interopRequireWildcard(require("react")); var _ChartProvider = require("../ChartProvider"); // import { FullCrossName } from '../constants'; // import Wplaceholder from '../Wplaceholder'; /** * errorWrap 错误捕获HOC * * */ /*#__PURE__*/ function errorWrap(Component) { var ErrorBoundary = /*#__PURE__*/function (_React$Component) { (0, _inheritsLoose2["default"])(ErrorBoundary, _React$Component); function ErrorBoundary() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this; _this.state = { errorStack: null }; _this.chart = void 0; _this.chartDom = void 0; _this.chartId = void 0; _this.size = void 0; _this.oldReactRef = function (chartInstance) { if (chartInstance) { // 复制旧版本可能用到的属性 _this.chart = chartInstance.chart; _this.chartId = chartInstance.chartId; _this.chartDom = chartInstance.chartDom; _this.size = chartInstance.size; } }; return _this; } var _proto = ErrorBoundary.prototype; _proto.shouldComponentUpdate = function shouldComponentUpdate() { if (this.state.errorStack) { // 在update前重置error标记 // 为顾及React16.3之前的用户,因此没有将该逻辑放在getDerivedStateFromProps里 this.setState({ errorStack: null }); } return true; }; _proto.componentDidCatch = function componentDidCatch(error) /*, info*/ { // Display fallback UI this.setState({ errorStack: error.stack }); } // 低版本React中,复制可能用到的属性。 ; _proto.render = function render() { if (this.state.errorStack) { var _this$props; // const { className = '', style, height } = this.props; // @ts-ignore var _Component$contextTyp = Component.contextType._currentValue, language = _Component$contextTyp.language, locale = _Component$contextTyp.locale; // You can render any custom fallback UI // return <pre className={`${FullCrossName} widgets-error-info ${className}`} style={style}>{this.state.errorStack}</pre>; // return ( // <Wplaceholder // error // // locale={{ // // // todo: 国际化 // // error: '图表异常', // // }} // style={{ // height: height ? Number(height) : undefined, // }} // /> // ); // @ts-ignore return /*#__PURE__*/React.createElement(Component, (0, _extends2["default"])({}, this.props, { errorInfo: (0, _ChartProvider.getText)('error', ((_this$props = this.props) === null || _this$props === void 0 ? void 0 : _this$props.language) || language, locale) })); } var _this$props2 = this.props, _this$props2$forwarde = _this$props2.forwardedRef, forwardedRef = _this$props2$forwarde === void 0 ? this.oldReactRef : _this$props2$forwarde, rest = (0, _objectWithoutPropertiesLoose2["default"])(_this$props2, ["forwardedRef"]); // @ts-ignore 将自定义的 prop 属性 “forwardedRef” 定义为 ref return /*#__PURE__*/React.createElement(Component, (0, _extends2["default"])({ ref: forwardedRef }, rest)); }; return ErrorBoundary; }(React.Component); ErrorBoundary.isG2Chart = true; ErrorBoundary.displayName = Component.displayName; ErrorBoundary.defaultProps = Component.defaultProps; ErrorBoundary.RawChart = Component; if (React.forwardRef) { var forwardRefFunc = function forwardRefFunc(props, ref) { return /*#__PURE__*/React.createElement(ErrorBoundary, (0, _extends2["default"])({}, props, { forwardedRef: ref })); }; var result = /*#__PURE__*/React.forwardRef(forwardRefFunc); // @ts-ignore result.isG2Chart = true; // @ts-ignore result.RawChart = Component; result.displayName = Component.displayName; // result.propTypes = Component.propTypes; result.defaultProps = Component.defaultProps; return result; } return ErrorBoundary; } var _default = errorWrap; exports["default"] = _default;