diff --git a/src/components/forms/FileInput/FileInput.stories.tsx b/src/components/forms/FileInput/FileInput.stories.tsx index 983fabbefb..8033450be4 100644 --- a/src/components/forms/FileInput/FileInput.stories.tsx +++ b/src/components/forms/FileInput/FileInput.stories.tsx @@ -118,7 +118,10 @@ export const WithRefAndCustomHandlers = { const [files, setFiles] = useState(null) const fileInputRef = useRef(null) - const handleClearFiles = (): void => fileInputRef.current?.clearFiles() + const handleClearFiles = (): void => { + fileInputRef.current?.clearFiles() + setFiles(null) + } const handleChange = (e: React.ChangeEvent): void => { argTypes.onChange(e) diff --git a/src/components/forms/FileInput/FileInput.test.tsx b/src/components/forms/FileInput/FileInput.test.tsx index 307f678f9a..b2e802ee72 100644 --- a/src/components/forms/FileInput/FileInput.test.tsx +++ b/src/components/forms/FileInput/FileInput.test.tsx @@ -408,9 +408,10 @@ describe('FileInput component', () => { 'display-none' ) - // Notice how input.files still exist because we can't programmatically set the value - expect(fileInputRef.current?.input?.files).toHaveLength(1) - // But the files state of the React "input" is cleared out + // Inputs files should be cleared out and reset + expect(fileInputRef.current?.input?.files).toHaveLength(0) + expect(fileInputRef.current?.input?.value).toEqual('') + // Files state of the React "input" is cleared out expect(fileInputRef.current?.files).toHaveLength(0) }) }) diff --git a/src/components/forms/FileInput/FileInput.tsx b/src/components/forms/FileInput/FileInput.tsx index 589740f07c..4e2bb57d07 100644 --- a/src/components/forms/FileInput/FileInput.tsx +++ b/src/components/forms/FileInput/FileInput.tsx @@ -69,7 +69,12 @@ export const FileInputForwardRef: React.ForwardRefRenderFunction< ref, () => ({ input: internalRef.current, - clearFiles: (): void => setFiles([]), + clearFiles: (): void => { + setFiles([]) + if (internalRef.current) { + internalRef.current.value = '' + } + }, files, }), [files]