2025-05-20 22:41:53 +00:00
|
|
|
import React, { useRef } from "react";
|
2024-07-11 13:44:09 +00:00
|
|
|
import { uniqueId } from "lodash";
|
|
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
|
|
|
|
import ReactTooltip from "react-tooltip";
|
|
|
|
|
import { COLORS } from "styles/var/colors";
|
2025-05-20 22:41:53 +00:00
|
|
|
import { useCheckTruncatedElement } from "hooks/useCheckTruncatedElement";
|
2024-07-11 13:44:09 +00:00
|
|
|
|
|
|
|
|
interface ITooltipTruncatedTextCellProps {
|
|
|
|
|
value: React.ReactNode;
|
|
|
|
|
/** Tooltip to display. If this is provided then this will be rendered as the tooltip content. If
|
|
|
|
|
* not, the value will be displayed as the tooltip content. Default: undefined */
|
|
|
|
|
tooltip?: React.ReactNode;
|
|
|
|
|
/** If set to `true` the text inside the tooltip will break on words instead of any character.
|
|
|
|
|
* By default the tooltip text breaks on any character. Default: false */
|
|
|
|
|
tooltipBreakOnWord?: boolean;
|
|
|
|
|
className?: string;
|
2025-07-07 15:29:09 +00:00
|
|
|
tooltipPosition?: "top" | "bottom" | "left" | "right";
|
2024-07-11 13:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseClass = "tooltip-truncated-text";
|
|
|
|
|
|
|
|
|
|
const TooltipTruncatedText = ({
|
|
|
|
|
value,
|
|
|
|
|
tooltip,
|
|
|
|
|
tooltipBreakOnWord = false,
|
|
|
|
|
className,
|
2025-07-07 15:29:09 +00:00
|
|
|
tooltipPosition = "top",
|
2024-07-11 13:44:09 +00:00
|
|
|
}: ITooltipTruncatedTextCellProps): JSX.Element => {
|
|
|
|
|
const classNames = classnames(baseClass, className, {
|
|
|
|
|
"tooltip-break-on-word": tooltipBreakOnWord,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Tooltip visibility logic: Enable only when text is truncated
|
|
|
|
|
const ref = useRef<HTMLInputElement>(null);
|
2025-05-20 22:41:53 +00:00
|
|
|
const isTruncated = useCheckTruncatedElement(ref);
|
2024-07-11 13:44:09 +00:00
|
|
|
|
|
|
|
|
const tooltipId = uniqueId();
|
|
|
|
|
return (
|
2025-05-20 22:41:53 +00:00
|
|
|
<div className={classNames}>
|
2025-02-24 21:01:55 +00:00
|
|
|
<div className="tooltip-truncated" data-tip data-for={tooltipId}>
|
2025-05-20 22:41:53 +00:00
|
|
|
<div ref={ref} className={isTruncated ? "truncated" : undefined}>
|
|
|
|
|
{value}
|
|
|
|
|
</div>
|
2024-07-11 13:44:09 +00:00
|
|
|
</div>
|
|
|
|
|
<ReactTooltip
|
2025-07-07 15:29:09 +00:00
|
|
|
place={tooltipPosition}
|
2024-07-11 13:44:09 +00:00
|
|
|
effect="solid"
|
|
|
|
|
backgroundColor={COLORS["tooltip-bg"]}
|
|
|
|
|
id={tooltipId}
|
|
|
|
|
data-html
|
|
|
|
|
className="truncated-tooltip" // responsive widths
|
|
|
|
|
clickable
|
|
|
|
|
delayHide={200} // need delay set to hover using clickable
|
2025-05-20 22:41:53 +00:00
|
|
|
disable={!isTruncated}
|
2024-07-11 13:44:09 +00:00
|
|
|
>
|
|
|
|
|
<>
|
|
|
|
|
{tooltip ?? value}
|
|
|
|
|
<div className="safari-hack"> </div>
|
|
|
|
|
{/* Fixes triple click selecting next element in Safari */}
|
|
|
|
|
</>
|
|
|
|
|
</ReactTooltip>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TooltipTruncatedText;
|