import { Tooltip } from 'antd'; import { HTMLAttributes, ReactElement, useEffect, useRef, useState, } from 'react'; import { truncate } from '../../utils/truncate'; interface TruncatedProps extends HTMLAttributes { className?: string; ellipsis?: string; children: (text: string) => ReactElement | string; text: string; } export const Truncated = ({ className, children, text, ellipsis, ...props }: TruncatedProps) => { const refContainer = useRef(null); const refOverflowed = useRef(null); const [isTruncated, setIsTruncated] = useState(false); useEffect(() => { if (!refOverflowed.current || !refContainer.current) return; const { height: oH } = refOverflowed.current.getBoundingClientRect(); const { height: cH } = refContainer.current.getBoundingClientRect(); setIsTruncated(oH > cH); }, []); return (
{children(text)}
{isTruncated && ellipsis && (
{ellipsis}
)}
); }; export default Truncated;