@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
126 lines (122 loc) • 3.92 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import cx from 'classnames';
import React__default, { useContext } from 'react';
import PropTypes from 'prop-types';
import deprecate from '../../prop-types/deprecate.js';
import { ListBoxSize, ListBoxType } from './ListBoxPropTypes.js';
import { usePrefix } from '../../internal/usePrefix.js';
import '../FluidForm/FluidForm.js';
import { FormContext } from '../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__default.forwardRef(function ListBox(_ref, ref) {
let {
children,
className: containerClassName,
disabled,
type,
size,
invalid,
invalidText,
warn,
warnText,
light,
isOpen,
...rest
} = _ref;
const prefix = usePrefix();
const {
isFluid
} = useContext(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__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", _extends({}, rest, {
className: className,
ref: ref,
onKeyDown: handleOnKeyDown,
onClick: handleClick,
"data-invalid": invalid || undefined
}), children), isFluid && /*#__PURE__*/React__default.createElement("hr", {
className: `${prefix}--list-box__divider`
}), invalid ? /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, invalidText) : null, showWarning ? /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--form-requirement`
}, 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.isRequired,
/**
* 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 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(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: ListBoxSize,
/**
* Specify the "type" of the ListBox. Currently supports either `default` or
* `inline` as an option.
*/
type: ListBoxType.isRequired,
/**
* 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.node
};
export { ListBox as default };