/** * 파일 업로드 컴포넌트 * * 시방서, 인정서, 전개도 등 파일 업로드 UI */ 'use client'; import { useState, useRef } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { X, Upload, FileText, CheckCircle2 } from 'lucide-react'; interface FileUploadProps { label: string; accept?: string; maxSize?: number; // MB currentFile?: { url: string; filename: string; }; onFileSelect: (file: File) => void; onFileRemove?: () => void; disabled?: boolean; required?: boolean; } export default function FileUpload({ label, accept = '*/*', maxSize = 10, // 10MB default currentFile, onFileSelect, onFileRemove, disabled = false, required = false, }: FileUploadProps) { const [selectedFile, setSelectedFile] = useState(null); const [error, setError] = useState(null); const [isDragging, setIsDragging] = useState(false); const fileInputRef = useRef(null); const handleFileChange = (file: File | null) => { if (!file) { setSelectedFile(null); setError(null); return; } // 파일 크기 검증 const fileSizeMB = file.size / (1024 * 1024); if (fileSizeMB > maxSize) { setError(`파일 크기는 ${maxSize}MB 이하여야 합니다`); return; } setError(null); setSelectedFile(file); onFileSelect(file); }; const handleInputChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] || null; handleFileChange(file); }; const handleDragEnter = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(true); }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); const file = e.dataTransfer.files?.[0] || null; handleFileChange(file); }; const handleRemove = () => { setSelectedFile(null); setError(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } onFileRemove?.(); }; const handleClick = () => { fileInputRef.current?.click(); }; return (
{/* 파일 입력 (숨김) */} {/* 드래그 앤 드롭 영역 */}
{selectedFile || currentFile ? (

{selectedFile?.name || currentFile?.filename}

{selectedFile ? `${(selectedFile.size / 1024).toFixed(1)} KB` : currentFile?.url && ( e.stopPropagation()} > 파일 보기 )}

{selectedFile && ( )}
{!disabled && ( )}
) : ( <>

클릭하거나 파일을 드래그하여 업로드

최대 {maxSize}MB

)}
{/* 에러 메시지 */} {error && (

{error}

)} {/* 도움말 */} {!error && accept !== '*/*' && (

허용 파일 형식: {accept}

)}
); }