Skip to content

Commit

Permalink
fix: improve onClick and disableClick props (fixes #530) (#694)
Browse files Browse the repository at this point in the history
- Let onClick callback on container propagate when `{disableClick }` is true
- Do not open file dialog if `.preventDefault()` was called on the event
  • Loading branch information
ibc authored and rolandjitsu committed Oct 13, 2018
1 parent 5fedd14 commit bb26403
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/index.js
Expand Up @@ -233,12 +233,16 @@ class Dropzone extends React.Component {

onClick(evt) {
const { onClick, disableClick } = this.props
if (!disableClick) {
evt.stopPropagation()

if (onClick) {
onClick.call(this, evt)
}
// if onClick prop is given, run it first
if (onClick) {
onClick.call(this, evt)
}

// if disableClick is not set and the event hasn't been default prefented within
// the onClick listener, open the file dialog
if (!disableClick && !evt.isDefaultPrevented()) {

This comment has been minimized.

Copy link
@jschill

jschill Dec 30, 2018

Contributor

Is there any reason not to use evt.defaultPrevented instead?

evt.stopPropagation()

// in IE11/Edge the file-browser dialog is blocking, ensure this is behind setTimeout
// this is so react can handle state changes in the onClick prop above above
Expand Down
18 changes: 18 additions & 0 deletions src/index.spec.js
Expand Up @@ -242,6 +242,24 @@ describe('Dropzone', () => {
expect(onClick).toHaveBeenCalled()
})

it('should call `onClick` if provided even if `disableClick` is set', () => {
const onClick = jest.fn()
const dropzone = mount(<Dropzone disableClick onClick={onClick} />)
const open = jest.spyOn(dropzone.instance(), 'open')
dropzone.simulate('click')
expect(open).toHaveBeenCalledTimes(0)
expect(onClick).toHaveBeenCalled()
})

it('should not call `open` if event was prevented in `onClick`', () => {
const onClick = jest.fn(event => event.preventDefault())
const dropzone = mount(<Dropzone onClick={onClick} />)
const open = jest.spyOn(dropzone.instance(), 'open')
dropzone.simulate('click')
expect(open).toHaveBeenCalledTimes(0)
expect(onClick).toHaveBeenCalled()
})

it('should reset the value of input', () => {
const dropzone = mount(<Dropzone />)
expect(
Expand Down

0 comments on commit bb26403

Please sign in to comment.