UNPKG

@carbon/react

Version:

React components for the Carbon Design System

95 lines (93 loc) 3.13 kB
/** * 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/next/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. */ /** * `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 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]; }; function ListBoxSelection({ clearSelection, selectionCount, translateWithId: t = defaultTranslateWithId, disabled, readOnly, onClearSelection, ...rest }) { const prefix = usePrefix(); const className = classNames(`${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 = classNames(`${prefix}--tag`, `${prefix}--tag--filter`, `${prefix}--tag--high-contrast`, { [`${prefix}--tag--disabled`]: disabled }); function onClick(event) { event.stopPropagation(); if (disabled || readOnly) return; clearSelection(event); if (onClearSelection) onClearSelection(event); } if (selectionCount) return /* @__PURE__ */ jsxs("div", { className: tagClasses, children: [/* @__PURE__ */ jsx("span", { className: `${prefix}--tag__label`, title: selectionCount?.toString(), children: selectionCount }), /* @__PURE__ */ jsx("button", { "aria-label": description, className: `${prefix}--tag__close-icon`, disabled: disabled || readOnly, onClick, tabIndex: -1, title: description, type: "button", "aria-disabled": readOnly ? true : void 0, children: /* @__PURE__ */ jsx(Close, {}) })] }); return /* @__PURE__ */ jsx("button", { ...rest, "aria-label": description, className, disabled: disabled || readOnly, onClick, tabIndex: -1, title: description, type: "button", "aria-disabled": readOnly ? true : void 0, children: /* @__PURE__ */ jsx(Close, {}) }); } ListBoxSelection.propTypes = { clearSelection: PropTypes.func.isRequired, disabled: PropTypes.bool, readOnly: PropTypes.bool, onClearSelection: PropTypes.func, onClick: PropTypes.func, onMouseUp: PropTypes.func, onKeyDown: PropTypes.func, selectionCount: PropTypes.number, translateWithId: PropTypes.func }; //#endregion export { ListBoxSelection as default };