UNPKG

@carbon/react

Version:

React components for the Carbon Design System

171 lines (169 loc) 7.79 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"); const require_Text = require("../Text/Text.js"); const require_useIsomorphicEffect = require("../../internal/useIsomorphicEffect.js"); const require_useId = require("../../internal/useId.js"); const require_deprecate = require("../../prop-types/deprecate.js"); const require_utils = require("../../internal/utils.js"); const require_useMergedRefs = require("../../internal/useMergedRefs.js"); const require_DefinitionTooltip = require("../Tooltip/DefinitionTooltip.js"); const require_index = require("../AILabel/index.js"); const require_isEllipsisActive = require("./isEllipsisActive.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/Tag/Tag.tsx /** * 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 TYPES = { red: "Red", magenta: "Magenta", purple: "Purple", blue: "Blue", cyan: "Cyan", teal: "Teal", green: "Green", gray: "Gray", "cool-gray": "Cool-Gray", "warm-gray": "Warm-Gray", "high-contrast": "High-Contrast", outline: "Outline" }; const SIZES = { sm: "sm", md: "md", lg: "lg" }; const Tag = react.default.forwardRef(({ children, className, decorator, id, type, filter, renderIcon: CustomIconElement, title = "Clear filter", disabled, onClose, size, as: BaseComponent, slug, ...other }, forwardRef) => { const prefix = require_usePrefix.usePrefix(); const tagRef = (0, react.useRef)(null); if (filter) console.warn("The `filter` prop for Tag has been deprecated and will be removed in the next major version. Use DismissibleTag instead."); if (onClose) console.warn("The `onClose` prop for Tag has been deprecated and will be removed in the next major version. Use DismissibleTag instead."); const ref = require_useMergedRefs.useMergedRefs([forwardRef, tagRef]); const generatedTagId = require_useId.useId(); const tagId = id ?? `tag-${generatedTagId}`; const [isEllipsisApplied, setIsEllipsisApplied] = (0, react.useState)(false); require_useIsomorphicEffect.default(() => { const newElement = tagRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0]; setIsEllipsisApplied(require_isEllipsisActive.isEllipsisActive(newElement)); }, [prefix, tagRef]); const isInteractiveTag = [ `${prefix}--tag--selectable`, `${prefix}--tag--filter`, `${prefix}--tag--operational` ].some((el) => className?.includes(el)); const tagClasses = (0, classnames.default)(`${prefix}--tag`, className, { [`${prefix}--tag--disabled`]: disabled, [`${prefix}--tag--filter`]: filter, [`${prefix}--tag--${size}`]: size, [`${prefix}--layout--size-${size}`]: size, [`${prefix}--tag--${type}`]: type, [`${prefix}--tag--interactive`]: other.onClick && !isInteractiveTag && isEllipsisApplied }); const typeText = type !== void 0 && type in Object.keys(TYPES) ? TYPES[type] : ""; const handleClose = (event) => { if (onClose) { event.stopPropagation(); onClose(event); } }; const candidate = slug ?? decorator; const normalizedDecorator = require_utils.isComponentElement(candidate, require_index.AILabel) && !isInteractiveTag ? (0, react.cloneElement)(candidate, { size: "sm", kind: "inline" }) : null; if (filter) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(BaseComponent ?? "div", { className: tagClasses, id: tagId, ...other, children: [ CustomIconElement && size !== "sm" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `${prefix}--tag__custom-icon`, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CustomIconElement, {}) }) : "", /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Text.Text, { title: typeof children === "string" ? children : void 0, className: `${prefix}--tag__label`, children: children !== null && children !== void 0 ? children : typeText }), normalizedDecorator, /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", { type: "button", className: `${prefix}--tag__close-icon`, onClick: handleClose, disabled, "aria-label": title, title, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_carbon_icons_react.Close, {}) }) ] }); const ComponentTag = BaseComponent ?? (other.onClick || className?.includes(`${prefix}--tag--operational`) ? "button" : "div"); const labelClasses = (0, classnames.default)({ [`${prefix}--tag__label`]: !isInteractiveTag }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(ComponentTag, { ref, disabled, className: tagClasses, id: tagId, type: ComponentTag === "button" ? "button" : void 0, ...other, children: [ CustomIconElement && size !== "sm" ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `${prefix}--tag__custom-icon`, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CustomIconElement, {}) }) : "", isEllipsisApplied && !isInteractiveTag ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_DefinitionTooltip.DefinitionTooltip, { openOnHover: false, definition: children !== null && children !== void 0 ? children : typeText, className: `${prefix}--definition--tooltip--tag`, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Text.Text, { title: children !== null && children !== void 0 && typeof children === "string" ? children : typeText, className: labelClasses, children: children !== null && children !== void 0 ? children : typeText }) }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_Text.Text, { title: children !== null && children !== void 0 && typeof children === "string" ? children : typeText, className: labelClasses, children: children !== null && children !== void 0 ? children : typeText }), slug ? normalizedDecorator : decorator ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: `${prefix}--tag__decorator`, children: normalizedDecorator }) : "" ] }); }); Tag.propTypes = { as: prop_types.default.elementType, children: prop_types.default.node, className: prop_types.default.string, decorator: prop_types.default.node, disabled: prop_types.default.bool, filter: require_deprecate.deprecate(prop_types.default.bool, "The `filter` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."), id: prop_types.default.string, onClose: require_deprecate.deprecate(prop_types.default.func, "The `onClose` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."), renderIcon: prop_types.default.oneOfType([prop_types.default.func, prop_types.default.object]), size: prop_types.default.oneOf(Object.keys(SIZES)), slug: require_deprecate.deprecate(prop_types.default.node, "The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead."), title: require_deprecate.deprecate(prop_types.default.string, "The `title` prop has been deprecated and will be removed in the next major version. Use DismissibleTag instead."), type: prop_types.default.oneOf(Object.keys(TYPES)) }; //#endregion exports.SIZES = SIZES; exports.TYPES = TYPES; exports.default = Tag;