import React, { useEffect, useLayoutEffect, useRef, useState } from 'react' import { createPortal } from 'react-dom' import type { FieldProps } from './field-types' const LongTextField: React.FC> = ({ value, onSave, readOnly }) => { const [isEditing, setIsEditing] = useState(false) const [localValue, setLocalValue] = useState(value || '') const textareaRef = useRef(null) const containerRef = useRef(null) const overlayRef = useRef(null) const [expanded, setExpanded] = useState(false) const [coords, setCoords] = useState<{ top: number left: number width: number height: number } | null>(null) useEffect(() => { setLocalValue(value || '') }, [value]) const updateCoords = () => { if (containerRef.current) { const rect = containerRef.current.getBoundingClientRect() setCoords({ top: rect.top, left: rect.left, width: rect.width, height: rect.height, }) } } useLayoutEffect(() => { if (expanded || isEditing) { updateCoords() } }, [expanded, isEditing]) const adjustHeight = () => { if (textareaRef.current) { textareaRef.current.style.height = 'auto' const baseHeight = Math.max(80, coords?.height || 0) const newHeight = Math.min(240, Math.max(baseHeight, textareaRef.current.scrollHeight)) textareaRef.current.style.height = `${newHeight}px` } } useLayoutEffect(() => { if (isEditing) { adjustHeight() } }, [isEditing, localValue]) useEffect(() => { if (isEditing && textareaRef.current) { textareaRef.current.focus() const len = textareaRef.current.value.length textareaRef.current.setSelectionRange(len, len) } }, [isEditing]) // Handle click outside and Escape to close expanded or editing state useEffect(() => { const handleClickOutside = (event: MouseEvent) => { const target = event.target as Node if ( containerRef.current && !containerRef.current.contains(target) && overlayRef.current && !overlayRef.current.contains(target) ) { if (isEditing) { handleBlur() } else { setExpanded(false) } } } const handleKeyDownGlobal = (event: KeyboardEvent) => { if (event.key === 'Escape') { if (isEditing) { setIsEditing(false) setLocalValue(value || '') } else { setExpanded(false) } } } const handleScrollOrResize = (event: Event) => { if (overlayRef.current && overlayRef.current.contains(event.target as Node)) { return } updateCoords() } if (expanded || isEditing) { document.addEventListener('mousedown', handleClickOutside) document.addEventListener('keydown', handleKeyDownGlobal) window.addEventListener('scroll', handleScrollOrResize, true) window.addEventListener('resize', handleScrollOrResize) } return () => { document.removeEventListener('mousedown', handleClickOutside) document.removeEventListener('keydown', handleKeyDownGlobal) window.removeEventListener('scroll', handleScrollOrResize, true) window.removeEventListener('resize', handleScrollOrResize) } }, [expanded, isEditing, localValue, value]) const handleBlur = () => { setIsEditing(false) setExpanded(false) if (localValue !== (value || '')) { onSave(localValue) } } const handlePlaceholderClick = (e: React.MouseEvent) => { e.stopPropagation() updateCoords() if (!readOnly) { setIsEditing(true) } else { setExpanded(true) } } const handleKeyDown = (e: React.KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') { e.currentTarget.blur() } } const setTextareaRef = (el: HTMLTextAreaElement | null) => { textareaRef.current = el overlayRef.current = el } return (
e.stopPropagation()} > {/* In-card placeholder: fixed 48px height so card height never changes */}
{value ? value : Empty}
{/* Expanded overlay or textarea: portaled to document.body so card border never clips it */} {(expanded || isEditing) && coords && typeof document !== 'undefined' && createPortal( isEditing ? (