@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
73 lines (69 loc) • 2.21 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
import { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import cx from 'classnames';
import React__default, { useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { usePrefix } from '../../internal/usePrefix.js';
function useIsTruncated(ref) {
const [isTruncated, setIsTruncated] = useState(false);
useEffect(() => {
const {
offsetWidth,
scrollWidth
} = ref.current;
setIsTruncated(offsetWidth < scrollWidth);
}, [ref, setIsTruncated]);
return isTruncated;
}
/**
* `ListBoxMenuItem` is a helper component for managing the container class
* name, alongside any classes for any corresponding states, for a generic list
* box menu item.
*/
const ListBoxMenuItem = /*#__PURE__*/React__default.forwardRef(function ListBoxMenuItem(_ref, forwardedRef) {
let {
children,
isActive,
isHighlighted,
title,
...rest
} = _ref;
const prefix = usePrefix();
const ref = useRef(null);
const isTruncated = useIsTruncated(forwardedRef?.menuItemOptionRef || ref);
const className = cx(`${prefix}--list-box__menu-item`, {
[`${prefix}--list-box__menu-item--active`]: isActive,
[`${prefix}--list-box__menu-item--highlighted`]: isHighlighted
});
return /*#__PURE__*/React__default.createElement("div", _extends({}, rest, {
className: className,
title: isTruncated ? title : undefined,
tabIndex: -1
}), /*#__PURE__*/React__default.createElement("div", {
className: `${prefix}--list-box__menu-item__option`,
ref: forwardedRef?.menuItemOptionRef || ref
}, children));
});
ListBoxMenuItem.displayName = 'ListBoxMenuItem';
ListBoxMenuItem.propTypes = {
/**
* Specify any children nodes that should be rendered inside of the ListBox
* Menu Item
*/
children: PropTypes.node,
/**
* Specify whether the current menu item is "active".
*/
isActive: PropTypes.bool.isRequired,
/**
* Specify whether the current menu item is "highlighted".
*/
isHighlighted: PropTypes.bool.isRequired,
/**
* Provide an optional tooltip for the ListBoxMenuItem
*/
title: PropTypes.string
};
export { ListBoxMenuItem as default };