Skip to content

Commit

Permalink
fix(scroll) don't close autosuggest when clicking on scrollbar (#64)
Browse files Browse the repository at this point in the history
* destroy event listeners in beforeDestroy lifecycle
* tests(listener) trigger mousedown as well
  • Loading branch information
darrenjennings authored Oct 11, 2018
1 parent fd68510 commit 5eb0f9a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions __tests__/autosuggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ describe("Autosuggest", () => {

input.trigger("click");
wrapper.setData({ searchInput: "G" });
window.document.dispatchEvent(new Event("mousedown"));
window.document.dispatchEvent(new Event("mouseup"));

await wrapper.vm.$nextTick(() => {});

const renderer = createRenderer();
renderer.renderToString(wrapper.vm, (err, str) => {
Expand Down
17 changes: 15 additions & 2 deletions src/Autosuggest.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export default {
defaultSectionConfig: {
name: "default",
type: "default-section"
}
},
clientXMouseDownInitial: null
};
},
computed: {
Expand Down Expand Up @@ -282,8 +283,13 @@ export default {
},
mounted() {
document.addEventListener("mouseup", this.onDocumentMouseUp);
document.addEventListener("mousedown", this.onDocumentMouseDown);
this.loading = true;
},
beforeDestroy() {
document.removeEventListener("mouseup", this.onDocumentMouseUp)
document.removeEventListener("mousedown", this.onDocumentMouseDown)
},
methods: {
getSectionRef(i) {
return "computed_section_" + i;
Expand Down Expand Up @@ -440,10 +446,17 @@ export default {
updateCurrentIndex(index) {
this.currentIndex = index;
},
clickedOnScrollbar(mouseX){
const results = document.querySelector(`.${this.componentAttrClassAutosuggestResultsContainer}`);
return results.offsetWidth <= mouseX;
},
onDocumentMouseDown(e) {
this.clientXMouseDownInitial = e.clientX
},
onDocumentMouseUp(e) {
/** Do not re-render list on input click */
const isChild = this.$el.contains(e.target);
if (isChild && e.target.tagName === 'INPUT') {
if (isChild && e.target.tagName === 'INPUT' || this.clickedOnScrollbar(this.clientXMouseDownInitial)) {
return;
}
Expand Down

0 comments on commit 5eb0f9a

Please sign in to comment.