@@ -37,20 +37,30 @@ export const FileInputField = React.forwardRef((props, ref) => {
3737 ...restProps
3838 } = props ;
3939
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-
4740 const formLayoutContext = useContext ( FormLayoutContext ) ;
4841 const inputGroupContext = useContext ( InputGroupContext ) ;
4942 const translations = useContext ( TranslationsContext ) ;
5043
5144 const [ selectedFileNames , setSelectedFileNames ] = useState ( [ ] ) ;
5245 const [ isDragging , setIsDragging ] = useState ( false ) ;
5346
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+
5464 const handleFileChange = ( files , event ) => {
5565 if ( files . length === 0 ) {
5666 setSelectedFileNames ( [ ] ) ;
0 commit comments