Skip to content

Commit

Permalink
feat: Drag In Drop (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhea-so committed Apr 9, 2024
2 parents a345480 + fbc26b5 commit 5b3f450
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions web/src/pages/root/components/add-photo.button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ const AddPhotoButton = () => {
const { t } = useTranslation();
const { photos, setPhotos } = useStore();

const onDragEnter = (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
};

const onDragLeave = (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
};

const onDragOver = (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
};

const onDrop = (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
const { files } = e.dataTransfer;
if (!files) return;
Promise.all(Array.from(files).map(Photo.create)).then((newPhotos) => {
setPhotos([...photos, ...newPhotos]);
});
};

const onChange = (event: ChangeEvent<HTMLInputElement>) => {
const { files } = event.target;
if (!files) return;
Expand All @@ -22,6 +47,10 @@ const AddPhotoButton = () => {

<Button
clear
onDragEnter={onDragEnter}
onDragLeave={onDragLeave}
onDragOver={onDragOver}
onDrop={onDrop}
onClick={() => {
const input: HTMLInputElement | null = document.querySelector('input[type="file"]');
if (input) input.click();
Expand Down

0 comments on commit 5b3f450

Please sign in to comment.