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

මූලික උදාහරණය

Dropup භාවිතා කිරීමේ සරලම ක්‍රමය.

අවම Setup

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>ගොනු මෙතැනට drag කරන්න හෝ තේරීමට click කරන්න</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>
);
}

Upload ක්‍රියාකාරීත්වය සමඟ

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>ගොනු මෙතැනට දමන්න හෝ තේරීමට click කරන්න</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>
)}

{/* Upload බොත්තම */}
<div style={{ marginTop: 20 }}>
<button
onClick={() => actions.upload()}
disabled={files.length === 0 || state.isUploading}
>
{state.isUploading ? `උඩුගත වෙමින්... ${state.progress}%` : 'සියල්ල Upload කරන්න'}
</button>

<button onClick={() => actions.reset()} style={{ marginLeft: 10 }}>
සියල්ල හිස් කරන්න
</button>
</div>
</div>
);
}

තනි ගොනු Upload

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} වලංගු image එකක් නොවේ`);
});
},
});

return (
<div
{...getDropProps()}
style={{
...styles.dropzone,
borderColor: state.isDragReject ? 'red' : '#ccc',
backgroundColor: state.isDragReject ? '#fff0f0' : 'white',
}}
>
<input {...getInputProps()} />
{state.isDragReject ? (
<p style={{ color: 'red' }}>Images පමණක් පිළිගනී!</p>
) : (
<p>Images මෙතැනට දමන්න</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()}>
Upload කරන්න (ගොනු {files.length}ක්)
</button>
</div>
);
}

වෙනම බොත්තම Trigger

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

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

return (
<div>
{/* Drops පිළිගන්නා සැඟවුණු drop zone */}
<div
{...getDropProps()}
style={{
border: '2px dashed #ccc',
padding: 20,
marginBottom: 10,
}}
>
<input {...getInputProps()} />
<p>ගොනු මෙතැනට දමන්න</p>
</div>

{/* File dialog විවෘත කිරීමට වෙනම බොත්තම */}
<button onClick={openFileDialog}>
ගොනු Browse කරන්න
</button>

{files.length > 0 && (
<p>ගොනු {files.length}ක් තෝරාගෙන ඇත</p>
)}
</div>
);
}

ස්වයංක්‍රීය Upload

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

function AutoUploader() {
const { files, getDropProps, getInputProps } = useDropup({
upload: { url: '/api/upload' },
autoUpload: true, // එකතු කළ විට ගොනු ස්වයංක්‍රීයව upload වේ
onUploadComplete: (file) => {
console.log('Uploaded:', file.uploadedUrl);
},
});

return (
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>ගොනු මෙතැනට දමන්න - ඒවා ස්වයංක්‍රීයව upload වේ!</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>
);
}

ඊළඟ පියවර