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

خطاف useDropup

الخطاف الرئيسي لرفع الملفات مع دعم السحب والإفلات.

الاستيراد

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

الاستخدام الأساسي

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

المعاملات

options (اختياري)

راجع الخيارات للتفاصيل الكاملة.

interface UseDropupOptions {
// التحقق
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];

// السلوك
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;

// تكوين الرفع
upload?: UploadConfig | CustomUploader;

// الاستدعاءات
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;
}

القيمة المُرجعة

راجع القيم المُرجعة للتفاصيل الكاملة.

interface UseDropupReturn {
// الحالة
files: DropupFile[];
state: DropupState;

// الإجراءات
actions: DropupActions;

// مُحصّلات الخصائص
getDropProps: <E extends HTMLElement>(props?: HTMLAttributes<E>) => DropZoneProps<E>;
getInputProps: (props?: InputHTMLAttributes) => InputProps;

// الأدوات المساعدة
openFileDialog: () => void;
}

مرجع سريع

مصفوفة الملفات

const { files } = useDropup();

// كل ملف يحتوي على:
files[0].id // معرف فريد
files[0].file // كائن File الأصلي
files[0].name // اسم الملف
files[0].size // الحجم بالبايتات
files[0].type // نوع MIME
files[0].preview // رابط المعاينة (للصور)
files[0].status // 'idle' | 'uploading' | 'complete' | 'error'
files[0].progress // 0-100
files[0].uploadedUrl // الرابط بعد الرفع
files[0].error // الخطأ في حالة الفشل

كائن الحالة

const { state } = useDropup();

state.isDragging // الملفات تُسحب فوقها
state.isDragAccept // الملفات المسحوبة صالحة
state.isDragReject // الملفات المسحوبة غير صالحة
state.isUploading // أي ملف قيد الرفع
state.progress // التقدم الإجمالي 0-100
state.status // 'idle' | 'uploading' | 'complete' | 'error'

كائن الإجراءات

const { actions } = useDropup();

actions.upload(fileIds?) // بدء الرفع
actions.cancel(fileId?) // إلغاء الرفع
actions.remove(fileId) // إزالة ملف
actions.reset() // مسح الكل
actions.retry(fileIds?) // إعادة محاولة الفاشلة
actions.addFiles(files) // إضافة ملفات برمجياً
actions.updateFileMeta(id, meta) // تحديث البيانات الوصفية

مُحصّلات الخصائص

const { getDropProps, getInputProps } = useDropup();

// تطبيق على منطقة الإفلات الخاصة بك
<div {...getDropProps()}>
<input {...getInputProps()} />
أفلت هنا
</div>

// مع خصائص مخصصة
<div {...getDropProps({ className: 'my-dropzone', onClick: handleClick })}>
...
</div>

أمثلة

مثال بسيط

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

return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>أفلت الملفات هنا</p>
<ul>
{files.map(f => <li key={f.id}>{f.name}</li>)}
</ul>
</div>
);
}

مع الرفع

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>أفلت الملفات هنا</p>
</div>

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

<button onClick={() => actions.upload()}>رفع الكل</button>
</div>
);
}

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

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

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

// الرفع
upload: {
url: '/api/upload',
method: 'POST',
},

// الاستدعاءات
onFilesAdded: (files) => console.log('تمت الإضافة:', files),
onUploadComplete: (file) => console.log('تم:', file.uploadedUrl),
onUploadError: (file, err) => console.error('خطأ:', err),
onAllComplete: () => console.log('اكتملت جميع عمليات الرفع!'),
});

return (
<div>
<div
{...getDropProps()}
style={{
border: `2px dashed ${state.isDragAccept ? 'green' : state.isDragReject ? 'red' : 'gray'}`,
padding: 40,
}}
>
<input {...getInputProps()} />
<p>أفلت الصور هنا (الحد الأقصى 5، 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>جاري الرفع... {state.progress}%</p>}
</div>
);
}