UNPKG

@carbon/react

Version:

React components for the Carbon Design System

164 lines (157 loc) 6.25 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var PropTypes = require('prop-types'); var React = require('react'); var cx = require('classnames'); var deprecate = require('../../prop-types/deprecate.js'); var usePrefix = require('../../internal/usePrefix.js'); var iconsReact = require('@carbon/icons-react'); var useId = require('../../internal/useId.js'); var index = require('../AILabel/index.js'); var utils = require('../../internal/utils.js'); var Checkbox = require('../Checkbox/Checkbox.js'); require('../Checkbox/Checkbox.Skeleton.js'); const CheckboxGroup = ({ children, className, decorator, helperText, invalid, invalidText, legendId, legendText, readOnly, warn, warnText, slug, orientation = 'vertical', ...rest }) => { const prefix = usePrefix.usePrefix(); const showWarning = !readOnly && !invalid && warn; const showHelper = !invalid && !warn; const checkboxGroupInstanceId = useId.useId(); const hasHelper = typeof helperText !== 'undefined' && helperText !== null; const helperId = !hasHelper ? undefined : `checkbox-group-helper-text-${checkboxGroupInstanceId}`; const helper = hasHelper && /*#__PURE__*/React.createElement("div", { id: helperId, className: `${prefix}--form__helper-text` }, helperText); const fieldsetClasses = cx(`${prefix}--checkbox-group`, className, { [`${prefix}--checkbox-group--${orientation}`]: orientation === 'horizontal', [`${prefix}--checkbox-group--readonly`]: readOnly, [`${prefix}--checkbox-group--invalid`]: !readOnly && invalid, [`${prefix}--checkbox-group--warning`]: showWarning, [`${prefix}--checkbox-group--slug`]: slug, [`${prefix}--checkbox-group--decorator`]: decorator }); // AILabel always size `mini` const candidate = slug ?? decorator; const candidateIsAILabel = utils.isComponentElement(candidate, index.AILabel); const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/React.cloneElement(candidate, { size: 'mini', kind: 'default' }) : candidate; const clonedChildren = React.Children.map(children, child => { if (/*#__PURE__*/React.isValidElement(child) && child.type === Checkbox.default) { const childProps = { ...(typeof invalid !== 'undefined' && typeof child.props.invalid === 'undefined' ? { invalid } : {}), ...(typeof readOnly !== 'undefined' && typeof child.props.readOnly === 'undefined' ? { readOnly } : {}), ...(typeof warn !== 'undefined' && typeof child.props.warn === 'undefined' ? { warn } : {}) }; return Object.keys(childProps).length ? /*#__PURE__*/React.cloneElement(child, childProps) : child; } return child; }); return /*#__PURE__*/React.createElement("fieldset", _rollupPluginBabelHelpers.extends({ className: fieldsetClasses, "data-invalid": invalid ? true : undefined, "aria-labelledby": rest['aria-labelledby'] || legendId, "aria-readonly": readOnly, "aria-describedby": !invalid && !warn && helper ? helperId : undefined }, rest), /*#__PURE__*/React.createElement("legend", { className: `${prefix}--label`, id: legendId || rest['aria-labelledby'] }, legendText, slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", { className: `${prefix}--checkbox-group-inner--decorator` }, normalizedDecorator) : ''), clonedChildren, /*#__PURE__*/React.createElement("div", { className: `${prefix}--checkbox-group__validation-msg` }, !readOnly && invalid && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(iconsReact.WarningFilled, { className: `${prefix}--checkbox__invalid-icon` }), /*#__PURE__*/React.createElement("div", { className: `${prefix}--form-requirement` }, invalidText)), showWarning && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(iconsReact.WarningAltFilled, { className: `${prefix}--checkbox__invalid-icon ${prefix}--checkbox__invalid-icon--warning` }), /*#__PURE__*/React.createElement("div", { className: `${prefix}--form-requirement` }, warnText))), showHelper && helper); }; CheckboxGroup.propTypes = { /** * Provide the children form elements to be rendered inside of the <fieldset> */ children: PropTypes.node, /** * Provide a custom className to be applied to the containing <fieldset> node */ className: PropTypes.string, /** * **Experimental**: Provide a decorator component to be rendered inside the `CheckboxGroup` component */ decorator: PropTypes.node, /** * Provide text for the form group for additional help */ helperText: PropTypes.node, /** * Specify whether the form group is currently invalid */ invalid: PropTypes.bool, /** * Provide the text that is displayed when the form group is in an invalid state */ invalidText: PropTypes.node, /** * Provide id for the fieldset <legend> which corresponds to the fieldset * `aria-labelledby` */ legendId: PropTypes.node, /** * Provide the text to be rendered inside of the fieldset <legend> */ legendText: PropTypes.node.isRequired, /** * Provide the orientation for how the checkbox should be displayed */ orientation: PropTypes.oneOf(['horizontal', 'vertical']), /** * Whether the CheckboxGroup should be read-only */ readOnly: PropTypes.bool, /** * **Experimental**: Provide a `Slug` component to be rendered inside the `CheckboxGroup` component */ slug: deprecate.deprecate(PropTypes.node, 'The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead.'), /** * Specify whether the form group is currently in warning state */ warn: PropTypes.bool, /** * Provide the text that is displayed when the form group is in warning state */ warnText: PropTypes.node }; exports.default = CheckboxGroup;