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

fix: reset file input using ref to allow re-selection of the same file #1405

Merged
merged 1 commit into from
Jan 31, 2025
Merged
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
9 changes: 7 additions & 2 deletions packages/web/src/common/components/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from "react";
import React, { useMemo, useRef, useState } from "react";

import { ApiFil001RequestBody } from "@sparcs-clubs/interface/api/file/apiFil001";
import { overlay } from "overlay-kit";
Expand Down Expand Up @@ -116,6 +116,7 @@ const FileUpload: React.FC<FileUploadProps> = ({
const { mutate: putFileS3Mutation } = usePutFileS3();

const [files, setFiles] = useState<FileDetail[]>(initialFiles);
const fileInputRef = useRef<HTMLInputElement | null>(null);

const updateFiles = (_files: FileDetail[]) => {
setFiles(_files);
Expand All @@ -126,7 +127,6 @@ const FileUpload: React.FC<FileUploadProps> = ({
const newFiles = _files.filter(file => !files.find(f => f.id === file.id));
const updatedFiles = multiple ? [...files, ...newFiles] : newFiles;
updateFiles(updatedFiles);
onChange(updatedFiles);
};
const removeFile = (_file: FileDetail) => {
const updatedFiles = files.filter(file => file.id !== _file.id);
Expand Down Expand Up @@ -208,6 +208,10 @@ const FileUpload: React.FC<FileUploadProps> = ({
file => ({ file, previewUrl: URL.createObjectURL(file) }),
);
onSubmit(newFiles);

if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};

const handleClick = () => {
Expand Down Expand Up @@ -243,6 +247,7 @@ const FileUpload: React.FC<FileUploadProps> = ({
<Icon type="file_upload_outlined" size={20} color="white" />
</UploadIcon>
<HiddenInput
ref={fileInputRef}
type="file"
accept={allowedTypes.join(",")}
multiple={multiple}
Expand Down