@mskcc/carbon-react
Version:
Carbon react components for the MSKCC DSM
138 lines (129 loc) • 4.72 kB
JavaScript
/**
* MSKCC 2021, 2024
*/
;
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 iconsReact = require('@carbon/icons-react');
var usePrefix = require('../../../internal/usePrefix.js');
var MskIcon = require('../../Icon/MskIcon.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);
var _Close, _Icon;
/**
* `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.
*/
function ListBoxSelection(_ref) {
let {
clearSelection,
selectionCount,
translateWithId: t,
disabled,
onClearSelection,
...rest
} = _ref;
const prefix = usePrefix.usePrefix();
const className = cx__default["default"](`${prefix}--list-box__selection`, {
[`${prefix}--tag--filter`]: selectionCount,
[`${prefix}--list-box__selection--multi`]: selectionCount
});
const description = selectionCount ? t('clear.all') : t('clear.selection');
const tagClasses = cx__default["default"](`${prefix}--tag`, `${prefix}--tag--filter`, `${prefix}--tag--high-contrast`, {
[`${prefix}--tag--disabled`]: disabled
});
function onClick(event) {
event.stopPropagation();
if (disabled) {
return;
}
clearSelection(event);
if (onClearSelection) {
onClearSelection(event);
}
}
if (selectionCount) {
return /*#__PURE__*/React__default["default"].createElement("div", {
className: tagClasses
}, /*#__PURE__*/React__default["default"].createElement("span", {
className: `${prefix}--tag__label`,
title: selectionCount
}, selectionCount), /*#__PURE__*/React__default["default"].createElement("button", {
"aria-label": description,
className: `${prefix}--tag__close-icon`,
disabled: disabled,
onClick: onClick,
tabIndex: -1,
title: description,
type: "button"
}, _Close || (_Close = /*#__PURE__*/React__default["default"].createElement(iconsReact.Close, null))));
}
return /*#__PURE__*/React__default["default"].createElement("button", _rollupPluginBabelHelpers["extends"]({}, rest, {
"aria-label": description,
className: className,
disabled: disabled,
onClick: onClick,
tabIndex: -1,
title: description,
type: "button"
}), _Icon || (_Icon = /*#__PURE__*/React__default["default"].createElement(MskIcon.Icon, {
icon: "clear",
size: "sm"
})));
}
const translationIds = {
'clear.all': 'clear.all',
'clear.selection': 'clear.selection'
};
const defaultTranslations = {
[translationIds['clear.all']]: 'Clear all selected items',
[translationIds['clear.selection']]: 'Clear selected item'
};
ListBoxSelection.propTypes = {
/**
* Specify a function to be invoked when a user interacts with the clear
* selection element.
*/
clearSelection: PropTypes__default["default"].func.isRequired,
/**
* Specify whether or not the clear selection element should be disabled
*/
disabled: PropTypes__default["default"].bool,
/**
* Specify an optional `onClearSelection` handler that is called when the underlying
* element is cleared
*/
onClearSelection: PropTypes__default["default"].func,
/**
* Specify an optional `onClick` handler that is called when the underlying
* clear selection element is clicked
*/
onClick: PropTypes__default["default"].func,
/**
* Specify an optional `onKeyDown` handler that is called when the underlying
* clear selection element fires a keydown event
*/
onKeyDown: PropTypes__default["default"].func,
/**
* 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__default["default"].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__default["default"].func.isRequired
};
ListBoxSelection.defaultProps = {
translateWithId: id => defaultTranslations[id]
};
exports["default"] = ListBoxSelection;
exports.translationIds = translationIds;