@carbon/react
Version:
React components for the Carbon Design System
79 lines (77 loc) • 2.64 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 { deprecate } from "../../prop-types/deprecate.js";
import { useMergedRefs } from "../../internal/useMergedRefs.js";
import React, { useEffect, useRef } from "react";
import PropTypes from "prop-types";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/components/InlineCheckbox/InlineCheckbox.tsx
/**
* Copyright IBM Corp. 2016, 2025
*
* 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 InlineCheckbox = React.forwardRef((props, forwardRef) => {
const { ["aria-label"]: ariaLabel, ariaLabel: deprecatedAriaLabel, checked = false, disabled, id, indeterminate, name, onChange = () => {}, onClick, onKeyDown, title } = props;
const prefix = usePrefix();
const inputRef = useRef(null);
const ref = useMergedRefs([inputRef, forwardRef]);
const inputProps = {
checked,
className: `${prefix}--checkbox`,
disabled,
id,
name,
onClick: onClick ? onClickCheckBoxInput : onClick,
onChange: (evt) => {
onChange(evt.target.checked, id, evt);
},
onKeyDown,
ref,
type: "checkbox"
};
if (indeterminate) inputProps.checked = false;
useEffect(() => {
if (inputRef?.current) inputRef.current.indeterminate = indeterminate || false;
}, [indeterminate]);
function onClickCheckBoxInput(evt) {
if (indeterminate) evt.target.checked = false;
onClick?.(evt);
}
return /* @__PURE__ */ jsxs("div", {
className: `${prefix}--checkbox--inline`,
children: [/* @__PURE__ */ jsx("input", { ...inputProps }), /* @__PURE__ */ jsx("label", {
htmlFor: id,
className: `${prefix}--checkbox-label`,
title,
onClick: (evt) => {
evt.stopPropagation();
},
children: /* @__PURE__ */ jsx("span", {
className: `${prefix}--visually-hidden`,
children: deprecatedAriaLabel || ariaLabel
})
})]
});
});
InlineCheckbox.propTypes = {
["aria-label"]: PropTypes.string.isRequired,
ariaLabel: deprecate(PropTypes.string.isRequired, "The `ariaLabel` prop has been deprecated in favor of `aria-label`. This prop will be removed in the next major release."),
checked: PropTypes.bool,
disabled: PropTypes.bool,
id: PropTypes.string.isRequired,
indeterminate: PropTypes.bool,
name: PropTypes.string.isRequired,
onChange: PropTypes.func,
onClick: PropTypes.func,
onKeyDown: PropTypes.func,
title: PropTypes.string
};
//#endregion
export { InlineCheckbox as default };