ข้ามไปยังเนื้อหาหลัก

Options Reference

ตัวเลือกการกำหนดค่าทั้งหมดสำหรับ useDropup hook

Validation Options

accept

ประเภทไฟล์ที่ยอมรับ

// MIME type เดียว
useDropup({ accept: 'image/png' });

// หลาย MIME types
useDropup({ accept: 'image/png, image/jpeg, image/gif' });

// Wildcard
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: [
// การตรวจสอบแบบ sync
(file) => {
if (file.name.includes('draft')) {
return 'ไม่อนุญาตไฟล์ draft';
}
return true;
},
// การตรวจสอบแบบ async
async (file) => {
const exists = await checkServerDuplicate(file);
return exists ? 'ไฟล์มีอยู่แล้ว' : true;
},
],
});
ประเภทค่าเริ่มต้น
ValidationRule[][]

ประเภท ValidationRule:

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

Behavior Options

multiple

อนุญาตให้เลือกหลายไฟล์

useDropup({ multiple: true });  // หลายไฟล์
useDropup({ multiple: false }); // ไฟล์เดียวเท่านั้น
ประเภทค่าเริ่มต้น
booleantrue

disabled

ปิดใช้งาน dropzone

useDropup({ disabled: true });
ประเภทค่าเริ่มต้น
booleanfalse

autoUpload

เริ่มอัปโหลดอัตโนมัติเมื่อเพิ่มไฟล์

useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
ประเภทค่าเริ่มต้น
booleanfalse

generatePreviews

สร้าง preview URLs สำหรับไฟล์รูปภาพ

useDropup({ generatePreviews: true });

// เข้าถึงพรีวิว
files[0].preview // "blob:http://..."
ประเภทค่าเริ่มต้น
booleantrue

Upload Configuration

upload

กำหนดค่าปลายทางและพฤติกรรมการอัปโหลด

การกำหนดค่าตาม URL

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

Properties ของ UploadConfig:

Propertyประเภทค่าเริ่มต้นคำอธิบาย
urlstringจำเป็นURL endpoint สำหรับอัปโหลด
methodstring'POST'HTTP method
headersRecord<string, string>{}Headers ของ request
fieldNamestring'file'ชื่อ form field สำหรับไฟล์
withCredentialsbooleanfalseรวม cookies
timeoutnumber0Request timeout (ms)
dataRecord<string, unknown>{}ข้อมูล form เพิ่มเติม

Custom 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:

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

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

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

Cloud Uploaders

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 Options

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}`,
},
},

// Callbacks
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} ไฟล์`);
},
});