Skip to content

Commit

Permalink
Incorrect Binding to Window Event Listeners Fix (#140)
Browse files Browse the repository at this point in the history
* Fix incorrect event listener bindings

Dragging now does not always bind event listeners to the global window object.

* Fix broken tests

When testing this.props.draggableRef is not defined. 
Therefore when testing, default to global window object.
  • Loading branch information
kgorgi authored and jaredcrowe committed Oct 16, 2017
1 parent 506bf99 commit 2f19969
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions src/view/drag-handle/drag-handle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -505,24 +505,30 @@ export default class DragHandle extends Component {
}
}

unbindWindowEvents = () => {
window.removeEventListener('mousemove', this.onWindowMouseMove);
window.removeEventListener('mouseup', this.onWindowMouseUp);
window.removeEventListener('mousedown', this.onWindowMouseDown);
window.removeEventListener('keydown', this.onWindowKeyDown);
window.removeEventListener('resize', this.onWindowResize);
window.removeEventListener('scroll', this.onWindowScroll);
window.removeEventListener('webkitmouseforcechanged', this.mouseForceChanged);
unbindWindowEvents = () => {
// this.props.draggableRef is not defined when testing. If testing, default to global window object.
const win = this.props.draggableRef ? this.props.draggableRef.ownerDocument.defaultView : window;

win.removeEventListener('mousemove', this.onWindowMouseMove);
win.removeEventListener('mouseup', this.onWindowMouseUp);
win.removeEventListener('mousedown', this.onWindowMouseDown);
win.removeEventListener('keydown', this.onWindowKeyDown);
win.removeEventListener('resize', this.onWindowResize);
win.removeEventListener('scroll', this.onWindowScroll);
win.removeEventListener('webkitmouseforcechanged', this.mouseForceChanged);
}

bindWindowEvents = () => {
window.addEventListener('mousemove', this.onWindowMouseMove);
window.addEventListener('mouseup', this.onWindowMouseUp);
window.addEventListener('mousedown', this.onWindowMouseDown);
window.addEventListener('keydown', this.onWindowKeyDown);
window.addEventListener('resize', this.onWindowResize);
window.addEventListener('scroll', this.onWindowScroll, { passive: true });
window.addEventListener('webkitmouseforcechanged', this.mouseForceChanged);
// this.props.draggableRef is not defined when testing. If testing, default to global window object.
const win = this.props.draggableRef ? this.props.draggableRef.ownerDocument.defaultView : window;

win.addEventListener('mousemove', this.onWindowMouseMove);
win.addEventListener('mouseup', this.onWindowMouseUp);
win.addEventListener('mousedown', this.onWindowMouseDown);
win.addEventListener('keydown', this.onWindowKeyDown);
win.addEventListener('resize', this.onWindowResize);
win.addEventListener('scroll', this.onWindowScroll, { passive: true });
win.addEventListener('webkitmouseforcechanged', this.mouseForceChanged);
}

getProvided = memoizeOne((isEnabled: boolean, isDragging: boolean): ?Provided => {
Expand Down

0 comments on commit 2f19969

Please sign in to comment.