-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7933667
commit 122b5f4
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sample/src/components/organisms/products/dropzone.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Dropzone from '@/components/organisms/products/dropzone' | ||
import type { Meta, StoryObj } from '@storybook/react' | ||
import { useState } from 'react' | ||
|
||
const meta: Meta<typeof Dropzone> = { | ||
title: 'organisms/products/dropzone', | ||
component: Dropzone, | ||
parameters: { | ||
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/configure/story-layout | ||
layout: 'centered', | ||
}, | ||
tags: ['autodocs'], | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof Dropzone> | ||
|
||
/** | ||
* 一括登録ボタン | ||
*/ | ||
export const Default: Story = { | ||
render: () => { | ||
const [file, setFile] = useState<File | null>(null) | ||
return <Dropzone file={file} setFile={setFile} /> | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React, { useCallback } from 'react' | ||
import { useDropzone } from 'react-dropzone' | ||
|
||
type Props = { | ||
file: File | null | ||
setFile: (file: File) => void | ||
} | ||
|
||
const DropZone = React.memo(function DropZone({ file, setFile }: Props) { | ||
const onDrop = useCallback( | ||
async (acceptedFiles: File[]) => { | ||
const importFile = acceptedFiles[0] | ||
if (!importFile) return | ||
|
||
setFile(importFile) | ||
}, | ||
[setFile], | ||
) | ||
const { | ||
getRootProps, | ||
getInputProps, | ||
isDragAccept, | ||
isDragReject, | ||
isDragActive, | ||
} = useDropzone({ | ||
onDrop, | ||
accept: { | ||
'text/csv': ['.csv'], | ||
}, | ||
maxFiles: 1, | ||
}) | ||
const message = useCallback(() => { | ||
if (file) return `${file.name} が読み込まれています` | ||
|
||
if (isDragReject) | ||
return 'ファイル形式が不正、もしくは複数のファイル取り込もうとしています。' | ||
|
||
if (isDragActive && isDragAccept) return '読み込み可能な形式です' | ||
|
||
return 'ファイルをここに D&D するか、クリックしてファイルを選択してください' | ||
}, [file, isDragReject, isDragActive, isDragAccept]) | ||
|
||
return ( | ||
<div | ||
{...getRootProps()} | ||
className={`w-full min-h-56 border-dashed border-gray-800 border-2 flex items-center rounded-3xl justify-center ${file ? 'bg-green-200' : ''}`} | ||
> | ||
<input {...getInputProps()} /> | ||
<p className="text-center">{message()}</p> | ||
</div> | ||
) | ||
}) | ||
|
||
export default DropZone |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters