import React, { useState, useEffect, useRef, useCallback } from 'react'; import styles from './FormGeneratorTable.module.css'; export interface ExpandableDetailRowProps { open: boolean; colSpan: number; children: React.ReactNode; } /** * Table detail row with smooth expand/collapse (grid 0fr → 1fr). * Stays mounted until the close animation finishes. */ export function ExpandableDetailRow({ open, colSpan, children }: ExpandableDetailRowProps) { const [mounted, setMounted] = useState(open); const [revealed, setRevealed] = useState(false); const openRef = useRef(open); openRef.current = open; useEffect(() => { if (open) { setMounted(true); const id = requestAnimationFrame(() => { requestAnimationFrame(() => { if (openRef.current) setRevealed(true); }); }); return () => cancelAnimationFrame(id); } setRevealed(false); const fallback = window.setTimeout(() => { if (!openRef.current) setMounted(false); }, 420); return () => window.clearTimeout(fallback); }, [open]); const handleTransitionEnd = useCallback((e: React.TransitionEvent) => { if (e.target !== e.currentTarget) return; if (e.propertyName !== 'grid-template-rows') return; if (!openRef.current) { setMounted(false); } }, []); if (!mounted) return null; return ( e.stopPropagation()} >
{children}
); } export default ExpandableDetailRow;