UNPKG

@carbon/react

Version:

React components for the Carbon Design System

91 lines (89 loc) 3.49 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. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_usePrefix = require("../../internal/usePrefix.js"); let classnames = require("classnames"); classnames = require_runtime.__toESM(classnames); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let react_jsx_runtime = require("react/jsx-runtime"); let _carbon_icons_react = require("@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 = require_usePrefix.usePrefix(); const className = (0, classnames.default)(`${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 = (0, classnames.default)(`${prefix}--tag`, `${prefix}--tag--filter`, `${prefix}--tag--high-contrast`, { [`${prefix}--tag--disabled`]: disabled }); return selectionCount ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: tagClasses, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: `${prefix}--tag__label`, title: `${selectionCount}`, children: selectionCount }), /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.Close, {}) })] }) : /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { role: "button", className, tabIndex: -1, onClick: handleOnClick, "aria-label": description, title: description, children: [selectionCount, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.Close, {})] }); }; ListBoxSelection.propTypes = { clearSelection: prop_types.default.func.isRequired, disabled: prop_types.default.bool, onClearSelection: prop_types.default.func, readOnly: prop_types.default.bool, selectionCount: prop_types.default.number, translateWithId: prop_types.default.func }; //#endregion exports.default = ListBoxSelection;