Skip to content

Commit

Permalink
fix(mouseup): do not reset when active element is in downshift (#383)
Browse files Browse the repository at this point in the history
Closes #356
  • Loading branch information
Kent C. Dodds authored Mar 20, 2018
1 parent 8f282af commit 8559b79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/downshift.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getA11yStatusMessage,
unwrapArray,
isDOMElement,
isOrContainsNode,
getElementProps,
noop,
requiredProp,
Expand Down Expand Up @@ -850,16 +851,15 @@ class Downshift extends Component {
const onMouseUp = event => {
const {document} = this.props.environment
this.isMouseDown = false
const targetIsRoot = event.target === this._rootNode
const rootContainsTarget =
this._rootNode && this._rootNode.contains(event.target)
const targetInDownshift = targetIsRoot || rootContainsTarget
const targetIsDownshiftInput =
this.inputId && document.activeElement.id === this.inputId
const targetInDownshift =
this._rootNode && isOrContainsNode(this._rootNode, event.target)
const activeElementInDownshift =
this._rootNode &&
isOrContainsNode(this._rootNode, document.activeElement)
if (
!targetInDownshift &&
this.getState().isOpen &&
!targetIsDownshiftInput
!activeElementInDownshift &&
this.getState().isOpen
) {
this.reset({type: Downshift.stateChangeTypes.mouseUp}, () =>
this.props.onOuterClick(this.getStateAndHelpers()),
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ function scrollIntoView(node, rootNode) {
// the item is within the scrollable area (do nothing)
}

/**
* @param {HTMLElement} parent the parent node
* @param {HTMLElement} child the child node
* @return {Boolean} whether the parent is the child or the child is in the parent
*/
function isOrContainsNode(parent, child) {
return parent === child || parent.contains(child)
}

/**
* Simple debounce implementation. Will call the given
* function once after the time given has passed since
Expand Down Expand Up @@ -287,6 +296,7 @@ export {
unwrapArray,
isDOMElement,
getElementProps,
isOrContainsNode,
noop,
requiredProp,
setIdCounter,
Expand Down

0 comments on commit 8559b79

Please sign in to comment.