UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

87 lines (79 loc) 2.84 kB
/** * MSKCC 2021, 2024 */ '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 usePrefix = require('../../internal/usePrefix.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); function useIsTruncated(ref) { const [isTruncated, setIsTruncated] = React.useState(false); React.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["default"].forwardRef(function ListBoxMenuItem(_ref, forwardedRef) { let { children, isActive, isHighlighted, title, ...rest } = _ref; const prefix = usePrefix.usePrefix(); const ref = React.useRef(null); const isTruncated = useIsTruncated(forwardedRef?.menuItemOptionRef || ref); const className = cx__default["default"](`${prefix}--list-box__menu-item`, { [`${prefix}--list-box__menu-item--active`]: isActive, [`${prefix}--list-box__menu-item--highlighted`]: isHighlighted }); return /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers["extends"]({}, rest, { className: className, title: isTruncated ? title : undefined, tabIndex: -1 }), /*#__PURE__*/React__default["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__default["default"].node, /** * Specify whether the current menu item is "active". */ isActive: PropTypes__default["default"].bool.isRequired, /** * Specify whether the current menu item is "highlighted". */ isHighlighted: PropTypes__default["default"].bool.isRequired, /** * Provide an optional tooltip for the ListBoxMenuItem */ title: PropTypes__default["default"].string }; ListBoxMenuItem.defaultProps = { isActive: false, isHighlighted: false }; exports["default"] = ListBoxMenuItem;