Skip to content

Commit 68e349d

Browse files
Remove setIsDragging(true) from handleDragOver() to improve performance
1 parent d50e8f2 commit 68e349d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/components/FileInputField/FileInputField.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import PropTypes from 'prop-types';
22
import React, {
33
useContext,
44
useEffect,
5+
useImperativeHandle,
6+
useRef,
57
useState,
68
} from 'react';
79
import { 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}>

src/components/FileInputField/FileInputField.module.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// 1. The drop zone is constructed as a button to support keyboard operation.
2+
// 2. Prevent pointer events on all children of the root element to not to trigger drag events on children.
23

34
@use "../../styles/tools/form-fields/box-field-elements";
45
@use "../../styles/tools/form-fields/box-field-layout";
@@ -15,6 +16,10 @@
1516
// Foundation
1617
.root {
1718
@include foundation.root();
19+
20+
* {
21+
pointer-events: none; // 2
22+
}
1823
}
1924

2025
.label {

0 commit comments

Comments
 (0)