メインコンテンツまでスキップ

基本的な例

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, // 1つのファイルのみ許可
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>
);
}

次のステップ