@carbon/react
Version:
React components for the Carbon Design System
123 lines (121 loc) • 5.4 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* 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 { usePrefix } from "../../internal/usePrefix.js";
import { useId } from "../../internal/useId.js";
import { deprecate } from "../../prop-types/deprecate.js";
import { isComponentElement } from "../../internal/utils.js";
import { useNormalizedInputProps } from "../../internal/useNormalizedInputProps.js";
import { AILabel } from "../AILabel/index.js";
import Checkbox$1 from "../Checkbox/Checkbox.js";
import classNames from "classnames";
import { Children, cloneElement } from "react";
import PropTypes from "prop-types";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { WarningAltFilled, WarningFilled } from "@carbon/icons-react";
//#region src/components/CheckboxGroup/CheckboxGroup.tsx
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const CheckboxGroup = ({ children, className, decorator, helperText, invalid, invalidText, legendId, legendText, readOnly, disabled, warn, warnText, slug, orientation = "vertical", ...rest }) => {
const prefix = usePrefix();
const checkboxGroupInstanceId = useId();
const normalizedProps = useNormalizedInputProps({
id: `checkbox-group-${checkboxGroupInstanceId}`,
readOnly,
disabled: disabled ?? false,
invalid: invalid ?? false,
invalidText,
warn: warn ?? false,
warnText
});
const showWarning = normalizedProps.warn;
const showHelper = !normalizedProps.invalid && !normalizedProps.warn;
const hasHelper = typeof helperText !== "undefined" && helperText !== null;
const helperId = !hasHelper ? void 0 : `checkbox-group-helper-text-${checkboxGroupInstanceId}`;
const helper = hasHelper && /* @__PURE__ */ jsx("div", {
id: helperId,
className: `${prefix}--form__helper-text`,
children: helperText
});
const fieldsetClasses = classNames(`${prefix}--checkbox-group`, className, {
[`${prefix}--checkbox-group--${orientation}`]: orientation === "horizontal",
[`${prefix}--checkbox-group--readonly`]: readOnly,
[`${prefix}--checkbox-group--invalid`]: normalizedProps.invalid,
[`${prefix}--checkbox-group--warning`]: showWarning,
[`${prefix}--checkbox-group--slug`]: slug,
[`${prefix}--checkbox-group--decorator`]: decorator
});
const candidate = slug ?? decorator;
const normalizedDecorator = isComponentElement(candidate, AILabel) ? cloneElement(candidate, {
size: "mini",
kind: "default"
}) : candidate;
const clonedChildren = Children.map(children, (child) => {
if (isComponentElement(child, Checkbox$1)) {
const childProps = {
...typeof invalid !== "undefined" && typeof child.props.invalid === "undefined" ? { invalid: normalizedProps.invalid } : {},
...typeof readOnly !== "undefined" && typeof child.props.readOnly === "undefined" ? { readOnly } : {},
...typeof warn !== "undefined" && typeof child.props.warn === "undefined" ? { warn: normalizedProps.warn } : {},
...typeof disabled !== "undefined" && typeof child.props.disabled === "undefined" ? { disabled } : {}
};
return Object.keys(childProps).length ? cloneElement(child, childProps) : child;
}
return child;
});
return /* @__PURE__ */ jsxs("fieldset", {
className: fieldsetClasses,
disabled,
"data-invalid": normalizedProps.invalid ? true : void 0,
"aria-labelledby": rest["aria-labelledby"] || legendId,
"aria-readonly": readOnly,
"aria-disabled": disabled,
"aria-describedby": !normalizedProps.invalid && !normalizedProps.warn && helper ? helperId : void 0,
...rest,
children: [
/* @__PURE__ */ jsxs("legend", {
className: `${prefix}--label`,
id: legendId || rest["aria-labelledby"],
children: [legendText, slug ? normalizedDecorator : decorator ? /* @__PURE__ */ jsx("div", {
className: `${prefix}--checkbox-group-inner--decorator`,
children: normalizedDecorator
}) : ""]
}),
clonedChildren,
/* @__PURE__ */ jsxs("div", {
className: `${prefix}--checkbox-group__validation-msg`,
children: [normalizedProps.invalid && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(WarningFilled, { className: `${prefix}--checkbox__invalid-icon` }), /* @__PURE__ */ jsx("div", {
className: `${prefix}--form-requirement`,
children: invalidText
})] }), showWarning && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(WarningAltFilled, { className: `${prefix}--checkbox__invalid-icon ${prefix}--checkbox__invalid-icon--warning` }), /* @__PURE__ */ jsx("div", {
className: `${prefix}--form-requirement`,
children: warnText
})] })]
}),
showHelper && helper
]
});
};
CheckboxGroup.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
decorator: PropTypes.node,
helperText: PropTypes.node,
invalid: PropTypes.bool,
invalidText: PropTypes.node,
legendId: PropTypes.node,
legendText: PropTypes.node.isRequired,
orientation: PropTypes.oneOf(["horizontal", "vertical"]),
readOnly: PropTypes.bool,
slug: deprecate(PropTypes.node, "The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead."),
warn: PropTypes.bool,
warnText: PropTypes.node
};
//#endregion
export { CheckboxGroup as default };