ප්‍රධාන අන්තර්ගතයට පනින්න

useDropup Hook

Drag-and-drop සහාය සහිත file uploads සඳහා ප්‍රධාන hook.

Import

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

මූලික භාවිතය

const {
files,
state,
actions,
getDropProps,
getInputProps,
openFileDialog,
} = useDropup(options);

පරාමිතීන්

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 configuration
upload?: UploadConfig | CustomUploader;

// Callbacks
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;
}

Return Value

සම්පූර්ණ විස්තර සඳහා Return Values බලන්න.

interface UseDropupReturn {
// State
files: DropupFile[];
state: DropupState;

// Actions
actions: DropupActions;

// Prop getters
getDropProps: <E extends HTMLElement>(props?: HTMLAttributes<E>) => DropZoneProps<E>;
getInputProps: (props?: InputHTMLAttributes) => InputProps;

// Utilities
openFileDialog: () => void;
}

ඉක්මන් Reference

Files Array

const { files } = useDropup();

// සෑම ගොනුවකම ඇත:
files[0].id // අනන්‍ය ID
files[0].file // මුල් File object
files[0].name // ගොනු නාමය
files[0].size // Bytes වලින් ප්‍රමාණය
files[0].type // MIME type
files[0].preview // Preview URL (images)
files[0].status // 'idle' | 'uploading' | 'complete' | 'error'
files[0].progress // 0-100
files[0].uploadedUrl // Upload එකෙන් පසු URL
files[0].error // අසාර්ථක නම් Error

State Object

const { state } = useDropup();

state.isDragging // ගොනු drag වෙමින්
state.isDragAccept // Drag කළ ගොනු වලංගුයි
state.isDragReject // Drag කළ ගොනු අවලංගුයි
state.isUploading // ගොනු upload වෙමින්
state.progress // සමස්ත progress 0-100
state.status // 'idle' | 'uploading' | 'complete' | 'error'

Actions Object

const { actions } = useDropup();

actions.upload(fileIds?) // Upload ආරම්භ කරන්න
actions.cancel(fileId?) // Upload අවලංගු කරන්න
actions.remove(fileId) // ගොනුව ඉවත් කරන්න
actions.reset() // සියල්ල හිස් කරන්න
actions.retry(fileIds?) // අසාර්ථක නැවත උත්සාහ කරන්න
actions.addFiles(files) // Programmatically ගොනු එකතු කරන්න
actions.updateFileMeta(id, meta) // Metadata යාවත්කාලීන කරන්න

Prop Getters

const { getDropProps, getInputProps } = useDropup();

// ඔබගේ drop zone එකට යොදන්න
<div {...getDropProps()}>
<input {...getInputProps()} />
මෙතැනට දමන්න
</div>

// Custom props සමඟ
<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>
);
}

Upload සමඟ

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()}>සියල්ල Upload කරන්න</button>
</div>
);
}

සියලුම Options සමඟ

function FullFeaturedUploader() {
const {
files,
state,
actions,
getDropProps,
getInputProps,
} = useDropup({
// වලංගුකරණය
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
maxFiles: 5,

// හැසිරීම
multiple: true,
autoUpload: true,
generatePreviews: true,

// Upload
upload: {
url: '/api/upload',
method: 'POST',
},

// Callbacks
onFilesAdded: (files) => console.log('එකතු විය:', files),
onUploadComplete: (file) => console.log('සම්පූර්ණයි:', file.uploadedUrl),
onUploadError: (file, err) => console.error('දෝෂය:', err),
onAllComplete: () => console.log('සියලුම uploads සම්පූර්ණයි!'),
});

return (
<div>
<div
{...getDropProps()}
style={{
border: `2px dashed ${state.isDragAccept ? 'green' : state.isDragReject ? 'red' : 'gray'}`,
padding: 40,
}}
>
<input {...getInputProps()} />
<p>Images මෙතැනට දමන්න (උපරිම 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>
);
}