Make suggestions interactive

This commit is contained in:
Maurice Oegerli
2021-04-06 12:10:17 +02:00
parent 9016032d48
commit f922dd74eb
2 changed files with 75 additions and 23 deletions

View File

@@ -1,9 +1,12 @@
<template>
<div
class="uk-position-absolute uk-panel uk-box-shadow-large uk-padding-small suggestions-container"
>
<div class="uk-position-absolute uk-panel uk-box-shadow-large suggestions-container">
<ul class="uk-list uk-margin-remove uk-text-secondary">
<li v-for="(suggestion, i) in searchSuggestions" :key="i">
<li
v-for="(suggestion, i) in searchSuggestions"
:key="i"
:class="{ selected: selected === i }"
class="uk-margin-remove suggestion"
>
{{ suggestion }}
</li>
</ul>
@@ -11,9 +14,57 @@
</template>
<script>
import Constants from "@/Constants.js";
export default {
props: {
searchSuggestions: Array
searchText: String
},
data() {
return {
selected: 0,
searchSuggestions: []
};
},
methods: {
onKeyUp(e) {
if (e.key === "ArrowUp") {
if (this.selected <= 0) {
this.selected = this.searchSuggestions.length - 1;
} else {
this.selected -= 1;
}
e.preventDefault();
} else if (e.key === "ArrowDown") {
if (this.selected >= this.searchSuggestions.length - 1) {
this.selected = 0;
} else {
this.selected += 1;
}
e.preventDefault();
} else {
this.refreshSuggestions();
}
},
async refreshSuggestions() {
this.searchSuggestions = await this.fetchJson(
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]);
}
}
},
mounted() {
window.addEventListener("keyup", this.onKeyUp);
},
beforeUnmount() {
window.removeEventListener("keyup", this.onKeyUp);
}
};
</script>
@@ -26,6 +77,13 @@ export default {
max-width: 640px;
width: 100%;
box-sizing: border-box;
padding: 5px 0;
}
.suggestion {
padding: 4px 15px;
}
.suggestion.selected {
background-color: #393d3d;
}
@media screen and (max-width: 959px) {
.suggestions-container {