Zum Hauptinhalt springen

Optionsreferenz

Alle Konfigurationsoptionen für den useDropup Hook.

Validierungsoptionen

accept

Akzeptierte Dateitypen.

// Einzelner MIME-Typ
useDropup({ accept: 'image/png' });

// Mehrere MIME-Typen
useDropup({ accept: 'image/png, image/jpeg, image/gif' });

// Wildcard
useDropup({ accept: 'image/*' });

// Nach Erweiterung
useDropup({ accept: '.pdf, .doc, .docx' });

// Array-Format
useDropup({ accept: ['image/*', 'application/pdf', '.txt'] });
TypStandard
string | string[]undefined (alle Dateien)

maxSize

Maximale Dateigröße in Bytes.

useDropup({ maxSize: 10 * 1024 * 1024 }); // 10MB
TypStandard
numberundefined (kein Limit)

minSize

Minimale Dateigröße in Bytes.

useDropup({ minSize: 1024 }); // 1KB Minimum
TypStandard
numberundefined (kein Limit)

maxFiles

Maximale Anzahl erlaubter Dateien.

useDropup({ maxFiles: 5 });
TypStandard
numberundefined (kein Limit)

maxWidth / maxHeight

Maximale Bildabmessungen in Pixeln.

useDropup({
accept: 'image/*',
maxWidth: 4096,
maxHeight: 4096,
});
TypStandard
numberundefined (kein Limit)

minWidth / minHeight

Minimale Bildabmessungen in Pixeln.

useDropup({
accept: 'image/*',
minWidth: 100,
minHeight: 100,
});
TypStandard
numberundefined (kein Limit)

customRules

Benutzerdefinierte Validierungsfunktionen.

useDropup({
customRules: [
// Synchrone Validierung
(file) => {
if (file.name.includes('draft')) {
return 'Entwurfsdateien nicht erlaubt';
}
return true;
},
// Asynchrone Validierung
async (file) => {
const exists = await checkServerDuplicate(file);
return exists ? 'Datei existiert bereits' : true;
},
],
});
TypStandard
ValidationRule[][]

ValidationRule-Typ:

type ValidationRule = (file: File) => boolean | string | Promise<boolean | string>;

Verhaltensoptionen

multiple

Mehrfachauswahl von Dateien erlauben.

useDropup({ multiple: true });  // Mehrere Dateien
useDropup({ multiple: false }); // Nur eine Datei
TypStandard
booleantrue

disabled

Ablagezone deaktivieren.

useDropup({ disabled: true });
TypStandard
booleanfalse

autoUpload

Upload automatisch starten, wenn Dateien hinzugefügt werden.

useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
TypStandard
booleanfalse

generatePreviews

Vorschau-URLs für Bilddateien generieren.

useDropup({ generatePreviews: true });

// Auf Vorschau zugreifen
files[0].preview // "blob:http://..."
TypStandard
booleantrue

Upload-Konfiguration

upload

Konfigurieren Sie das Upload-Ziel und -Verhalten.

URL-basierte Konfiguration

useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': 'Bearer token',
},
fieldName: 'file',
withCredentials: true,
timeout: 30000,
},
});

UploadConfig-Eigenschaften:

EigenschaftTypStandardBeschreibung
urlstringErforderlichUpload-Endpunkt-URL
methodstring'POST'HTTP-Methode
headersRecord<string, string>{}Request-Header
fieldNamestring'file'Formularfeldname für Datei
withCredentialsbooleanfalseCookies einschließen
timeoutnumber0Request-Timeout (ms)
dataRecord<string, unknown>{}Zusätzliche Formulardaten

Benutzerdefinierter Uploader

useDropup({
upload: async (file, options) => {
const formData = new FormData();
formData.append('file', file.file);

const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
signal: options.signal,
});

const data = await response.json();
return { url: data.url };
},
});

CustomUploader-Typ:

type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;

interface UploadOptions {
signal: AbortSignal;
onProgress: (progress: number) => void;
}

interface UploadResult {
url?: string;
response?: unknown;
}

Cloud-Uploader

import { createS3Uploader } from '@samithahansaka/dropup/cloud/s3';

useDropup({
upload: createS3Uploader({
getPresignedUrl: async (file) => {
const res = await fetch('/api/s3-presign', {
method: 'POST',
body: JSON.stringify({ filename: file.name, type: file.type }),
});
return res.json();
},
}),
});

Callback-Optionen

onFilesAdded

Wird aufgerufen, wenn Dateien hinzugefügt werden (nach Validierung).

useDropup({
onFilesAdded: (files) => {
console.log('Hinzugefügt:', files.map(f => f.name));
},
});
Typ
(files: DropupFile[]) => void

onFileRemoved

Wird aufgerufen, wenn eine Datei entfernt wird.

useDropup({
onFileRemoved: (file) => {
console.log('Entfernt:', file.name);
},
});
Typ
(file: DropupFile) => void

onValidationError

Wird aufgerufen, wenn Dateien die Validierung nicht bestehen.

useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
console.log(`${file.name}: ${errors.join(', ')}`);
});
},
});
Typ
(errors: ValidationError[]) => void

onUploadStart

Wird aufgerufen, wenn eine Datei mit dem Upload beginnt.

useDropup({
onUploadStart: (file) => {
console.log('Upload gestartet:', file.name);
},
});
Typ
(file: DropupFile) => void

onUploadProgress

Wird während des Upload-Fortschritts aufgerufen.

useDropup({
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
});
Typ
(file: DropupFile, progress: number) => void

onUploadComplete

Wird aufgerufen, wenn ein Datei-Upload abgeschlossen ist.

useDropup({
onUploadComplete: (file, response) => {
console.log('Hochgeladen:', file.uploadedUrl);
console.log('Server-Antwort:', response);
},
});
Typ
(file: DropupFile, response: unknown) => void

onUploadError

Wird aufgerufen, wenn ein Datei-Upload fehlschlägt.

useDropup({
onUploadError: (file, error) => {
console.error(`Fehlgeschlagen: ${file.name}`, error.message);
},
});
Typ
(file: DropupFile, error: DropupError) => void

onAllComplete

Wird aufgerufen, wenn alle Dateien hochgeladen sind.

useDropup({
onAllComplete: (files) => {
const successful = files.filter(f => f.status === 'complete');
console.log(`${successful.length}/${files.length} erfolgreich hochgeladen`);
},
});
Typ
(files: DropupFile[]) => void

Vollständiges Beispiel

const { files, actions, state, getDropProps, getInputProps } = useDropup({
// Validierung
accept: ['image/*', 'application/pdf'],
maxSize: 10 * 1024 * 1024,
maxFiles: 5,

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

// Upload
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
},

// Callbacks
onFilesAdded: (files) => {
toast.info(`${files.length} Datei(en) hinzugefügt`);
},
onValidationError: (errors) => {
toast.error(`${errors.length} Datei(en) abgelehnt`);
},
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
onUploadComplete: (file) => {
toast.success(`${file.name} hochgeladen!`);
},
onUploadError: (file, error) => {
toast.error(`${file.name} fehlgeschlagen: ${error.message}`);
},
onAllComplete: (files) => {
const count = files.filter(f => f.status === 'complete').length;
toast.success(`Fertig! ${count} Dateien hochgeladen.`);
},
});