Zum Hauptinhalt springen

useDropup Hook

Der Haupt-Hook für Datei-Uploads mit Drag-and-Drop-Unterstützung.

Import

import { useDropup } from '@samithahansaka/dropup';

Grundlegende Verwendung

const {
files,
state,
actions,
getDropProps,
getInputProps,
openFileDialog,
} = useDropup(options);

Parameter

options (optional)

Siehe Optionen für vollständige Details.

interface UseDropupOptions {
// Validierung
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];

// Verhalten
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;

// Upload-Konfiguration
upload?: UploadConfig | CustomUploader;

// Callbacks
onFilesAdded?: (files: DropupFile[]) => void;
onFileRemoved?: (file: DropupFile) => void;
onValidationError?: (errors: ValidationError[]) => void;
onUploadStart?: (file: DropupFile) => void;
onUploadProgress?: (file: DropupFile, progress: number) => void;
onUploadComplete?: (file: DropupFile, response: unknown) => void;
onUploadError?: (file: DropupFile, error: DropupError) => void;
onAllComplete?: (files: DropupFile[]) => void;
}

Rückgabewert

Siehe Rückgabewerte für vollständige Details.

interface UseDropupReturn {
// Zustand
files: DropupFile[];
state: DropupState;

// Aktionen
actions: DropupActions;

// Prop-Getter
getDropProps: <E extends HTMLElement>(props?: HTMLAttributes<E>) => DropZoneProps<E>;
getInputProps: (props?: InputHTMLAttributes) => InputProps;

// Hilfsfunktionen
openFileDialog: () => void;
}

Kurzreferenz

Files-Array

const { files } = useDropup();

// Jede Datei hat:
files[0].id // Eindeutige ID
files[0].file // Ursprüngliches File-Objekt
files[0].name // Dateiname
files[0].size // Größe in Bytes
files[0].type // MIME-Typ
files[0].preview // Vorschau-URL (Bilder)
files[0].status // 'idle' | 'uploading' | 'complete' | 'error'
files[0].progress // 0-100
files[0].uploadedUrl // URL nach Upload
files[0].error // Fehler bei Fehlschlag

State-Objekt

const { state } = useDropup();

state.isDragging // Dateien werden über Zone gezogen
state.isDragAccept // Gezogene Dateien sind gültig
state.isDragReject // Gezogene Dateien sind ungültig
state.isUploading // Eine Datei wird hochgeladen
state.progress // Gesamtfortschritt 0-100
state.status // 'idle' | 'uploading' | 'complete' | 'error'

Actions-Objekt

const { actions } = useDropup();

actions.upload(fileIds?) // Upload starten
actions.cancel(fileId?) // Upload abbrechen
actions.remove(fileId) // Datei entfernen
actions.reset() // Alles löschen
actions.retry(fileIds?) // Fehlgeschlagene wiederholen
actions.addFiles(files) // Dateien programmatisch hinzufügen
actions.updateFileMeta(id, meta) // Metadaten aktualisieren

Prop-Getter

const { getDropProps, getInputProps } = useDropup();

// Auf Ablagezone anwenden
<div {...getDropProps()}>
<input {...getInputProps()} />
Hier ablegen
</div>

// Mit benutzerdefinierten Props
<div {...getDropProps({ className: 'meine-dropzone', onClick: handleClick })}>
...
</div>

Beispiele

Minimales Beispiel

function Uploader() {
const { files, getDropProps, getInputProps } = useDropup();

return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>Dateien hier ablegen</p>
<ul>
{files.map(f => <li key={f.id}>{f.name}</li>)}
</ul>
</div>
);
}

Mit Upload

function Uploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: { 'Authorization': 'Bearer token' },
},
});

return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>Dateien hier ablegen</p>
</div>

{files.map(f => (
<div key={f.id}>
{f.name} - {f.status}
{f.status === 'uploading' && ` ${f.progress}%`}
</div>
))}

<button onClick={() => actions.upload()}>Alle hochladen</button>
</div>
);
}

Mit allen Optionen

function VollausgestatteterUploader() {
const {
files,
state,
actions,
getDropProps,
getInputProps,
} = useDropup({
// Validierung
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
maxFiles: 5,

// Verhalten
multiple: true,
autoUpload: true,
generatePreviews: true,

// Upload
upload: {
url: '/api/upload',
method: 'POST',
},

// Callbacks
onFilesAdded: (files) => console.log('Hinzugefügt:', files),
onUploadComplete: (file) => console.log('Fertig:', file.uploadedUrl),
onUploadError: (file, err) => console.error('Fehler:', err),
onAllComplete: () => console.log('Alle Uploads abgeschlossen!'),
});

return (
<div>
<div
{...getDropProps()}
style={{
border: `2px dashed ${state.isDragAccept ? 'green' : state.isDragReject ? 'red' : 'gray'}`,
padding: 40,
}}
>
<input {...getInputProps()} />
<p>Bilder hier ablegen (max 5, je 10MB)</p>
</div>

{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} width={50} />}
<span>{file.name}</span>
<span>{file.status}</span>
{file.status === 'uploading' && <span>{file.progress}%</span>}
<button onClick={() => actions.remove(file.id)}>×</button>
</div>
))}

{state.isUploading && <p>Wird hochgeladen... {state.progress}%</p>}
</div>
);
}