@carbon/react
Version:
React components for the Carbon Design System
160 lines (155 loc) • 5.18 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* 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 { extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import PropTypes from 'prop-types';
import React, { forwardRef, useRef, useState, useLayoutEffect, cloneElement } from 'react';
import cx from 'classnames';
import { useId } from '../../internal/useId.js';
import { usePrefix } from '../../internal/usePrefix.js';
import { deprecate } from '../../prop-types/deprecate.js';
import Tag, { TYPES, SIZES } from './Tag.js';
import { Close } from '@carbon/icons-react';
import '../Tooltip/DefinitionTooltip.js';
import { Tooltip } from '../Tooltip/Tooltip.js';
import '../Text/index.js';
import { isEllipsisActive } from './isEllipsisActive.js';
import mergeRefs from '../../tools/mergeRefs.js';
import { AILabel } from '../AILabel/index.js';
import { isComponentElement } from '../../internal/utils.js';
import { Text } from '../Text/Text.js';
var _Close;
const DismissibleTag = /*#__PURE__*/forwardRef(({
className,
decorator,
disabled,
id,
renderIcon,
title = 'Dismiss',
onClose,
slug,
size,
text,
tagTitle,
type,
dismissTooltipLabel = 'Dismiss tag',
...other
}, forwardRef) => {
const prefix = usePrefix();
const tagLabelRef = useRef(null);
const tagId = id || `tag-${useId()}`;
const tagClasses = cx(`${prefix}--tag--filter`, className);
const [isEllipsisApplied, setIsEllipsisApplied] = useState(false);
useLayoutEffect(() => {
const newElement = tagLabelRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0];
setIsEllipsisApplied(isEllipsisActive(newElement));
}, [prefix, tagLabelRef]);
const combinedRef = mergeRefs(tagLabelRef, forwardRef);
const handleClose = event => {
if (onClose) {
event.stopPropagation();
onClose(event);
}
};
const candidate = slug ?? decorator;
const candidateIsAILabel = isComponentElement(candidate, AILabel);
const normalizedDecorator = candidateIsAILabel ? /*#__PURE__*/cloneElement(candidate, {
size: 'sm',
kind: 'inline'
}) : null;
const tooltipClasses = cx(`${prefix}--icon-tooltip`, `${prefix}--tag-label-tooltip`);
// Removing onClick from the spread operator
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {
onClick,
...otherProps
} = other;
const dismissActionLabel = isEllipsisApplied ? dismissTooltipLabel : title;
return /*#__PURE__*/React.createElement(Tag, _extends({
ref: combinedRef,
type: type,
size: size,
renderIcon: renderIcon,
disabled: disabled,
className: tagClasses,
id: tagId
}, otherProps), /*#__PURE__*/React.createElement("div", {
className: `${prefix}--interactive--tag-children`
}, /*#__PURE__*/React.createElement(Text, {
title: tagTitle ? tagTitle : text,
className: `${prefix}--tag__label`
}, text), slug ? normalizedDecorator : decorator ? /*#__PURE__*/React.createElement("div", {
className: `${prefix}--tag__decorator`
}, normalizedDecorator) : '', /*#__PURE__*/React.createElement(Tooltip, {
label: dismissActionLabel,
align: "bottom",
className: tooltipClasses,
leaveDelayMs: 0,
closeOnActivation: true
}, /*#__PURE__*/React.createElement("button", {
type: "button",
className: `${prefix}--tag__close-icon`,
onClick: handleClose,
disabled: disabled,
"aria-label": dismissActionLabel
}, _Close || (_Close = /*#__PURE__*/React.createElement(Close, null))))));
});
DismissibleTag.propTypes = {
/**
* Provide a custom className that is applied to the containing <span>
*/
className: PropTypes.string,
/**
* **Experimental:** Provide a `decorator` component to be rendered inside the `DismissibleTag` component
*/
decorator: PropTypes.node,
/**
* Specify if the `DismissibleTag` is disabled
*/
disabled: PropTypes.bool,
/**
* Provide a custom tooltip label for the dismiss button
*/
dismissTooltipLabel: PropTypes.string,
/**
* Specify the id for the tag.
*/
id: PropTypes.string,
/**
* Click handler for filter tag close button.
*/
onClose: PropTypes.func,
/**
* A component used to render an icon.
*/
renderIcon: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
/**
* Specify the size of the Tag. Currently supports either `sm`,
* `md` (default) or `lg` sizes.
*/
size: PropTypes.oneOf(Object.keys(SIZES)),
/**
* **Experimental:** Provide a `Slug` component to be rendered inside the `DismissibleTag` component
*/
slug: deprecate(PropTypes.node, 'The `slug` prop has been deprecated and will be removed in the next major version. Use the decorator prop instead.'),
/**
* Provide text to be rendered inside of a the tag.
*/
text: PropTypes.string,
/**
* Provide a custom `title` to be inserted in the tag.
*/
tagTitle: PropTypes.string,
/**
* Text to show on clear filters
*/
title: PropTypes.string,
/**
* Specify the type of the `Tag`
*/
type: PropTypes.oneOf(Object.keys(TYPES))
};
export { DismissibleTag as default };