@@ -37,20 +37,38 @@ 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
+ const handleReset = useCallback ( ( event ) => {
50
+ setSelectedFileNames ( [ ] ) ;
51
+ onFilesChanged ( [ ] , event ) ;
52
+ } , [ onFilesChanged ] ) ;
53
+
54
+ // We need to have a reference to the input element to be able to call its methods,
55
+ // but at the same time we want to expose this reference to the parent component in
56
+ // case someone wants to call input methods from outside the component.
57
+ useImperativeHandle (
58
+ ref ,
59
+ ( ) => {
60
+ // The reason of extending object instead of using spread operator is that
61
+ // if it is transformed to the object, it changes the reference of the object
62
+ // and its prototype chain.
63
+ const inputEl = internalInputRef ?. current ?? { } ;
64
+ inputEl . resetState = ( ) => {
65
+ handleReset ( null ) ;
66
+ } ;
67
+ return inputEl ;
68
+ } ,
69
+ [ handleReset ] ,
70
+ ) ;
71
+
54
72
const handleFileChange = ( files , event ) => {
55
73
if ( files . length === 0 ) {
56
74
setSelectedFileNames ( [ ] ) ;
@@ -101,11 +119,6 @@ export const FileInputField = React.forwardRef((props, ref) => {
101
119
}
102
120
} ;
103
121
104
- const handleReset = useCallback ( ( event ) => {
105
- setSelectedFileNames ( [ ] ) ;
106
- onFilesChanged ( [ ] , event ) ;
107
- } , [ onFilesChanged ] ) ;
108
-
109
122
useEffect ( ( ) => {
110
123
const inputEl = internalInputRef . current ;
111
124
if ( ! inputEl ) {
0 commit comments