UNPKG

@xgovformbuilder/govuk-react-jsx

Version:

> This package is no longer maintained and will not receive updates bringing it beyond govuk-frontend 4.0.1. If you are using this in your project the simplest way forward is to copy and paste the components from here into your project allowing you to kee

96 lines (85 loc) 3.8 kB
import _extends from "@babel/runtime/helpers/extends"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["className", "descriptionChildren", "errorList", "titleChildren", "disableAutoFocus"], _excluded2 = ["reactListKey", "children", "href"]; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useEffect } from 'react'; var defaultRef = /*#__PURE__*/React.createRef(); var ErrorSummary = /*#__PURE__*/React.forwardRef(function (props, ref) { var className = props.className, descriptionChildren = props.descriptionChildren, errorList = props.errorList, titleChildren = props.titleChildren, disableAutoFocus = props.disableAutoFocus, attributes = _objectWithoutProperties(props, _excluded); var errorSummaryRef = ref || defaultRef; useEffect(function () { _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _yield$import, ErrorSummaryJS; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!(typeof document !== 'undefined')) { _context.next = 6; break; } _context.next = 3; return import( /* webpackChunkName: "govuk-frontend-error-summary" */ /* webpackMode: "lazy" */ /* webpackPrefetch: true */ 'govuk-frontend/govuk/components/error-summary/error-summary'); case 3: _yield$import = _context.sent; ErrorSummaryJS = _yield$import["default"]; if (errorSummaryRef.current) { // Just bind the click event handlers from the gov error summary // This is because we don't want to focus by default - that's up to the calling app errorSummaryRef.current.addEventListener('click', ErrorSummaryJS.prototype.handleClick.bind(ErrorSummaryJS.prototype)); } case 6: case "end": return _context.stop(); } } }, _callee); }))(); }, [errorSummaryRef]); var description; if (descriptionChildren) { description = /*#__PURE__*/React.createElement("p", null, descriptionChildren); } return /*#__PURE__*/React.createElement("div", _extends({ className: "govuk-error-summary ".concat(className || ''), "aria-labelledby": "error-summary-title", role: "alert", "data-disable-auto-focus": disableAutoFocus ? 'true' : null }, attributes, { "data-module": "govuk-error-summary", ref: errorSummaryRef }), /*#__PURE__*/React.createElement("h2", { className: "govuk-error-summary__title", id: "error-summary-title" }, titleChildren), /*#__PURE__*/React.createElement("div", { className: "govuk-error-summary__body" }, description, /*#__PURE__*/React.createElement("ul", { className: "govuk-list govuk-error-summary__list" }, errorList ? errorList.map(function (error, index) { var reactListKey = error.reactListKey, children = error.children, href = error.href, errorAttributes = _objectWithoutProperties(error, _excluded2); return /*#__PURE__*/React.createElement("li", { key: reactListKey || index }, href ? /*#__PURE__*/React.createElement("a", _extends({}, errorAttributes, { href: href }), children) : /*#__PURE__*/React.createElement(React.Fragment, null, children)); }) : null))); }); ErrorSummary.defaultProps = { titleChildren: 'There is a problem' }; ErrorSummary.displayName = 'ErrorSummary'; export { ErrorSummary };