2022-09-02 22:05:07 +00:00
|
|
|
import React, { useState, useRef, useLayoutEffect } from "react";
|
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
|
|
|
|
|
|
import ReactTooltip from "react-tooltip";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const id = uuidv4();
|
|
|
|
|
const tooltipDisabled = offsetWidth === scrollWidth;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
data-for={id}
|
|
|
|
|
data-tip-disable={tooltipDisabled}
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className={`data-table__truncated-text--cell ${
|
2022-09-07 19:12:37 +00:00
|
|
|
tooltipDisabled ? "" : "truncated"
|
2022-09-02 22:05:07 +00:00
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{value}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<ReactTooltip
|
|
|
|
|
place="bottom"
|
|
|
|
|
effect="solid"
|
|
|
|
|
backgroundColor="#3e4771"
|
|
|
|
|
id={id}
|
|
|
|
|
data-html
|
2022-10-19 15:32:55 +00:00
|
|
|
className={"truncated-tooltip"} // responsive widths
|
2022-09-02 22:05:07 +00:00
|
|
|
>
|
2022-10-19 15:32:55 +00:00
|
|
|
{value}
|
2022-09-02 22:05:07 +00:00
|
|
|
</ReactTooltip>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TruncatedTextCell;
|