@@ -37,20 +37,30 @@ export const FileInputField = React.forwardRef((props, ref) => {
37
37
...restProps
38
38
} = props ;
39
39
40
- const internalInputRef = useRef ( ) ;
41
-
42
- // We need to have a reference to the input element to be able to call its methods,
43
- // but at the same time we want to expose this reference to the parent component for
44
- // case someone wants to call input methods from outside the component.
45
- useImperativeHandle ( ref , ( ) => internalInputRef . current ) ;
46
-
47
40
const formLayoutContext = useContext ( FormLayoutContext ) ;
48
41
const inputGroupContext = useContext ( InputGroupContext ) ;
49
42
const translations = useContext ( TranslationsContext ) ;
50
43
51
44
const [ selectedFileNames , setSelectedFileNames ] = useState ( [ ] ) ;
52
45
const [ isDragging , setIsDragging ] = useState ( false ) ;
53
46
47
+ const internalInputRef = useRef ( ) ;
48
+
49
+ // We need to have a reference to the input element to be able to call its methods,
50
+ // but at the same time we want to expose this reference to the parent component for
51
+ // case someone wants to call input methods from outside the component.
52
+ useImperativeHandle (
53
+ ref ,
54
+ ( ) => {
55
+ const inputEl = internalInputRef ?. current ?? { } ;
56
+ inputEl . resetState = ( ) => {
57
+ setSelectedFileNames ( [ ] ) ;
58
+ } ;
59
+ return inputEl ;
60
+ } ,
61
+ [ setSelectedFileNames ] ,
62
+ ) ;
63
+
54
64
const handleFileChange = ( files , event ) => {
55
65
if ( files . length === 0 ) {
56
66
setSelectedFileNames ( [ ] ) ;
0 commit comments