UNPKG

@alicloud/cloud-charts

Version:

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

141 lines (115 loc) 4.52 kB
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose"; import _extends from "@babel/runtime/helpers/extends"; import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as React from 'react'; // import { FullCrossName } from '../constants'; // import Wplaceholder from '../Wplaceholder'; import { getText } from '../ChartProvider'; /** * errorWrap 错误捕获HOC * * */ /*#__PURE__*/ function errorWrap(Component) { var ErrorBoundary = /*#__PURE__*/function (_React$Component) { _inheritsLoose(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, _extends({}, this.props, { errorInfo: 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 = _objectWithoutPropertiesLoose(_this$props2, ["forwardedRef"]); // @ts-ignore 将自定义的 prop 属性 “forwardedRef” 定义为 ref return /*#__PURE__*/React.createElement(Component, _extends({ 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, _extends({}, 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; } export default errorWrap;