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

79 lines (71 loc) 3.1 kB
import _extends from "@babel/runtime/helpers/extends"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["id", "className", "maxlength", "threshold", "maxwords", "errorMessage", "countMessage"]; import _regeneratorRuntime from "@babel/runtime/regenerator"; import React, { useEffect, useRef } from 'react'; import { Textarea, Hint } from '../..'; var CharacterCount = /*#__PURE__*/React.forwardRef(function (props, ref) { var id = props.id, className = props.className, maxlength = props.maxlength, threshold = props.threshold, maxwords = props.maxwords, errorMessage = props.errorMessage, countMessage = props.countMessage, attributes = _objectWithoutProperties(props, _excluded); var characterCountRef = useRef(); var characterCountInfoId = "".concat(id, "-info"); useEffect(function () { _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var _yield$import, CharacterCountJS; 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-character-count" */ /* webpackMode: "lazy" */ /* webpackPrefetch: true */ 'govuk-frontend/govuk/components/character-count/character-count'); case 3: _yield$import = _context.sent; CharacterCountJS = _yield$import["default"]; if (characterCountRef.current) { new CharacterCountJS(characterCountRef.current).init(); } case 6: case "end": return _context.stop(); } } }, _callee); }))(); }, [characterCountRef]); return /*#__PURE__*/React.createElement("div", { className: "govuk-character-count", "data-module": "govuk-character-count", "data-maxlength": maxlength, "data-threshold": threshold, "data-maxwords": maxwords, ref: characterCountRef }, /*#__PURE__*/React.createElement(Textarea, _extends({ id: id }, attributes, { errorMessage: errorMessage, className: "govuk-js-character-count ".concat(className || '').concat(errorMessage ? ' govuk-textarea--error' : ''), "aria-describedby": characterCountInfoId, ref: ref })), /*#__PURE__*/React.createElement(Hint, { id: characterCountInfoId, className: "govuk-hint govuk-character-count__message ".concat((countMessage === null || countMessage === void 0 ? void 0 : countMessage.className) || ''), "aria-live": "polite" }, "You can enter up to ", maxlength || maxwords, ' ', maxwords ? 'words' : 'characters')); }); CharacterCount.displayName = 'CharacterCount'; export { CharacterCount };