/** * InputDialog — replaces window.prompt(). * Usage: * handleSave(value)} * /> */ import { useState, useEffect, useRef } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; export function InputDialog({ open, onOpenChange, title, description, label, defaultValue = '', placeholder = '', confirmLabel = 'Save', cancelLabel = 'Cancel', validate, // optional (value: string) => string | null (null = valid) onConfirm, loading = false, }) { const [value, setValue] = useState(defaultValue); const [error, setError] = useState(''); const inputRef = useRef(null); // Reset value when dialog opens useEffect(() => { if (open) { setValue(defaultValue); setError(''); // autofocus + select all after animation frame requestAnimationFrame(() => { inputRef.current?.focus(); inputRef.current?.select(); }); } }, [open, defaultValue]); const handleConfirm = () => { const trimmed = value.trim(); if (!trimmed) { setError('This field is required.'); return; } if (validate) { const msg = validate(trimmed); if (msg) { setError(msg); return; } } onConfirm?.(trimmed); }; const handleKeyDown = (e) => { if (e.key === 'Enter') { e.preventDefault(); handleConfirm(); } if (e.key === 'Escape') onOpenChange?.(false); }; return ( {title} {description && {description}}
{label && } { setValue(e.target.value); setError(''); }} onKeyDown={handleKeyDown} placeholder={placeholder} className={error ? 'border-destructive focus-visible:ring-destructive' : ''} /> {error &&

{error}

}
); }