2022-09-02 22:05:07 +00:00
|
|
|
import React, { useState, useRef, useLayoutEffect } from "react";
|
2022-12-07 17:59:38 +00:00
|
|
|
import { uniqueId } from "lodash";
|
2022-09-02 22:05:07 +00:00
|
|
|
|
|
|
|
|
import ReactTooltip from "react-tooltip";
|
2023-02-22 16:13:12 +00:00
|
|
|
import { DEFAULT_EMPTY_CELL_VALUE } from "utilities/constants";
|
2022-09-02 22:05:07 +00:00
|
|
|
|
|
|
|
|
interface ITruncatedTextCellProps {
|
|
|
|
|
value: string | number | boolean;
|
|
|
|
|
classes?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 15:32:55 +00:00
|
|
|
const baseClass = "truncated-cell";
|
|
|
|
|
|
2022-09-02 22:05:07 +00:00
|
|
|
const TruncatedTextCell = ({
|
|
|
|
|
value,
|
|
|
|
|
classes = "w250",
|
|
|
|
|
}: ITruncatedTextCellProps): JSX.Element => {
|
|
|
|
|
const ref = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
const [offsetWidth, setOffsetWidth] = useState(0);
|
|
|
|
|
const [scrollWidth, setScrollWidth] = useState(0);
|
|
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
if (ref?.current !== null) {
|
|
|
|
|
setOffsetWidth(ref.current.offsetWidth);
|
|
|
|
|
setScrollWidth(ref.current.scrollWidth);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-12-07 17:59:38 +00:00
|
|
|
const tooltipId = uniqueId();
|
2022-09-02 22:05:07 +00:00
|
|
|
const tooltipDisabled = offsetWidth === scrollWidth;
|
2023-02-22 16:13:12 +00:00
|
|
|
const isDefaultValue = value === DEFAULT_EMPTY_CELL_VALUE;
|
2022-09-02 22:05:07 +00:00
|
|
|
return (
|
2022-10-19 15:32:55 +00:00
|
|
|
<div ref={ref} className={`${baseClass} ${classes}`}>
|
2022-09-02 22:05:07 +00:00
|
|
|
<div
|
|
|
|
|
className={"data-table__truncated-text"}
|
|
|
|
|
data-tip
|
2022-12-07 17:59:38 +00:00
|
|
|
data-for={tooltipId}
|
2022-09-02 22:05:07 +00:00
|
|
|
data-tip-disable={tooltipDisabled}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className={`data-table__truncated-text--cell ${
|
2023-02-22 16:13:12 +00:00
|
|
|
isDefaultValue ? "text-muted" : ""
|
|
|
|
|
} ${tooltipDisabled ? "" : "truncated"}`}
|
2022-09-02 22:05:07 +00:00
|
|
|
>
|
|
|
|
|
{value}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<ReactTooltip
|
2023-01-19 14:21:52 +00:00
|
|
|
place="top"
|
2022-09-02 22:05:07 +00:00
|
|
|
effect="solid"
|
|
|
|
|
backgroundColor="#3e4771"
|
2022-12-07 17:59:38 +00:00
|
|
|
id={tooltipId}
|
2022-09-02 22:05:07 +00:00
|
|
|
data-html
|
2022-10-19 15:32:55 +00:00
|
|
|
className={"truncated-tooltip"} // responsive widths
|
2023-01-19 14:21:52 +00:00
|
|
|
clickable
|
|
|
|
|
delayHide={200} // need delay set to hover using clickable
|
2022-09-02 22:05:07 +00:00
|
|
|
>
|
2023-01-24 15:04:47 +00:00
|
|
|
<>
|
|
|
|
|
{value}
|
|
|
|
|
<div className="safari-hack"> </div>
|
|
|
|
|
{/* Fixes triple click selecting next element in Safari */}
|
|
|
|
|
</>
|
2022-09-02 22:05:07 +00:00
|
|
|
</ReactTooltip>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TruncatedTextCell;
|