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

Return Values Reference

ทุกอย่างที่ส่งคืนโดย useDropup hook

ภาพรวม

const {
files, // อาร์เรย์ของ file objects
state, // สถานะปัจจุบัน
actions, // actions ที่พร้อมใช้
getDropProps, // Props สำหรับ drop zone
getInputProps, // Props สำหรับ input element
openFileDialog, // เปิด file picker
} = useDropup();

files

อาร์เรย์ของ DropupFile objects แทนไฟล์ทั้งหมดที่เพิ่ม

const { files } = useDropup();

files.forEach(file => {
console.log(file.id); // "abc123"
console.log(file.name); // "photo.jpg"
console.log(file.size); // 1234567
console.log(file.type); // "image/jpeg"
console.log(file.status); // "idle" | "uploading" | "complete" | "error"
console.log(file.progress); // 0-100
console.log(file.preview); // "blob:http://..." (รูปภาพเท่านั้น)
console.log(file.uploadedUrl); // "https://..." (หลังอัปโหลด)
console.log(file.error); // DropupError (ถ้าล้มเหลว)
});

Properties ของ DropupFile

Propertyประเภทคำอธิบาย
idstringตัวระบุเฉพาะ
fileFileFile object ดั้งเดิมของเบราว์เซอร์
namestringชื่อไฟล์
sizenumberขนาดเป็นไบต์
typestringMIME type
statusFileStatusสถานะปัจจุบัน
progressnumberความคืบหน้าการอัปโหลด (0-100)
previewstring | undefinedPreview URL สำหรับรูปภาพ
uploadedUrlstring | undefinedURL หลังอัปโหลดสำเร็จ
responseunknownข้อมูลตอบกลับจากเซิร์ฟเวอร์
errorDropupError | undefinedข้อผิดพลาดถ้าอัปโหลดล้มเหลว
metaRecord<string, unknown>Metadata ที่กำหนดเอง

ค่า FileStatus

สถานะคำอธิบาย
'idle'เพิ่มไฟล์แล้ว ยังไม่อัปโหลด
'uploading'กำลังอัปโหลด
'complete'อัปโหลดสำเร็จ
'error'อัปโหลดล้มเหลว
'paused'หยุดการอัปโหลดชั่วคราว (การอัปโหลดแบบ resumable)

state

สถานะปัจจุบันของ dropzone

const { state } = useDropup();

console.log(state.isDragging); // true เมื่อลากอยู่เหนือ
console.log(state.isDragAccept); // true เมื่อไฟล์ที่ลากผ่านการตรวจสอบ
console.log(state.isDragReject); // true เมื่อไฟล์ที่ลากไม่ผ่านการตรวจสอบ
console.log(state.isUploading); // true เมื่อมีไฟล์กำลังอัปโหลด
console.log(state.progress); // ความคืบหน้าโดยรวม 0-100
console.log(state.status); // 'idle' | 'uploading' | 'complete' | 'error'

Properties ของ State

Propertyประเภทคำอธิบาย
isDraggingbooleanกำลังลากไฟล์อยู่เหนือ drop zone
isDragAcceptbooleanไฟล์ที่ลากผ่านการตรวจสอบ
isDragRejectbooleanไฟล์ที่ลากไม่ผ่านการตรวจสอบ
isUploadingbooleanมีไฟล์กำลังอัปโหลดอยู่
progressnumberความคืบหน้าเฉลี่ยของไฟล์ทั้งหมด (0-100)
statusDropupStatusสถานะการอัปโหลดโดยรวม

ค่า DropupStatus

สถานะคำอธิบาย
'idle'ไม่มีการอัปโหลด
'uploading'มีไฟล์หนึ่งหรือหลายไฟล์กำลังอัปโหลด
'complete'การอัปโหลดทั้งหมดเสร็จสำเร็จ
'error'มีการอัปโหลดหนึ่งหรือหลายรายการล้มเหลว

actions

Object ที่มี actions ทั้งหมดที่พร้อมใช้

const { actions } = useDropup();

// เริ่มอัปโหลด
actions.upload(); // อัปโหลดไฟล์ idle ทั้งหมด
actions.upload(['id1', 'id2']); // อัปโหลดไฟล์ที่ระบุ

// ยกเลิกการอัปโหลด
actions.cancel(); // ยกเลิกการอัปโหลดทั้งหมด
actions.cancel('file-id'); // ยกเลิกไฟล์ที่ระบุ

// ลบไฟล์
actions.remove('file-id'); // ลบไฟล์ที่ระบุ
actions.reset(); // ลบไฟล์ทั้งหมด

// ลองใหม่การอัปโหลดที่ล้มเหลว
actions.retry(); // ลองใหม่ทั้งหมดที่ล้มเหลว
actions.retry(['id1']); // ลองใหม่ไฟล์ที่ระบุ

// เพิ่มไฟล์แบบ programmatic
actions.addFiles(fileList); // เพิ่ม File[] หรือ FileList

// อัปเดต metadata ของไฟล์
actions.updateFileMeta('file-id', { tag: 'important' });

Action Methods

upload(fileIds?)

เริ่มอัปโหลดไฟล์

// อัปโหลดไฟล์ idle ทั้งหมด
actions.upload();

// อัปโหลดไฟล์ที่ระบุ
actions.upload(['file-id-1', 'file-id-2']);
พารามิเตอร์ประเภทคำอธิบาย
fileIdsstring[] (ไม่บังคับ)ID ไฟล์ที่จะอัปโหลด

cancel(fileId?)

ยกเลิกการอัปโหลดที่กำลังดำเนินการ

// ยกเลิกการอัปโหลดทั้งหมด
actions.cancel();

// ยกเลิกไฟล์ที่ระบุ
actions.cancel('file-id');
พารามิเตอร์ประเภทคำอธิบาย
fileIdstring (ไม่บังคับ)ID ไฟล์ที่จะยกเลิก

remove(fileId)

ลบไฟล์ออกจากรายการ

actions.remove('file-id');
พารามิเตอร์ประเภทคำอธิบาย
fileIdstringID ไฟล์ที่จะลบ

reset()

ลบไฟล์ทั้งหมดและรีเซ็ต state

actions.reset();

retry(fileIds?)

ลองใหม่การอัปโหลดที่ล้มเหลว

// ลองใหม่การอัปโหลดที่ล้มเหลวทั้งหมด
actions.retry();

// ลองใหม่ไฟล์ที่ระบุ
actions.retry(['file-id-1']);
พารามิเตอร์ประเภทคำอธิบาย
fileIdsstring[] (ไม่บังคับ)ID ไฟล์ที่จะลองใหม่

addFiles(files)

เพิ่มไฟล์แบบ programmatic

// จาก clipboard
document.addEventListener('paste', (e) => {
const files = e.clipboardData?.files;
if (files) {
actions.addFiles(files);
}
});

// จากแหล่งอื่น
actions.addFiles([file1, file2]);
พารามิเตอร์ประเภทคำอธิบาย
filesFile[] | FileListไฟล์ที่จะเพิ่ม

updateFileMeta(fileId, meta)

อัปเดต metadata ของไฟล์

actions.updateFileMeta('file-id', {
description: 'รูปถ่ายของฉัน',
tags: ['vacation', '2024'],
});
พารามิเตอร์ประเภทคำอธิบาย
fileIdstringID ไฟล์ที่จะอัปเดต
metaRecord<string, unknown>Metadata ที่จะรวม

getDropProps

ส่งคืน props ที่จะ spread บน drop zone element

const { getDropProps, getInputProps } = useDropup();

<div {...getDropProps()}>
<input {...getInputProps()} />
วางไฟล์ที่นี่
</div>

พร้อม Props ที่กำหนดเอง

<div
{...getDropProps({
className: 'my-dropzone',
onClick: (e) => {
// Click handler ที่กำหนดเองของคุณ
console.log('คลิกแล้ว!');
},
})}
>
<input {...getInputProps()} />
</div>

Props ที่ส่งคืน

Propertyประเภทคำอธิบาย
onDragEnterfunctionDrag enter handler
onDragOverfunctionDrag over handler
onDragLeavefunctionDrag leave handler
onDropfunctionDrop handler
onClickfunctionClick handler (เปิด dialog)
rolestringAccessibility role
tabIndexnumberTab index สำหรับ focus

getInputProps

ส่งคืน props ที่จะ spread บน hidden file input

const { getInputProps } = useDropup();

<input {...getInputProps()} />

พร้อม Props ที่กำหนดเอง

<input
{...getInputProps({
id: 'file-input',
name: 'files',
})}
/>

Props ที่ส่งคืน

Propertyประเภทคำอธิบาย
type'file'ประเภท input
acceptstringประเภทไฟล์ที่ยอมรับ
multiplebooleanอนุญาตหลายไฟล์
onChangefunctionChange handler
styleobjectStyles ที่ซ่อน

openFileDialog

ฟังก์ชันเปิด file picker แบบ programmatic

const { openFileDialog } = useDropup();

<button onClick={openFileDialog}>
เรียกดูไฟล์
</button>

ตัวอย่างการใช้งาน

รายการไฟล์แบบสมบูรณ์

function FileList() {
const { files, actions, state } = useDropup({
upload: { url: '/api/upload' },
});

return (
<div>
<h3>ไฟล์ ({files.length})</h3>

{files.map(file => (
<div key={file.id}>
{file.preview && (
<img src={file.preview} alt="" width={50} />
)}
<span>{file.name}</span>
<span>{(file.size / 1024).toFixed(1)} KB</span>
<span>{file.status}</span>

{file.status === 'uploading' && (
<progress value={file.progress} max={100} />
)}

{file.status === 'error' && (
<>
<span style={{ color: 'red' }}>{file.error?.message}</span>
<button onClick={() => actions.retry([file.id])}>ลองใหม่</button>
</>
)}

<button onClick={() => actions.remove(file.id)}>ลบ</button>
</div>
))}

{state.isUploading && (
<p>กำลังอัปโหลด... {state.progress}%</p>
)}

<button
onClick={() => actions.upload()}
disabled={state.isUploading || files.length === 0}
>
อัปโหลดทั้งหมด
</button>

<button onClick={() => actions.reset()}>
ล้างทั้งหมด
</button>
</div>
);
}

การจัดสไตล์ Drag State

function StyledDropzone() {
const { state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
});

const getStyle = () => {
if (state.isDragAccept) return { borderColor: 'green', backgroundColor: '#e8f5e9' };
if (state.isDragReject) return { borderColor: 'red', backgroundColor: '#ffebee' };
if (state.isDragging) return { borderColor: 'blue', backgroundColor: '#e3f2fd' };
return { borderColor: 'gray', backgroundColor: 'white' };
};

return (
<div
{...getDropProps()}
style={{
border: '2px dashed',
borderRadius: 8,
padding: 40,
textAlign: 'center',
transition: 'all 0.2s',
...getStyle(),
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p>ยอมรับเฉพาะรูปภาพเท่านั้น!</p>
) : (
<p>วางรูปภาพที่นี่</p>
)}
</div>
);
}