@carbon/react
Version:
React components for the Carbon Design System
87 lines (85 loc) • 2.95 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { usePrefix } from "../../internal/usePrefix.js";
import classNames from "classnames";
import "react";
import PropTypes from "prop-types";
import { jsx, jsxs } from "react/jsx-runtime";
import { Close } from "@carbon/icons-react";
//#region src/components/ListBox/ListBoxSelection.tsx
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
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"
};
const defaultTranslateWithId = (messageId) => {
return defaultTranslations[messageId];
};
/**
* `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 = ({ clearSelection, selectionCount, translateWithId: t = defaultTranslateWithId, disabled, onClearSelection, readOnly }) => {
const prefix = usePrefix();
const className = classNames(`${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 = classNames(`${prefix}--tag`, `${prefix}--tag--filter`, `${prefix}--tag--high-contrast`, { [`${prefix}--tag--disabled`]: disabled });
return selectionCount ? /* @__PURE__ */ jsxs("div", {
className: tagClasses,
children: [/* @__PURE__ */ jsx("span", {
className: `${prefix}--tag__label`,
title: `${selectionCount}`,
children: selectionCount
}), /* @__PURE__ */ jsx("div", {
role: "button",
tabIndex: -1,
className: `${prefix}--tag__close-icon`,
onClick: handleOnClick,
"aria-label": t("clear.all"),
title: description,
"aria-disabled": readOnly ? true : void 0,
children: /* @__PURE__ */ jsx(Close, {})
})]
}) : /* @__PURE__ */ jsxs("div", {
role: "button",
className,
tabIndex: -1,
onClick: handleOnClick,
"aria-label": description,
title: description,
children: [selectionCount, /* @__PURE__ */ jsx(Close, {})]
});
};
ListBoxSelection.propTypes = {
clearSelection: PropTypes.func.isRequired,
disabled: PropTypes.bool,
onClearSelection: PropTypes.func,
readOnly: PropTypes.bool,
selectionCount: PropTypes.number,
translateWithId: PropTypes.func
};
//#endregion
export { ListBoxSelection as default };