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

88 lines (76 loc) 3.23 kB
import _extends from "@babel/runtime/helpers/extends"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["headingLevel", "items", "className", "id"]; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useEffect, useRef } from 'react'; function Accordion(props) { var accordionRef = useRef(); var headingLevel = props.headingLevel, items = props.items, className = props.className, id = props.id, attributes = _objectWithoutProperties(props, _excluded); useEffect(function () { _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _yield$import, AccordionJS; 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-accordion" */ /* webpackMode: "lazy" */ /* webpackPrefetch: true */ 'govuk-frontend/govuk/components/accordion/accordion'); case 3: _yield$import = _context.sent; AccordionJS = _yield$import["default"]; if (accordionRef.current) { new AccordionJS(accordionRef.current).init(); } case 6: case "end": return _context.stop(); } } }, _callee); }))(); }, [accordionRef]); var HeadingLevel = headingLevel ? "h".concat(headingLevel) : 'h2'; var innerHtml = items.map(function (item, index) { if (!item) { return; } return /*#__PURE__*/React.createElement("div", { key: item.reactListKey || index, className: "govuk-accordion__section ".concat(item.expanded ? 'govuk-accordion__section--expanded' : '') }, /*#__PURE__*/React.createElement("div", { className: "govuk-accordion__section-header" }, /*#__PURE__*/React.createElement(HeadingLevel, { className: "govuk-accordion__section-heading" }, /*#__PURE__*/React.createElement("span", { className: "govuk-accordion__section-button", id: "".concat(id, "-heading-").concat(index + 1) }, item.heading.children)), item.summary ? /*#__PURE__*/React.createElement("div", { className: "govuk-accordion__section-summary govuk-body", id: "".concat(id, "-summary-").concat(index + 1) }, item.summary.children) : ''), /*#__PURE__*/React.createElement("div", { id: "".concat(id, "-content-").concat(index + 1), className: "govuk-accordion__section-content", "aria-labelledby": "".concat(id, "-heading-").concat(index + 1) }, item.content.children)); }); return /*#__PURE__*/React.createElement("div", _extends({}, attributes, { id: id, className: "govuk-accordion ".concat(className || ''), "data-module": "govuk-accordion", ref: accordionRef }), innerHtml); } export { Accordion };