useDropup हुक
ड्रैग-एंड-ड्रॉप सपोर्ट के साथ फाइल अपलोड के लिए मुख्य हुक।
आयात
import { useDropup } from '@samithahansaka/dropup';
मूल उपयोग
const {
files,
state,
actions,
getDropProps,
getInputProps,
openFileDialog,
} = useDropup(options);
पैरामीटर
options (वैकल्पिक)
पूर्ण विवरण के लिए विकल्प देखें।
interface UseDropupOptions {
// सत्यापन
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];
// व्यवहार
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;
// अपलोड कॉन्फ़िगरेशन
upload?: UploadConfig | CustomUploader;
// कॉलबैक
onFilesAdded?: (files: DropupFile[]) => void;
onFileRemoved?: (file: DropupFile) => void;
onValidationError?: (errors: ValidationError[]) => void;
onUploadStart?: (file: DropupFile) => void;
onUploadProgress?: (file: DropupFile, progress: number) => void;
onUploadComplete?: (file: DropupFile, response: unknown) => void;
onUploadError?: (file: DropupFile, error: DropupError) => void;
onAllComplete?: (files: DropupFile[]) => void;
}
रिटर्न वैल्यू
पूर्ण विवरण के लिए रिटर्न वैल्यूज देखें।
interface UseDropupReturn {
// स्टेट
files: DropupFile[];
state: DropupState;
// क्रियाएं
actions: DropupActions;
// प्रॉप गेटर्स
getDropProps: <E extends HTMLElement>(props?: HTMLAttributes<E>) => DropZoneProps<E>;
getInputProps: (props?: InputHTMLAttributes) => InputProps;
// उपयोगिताएं
openFileDialog: () => void;
}
त्वरित संदर्भ
फाइल्स ऐरे
const { files } = useDropup();
// प्रत्येक फाइल में है:
files[0].id // अद्वितीय ID
files[0].file // मूल File ऑब्जेक्ट
files[0].name // फाइल का नाम
files[0].size // बाइट्स में आकार
files[0].type // MIME प्रकार
files[0].preview // प्रीव्यू URL (छवियां)
files[0].status // 'idle' | 'uploading' | 'complete' | 'error'
files[0].progress // 0-100
files[0].uploadedUrl // अपलोड के बाद URL
files[0].error // विफल होने पर Error
स्टेट ऑब्जेक्ट
const { state } = useDropup();
state.isDragging // फाइलें ड्रैग की जा रही हैं
state.isDragAccept // ड्रैग की गई फाइलें वैध हैं
state.isDragReject // ड्रैग की गई फाइलें अवैध हैं
state.isUploading // कोई फाइल अपलोड हो रही है
state.progress // समग्र प्रगति 0-100
state.status // 'idle' | 'uploading' | 'complete' | 'error'
एक्शन्स ऑब्जेक्ट
const { actions } = useDropup();
actions.upload(fileIds?) // अपलोड शुरू करें
actions.cancel(fileId?) // अपलोड रद्द करें
actions.remove(fileId) // फाइल हटाएं
actions.reset() // सभी साफ करें
actions.retry(fileIds?) // विफल को पुनः प्रयास करें
actions.addFiles(files) // प्रोग्रामेटिकली फाइलें जोड़ें
actions.updateFileMeta(id, meta) // मेटाडेटा अपडेट करें
प्रॉप गेटर्स
const { getDropProps, getInputProps } = useDropup();
// अपने ड्रॉप जोन पर लागू करें
<div {...getDropProps()}>
<input {...getInputProps()} />
यहां ड्रॉप करें
</div>
// कस्टम प्रॉप्स के साथ
<div {...getDropProps({ className: 'my-dropzone', onClick: handleClick })}>
...
</div>
उदाहरण
न्यूनतम उदाहरण
function Uploader() {
const { files, getDropProps, getInputProps } = useDropup();
return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>फाइलें यहां ड्रॉप करें</p>
<ul>
{files.map(f => <li key={f.id}>{f.name}</li>)}
</ul>
</div>
);
}
अपलोड के साथ
function Uploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: { 'Authorization': 'Bearer token' },
},
});
return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>फाइलें यहां ड्रॉप करें</p>
</div>
{files.map(f => (
<div key={f.id}>
{f.name} - {f.status}
{f.status === 'uploading' && ` ${f.progress}%`}
</div>
))}
<button onClick={() => actions.upload()}>सभी अपलोड करें</button>
</div>
);
}
सभी विकल्पों के साथ
function FullFeaturedUploader() {
const {
files,
state,
actions,
getDropProps,
getInputProps,
} = useDropup({
// सत्यापन
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
maxFiles: 5,
// व्यवहार
multiple: true,
autoUpload: true,
generatePreviews: true,
// अपलोड
upload: {
url: '/api/upload',
method: 'POST',
},
// कॉलबैक
onFilesAdded: (files) => console.log('जोड़ा गया:', files),
onUploadComplete: (file) => console.log('पूर्ण:', file.uploadedUrl),
onUploadError: (file, err) => console.error('त्रुटि:', err),
onAllComplete: () => console.log('सभी अपलोड पूर्ण!'),
});
return (
<div>
<div
{...getDropProps()}
style={{
border: `2px dashed ${state.isDragAccept ? 'green' : state.isDragReject ? 'red' : 'gray'}`,
padding: 40,
}}
>
<input {...getInputProps()} />
<p>छवियां यहां ड्रॉप करें (अधिकतम 5, प्रत्येक 10MB)</p>
</div>
{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} width={50} />}
<span>{file.name}</span>
<span>{file.status}</span>
{file.status === 'uploading' && <span>{file.progress}%</span>}
<button onClick={() => actions.remove(file.id)}>×</button>
</div>
))}
{state.isUploading && <p>अपलोड हो रहा है... {state.progress}%</p>}
</div>
);
}