UNPKG

@carbon/react

Version:

React components for the Carbon Design System

99 lines (97 loc) 3.7 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/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 = require_usePrefix.usePrefix(); const className = (0, classnames.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 = (0, classnames.default)(`${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__ */ (0, react_jsx_runtime.jsxs)("div", { className: tagClasses, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: `${prefix}--tag__label`, title: selectionCount?.toString(), children: selectionCount }), /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.Close, {}) })] }); return /* @__PURE__ */ (0, react_jsx_runtime.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__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.Close, {}) }); } ListBoxSelection.propTypes = { clearSelection: prop_types.default.func.isRequired, disabled: prop_types.default.bool, readOnly: prop_types.default.bool, onClearSelection: prop_types.default.func, onClick: prop_types.default.func, onMouseUp: prop_types.default.func, onKeyDown: prop_types.default.func, selectionCount: prop_types.default.number, translateWithId: prop_types.default.func }; //#endregion exports.default = ListBoxSelection;