@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
61 lines (56 loc) • 2.42 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["className", "aria-describedby", "errorMessage", "formGroup", "hint", "id", "items", "label"],
_excluded2 = ["reactListKey", "children"];
import React from 'react';
import { Label, Hint, ErrorMessage } from '../..';
var Select = /*#__PURE__*/React.forwardRef(function (props, ref) {
var className = props.className,
describedBy = props['aria-describedby'],
errorMessage = props.errorMessage,
formGroup = props.formGroup,
hint = props.hint,
id = props.id,
items = props.items,
label = props.label,
attributes = _objectWithoutProperties(props, _excluded);
var describedByValue = describedBy || '';
var hintComponent;
var errorMessageComponent;
if (hint) {
var hintId = "".concat(id, "-hint");
describedByValue += " ".concat(hintId);
hintComponent = /*#__PURE__*/React.createElement(Hint, _extends({}, hint, {
id: hintId
}));
}
if (errorMessage) {
var errorId = id ? "".concat(id, "-error") : '';
describedByValue += " ".concat(errorId);
errorMessageComponent = /*#__PURE__*/React.createElement(ErrorMessage, _extends({}, errorMessage, {
id: errorId
}));
}
var options = items ? items.filter(function (item) {
return item;
}).map(function (option, index) {
var reactListKey = option.reactListKey,
children = option.children,
optionAttributes = _objectWithoutProperties(option, _excluded2);
return /*#__PURE__*/React.createElement("option", _extends({}, optionAttributes, {
key: reactListKey || index
}), children);
}) : null;
return /*#__PURE__*/React.createElement("div", {
className: "govuk-form-group".concat(errorMessage ? ' govuk-form-group--error' : '', " ").concat((formGroup === null || formGroup === void 0 ? void 0 : formGroup.className) || '')
}, /*#__PURE__*/React.createElement(Label, _extends({}, label, {
htmlFor: id
})), hintComponent, errorMessageComponent, /*#__PURE__*/React.createElement("select", _extends({
className: "govuk-select ".concat(className || '').concat(errorMessage ? ' govuk-select--error' : ''),
id: id,
ref: ref,
"aria-describedby": describedByValue || null
}, attributes), options));
});
Select.displayName = 'Select';
export { Select };