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

مثال أساسي

أبسط طريقة لاستخدام Dropup.

إعداد بسيط

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

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

return (
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center',
cursor: 'pointer',
}}
>
<input {...getInputProps()} />
<p>اسحب الملفات هنا أو انقر للاختيار</p>

{files.length > 0 && (
<ul style={{ listStyle: 'none', padding: 0, marginTop: 20 }}>
{files.map(file => (
<li key={file.id}>
{file.name} ({(file.size / 1024).toFixed(1)} KB)
</li>
))}
</ul>
)}
</div>
);
}

مع وظيفة الرفع

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

function BasicUploadWithSubmit() {
const {
files,
actions,
state,
getDropProps,
getInputProps,
} = useDropup({
upload: {
url: '/api/upload',
method: 'POST',
},
});

return (
<div>
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center',
}}
>
<input {...getInputProps()} />
<p>أفلت الملفات هنا أو انقر للاختيار</p>
</div>

{/* قائمة الملفات */}
{files.length > 0 && (
<div style={{ marginTop: 20 }}>
<h3>الملفات المختارة:</h3>
{files.map(file => (
<div
key={file.id}
style={{
display: 'flex',
justifyContent: 'space-between',
padding: 10,
borderBottom: '1px solid #eee',
}}
>
<span>{file.name}</span>
<span>{file.status}</span>
<button onClick={() => actions.remove(file.id)}>إزالة</button>
</div>
))}
</div>
)}

{/* زر الرفع */}
<div style={{ marginTop: 20 }}>
<button
onClick={() => actions.upload()}
disabled={files.length === 0 || state.isUploading}
>
{state.isUploading ? `جاري الرفع... ${state.progress}%` : 'رفع الكل'}
</button>

<button onClick={() => actions.reset()} style={{ marginLeft: 10 }}>
مسح الكل
</button>
</div>
</div>
);
}

رفع ملف واحد

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

function SingleFileUploader() {
const { files, getDropProps, getInputProps } = useDropup({
multiple: false, // السماح بملف واحد فقط
maxFiles: 1,
});

const file = files[0];

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
{file ? (
<p>المختار: {file.name}</p>
) : (
<p>أفلت ملفاً هنا</p>
)}
</div>
);
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
},
};

مع تقييد نوع الملف

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

function ImageOnlyUploader() {
const { files, state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file }) => {
alert(`${file.name} ليست صورة صالحة`);
});
},
});

return (
<div
{...getDropProps()}
style={{
...styles.dropzone,
borderColor: state.isDragReject ? 'red' : '#ccc',
backgroundColor: state.isDragReject ? '#fff0f0' : 'white',
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p style={{ color: 'red' }}>يُقبل الصور فقط!</p>
) : (
<p>أفلت الصور هنا</p>
)}
</div>
);
}

مع الاستدعاءات

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

function UploaderWithCallbacks() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },

onFilesAdded: (newFiles) => {
console.log('تمت إضافة الملفات:', newFiles.map(f => f.name));
},

onUploadStart: (file) => {
console.log('بدء:', file.name);
},

onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},

onUploadComplete: (file, response) => {
console.log('اكتمل:', file.name, file.uploadedUrl);
},

onUploadError: (file, error) => {
console.error('فشل:', file.name, error.message);
},

onAllComplete: (allFiles) => {
const successful = allFiles.filter(f => f.status === 'complete');
console.log(`تم! ${successful.length}/${allFiles.length} نجحت`);
},
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>أفلت الملفات هنا</p>
</div>

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

زر تشغيل منفصل

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

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

return (
<div>
{/* منطقة إفلات مخفية تقبل الإفلات */}
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
padding: 20,
marginBottom: 10,
}}
>
<input {...getInputProps()} />
<p>أفلت الملفات هنا</p>
</div>

{/* زر منفصل لفتح حوار الملفات */}
<button onClick={openFileDialog}>
تصفح الملفات
</button>

{files.length > 0 && (
<p>تم اختيار {files.length} ملف(ات)</p>
)}
</div>
);
}

رفع تلقائي

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

function AutoUploader() {
const { files, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
autoUpload: true, // الملفات تُرفع تلقائياً عند إضافتها
onUploadComplete: (file) => {
console.log('تم الرفع:', file.uploadedUrl);
},
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>أفلت الملفات هنا - يتم رفعها تلقائياً!</p>

{files.map(file => (
<div key={file.id}>
{file.name}:
{file.status === 'uploading' && ` ${file.progress}%`}
{file.status === 'complete' && ' تم!'}
{file.status === 'error' && ` خطأ: ${file.error?.message}`}
</div>
))}
</div>
);
}

الخطوات التالية