@@ -2,6 +2,8 @@ import PropTypes from 'prop-types';
22import React , {
33 useContext ,
44 useEffect ,
5+ useImperativeHandle ,
6+ useRef ,
57 useState ,
68} from 'react' ;
79import { withGlobalProps } from '../../providers/globalProps' ;
@@ -34,6 +36,13 @@ export const FileInputField = React.forwardRef((props, ref) => {
3436 ...restProps
3537 } = props ;
3638
39+ const internalInputRef = useRef ( ) ;
40+
41+ // We need to have a reference to the input element to be able to call its methods,
42+ // but at the same time we want to expose this reference to the parent component for
43+ // case someone wants to call input methods from outside the component.
44+ useImperativeHandle ( ref , ( ) => internalInputRef . current ) ;
45+
3746 const formLayoutContext = useContext ( FormLayoutContext ) ;
3847 const inputGroupContext = useContext ( InputGroupContext ) ;
3948 const translations = useContext ( TranslationsContext ) ;
@@ -69,8 +78,8 @@ export const FileInputField = React.forwardRef((props, ref) => {
6978 handleFileChange ( event . target . files , event ) ;
7079 } ;
7180
72- const handleClick = ( event ) => {
73- event . currentTarget . previousElementSibling . click ( ) ;
81+ const handleClick = ( ) => {
82+ internalInputRef ?. current . click ( ) ;
7483 } ;
7584
7685 const handleDrop = ( event ) => {
@@ -81,7 +90,6 @@ export const FileInputField = React.forwardRef((props, ref) => {
8190
8291 const handleDragOver = ( event ) => {
8392 event . preventDefault ( ) ;
84- setIsDragging ( true ) ;
8593 } ;
8694
8795 const handleDragEnter = ( ) => {
@@ -116,6 +124,7 @@ export const FileInputField = React.forwardRef((props, ref) => {
116124 getRootValidationStateClassName ( validationState , styles ) ,
117125 ) }
118126 id = { id && `${ id } __root` }
127+ onClick = { handleClick }
119128 onDragEnter = { ! disabled && isDragAndDropSupported ? handleDragEnter : undefined }
120129 onDragLeave = { ! disabled && isDragAndDropSupported ? handleDragLeave : undefined }
121130 onDragOver = { ! disabled && isDragAndDropSupported ? handleDragOver : undefined }
@@ -140,15 +149,14 @@ export const FileInputField = React.forwardRef((props, ref) => {
140149 id = { id }
141150 multiple = { multiple }
142151 onChange = { handleInputChange }
143- ref = { ref }
152+ ref = { internalInputRef }
144153 required = { required }
145154 tabIndex = { - 1 }
146155 type = "file"
147156 />
148157 < button
149158 className = { styles . dropZone }
150159 disabled = { resolveContextOrProp ( inputGroupContext && inputGroupContext . disabled , disabled ) }
151- onClick = { handleClick }
152160 type = "button"
153161 >
154162 < Text lines = { 1 } >
0 commit comments