@carbon/react
Version:
React components for the Carbon Design System
127 lines (125 loc) • 6.25 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.
*/
const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("../../internal/usePrefix.js");
const require_useId = require("../../internal/useId.js");
const require_deprecate = require("../../prop-types/deprecate.js");
const require_utils = require("../../internal/utils.js");
const require_useNormalizedInputProps = require("../../internal/useNormalizedInputProps.js");
const require_index = require("../AILabel/index.js");
const require_Checkbox = require("../Checkbox/Checkbox.js");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let react_jsx_runtime = require("react/jsx-runtime");
let _carbon_icons_react = require("@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 = require_usePrefix.usePrefix();
const checkboxGroupInstanceId = require_useId.useId();
const normalizedProps = require_useNormalizedInputProps.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__ */ (0, react_jsx_runtime.jsx)("div", {
id: helperId,
className: `${prefix}--form__helper-text`,
children: helperText
});
const fieldsetClasses = (0, classnames.default)(`${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 = require_utils.isComponentElement(candidate, require_index.AILabel) ? (0, react.cloneElement)(candidate, {
size: "mini",
kind: "default"
}) : candidate;
const clonedChildren = react.Children.map(children, (child) => {
if (require_utils.isComponentElement(child, require_Checkbox.default)) {
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 ? (0, react.cloneElement)(child, childProps) : child;
}
return child;
});
return /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsxs)("legend", {
className: `${prefix}--label`,
id: legendId || rest["aria-labelledby"],
children: [legendText, slug ? normalizedDecorator : decorator ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--checkbox-group-inner--decorator`,
children: normalizedDecorator
}) : ""]
}),
clonedChildren,
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
className: `${prefix}--checkbox-group__validation-msg`,
children: [normalizedProps.invalid && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.WarningFilled, { className: `${prefix}--checkbox__invalid-icon` }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--form-requirement`,
children: invalidText
})] }), showWarning && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.WarningAltFilled, { className: `${prefix}--checkbox__invalid-icon ${prefix}--checkbox__invalid-icon--warning` }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
className: `${prefix}--form-requirement`,
children: warnText
})] })]
}),
showHelper && helper
]
});
};
CheckboxGroup.propTypes = {
children: prop_types.default.node,
className: prop_types.default.string,
decorator: prop_types.default.node,
helperText: prop_types.default.node,
invalid: prop_types.default.bool,
invalidText: prop_types.default.node,
legendId: prop_types.default.node,
legendText: prop_types.default.node.isRequired,
orientation: prop_types.default.oneOf(["horizontal", "vertical"]),
readOnly: prop_types.default.bool,
slug: require_deprecate.deprecate(prop_types.default.node, "The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead."),
warn: prop_types.default.bool,
warnText: prop_types.default.node
};
//#endregion
exports.default = CheckboxGroup;