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
<Dropzone> cause onClick to fire twice on parent <div> #252
Comments
A few more observations:
|
https://github.com/okonet/react-dropzone/blob/master/src/index.js#L276 You should prevent the event going down the tree in your handler if you want to prevent this behavior. |
I'm not sure I understand. Bubbling shouldn't result in multiple events.... you're saying that because Dropzone has an |
I think this is the problem: https://github.com/okonet/react-dropzone/blob/master/src/index.js#L182 You are calling Similarly for the drag event handlers such as https://github.com/okonet/react-dropzone/blob/master/src/index.js#L41 I think in both cases |
im not quite sure about the behavior. Can you create an isolated example with a vanilla JS and DOM? We probably should stop propagation as you said. Mind creating a PR? |
Sure I'll give this a shot next week. |
@okonet Looking into this. Just curious about the workflow (I don't maintan any libraries myself), how do you go about visually testing this? Do you just create a separate project and add |
Yes, I'd do something like it. It would be great to get the isolated case in the end if not the PR with a the bug fix. |
As far as an isolated case, a fresh project (using
Gives this output when you click the dropzone: Notice the first event target is If I stop the
Then I just get the single As for the drag events firing twice, that does not happen in the above simple test, so I'll have to dig deeper to find out why that was happening in my real app. Maybe nothing to do with |
I've tried to add a test but the bug isn't really introducible. This is what I've used: it('invoke onClick only once', () => {
const onClickSpy = spy();
const component = TestUtils.renderIntoDocument(
<div id="content" onClick={onClickSpy}>
<Dropzone>
<div>Click me</div>
</Dropzone>
</div>
);
const content = TestUtils.find(component, '#content')[0];
TestUtils.Simulate.click(content);
expect(onClickSpy.callCount).toEqual(1);
}); Interestingly, even if I remove your fix, it is still |
You need to simulate the click on the inner div (or possibly the inner Dropzone though I'm not sure that's an accurate test since it's not a DOM element.) I couldn't work out how to do it, all my attempts to select the inner element with TestUtils led to invariants about trying to find the wrong thing, or simply gave me 0 results. |
No matter where I simulate the click, it only triggers 1 call :/ |
Hm. Does my original example not exhibit the behavior for you? It could be that simulate click doesn't behave the same (intentionally?) as a real DOM click. It's certainly a weird test case: testing the behavior of a wrapping component when the component under test is put inside it... |
I'm trying Enzyme today and will see if I can repro it on JSDom. Ill let you know |
i have same issue , but not working yet |
It's been fixed in https://github.com/okonet/react-dropzone/releases/tag/v3.8.0. Please upgrade |
already upgrade v3.8.0 |
@pouu69 I'm not sure what you're saying. Is it still reproducible in 3.8? |
I am already version 3.8. |
<div
className="block-img-wrapper"
onClick={this.changeRootSlide}
>
<div>
<Dropzone
multiple={false}
className="block-dropzone"
disableClick={true}
accept="image/*"
ref='dropzone'
style={styles.dropzone}
onDrop={this.html5FileHandling}
>
<div className="trigger">
</div>
</Dropzone>
</div>
</div> |
|
With current fix an IconMenu from MaterialUI stopped working properly as it is not hiding after clicking on a DropZone. |
Just add a |
That's a bad advice since it will break click to open file dialog functionality AFAIK. Also it seems that it was resolved or is it still happening? |
@okonet it does not break click to open file dialog functionality. The bug is still happening, just tried it yesterday on v3.13.1. I know it sounds way off and ugly with the css-fix, but the alternative for me is that the "open dialog" hit's twice - where the first file selection is dropped. |
@LaustAxelsen the reason for me saying this is that I tried it and it broke something for me. I can't remember what was it though. I think this issue should be fixed in this package. Anyone? |
Can someone create a reproducible example? |
Same issue here. Newest version. Use dropzone in a semantic-ui project, and the Behaviour seems consistent across browsers. Use newest release (3.11). It's obviously an edge case, thus reproducing this bug might headache on its own. I will try to find the time and make a proper Bug Report and maybe PR. My current assumption is, this happens when Dropzone has children like |
fixed my bug. really obvious, sorry for the noise:
|
I am also getting this same issue. Version 3.13.1. Even with |
I am also getting this on |
Can someone create a reproducible example here http://react-dropzone.netlify.com/ and paste code sample here? |
@okonet https://github.com/kylepotts/react-dropzone-bug-example Just do the normal
|
I've created a bugfix here #437. It should still respect the onClick event in inputProps if that is set. |
I've spent some time on this one today and I still can't reproduce it the way the issue title says. Here is what I tried: <div
className="block-img-wrapper"
onClick={(evt) => {
console.log('parent')
}}
>
<Dropzone
disableClick={false}
>
</Dropzone>
</div> This is example code from one of the comments. If you paste it on https://react-dropzone.js.org, depending on As for @kylepotts example, it's less trivial. I've tested the code you provided at https://react-dropzone.js.org/ and couldn't reproduce the issue with invoking twice. But I could reproduce it when checked out from GH locally! Looking at your example, it seems to be a different from the described here since you don't nest the button element in the Dropzone. Changing the example to the following (note how I nest a button in dropzone):
(You can paste it into one of the example's code on https://react-dropzone.js.org/ to test) This will open the file dialog twice but it will log once. That is expected behavior. Since by default Dropzone will open File Dialog when you click on it. See https://react-dropzone.js.org/#proptypes. If you don't want this behavior, you have to use @kylepotts I think there is another issue that causes this but I don't understand what it is. I've tried to reproduce it on webpackbin: https://www.webpackbin.com/bins/-Km_p_Qa7aTNt4sn5PxC but it works as expected there. ~~~Any idea?~~~ There is a React issue about this: facebook/react#1691 |
@okonet I'm still experiencing this – the "Choose File" dialogue appears twice. If I exit after the first dialogue, no file is uploaded. EDIT: Worked around this by adding |
adding this did not help |
I also got the "Choose File" dialog twice, but only if the
If I modify the above code like this, then I only get one upload dialog:
|
Could be helpful to open a new issue with a bit of description and a reproducible example because it's hard to figure out from all the comments what the issue is. |
Maybe a few more pointers: import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps, open} = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.name}>
{file.name} - {file.size} bytes
</li>
));
return (
<section className="container">
<div {...getRootProps()}>
<input {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
<button onClick={open}>Browse Files</button>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
<Basic /> Reproducing:
Using <button onClick={(e) => { e.preventDefault(); open(e)}>Browse Files</button> only removing the |
Can confirm this issue still persists |
Working in an old project and landed with this issue. Someone got that solved? Like that seems to work:
|
Adding in onClick the functions: e.stopPropagation(), e.preventDefault() and open(); it fixed the issue |
it might not be a solution for other developers who commented here.
what happens here is that the label links to the closest input child. I just changed the label element to a div. even though I prefer label for these cases. |
I ran into an issue with click handlers and drag handlers being fired twice on a
<div>
, and found that it happens when I have<Dropzone>
as a child of the<div>
.Before
<Dropzone>
:Clicking the
<div>
results in:After
<Dropzone>
:Clicking the
<div>
results in two events:I've never seen this kind of thing in React before. Any idea what's going on?
The text was updated successfully, but these errors were encountered: