Piped/src/components/SearchSuggestions.vue

99 lines
2.5 KiB
Vue
Raw Normal View History

2021-03-31 19:14:21 +00:00
<template>
2021-04-06 10:10:17 +00:00
<div class="uk-position-absolute uk-panel uk-box-shadow-large suggestions-container">
2021-03-31 22:09:39 +00:00
<ul class="uk-list uk-margin-remove uk-text-secondary">
2021-04-06 10:10:17 +00:00
<li
v-for="(suggestion, i) in searchSuggestions"
:key="i"
:class="{ selected: selected === i }"
class="uk-margin-remove suggestion"
>
2021-03-31 22:09:39 +00:00
{{ suggestion }}
</li>
</ul>
</div>
2021-03-31 19:14:21 +00:00
</template>
<script>
2021-04-06 10:10:17 +00:00
import Constants from "@/Constants.js";
2021-03-31 19:14:21 +00:00
export default {
props: {
2021-04-06 10:10:17 +00:00
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);
2021-03-31 19:14:21 +00:00
}
};
</script>
2021-03-31 22:09:39 +00:00
<style>
.suggestions-container {
background-color: #242727;
left: 50%;
transform: translateX(-50%);
max-width: 640px;
width: 100%;
box-sizing: border-box;
2021-04-06 10:10:17 +00:00
padding: 5px 0;
}
.suggestion {
padding: 4px 15px;
}
.suggestion.selected {
background-color: #393d3d;
2021-03-31 22:09:39 +00:00
}
@media screen and (max-width: 959px) {
.suggestions-container {
max-width: calc(100% - 60px);
}
}
@media screen and (max-width: 639px) {
.suggestions-container {
max-width: calc(100% - 30px);
}
}
</style>