import { useRef } from 'preact/hooks'; import IconFile from '../icons/file.svg?react'; import IconTrash from '../icons/trash.svg?react'; import Button from './Button'; interface FileInputProps { files: File[]; onFilesChange: (files: File[]) => void; maxFiles?: number; disabled?: boolean; label?: string; } function FileInput({ files, onFilesChange, maxFiles = 3, disabled = false, label = 'Attach files', }: FileInputProps) { const fileInputRef = useRef(null); const handleFileSelect = (e: Event) => { const target = e.target as HTMLInputElement; if (target.files) { const newFiles = Array.from(target.files); onFilesChange([...files, ...newFiles].slice(0, maxFiles)); } }; const removeFile = (index: number) => { onFilesChange(files.filter((_, i) => i !== index)); }; const openFileDialog = () => { fileInputRef.current?.click(); }; return (
{label} (optional, max {maxFiles})
{files.map((file, index) => (
{file.name} {(file.size / 1024).toFixed(2)}KB
))} {files.length < maxFiles && ( )}
); } export default FileInput;