UNPKG

@carbon/react

Version:

React components for the Carbon Design System

95 lines (93 loc) 3.75 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. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_usePrefix = require("../../internal/usePrefix.js"); const require_deprecate = require("../../prop-types/deprecate.js"); const require_ListBoxPropTypes = require("./ListBoxPropTypes.js"); const require_FormContext = require("../FluidForm/FormContext.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"); //#region src/components/ListBox/ListBox.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 handleOnKeyDown = (event) => { if (event.keyCode === 27) event.stopPropagation(); }; const handleClick = (event) => { event.preventDefault(); event.stopPropagation(); }; /** * `ListBox` is a generic container component that handles creating the * container class name in response to certain props. */ const ListBox = (0, react.forwardRef)((props, ref) => { const { children, className: containerClassName, disabled = false, type = "default", size, invalid, invalidText, invalidTextId, warn, warnText, warnTextId, light, isOpen, ...rest } = props; const prefix = require_usePrefix.usePrefix(); const { isFluid } = (0, react.useContext)(require_FormContext.FormContext); const showWarning = !invalid && warn; const className = (0, classnames.default)({ ...containerClassName && { [containerClassName]: true }, [`${prefix}--list-box`]: true, [`${prefix}--list-box--${size}`]: size, [`${prefix}--list-box--inline`]: type === "inline", [`${prefix}--list-box--disabled`]: disabled, [`${prefix}--list-box--light`]: light, [`${prefix}--list-box--expanded`]: isOpen, [`${prefix}--list-box--invalid`]: invalid, [`${prefix}--list-box--warning`]: showWarning }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...rest, className, ref, onKeyDown: handleOnKeyDown, onClick: handleClick, "data-invalid": invalid || void 0, children }), isFluid && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("hr", { className: `${prefix}--list-box__divider` }), invalid ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `${prefix}--form-requirement`, id: invalidTextId, children: invalidText }) : null, showWarning ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `${prefix}--form-requirement`, id: warnTextId, children: warnText }) : null ] }); }); ListBox.displayName = "ListBox"; ListBox.propTypes = { children: prop_types.default.node, className: prop_types.default.string, disabled: prop_types.default.bool, invalid: prop_types.default.bool, invalidText: prop_types.default.node, invalidTextId: prop_types.default.string, isOpen: prop_types.default.bool, light: require_deprecate.deprecate(prop_types.default.bool, "The `light` prop for `ListBox` has been deprecated in favor of the new `Layer` component. It will be removed in the next major release."), size: require_ListBoxPropTypes.ListBoxSizePropType, type: require_ListBoxPropTypes.ListBoxTypePropType, warn: prop_types.default.bool, warnText: prop_types.default.string, warnTextId: prop_types.default.string }; //#endregion exports.default = ListBox;