Bỏ qua để đến nội dung chính

Ví dụ cơ bản

Cách đơn giản nhất để sử dụng Dropup.

Thiết lập tối thiểu

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>Kéo tệp vào đây hoặc nhấp để chọn</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>
);
}

Với chức năng tải lên

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>Thả tệp vào đây hoặc nhấp để chọn</p>
</div>

{/* Danh sách tệp */}
{files.length > 0 && (
<div style={{ marginTop: 20 }}>
<h3>Các tệp đã chọn:</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)}>Xóa</button>
</div>
))}
</div>
)}

{/* Nút tải lên */}
<div style={{ marginTop: 20 }}>
<button
onClick={() => actions.upload()}
disabled={files.length === 0 || state.isUploading}
>
{state.isUploading ? `Đang tải lên... ${state.progress}%` : 'Tải lên tất cả'}
</button>

<button onClick={() => actions.reset()} style={{ marginLeft: 10 }}>
Xóa tất cả
</button>
</div>
</div>
);
}

Tải lên một tệp

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

function SingleFileUploader() {
const { files, getDropProps, getInputProps } = useDropup({
multiple: false, // Chỉ cho phép một tệp
maxFiles: 1,
});

const file = files[0];

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
{file ? (
<p>Đã chọn: {file.name}</p>
) : (
<p>Thả tệp vào đây</p>
)}
</div>
);
}

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

Với giới hạn loại tệp

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

function ImageOnlyUploader() {
const { files, state, getDropProps, getInputProps } = useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file }) => {
alert(`${file.name} không phải là hình ảnh hợp lệ`);
});
},
});

return (
<div
{...getDropProps()}
style={{
...styles.dropzone,
borderColor: state.isDragReject ? 'red' : '#ccc',
backgroundColor: state.isDragReject ? '#fff0f0' : 'white',
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p style={{ color: 'red' }}>Chỉ chấp nhận hình ảnh!</p>
) : (
<p>Thả hình ảnh vào đây</p>
)}
</div>
);
}

Với Callbacks

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

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

onFilesAdded: (newFiles) => {
console.log('Đã thêm tệp:', newFiles.map(f => f.name));
},

onUploadStart: (file) => {
console.log('Bắt đầu:', file.name);
},

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

onUploadComplete: (file, response) => {
console.log('Hoàn thành:', file.name, file.uploadedUrl);
},

onUploadError: (file, error) => {
console.error('Thất bại:', file.name, error.message);
},

onAllComplete: (allFiles) => {
const successful = allFiles.filter(f => f.status === 'complete');
console.log(`Xong! ${successful.length}/${allFiles.length} thành công`);
},
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>Thả tệp vào đây</p>
</div>

<button onClick={() => actions.upload()}>
Tải lên ({files.length} tệp)
</button>
</div>
);
}

Nút kích hoạt riêng biệt

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

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

return (
<div>
{/* Vùng thả ẩn nhưng vẫn chấp nhận thả */}
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
padding: 20,
marginBottom: 10,
}}
>
<input {...getInputProps()} />
<p>Thả tệp vào đây</p>
</div>

{/* Nút riêng biệt để mở hộp thoại tệp */}
<button onClick={openFileDialog}>
Duyệt tệp
</button>

{files.length > 0 && (
<p>Đã chọn {files.length} tệp</p>
)}
</div>
);
}

Tự động tải lên

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

function AutoUploader() {
const { files, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
autoUpload: true, // Tệp tự động tải lên khi được thêm
onUploadComplete: (file) => {
console.log('Đã tải lên:', file.uploadedUrl);
},
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>Thả tệp vào đây - chúng sẽ tự động tải lên!</p>

{files.map(file => (
<div key={file.id}>
{file.name}:
{file.status === 'uploading' && ` ${file.progress}%`}
{file.status === 'complete' && ' Xong!'}
{file.status === 'error' && ` Lỗi: ${file.error?.message}`}
</div>
))}
</div>
);
}

Các bước tiếp theo