import { FileIcon, RotateCcw, UploadIcon, XIcon } from 'lucide-react'; import { useRef, useState } from 'react'; import InputError from '@/components/input-error'; import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle, AttachmentTrigger, } from '@/components/ui/attachment'; import { Label } from '@/components/ui/label'; type FileUploadFieldProps = { label?: string; /** Form field name for the native file input. */ name?: string; existingFileName?: string | null; existingFileUrl?: string | null; error?: string; accept?: string; helpText?: string; }; export function FileUploadField({ label = 'File', name = 'file', existingFileName, existingFileUrl, error, accept, helpText = 'PDF, Word, PPT, Excel, gambar, video, atau zip (maks 10MB)', }: FileUploadFieldProps) { const inputRef = useRef(null); const [selectedFile, setSelectedFile] = useState(null); function handleFileChange(e: React.ChangeEvent) { setSelectedFile(e.target.files?.[0] ?? null); } function handleReset() { setSelectedFile(null); if (inputRef.current) { inputRef.current.value = ''; } } const hasNewFile = selectedFile !== null; const hasExistingFile = !hasNewFile && !!existingFileName; const displayName = selectedFile?.name ?? existingFileName ?? null; const displayUrl = hasNewFile ? null : existingFileUrl; const state = error ? 'error' : displayName ? 'done' : 'idle'; return (
inputRef.current?.click()} /> {displayName ?? 'Klik untuk pilih file'} {displayName ? hasExistingFile ? 'File saat ini — klik untuk mengganti' : 'Klik untuk mengganti file' : helpText} {displayUrl && ( e.stopPropagation()} > )} {hasNewFile && ( { e.stopPropagation(); handleReset(); }} > {hasExistingFile ? : } )}
); }