UNPKG

@carbon/react

Version:

React components for the Carbon Design System

152 lines (144 loc) 5.19 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var cx = require('classnames'); var React = require('react'); var PropTypes = require('prop-types'); var deprecate = require('../../prop-types/deprecate.js'); require('./index.js'); var usePrefix = require('../../internal/usePrefix.js'); require('../FluidForm/FluidForm.js'); var FormContext = require('../FluidForm/FormContext.js'); var ListBoxPropTypes = require('./ListBoxPropTypes.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx); var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes); 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 = /*#__PURE__*/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 = usePrefix.usePrefix(); const { isFluid } = React.useContext(FormContext.FormContext); const showWarning = !invalid && warn; const className = cx__default["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__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers["extends"]({}, rest, { className: className, ref: ref, onKeyDown: handleOnKeyDown, onClick: handleClick, "data-invalid": invalid || undefined }), children), isFluid && /*#__PURE__*/React__default["default"].createElement("hr", { className: `${prefix}--list-box__divider` }), invalid ? /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-requirement`, id: invalidTextId }, invalidText) : null, showWarning ? /*#__PURE__*/React__default["default"].createElement("div", { className: `${prefix}--form-requirement`, id: warnTextId }, warnText) : null); }); ListBox.displayName = 'ListBox'; ListBox.propTypes = { /** * Provide the contents of your ListBox */ children: PropTypes__default["default"].node, /** * Specify a class name to be applied on the containing list box node */ className: PropTypes__default["default"].string, /** * Specify whether the ListBox is currently disabled */ disabled: PropTypes__default["default"].bool, /** * Specify whether the control is currently invalid */ invalid: PropTypes__default["default"].bool, /** * Specify the text to be displayed when the control is invalid */ invalidText: PropTypes__default["default"].node, /** * Specify the id to be applied to the element containing the invalid text */ invalidTextId: PropTypes__default["default"].string, /** * Specify if the control should render open */ isOpen: PropTypes__default["default"].bool, /** * `true` to use the light version. For use on $ui-01 backgrounds only. * Don't use this to make tile background color same as container background color. */ light: deprecate["default"](PropTypes__default["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.'), /** * Specify the size of the ListBox. Currently supports either `sm`, `md` or `lg` as an option. */ size: ListBoxPropTypes.ListBoxSizePropType, /** * Specify the "type" of the ListBox. Currently supports either `default` or * `inline` as an option. */ type: ListBoxPropTypes.ListBoxTypePropType, /** * Specify whether the control is currently in warning state */ warn: PropTypes__default["default"].bool, /** * Provide the text that is displayed when the control is in warning state */ warnText: PropTypes__default["default"].string, /** * Specify the id to be applied to the element containing the warn text */ warnTextId: PropTypes__default["default"].string }; exports["default"] = ListBox;