Skip to content

Commit

Permalink
Add option to prevent native dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
grafluxe committed Nov 22, 2017
1 parent 927162e commit 12cc697
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Dropdownizer.js
Expand Up @@ -36,6 +36,10 @@ class Dropdownizer{
this._onChange = callback;
}

static preventNative() {
Dropdownize._preventNative = true;
}

}

class Dropdownize {
Expand Down Expand Up @@ -69,6 +73,7 @@ class Dropdownize {
this._onMouseLeave = () => setTimeout(this._closeList.bind(this), 250);
this._onChange = this._syncDropdowns.bind(this);
this._onClickListItem = this._listSelect.bind(this);
this._onDocClick = this._preventNativeClick.bind(this);
}

_convertOptionsToListItems() {
Expand Down Expand Up @@ -115,7 +120,7 @@ class Dropdownize {
return;
}

if (this._touchable) {
if (this._touchable && !Dropdownize._preventNative) {
this._el.classList.remove("dd-x");
this._el.focus();
this._el.classList.add("dd-x");
Expand All @@ -124,11 +129,23 @@ class Dropdownize {
this._closeList();
} else {
this._ui.div.classList.add("dd-open");
this._ui.div.addEventListener("mouseleave", this._onMouseLeave);

if (Dropdownize._preventNative) {
document.addEventListener("click", this._onDocClick);
} else {
this._ui.div.addEventListener("mouseleave", this._onMouseLeave);
}
}
}
}

_preventNativeClick(evt) {
if (evt.target.parentNode !== this._ui.div) {
document.removeEventListener("click", this._onDocClick);
this._closeList();
}
}

_closeList() {
this._ui.div.classList.remove("dd-open");
}
Expand Down Expand Up @@ -232,6 +249,7 @@ class Dropdownize {
this._ui.btn.removeEventListener("click", this._onClickBtn);
this._ui.div.removeEventListener("mouseleave", this._onMouseLeave);
this._el.removeEventListener("change", this._onChange);
document.removeEventListener("click", this._onDocClick);

this._listItems.forEach(listItem => {
listItem.removeEventListener("click", this._onClickListItem);
Expand Down

0 comments on commit 12cc697

Please sign in to comment.