Skip to content

Commit

Permalink
fix: fix click event propagation and close #826
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandjitsu committed Jul 21, 2019
1 parent 93bded7 commit a159354
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
39 changes: 17 additions & 22 deletions src/index.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -454,27 +454,20 @@ export function useDropzone({
}, []) }, [])


// Cb to open the file dialog when click occurs on the dropzone // Cb to open the file dialog when click occurs on the dropzone
const onClickCb = useCallback( const onClickCb = useCallback(() => {
event => { if (noClick) {
// Prevent click events from propagating to the <input> when the click event return
// originated from a <label> that wraps the dropzone }
event.preventDefault()

if (noClick) {
return
}


// In IE11/Edge the file-browser dialog is blocking, therefore, use setTimeout() // In IE11/Edge the file-browser dialog is blocking, therefore, use setTimeout()
// to ensure React can handle state changes // to ensure React can handle state changes
// See: https://github.com/react-dropzone/react-dropzone/issues/450 // See: https://github.com/react-dropzone/react-dropzone/issues/450
if (isIeOrEdge()) { if (isIeOrEdge()) {
setTimeout(openFileDialog, 0) setTimeout(openFileDialog, 0)
} else { } else {
openFileDialog() openFileDialog()
} }
}, }, [inputRef, noClick])
[inputRef, noClick]
)


const [dragTargets, setDragTargets] = useState([]) const [dragTargets, setDragTargets] = useState([])
const onDocumentDrop = event => { const onDocumentDrop = event => {
Expand Down Expand Up @@ -688,6 +681,7 @@ export function useDropzone({
onDragLeave: composeDragHandler(composeEventHandlers(onDragLeave, onDragLeaveCb)), onDragLeave: composeDragHandler(composeEventHandlers(onDragLeave, onDragLeaveCb)),
onDrop: composeDragHandler(composeEventHandlers(onDrop, onDropCb)), onDrop: composeDragHandler(composeEventHandlers(onDrop, onDropCb)),
[refKey]: rootRef, [refKey]: rootRef,
...(rootRef.current && rootRef.current.tagName === 'LABEL' ? { htmlFor: 'noop' } : {}),
...(!disabled && !noKeyboard ? { tabIndex: 0 } : {}), ...(!disabled && !noKeyboard ? { tabIndex: 0 } : {}),
...rest ...rest
}), }),
Expand All @@ -712,7 +706,7 @@ export function useDropzone({
}, []) }, [])


const getInputProps = useMemo( const getInputProps = useMemo(
() => ({ refKey = 'ref', onChange, onClick, ...rest } = {}) => { () => ({ refKey = 'ref', onChange, onClick, disabled, ...rest } = {}) => {
const inputProps = { const inputProps = {
accept, accept,
multiple, multiple,
Expand All @@ -722,6 +716,7 @@ export function useDropzone({
onClick: composeHandler(composeEventHandlers(onClick, onInputElementClick)), onClick: composeHandler(composeEventHandlers(onClick, onInputElementClick)),
autoComplete: 'off', autoComplete: 'off',
tabIndex: -1, tabIndex: -1,
disabled: disabled || noClick,
[refKey]: inputRef [refKey]: inputRef
} }


Expand All @@ -730,7 +725,7 @@ export function useDropzone({
...rest ...rest
} }
}, },
[inputRef, accept, multiple, onDropCb, disabled] [inputRef, accept, multiple, onDropCb, disabled, noClick]
) )


const fileCount = draggedFiles.length const fileCount = draggedFiles.length
Expand Down
2 changes: 0 additions & 2 deletions src/index.spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -530,14 +530,12 @@ describe('useDropzone() hook', () => {
const dropzone = container.querySelector('label') const dropzone = container.querySelector('label')


const event = new Event('click', { bubbles: true, cancelable: true }) const event = new Event('click', { bubbles: true, cancelable: true })
const preventDefaultSpy = jest.spyOn(event, 'preventDefault')


fireEvent(dropzone, event) fireEvent(dropzone, event)


const ref = activeRef.current const ref = activeRef.current
expect(ref).not.toBeNull() expect(ref).not.toBeNull()
expect(dropzone).toContainElement(ref) expect(dropzone).toContainElement(ref)
expect(preventDefaultSpy).toHaveBeenCalled()
expect(onClickSpy).toHaveBeenCalledTimes(1) expect(onClickSpy).toHaveBeenCalledTimes(1)
}) })
}) })
Expand Down

0 comments on commit a159354

Please sign in to comment.