UNPKG

@carbon/react

Version:

React components for the Carbon Design System

136 lines (134 loc) 5.38 kB
/** * 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 { Text } from "../Text/Text.js"; import { useId } from "../../internal/useId.js"; import { noopFn } from "../../internal/noopFn.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 classNames from "classnames"; import React, { 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/Checkbox/Checkbox.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 Checkbox$1 = React.forwardRef(({ className, decorator, helperText, id, labelText, onChange = noopFn, onClick, indeterminate = false, invalid, invalidText, hideLabel, readOnly, title = "", warn, warnText, disabled, slug, ...other }, ref) => { const prefix = usePrefix(); const normalizedProps = useNormalizedInputProps({ id, readOnly, disabled: disabled ?? false, invalid: invalid ?? false, invalidText, warn: warn ?? false, warnText }); const showWarning = normalizedProps.warn; const showHelper = !normalizedProps.invalid && !normalizedProps.warn; const checkboxGroupInstanceId = useId(); const hasHelper = typeof helperText !== "undefined" && helperText !== null; const helperId = !hasHelper ? void 0 : `checkbox-helper-text-${checkboxGroupInstanceId}`; const helper = hasHelper && /* @__PURE__ */ jsx("div", { id: helperId, className: `${prefix}--form__helper-text`, children: helperText }); const wrapperClasses = classNames(`${prefix}--form-item`, `${prefix}--checkbox-wrapper`, className, { [`${prefix}--checkbox-wrapper--readonly`]: readOnly, [`${prefix}--checkbox-wrapper--invalid`]: normalizedProps.invalid, [`${prefix}--checkbox-wrapper--warning`]: showWarning, [`${prefix}--checkbox-wrapper--slug`]: slug, [`${prefix}--checkbox-wrapper--decorator`]: decorator }); const innerLabelClasses = classNames(`${prefix}--checkbox-label-text`, { [`${prefix}--visually-hidden`]: hideLabel }); const candidate = slug ?? decorator; const normalizedDecorator = isComponentElement(candidate, AILabel) ? cloneElement(candidate, { size: candidate.props.kind === "inline" ? "md" : "mini" }) : candidate; return /* @__PURE__ */ jsxs("div", { className: wrapperClasses, children: [ /* @__PURE__ */ jsx("input", { ...other, disabled, type: "checkbox", "data-invalid": normalizedProps.invalid ? true : void 0, onChange: (evt) => { if (!readOnly && onChange) onChange(evt, { checked: evt.target.checked, id }); }, className: `${prefix}--checkbox`, id, ref: (el) => { if (el) el.indeterminate = indeterminate ?? false; if (typeof ref === "function") ref(el); else if (ref && "current" in ref) ref.current = el; }, "aria-readonly": readOnly, onClick: (evt) => { if (readOnly) evt.preventDefault(); if (onClick) onClick(evt); } }), /* @__PURE__ */ jsx("label", { htmlFor: id, className: `${prefix}--checkbox-label`, title, children: /* @__PURE__ */ jsxs(Text, { as: "div", className: innerLabelClasses, children: [labelText, slug ? normalizedDecorator : decorator ? /* @__PURE__ */ jsx("div", { className: `${prefix}--checkbox-wrapper-inner--decorator`, children: normalizedDecorator }) : ""] }) }), /* @__PURE__ */ jsxs("div", { className: `${prefix}--checkbox__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 ] }); }); Checkbox$1.propTypes = { checked: PropTypes.bool, className: PropTypes.string, decorator: PropTypes.node, defaultChecked: PropTypes.bool, disabled: PropTypes.bool, helperText: PropTypes.node, hideLabel: PropTypes.bool, id: PropTypes.string.isRequired, indeterminate: PropTypes.bool, invalid: PropTypes.bool, invalidText: PropTypes.node, labelText: PropTypes.node.isRequired, onChange: PropTypes.func, 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."), title: PropTypes.string, warn: PropTypes.bool, warnText: PropTypes.node }; Checkbox$1.displayName = "Checkbox"; //#endregion export { Checkbox$1 as default };