مرجع الخيارات
جميع خيارات التكوين لخطاف 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
| النوع | الافتراضي |
|---|---|
number | undefined (بدون حد) |
minSize
الحد الأدنى لحجم الملف بالبايتات.
useDropup({ minSize: 1024 }); // 1KB كحد أدنى
| النوع | الافتراضي |
|---|---|
number | undefined (بدون حد) |
maxFiles
الحد الأقصى لعدد الملفات المسموح بها.
useDropup({ maxFiles: 5 });
| النوع | الافتراضي |
|---|---|
number | undefined (بدون حد) |
maxWidth / maxHeight
الحد الأقصى لأبعاد الصورة بالبكسل.
useDropup({
accept: 'image/*',
maxWidth: 4096,
maxHeight: 4096,
});
| النوع | الافتراضي |
|---|---|
number | undefined (بدون حد) |
minWidth / minHeight
الحد الأدنى لأبعاد الصورة بالبكسل.
useDropup({
accept: 'image/*',
minWidth: 100,
minHeight: 100,
});
| النوع | الافتراضي |
|---|---|
number | undefined (بدون حد) |
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 }); // ملف واحد فقط
| النوع | الافتراضي |
|---|---|
boolean | true |
disabled
تعطيل منطقة الإفلات.
useDropup({ disabled: true });
| النوع | الافتراضي |
|---|---|
boolean | false |
autoUpload
بدء الرفع تلقائياً عند إضافة الملفات.
useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
| النوع | الافتراضي |
|---|---|
boolean | false |
generatePreviews
إنشاء روابط معاينة لملفات الصور.
useDropup({ generatePreviews: true });
// الوصول للمعاينة
files[0].preview // "blob:http://..."
| النوع | الافتراضي |
|---|---|
boolean | true |
تكوين الرفع
upload
تكوين وجهة الرفع والسلوك.
التكوين القائم على URL
useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': 'Bearer token',
},
fieldName: 'file',
withCredentials: true,
timeout: 30000,
},
});
خصائص UploadConfig:
| الخاصية | النوع | الافتراضي | الوصف |
|---|---|---|---|
url | string | مطلوب | رابط نقطة نهاية الرفع |
method | string | 'POST' | طريقة HTTP |
headers | Record<string, string> | {} | ترويسات الطلب |
fieldName | string | 'file' | اسم حقل النموذج للملف |
withCredentials | boolean | false | تضمين ملفات تعريف الارتباط |
timeout | number | 0 | مهلة الطلب (مللي ثانية) |
data | Record<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} ملفات.`);
},
});