Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mouseup): do not reset when active element is in downshift #383

Merged
merged 1 commit into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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