تخطي إلى المحتوى الرئيسي

مرجع الخيارات

جميع خيارات التكوين لخطاف useDropup.

خيارات التحقق

accept

أنواع الملفات المقبولة.

// نوع MIME واحد
useDropup({ accept: 'image/png' });

// أنواع MIME متعددة
useDropup({ accept: 'image/png, image/jpeg, image/gif' });

// علامة شاملة
useDropup({ accept: 'image/*' });

// حسب الامتداد
useDropup({ accept: '.pdf, .doc, .docx' });

// صيغة المصفوفة
useDropup({ accept: ['image/*', 'application/pdf', '.txt'] });
النوعالافتراضي
string | string[]undefined (جميع الملفات)

maxSize

الحد الأقصى لحجم الملف بالبايتات.

useDropup({ maxSize: 10 * 1024 * 1024 }); // 10MB
النوعالافتراضي
numberundefined (بدون حد)

minSize

الحد الأدنى لحجم الملف بالبايتات.

useDropup({ minSize: 1024 }); // 1KB كحد أدنى
النوعالافتراضي
numberundefined (بدون حد)

maxFiles

الحد الأقصى لعدد الملفات المسموح بها.

useDropup({ maxFiles: 5 });
النوعالافتراضي
numberundefined (بدون حد)

maxWidth / maxHeight

الحد الأقصى لأبعاد الصورة بالبكسل.

useDropup({
accept: 'image/*',
maxWidth: 4096,
maxHeight: 4096,
});
النوعالافتراضي
numberundefined (بدون حد)

minWidth / minHeight

الحد الأدنى لأبعاد الصورة بالبكسل.

useDropup({
accept: 'image/*',
minWidth: 100,
minHeight: 100,
});
النوعالافتراضي
numberundefined (بدون حد)

customRules

دوال تحقق مخصصة.

useDropup({
customRules: [
// تحقق متزامن
(file) => {
if (file.name.includes('draft')) {
return 'ملفات المسودة غير مسموح بها';
}
return true;
},
// تحقق غير متزامن
async (file) => {
const exists = await checkServerDuplicate(file);
return exists ? 'الملف موجود بالفعل' : true;
},
],
});
النوعالافتراضي
ValidationRule[][]

نوع ValidationRule:

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

خيارات السلوك

multiple

السماح باختيار ملفات متعددة.

useDropup({ multiple: true });  // ملفات متعددة
useDropup({ multiple: false }); // ملف واحد فقط
النوعالافتراضي
booleantrue

disabled

تعطيل منطقة الإفلات.

useDropup({ disabled: true });
النوعالافتراضي
booleanfalse

autoUpload

بدء الرفع تلقائياً عند إضافة الملفات.

useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
النوعالافتراضي
booleanfalse

generatePreviews

إنشاء روابط معاينة لملفات الصور.

useDropup({ generatePreviews: true });

// الوصول للمعاينة
files[0].preview // "blob:http://..."
النوعالافتراضي
booleantrue

تكوين الرفع

upload

تكوين وجهة الرفع والسلوك.

التكوين القائم على URL

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

خصائص UploadConfig:

الخاصيةالنوعالافتراضيالوصف
urlstringمطلوبرابط نقطة نهاية الرفع
methodstring'POST'طريقة HTTP
headersRecord<string, string>{}ترويسات الطلب
fieldNamestring'file'اسم حقل النموذج للملف
withCredentialsbooleanfalseتضمين ملفات تعريف الارتباط
timeoutnumber0مهلة الطلب (مللي ثانية)
dataRecord<string, unknown>{}بيانات نموذج إضافية

رافع مخصص

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:

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

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

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

رافعات السحابة

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();
},
}),
});

خيارات الاستدعاءات

onFilesAdded

يُستدعى عند إضافة الملفات (بعد التحقق).

useDropup({
onFilesAdded: (files) => {
console.log('تمت الإضافة:', files.map(f => f.name));
},
});
النوع
(files: DropupFile[]) => void

onFileRemoved

يُستدعى عند إزالة ملف.

useDropup({
onFileRemoved: (file) => {
console.log('تمت الإزالة:', file.name);
},
});
النوع
(file: DropupFile) => void

onValidationError

يُستدعى عند فشل الملفات في التحقق.

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

onUploadStart

يُستدعى عند بدء رفع ملف.

useDropup({
onUploadStart: (file) => {
console.log('بدء الرفع:', file.name);
},
});
النوع
(file: DropupFile) => void

onUploadProgress

يُستدعى أثناء تقدم الرفع.

useDropup({
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
});
النوع
(file: DropupFile, progress: number) => void

onUploadComplete

يُستدعى عند اكتمال رفع ملف.

useDropup({
onUploadComplete: (file, response) => {
console.log('تم الرفع:', file.uploadedUrl);
console.log('استجابة الخادم:', response);
},
});
النوع
(file: DropupFile, response: unknown) => void

onUploadError

يُستدعى عند فشل رفع ملف.

useDropup({
onUploadError: (file, error) => {
console.error(`فشل: ${file.name}`, error.message);
},
});
النوع
(file: DropupFile, error: DropupError) => void

onAllComplete

يُستدعى عند انتهاء رفع جميع الملفات.

useDropup({
onAllComplete: (files) => {
const successful = files.filter(f => f.status === 'complete');
console.log(`${successful.length}/${files.length} تم رفعها بنجاح`);
},
});
النوع
(files: DropupFile[]) => void

مثال كامل

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

// السلوك
multiple: true,
autoUpload: false,
generatePreviews: true,

// الرفع
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
},

// الاستدعاءات
onFilesAdded: (files) => {
toast.info(`تمت إضافة ${files.length} ملف(ات)`);
},
onValidationError: (errors) => {
toast.error(`تم رفض ${errors.length} ملف(ات)`);
},
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
onUploadComplete: (file) => {
toast.success(`تم رفع ${file.name}!`);
},
onUploadError: (file, error) => {
toast.error(`فشل ${file.name}: ${error.message}`);
},
onAllComplete: (files) => {
const count = files.filter(f => f.status === 'complete').length;
toast.success(`انتهى! تم رفع ${count} ملفات.`);
},
});