@carbon/react
Version:
React components for the Carbon Design System
129 lines (123 loc) • 4.01 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.
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var PropTypes = require('prop-types');
var React = require('react');
var cx = require('classnames');
var useId = require('../../internal/useId.js');
var usePrefix = require('../../internal/usePrefix.js');
var Tag = require('./Tag.js');
require('../Tooltip/DefinitionTooltip.js');
var Tooltip = require('../Tooltip/Tooltip.js');
var Text = require('../Text/Text.js');
require('../Text/TextDirection.js');
var isEllipsisActive = require('./isEllipsisActive.js');
var mergeRefs = require('../../tools/mergeRefs.js');
var useIsomorphicEffect = require('../../internal/useIsomorphicEffect.js');
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'
};
// eslint-disable-next-line react/display-name -- https://github.com/carbon-design-system/carbon/issues/20452
const OperationalTag = /*#__PURE__*/React.forwardRef(({
className,
disabled,
id,
renderIcon,
size,
text,
type = 'gray',
...other
}, forwardRef) => {
const prefix = usePrefix.usePrefix();
const tagRef = React.useRef(null);
// eslint-disable-next-line react-hooks/rules-of-hooks -- https://github.com/carbon-design-system/carbon/issues/20452
const tagId = id || `tag-${useId.useId()}`;
const tagClasses = cx(`${prefix}--tag--operational`, className);
const [isEllipsisApplied, setIsEllipsisApplied] = React.useState(false);
useIsomorphicEffect.default(() => {
const newElement = tagRef.current?.getElementsByClassName(`${prefix}--tag__label`)[0];
setIsEllipsisApplied(isEllipsisActive.isEllipsisActive(newElement));
}, [prefix, tagRef]);
const tooltipClasses = cx(`${prefix}--icon-tooltip`, `${prefix}--tag-label-tooltip`);
const combinedRef = mergeRefs.mergeRefs(tagRef, forwardRef);
if (isEllipsisApplied) {
return /*#__PURE__*/React.createElement(Tooltip.Tooltip, {
label: text,
align: "bottom",
className: tooltipClasses,
leaveDelayMs: 0,
onMouseEnter: () => false,
closeOnActivation: true
}, /*#__PURE__*/React.createElement(Tag.default, _rollupPluginBabelHelpers.extends({
ref: combinedRef,
type: type,
size: size,
renderIcon: renderIcon,
disabled: disabled,
className: tagClasses,
id: tagId
}, other), /*#__PURE__*/React.createElement(Text.Text, {
title: text,
className: `${prefix}--tag__label`
}, text)));
}
return /*#__PURE__*/React.createElement(Tag.default, _rollupPluginBabelHelpers.extends({
ref: combinedRef,
type: type,
size: size,
renderIcon: renderIcon,
disabled: disabled,
className: tagClasses,
id: tagId
}, other), /*#__PURE__*/React.createElement(Text.Text, {
title: text,
className: `${prefix}--tag__label`
}, text));
});
OperationalTag.propTypes = {
/**
* Provide a custom className that is applied to the containing <span>
*/
className: PropTypes.string,
/**
* Specify if the `OperationalTag` is disabled
*/
disabled: PropTypes.bool,
/**
* Specify the id for the tag.
*/
id: PropTypes.string,
/**
* 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(Tag.SIZES)),
/**
* Provide text to be rendered inside of a the tag.
*/
text: PropTypes.string,
/**
* Specify the type of the `Tag`
*/
type: PropTypes.oneOf(Object.keys(TYPES))
};
exports.default = OperationalTag;