Skip to content

Commit

Permalink
Merge pull request #19998 from gitstart/fix/file-control-reset
Browse files Browse the repository at this point in the history
Controls: Fix file controls not resetting
  • Loading branch information
JReinhold authored Dec 8, 2022
2 parents 8f647a4 + f953a6d commit f5aa7bd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion code/ui/blocks/src/controls/Files.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useRef } from 'react';
import type { ChangeEvent, FC } from 'react';
import React from 'react';
import { styled } from '@storybook/theming';
import { Form } from '@storybook/components';

Expand Down Expand Up @@ -39,6 +39,8 @@ export const FilesControl: FC<FilesControlProps> = ({
accept = 'image/*',
value,
}) => {
const inputElement = useRef<HTMLInputElement>(null);

function handleFileChange(e: ChangeEvent<HTMLInputElement>) {
if (!e.target.files) {
return;
Expand All @@ -48,8 +50,16 @@ export const FilesControl: FC<FilesControlProps> = ({
revokeOldUrls(value);
}

// Added useEffect hook to reset the file value when value is null
useEffect(() => {
if (value == null && inputElement.current) {
inputElement.current.value = null;
}
}, [value, name]);

return (
<FileInput
ref={inputElement}
id={getControlId(name)}
type="file"
name={name}
Expand Down

0 comments on commit f5aa7bd

Please sign in to comment.