Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8789: Fix double handling of file upload on window d&d #1332

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';

import { getTranslator } from '@ibexa-admin-ui/src/bundle/Resources/public/js/scripts/helpers/context.helper';
Expand All @@ -10,6 +10,7 @@ export default class DropAreaComponent extends Component {
super(props);

this._refFileInput = null;
this._refForm = createRef();
this.hasMultiMsgForFileSizes = this.props.maxFileSizes.length > 1;

this.state = {
Expand Down Expand Up @@ -104,6 +105,8 @@ export default class DropAreaComponent extends Component {
componentDidMount() {
window.addEventListener('drop', this.props.preventDefaultAction, false);
window.addEventListener('dragover', this.props.preventDefaultAction, false);

this._refForm.current.addEventListener('drop', this.handleUpload, false);
}

componentWillUnmount() {
Expand All @@ -122,7 +125,7 @@ export default class DropAreaComponent extends Component {
});

return (
<form className="c-drop-area" multiple={true} onDrop={this.handleUpload}>
<form className="c-drop-area" onDrop={this.handleUpload} ref={this._refForm}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that multiple attribute does nothing and can be deleted.

<div className="c-drop-area__message c-drop-area__message--main">{dropActionMessage}</div>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to use standard addEventListener because of how React handles the events, namely React has one event listener on the root instead of individual elements and an event listener from MultiFileUploadModule added with removeEventListener was called before onDrop listener even though onDrop is attached on a deeper node in the DOM.

<div className="c-drop-area__message c-drop-area__message--separator">{separatorMessage}</div>
<button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default class MultiFileUploadModule extends Component {

handleDropOnWindow(event) {
this.preventDefaultAction(event);
event.stopImmediatePropagation();
Copy link
Contributor Author

@tischsoic tischsoic Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: MFU is rendered twice in Sub-items, because extraAction items are rendered twice - once in the normal list and once in the popup list. Even though at the given time one of them always is hidden, they are always both rendered.
Because of this we want to prevent one of them from handling file d&d, so that it is handled only by one of them. Regular event.stopPropagation() does not work, because both handlers are attached to the same node thus we need in this case event.stopImmediatePropagation().
ref. https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation


const itemsToUpload = this.processUploadedFiles(event);

Expand Down
Loading