|
| 1 | +import { useState, useRef, useLayoutEffect } from 'react' |
| 2 | + |
| 3 | +interface EmployeeNameCellProps { |
| 4 | + name: string |
| 5 | + notes?: string | null |
| 6 | +} |
| 7 | + |
| 8 | +export function EmployeeNameCell({ name, notes }: EmployeeNameCellProps) { |
| 9 | + const [isExpanded, setIsExpanded] = useState(false) |
| 10 | + const [isTruncated, setIsTruncated] = useState(false) |
| 11 | + const textRef = useRef<HTMLDivElement>(null) |
| 12 | + |
| 13 | + useLayoutEffect(() => { |
| 14 | + const checkTruncation = () => { |
| 15 | + if (textRef.current) { |
| 16 | + const element = textRef.current |
| 17 | + setIsTruncated(element.scrollHeight > element.clientHeight) |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + checkTruncation() |
| 22 | + const timeoutId = setTimeout(checkTruncation, 0) |
| 23 | + |
| 24 | + return () => clearTimeout(timeoutId) |
| 25 | + }, [notes, isExpanded]) |
| 26 | + |
| 27 | + return ( |
| 28 | + <div className="max-w-[300px] pl-4"> |
| 29 | + <div className="text-sm font-bold">{name}</div> |
| 30 | + {notes && ( |
| 31 | + <div className="mt-1 mb-2 ml-2 border-l-2 border-gray-200 py-1 pl-2 text-xs text-gray-500 italic"> |
| 32 | + <div className="relative"> |
| 33 | + <p |
| 34 | + ref={textRef} |
| 35 | + className={`break-words whitespace-normal ${isExpanded ? '' : 'line-clamp-2'}`} |
| 36 | + > |
| 37 | + {notes} |
| 38 | + </p> |
| 39 | + {isTruncated && !isExpanded && ( |
| 40 | + <button |
| 41 | + onClick={(e) => { |
| 42 | + e.stopPropagation() |
| 43 | + setIsExpanded(true) |
| 44 | + }} |
| 45 | + className="absolute right-0 bottom-0 bg-gradient-to-l from-white from-50% via-white via-70% to-transparent pl-10 font-semibold text-gray-500 not-italic hover:text-blue-700" |
| 46 | + > |
| 47 | + read more |
| 48 | + </button> |
| 49 | + )} |
| 50 | + </div> |
| 51 | + {isExpanded && ( |
| 52 | + <button |
| 53 | + onClick={(e) => { |
| 54 | + e.stopPropagation() |
| 55 | + setIsExpanded(false) |
| 56 | + }} |
| 57 | + className="mt-1 font-semibold text-gray-500 not-italic hover:text-blue-700" |
| 58 | + > |
| 59 | + read less |
| 60 | + </button> |
| 61 | + )} |
| 62 | + </div> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + ) |
| 66 | +} |
0 commit comments