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

ตัวอย่างพื้นฐาน

วิธีที่ง่ายที่สุดในการใช้งาน 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>
);
}

พร้อม Callbacks

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>
);
}

ปุ่มแยกเพื่อเปิด Dialog

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>

{/* ปุ่มแยกเพื่อเปิด dialog */}
<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>
);
}

ขั้นตอนถัดไป