diff --git a/components/dragablePreview.tsx b/components/dragablePreview.tsx new file mode 100644 index 0000000..ce21c27 --- /dev/null +++ b/components/dragablePreview.tsx @@ -0,0 +1,121 @@ +import { Dispatch, SetStateAction, useState } from 'react'; +import { ImagePreviewType } from './imageInputForm'; + +interface DragablePreviewProps { + preview: ImagePreviewType[]; + setPreview: Dispatch>; + fileList: File[]; + setFileList: Dispatch>; +} + +export default function DragablePreview({ + preview, + setPreview, + fileList, + setFileList, +}: DragablePreviewProps) { + const [nowDragIndex, setNowDragIndex] = useState(-1); + + const deleteImage = (index: number) => { + const newFileList = [...fileList]; + const newPreview = [...preview]; + newFileList.splice(index, 1); + newPreview.splice(index, 1); + setFileList(newFileList); + setPreview(newPreview); + }; + const onDragStart = ( + e: React.DragEvent, + selectedIndex: number + ) => { + setNowDragIndex(selectedIndex); + }; + const onDrop = ( + e: React.DragEvent, + selectedIndex: number + ) => { + e.preventDefault(); + e.stopPropagation(); + if (nowDragIndex < 0) return; + + const newFileList = [...fileList]; + const selectedFile = newFileList.splice(nowDragIndex, 1); + newFileList.splice( + selectedIndex > nowDragIndex ? selectedIndex - 1 : selectedIndex, + 0, + selectedFile[0] + ); + + const newPreview = [...preview]; + const selectedPreview = newPreview.splice(nowDragIndex, 1); + newPreview.splice( + selectedIndex > nowDragIndex ? selectedIndex - 1 : selectedIndex, + 0, + selectedPreview[0] + ); + + setFileList(newFileList); + setPreview(newPreview); + setNowDragIndex(-1); + }; + return ( +
+ {preview.map((item, index) => ( +
+
{ + onDrop(e, index); + e.currentTarget.style.opacity = '0'; + }} + onDragOver={(e) => { + e.currentTarget.style.opacity = '100'; + }} + onDragLeave={(e) => { + e.currentTarget.style.opacity = '0'; + }} + > +
+
+
onDragStart(e, index)} + > + {item.alt} + +
{ + deleteImage(index); + }} + > + x +
+
+
+ ))} +
{ + onDrop(e, preview.length); + e.currentTarget.style.opacity = '0'; + }} + onDragOver={(e) => { + e.currentTarget.style.opacity = '100'; + }} + onDragLeave={(e) => { + e.currentTarget.style.opacity = '0'; + }} + > +
+
+
+ ); +} diff --git a/components/imageInputForm.tsx b/components/imageInputForm.tsx index 5a1e9b7..c2dc642 100644 --- a/components/imageInputForm.tsx +++ b/components/imageInputForm.tsx @@ -1,4 +1,5 @@ -import { useEffect, useState } from 'react'; +import { useState } from 'react'; +import DragablePreview from './dragablePreview'; import { FormLabel } from './ui/form'; import { Input } from './ui/input'; @@ -28,7 +29,6 @@ export default function ImageInputForm({ alt: 'loading', })), ]; - console.log('!', previews); files.forEach((file, index) => { const reader = new FileReader(); reader.readAsDataURL(file); @@ -52,15 +52,6 @@ export default function ImageInputForm({ updatePreview(files); }; - const deleteImage = (index: number) => { - const newFileList = [...fileList]; - const newPreview = [...preview]; - newFileList.splice(index, 1); - newPreview.splice(index, 1); - setFileList(newFileList); - setPreview(newPreview); - }; - const onDragEnter = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); @@ -90,7 +81,6 @@ export default function ImageInputForm({ updatePreview(newFiles); setIsDragging(false); }; - console.log(fileList); return (
- {preview.length > 0 && ( -
- {preview.map((item, index) => ( -
- {item.alt} - -
{ - deleteImage(index); - }} - > - x -
-
- ))} -
- )} +
); }