@carbon/react
Version:
React components for the Carbon Design System
140 lines (135 loc) • 5.25 kB
JavaScript
/**
* 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.
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React, { cloneElement } from 'react';
import cx from 'classnames';
import { deprecate } from '../../prop-types/deprecate.js';
import { usePrefix } from '../../internal/usePrefix.js';
import { WarningFilled, WarningAltFilled } from '@carbon/icons-react';
import { useId } from '../../internal/useId.js';
import { AILabel } from '../AILabel/index.js';
import { isComponentElement } from '../../internal/utils.js';
const CheckboxGroup = ({
children,
className,
decorator,
helperText,
invalid,
invalidText,
legendId,
legendText,
readOnly,
warn,
warnText,
slug,
orientation = 'vertical',
...rest
}) => {
const prefix = usePrefix();
const showWarning = !readOnly && !invalid && warn;
const showHelper = !invalid && !warn;
const checkboxGroupInstanceId = useId();
const helperId = !helperText ? undefined : `checkbox-group-helper-text-${checkboxGroupInstanceId}`;
const helper = helperText ? /*#__PURE__*/React.createElement("div", {
id: helperId,
className: `${prefix}--form__helper-text`
}, helperText) : null;
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 = isComponentElement(candidate, AILabel);
const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/cloneElement(candidate, {
size: 'mini',
kind: 'default'
}) : null;
return /*#__PURE__*/React.createElement("fieldset", _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) : ''), children, /*#__PURE__*/React.createElement("div", {
className: `${prefix}--checkbox-group__validation-msg`
}, !readOnly && invalid && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(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(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(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
};
export { CheckboxGroup as default };