UNPKG

@mskcc/carbon-react

Version:

Carbon react components for the MSKCC DSM

122 lines (117 loc) 4.02 kB
/** * MSKCC 2021, 2024 */ import cx from 'classnames'; import React__default from 'react'; import PropTypes from 'prop-types'; import { usePrefix } from '../../internal/usePrefix.js'; var _span, _span2, _span3; /** * `ListBoxSelection` is used to provide controls for clearing a selection, in * addition to conditionally rendering a badge if the control has more than one * selection. */ const ListBoxSelection = _ref => { let { clearSelection, selectionCount, translateWithId: t, disabled, onClearSelection, label, readOnly } = _ref; const prefix = usePrefix(); const className = cx(`${prefix}--list-box__selection`, { [`${prefix}--tag--filter`]: selectionCount, [`${prefix}--list-box__selection--multi`]: selectionCount }); const handleOnClick = event => { event.stopPropagation(); if (disabled || readOnly) { return; } clearSelection(event); if (onClearSelection) { onClearSelection(event); } }; const description = selectionCount ? t('clear.all') : t('clear.selection'); const tagClasses = cx(`${prefix}--tag`, `${prefix}--tag--filter`, `${prefix}--tag--high-contrast`, { [`${prefix}--tag--disabled`]: disabled }); /* eslint-disable jsx-a11y/click-events-have-key-events */ return selectionCount ? /*#__PURE__*/React__default.createElement("div", { className: tagClasses }, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--tag__label`, title: `${selectionCount}` }, selectionCount), /*#__PURE__*/React__default.createElement("div", { role: "button", tabIndex: -1, className: `${prefix}--tag__close-icon`, onClick: handleOnClick, "aria-label": t('clear.all'), title: description, "aria-disabled": readOnly ? true : undefined }, _span || (_span = /*#__PURE__*/React__default.createElement("span", { className: "msk-icon msk-tag--close-icon" }, "clear")))) : label ? /*#__PURE__*/React__default.createElement("div", { className: tagClasses }, /*#__PURE__*/React__default.createElement("span", { className: `${prefix}--tag__label`, title: `${label}` }, label), /*#__PURE__*/React__default.createElement("div", { role: "button", tabIndex: -1, className: `${prefix}--tag__close-icon`, onClick: handleOnClick, "aria-label": t('clear.all'), title: description, "aria-disabled": readOnly ? true : undefined }, _span2 || (_span2 = /*#__PURE__*/React__default.createElement("span", { className: "msk-icon msk-tag--close-icon" }, "clear")))) : /*#__PURE__*/React__default.createElement("div", { role: "button", className: className, tabIndex: -1, onClick: handleOnClick, "aria-label": description, title: description }, selectionCount, _span3 || (_span3 = /*#__PURE__*/React__default.createElement("span", { className: "msk-icon msk-tag--close-icon" }, "clear"))); }; ListBoxSelection.propTypes = { /** * Specify a function to be invoked when a user interacts with the clear * selection element. */ clearSelection: PropTypes.func.isRequired, /** * Specify whether or not the clear selection element should be disabled */ disabled: PropTypes.bool, label: PropTypes.string, /** * Specify an optional `onClearSelection` handler that is called when the underlying * element is cleared */ onClearSelection: PropTypes.func, /** * Whether or not the Dropdown is readonly */ readOnly: PropTypes.bool, /** * Specify an optional `selectionCount` value that will be used to determine * whether the selection should display a badge or a single clear icon. */ selectionCount: PropTypes.number, /** * i18n hook used to provide the appropriate description for the given menu * icon. This function takes in an id defined in `translationIds` and should * return a string message for that given message id. */ translateWithId: PropTypes.func.isRequired }; export { ListBoxSelection as default };