UNPKG

@momentum-ui/react

Version:

Cisco Momentum UI framework for ReactJs applications

81 lines (65 loc) 2.71 kB
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } /** @component error-boundary */ import React from 'react'; import PropTypes from 'prop-types'; 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 = { error: null, errorInfo: null }; return _this; } var _proto = ErrorBoundary.prototype; _proto.componentDidCatch = function componentDidCatch(error, errorInfo) { this.setState({ error: error, errorInfo: errorInfo }); if (this.props.errorCallback) { this.props.errorCallback(error, errorInfo); } }; _proto.render = function render() { var _this$state = this.state, error = _this$state.error, errorInfo = _this$state.errorInfo; var _this$props = this.props, children = _this$props.children, fallbackComponent = _this$props.fallbackComponent; var errorComponent = function errorComponent() { return fallbackComponent ? /*#__PURE__*/React.cloneElement(fallbackComponent, { error: error, errorInfo: errorInfo }) : /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("h2", null, "Something went wrong."), /*#__PURE__*/React.createElement("details", { style: { whiteSpace: 'pre-wrap' } }, error && error.toString(), /*#__PURE__*/React.createElement("br", null), errorInfo.componentStack)); }; return errorInfo ? errorComponent() : children; }; return ErrorBoundary; }(React.Component); ErrorBoundary.propTypes = { /** @prop Children nodes to render inside the ErrorBoundary | null */ children: PropTypes.node, /** @prop Callback function invoked when an error has occured | null */ errorCallback: PropTypes.func, /** @prop Sets a backup Component in the case of a failure | null */ fallbackComponent: PropTypes.node }; ErrorBoundary.defaultProps = { children: null, errorCallback: null, fallbackComponent: null }; ErrorBoundary.displayName = 'ErrorBoundary'; export default ErrorBoundary;