@carbon/react
Version:
React components for the Carbon Design System
145 lines (139 loc) • 4.49 kB
JavaScript
/**
* 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.
*/
;
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');
var ListBoxPropTypes = require('./ListBoxPropTypes.js');
var usePrefix = require('../../internal/usePrefix.js');
require('../FluidForm/FluidForm.js');
var FormContext = require('../FluidForm/FormContext.js');
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({
...(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.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", _rollupPluginBabelHelpers.extends({}, rest, {
className: className,
ref: ref,
onKeyDown: handleOnKeyDown,
onClick: handleClick,
"data-invalid": invalid || undefined
}), children), isFluid && /*#__PURE__*/React.createElement("hr", {
className: `${prefix}--list-box__divider`
}), invalid ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--form-requirement`,
id: invalidTextId
}, invalidText) : null, showWarning ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--form-requirement`,
id: warnTextId
}, warnText) : null);
});
ListBox.displayName = 'ListBox';
ListBox.propTypes = {
/**
* Provide the contents of your ListBox
*/
children: PropTypes.node,
/**
* Specify a class name to be applied on the containing list box node
*/
className: PropTypes.string,
/**
* Specify whether the ListBox is currently disabled
*/
disabled: PropTypes.bool,
/**
* Specify whether the control is currently invalid
*/
invalid: PropTypes.bool,
/**
* Specify the text to be displayed when the control is invalid
*/
invalidText: PropTypes.node,
/**
* Specify the id to be applied to the element containing the invalid text
*/
invalidTextId: PropTypes.string,
/**
* Specify if the control should render open
*/
isOpen: PropTypes.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.deprecate(PropTypes.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.bool,
/**
* Provide the text that is displayed when the control is in warning state
*/
warnText: PropTypes.string,
/**
* Specify the id to be applied to the element containing the warn text
*/
warnTextId: PropTypes.string
};
exports.default = ListBox;