Skip to content

Commit 56251fe

Browse files
Kyle Pottsokonet
authored andcommitted
fix: Prevent onClick event being called twice on input element (react-dropzone#437)
Closes react-dropzone#252
1 parent 9a5e703 commit 56251fe

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Dropzone extends React.Component {
3232
this.onDrop = this.onDrop.bind(this)
3333
this.onFileDialogCancel = this.onFileDialogCancel.bind(this)
3434
this.setRef = this.setRef.bind(this)
35+
this.setRefs = this.setRefs.bind(this)
36+
this.onInputElementClick = this.onInputElementClick.bind(this)
3537
this.isFileDialogActive = false
3638
this.state = {
3739
draggedFiles: [],
@@ -48,6 +50,7 @@ class Dropzone extends React.Component {
4850
document.addEventListener('dragover', Dropzone.onDocumentDragOver, false)
4951
document.addEventListener('drop', this.onDocumentDrop, false)
5052
}
53+
this.fileInputEl.addEventListener('click', this.onInputElementClick, false)
5154
// Tried implementing addEventListener, but didn't work out
5255
document.body.onfocus = this.onFileDialogCancel
5356
}
@@ -58,6 +61,7 @@ class Dropzone extends React.Component {
5861
document.removeEventListener('dragover', Dropzone.onDocumentDragOver)
5962
document.removeEventListener('drop', this.onDocumentDrop)
6063
}
64+
this.fileInputEl.removeEventListener('click', this.onInputElementClick, false)
6165
// Can be replaced with removeEventListener, if addEventListener works
6266
document.body.onfocus = null
6367
}
@@ -196,6 +200,13 @@ class Dropzone extends React.Component {
196200
}
197201
}
198202

203+
onInputElementClick(evt) {
204+
evt.stopPropagation()
205+
if (this.props.inputProps && this.props.inputProps.onClick) {
206+
this.props.inputProps.onClick()
207+
}
208+
}
209+
199210
onFileDialogCancel() {
200211
// timeout will not recognize context of this method
201212
const { onFileDialogCancel } = this.props
@@ -219,6 +230,10 @@ class Dropzone extends React.Component {
219230
this.node = ref
220231
}
221232

233+
setRefs(ref) {
234+
this.fileInputEl = ref
235+
}
236+
222237
fileMatchSize(file) {
223238
return file.size <= this.props.maxSize && file.size >= this.props.minSize
224239
}
@@ -323,7 +338,7 @@ class Dropzone extends React.Component {
323338
type: 'file',
324339
style: { display: 'none' },
325340
multiple: supportMultiple && multiple,
326-
ref: el => this.fileInputEl = el, // eslint-disable-line
341+
ref: this.setRefs,
327342
onChange: this.onDrop
328343
}
329344

src/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ describe('Dropzone', () => {
246246
component.find('Dropzone').simulate('click')
247247
expect(onClickOuterSpy.callCount).toEqual(1)
248248
})
249+
250+
it('should invoke inputProps onClick if provided', () => {
251+
const inputPropsClickSpy = spy()
252+
const component = mount(<Dropzone inputProps={{ onClick: inputPropsClickSpy }} />)
253+
254+
component.find('Dropzone').simulate('click')
255+
expect(inputPropsClickSpy.callCount).toEqual(1)
256+
})
249257
})
250258

251259
describe('drag-n-drop', () => {

0 commit comments

Comments
 (0)