메인 콘텐츠로 건너뛰기

기본 예제

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

콜백 사용

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

별도 버튼 트리거

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>

{/* 파일 대화 상자를 여는 별도 버튼 */}
<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>
);
}

다음 단계