Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: image미리보기 순서변경 구현 #53

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions components/dragablePreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Dispatch, SetStateAction, useState } from 'react';
import { ImagePreviewType } from './imageInputForm';

interface DragablePreviewProps {
preview: ImagePreviewType[];
setPreview: Dispatch<SetStateAction<ImagePreviewType[]>>;
fileList: File[];
setFileList: Dispatch<SetStateAction<File[]>>;
}

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<HTMLDivElement>,
selectedIndex: number
) => {
setNowDragIndex(selectedIndex);
};
const onDrop = (
e: React.DragEvent<HTMLDivElement>,
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 (
<div className="flex h-20 w-full overflow-x-auto mb-5 relative">
{preview.map((item, index) => (
<div key={index} className="h-full flex">
<div
className={
'w-5 relative flex justify-center items-center opacity-0'
}
onDrop={(e) => {
onDrop(e, index);
e.currentTarget.style.opacity = '0';
}}
onDragOver={(e) => {
e.currentTarget.style.opacity = '100';
}}
onDragLeave={(e) => {
e.currentTarget.style.opacity = '0';
}}
>
<div className="w-1 rounded bg-black h-full"></div>
</div>
<div
key={index}
className="relative flex-shrink-0 h-full"
onDragStart={(e) => onDragStart(e, index)}
>
<img
src={item.src}
alt={item.alt}
className="h-full w-auto rounded-sm mb-4"
/>

<div
className="absolute border bg-white rounded-sm top-0 right-0 text-sm w-5 text-center cursor-pointer"
onClick={() => {
deleteImage(index);
}}
>
x
</div>
</div>
</div>
))}
<div
className={'w-5 relative flex justify-center items-center opacity-0'}
onDrop={(e) => {
onDrop(e, preview.length);
e.currentTarget.style.opacity = '0';
}}
onDragOver={(e) => {
e.currentTarget.style.opacity = '100';
}}
onDragLeave={(e) => {
e.currentTarget.style.opacity = '0';
}}
>
<div className="w-1 rounded bg-black h-full"></div>
</div>
</div>
);
}
42 changes: 8 additions & 34 deletions components/imageInputForm.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -28,7 +29,6 @@ export default function ImageInputForm({
alt: 'loading',
})),
];
console.log('!', previews);
files.forEach((file, index) => {
const reader = new FileReader();
reader.readAsDataURL(file);
Expand All @@ -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<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
Expand Down Expand Up @@ -90,7 +81,6 @@ export default function ImageInputForm({
updatePreview(newFiles);
setIsDragging(false);
};
console.log(fileList);
return (
<div
{...props}
Expand Down Expand Up @@ -118,28 +108,12 @@ export default function ImageInputForm({
파일 선택
</label>

{preview.length > 0 && (
<div className="flex gap-3 w-full overflow-x-auto mb-5">
{preview.map((item, index) => (
<div key={index} className="relative flex-shrink-0">
<img
src={item.src}
alt={item.alt}
className="h-20 w-auto rounded-sm mb-4"
/>

<div
className="absolute border bg-white rounded-sm top-0 right-0 text-sm w-5 text-center cursor-pointer"
onClick={() => {
deleteImage(index);
}}
>
x
</div>
</div>
))}
</div>
)}
<DragablePreview
fileList={fileList}
setFileList={setFileList}
preview={preview}
setPreview={setPreview}
/>
</div>
);
}