make suggestion clickable, fix event listener issue

This commit is contained in:
Maurice Oegerli 2021-04-07 11:22:55 +02:00
parent f922dd74eb
commit 6528ce7bf8
2 changed files with 27 additions and 19 deletions

View file

@ -15,7 +15,7 @@
type="text"
placeholder="Search"
v-model="searchText"
@keydown="onKeyDown"
@keyup="onKeyUp"
@focus="onInputFocus"
@blur="onInputBlur"
/>
@ -40,7 +40,7 @@
type="text"
placeholder="Search"
v-model="searchText"
@keydown="onKeyDown"
@keyup="onKeyUp"
@focus="onInputFocus"
@blur="onInputBlur"
/>
@ -49,6 +49,7 @@
v-show="searchText && suggestionsVisible"
:searchText="searchText"
@searchchange="onSearchTextChange"
ref="searchSuggestions"
/>
</template>
@ -66,7 +67,7 @@ export default {
};
},
methods: {
onKeyDown(e) {
onKeyUp(e) {
if (e.key === "Enter") {
this.$router.push({
name: "SearchResults",
@ -76,6 +77,7 @@ export default {
} else if (e.key === "ArrowUp" || e.key === "ArrowDown") {
e.preventDefault();
}
this.$refs.searchSuggestions.onKeyUp(e);
},
onInputFocus() {
this.suggestionsVisible = true;

View file

@ -5,6 +5,8 @@
v-for="(suggestion, i) in searchSuggestions"
:key="i"
:class="{ selected: selected === i }"
@mouseover="onMouseOver(i)"
@mousedown.stop="onClick(i)"
class="uk-margin-remove suggestion"
>
{{ suggestion }}
@ -30,16 +32,16 @@ export default {
onKeyUp(e) {
if (e.key === "ArrowUp") {
if (this.selected <= 0) {
this.selected = this.searchSuggestions.length - 1;
this.setSelected(this.searchSuggestions.length - 1);
} else {
this.selected -= 1;
this.setSelected(this.selected - 1);
}
e.preventDefault();
} else if (e.key === "ArrowDown") {
if (this.selected >= this.searchSuggestions.length - 1) {
this.selected = 0;
this.setSelected(0);
} else {
this.selected += 1;
this.setSelected(this.selected + 1);
}
e.preventDefault();
} else {
@ -51,20 +53,24 @@ export default {
Constants.BASE_URL + "/suggestions?query=" + encodeURI(this.searchText)
);
this.searchSuggestions.unshift(this.searchText);
}
},
watch: {
selected(newValue, oldValue) {
if (newValue !== oldValue && this.searchSuggestions[this.selected]) {
this.$emit("searchchange", this.searchSuggestions[this.selected]);
this.setSelected(0);
},
onMouseOver(i) {
if (i !== this.selected) {
this.selected = i;
}
},
onClick(i) {
this.setSelected(i);
this.$router.push({
name: "SearchResults",
query: { search_query: this.searchText }
});
},
setSelected(val) {
this.selected = val;
this.$emit("searchchange", this.searchSuggestions[this.selected]);
}
},
mounted() {
window.addEventListener("keyup", this.onKeyUp);
},
beforeUnmount() {
window.removeEventListener("keyup", this.onKeyUp);
}
};
</script>