Piped/src/components/SearchSuggestions.vue

103 lines
2.8 KiB
Vue
Raw Normal View History

2021-03-31 19:14:21 +00:00
<template>
<div
class="uk-position-absolute uk-panel uk-box-shadow-large suggestions-container"
:style="[{ background: secondaryBackgroundColor }]"
>
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"
:style="[selected === i ? { background: secondaryForegroundColor } : {}]"
@mouseover="onMouseOver(i)"
@mousedown.stop="onClick(i)"
2021-04-06 10:10:17 +00:00
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>
export default {
props: {
2021-05-10 18:14:28 +00:00
searchText: String,
2021-04-06 10:10:17 +00:00
},
data() {
return {
selected: 0,
2021-05-10 18:14:28 +00:00
searchSuggestions: [],
2021-04-06 10:10:17 +00:00
};
},
methods: {
onKeyUp(e) {
if (e.key === "ArrowUp") {
if (this.selected <= 0) {
this.setSelected(this.searchSuggestions.length - 1);
2021-04-06 10:10:17 +00:00
} else {
this.setSelected(this.selected - 1);
2021-04-06 10:10:17 +00:00
}
e.preventDefault();
} else if (e.key === "ArrowDown") {
if (this.selected >= this.searchSuggestions.length - 1) {
this.setSelected(0);
2021-04-06 10:10:17 +00:00
} else {
this.setSelected(this.selected + 1);
2021-04-06 10:10:17 +00:00
}
e.preventDefault();
} else {
this.refreshSuggestions();
}
},
async refreshSuggestions() {
this.searchSuggestions = await this.fetchJson(this.apiUrl() + "/suggestions", {
query: this.searchText,
});
2021-04-06 10:10:17 +00:00
this.searchSuggestions.unshift(this.searchText);
this.setSelected(0);
},
onMouseOver(i) {
if (i !== this.selected) {
this.selected = i;
2021-04-06 10:10:17 +00:00
}
},
onClick(i) {
this.setSelected(i);
this.$router.push({
name: "SearchResults",
2021-05-10 18:14:28 +00:00
query: { search_query: this.searchSuggestions[i] },
});
},
setSelected(val) {
this.selected = val;
this.$emit("searchchange", this.searchSuggestions[this.selected]);
2021-05-10 18:14:28 +00:00
},
},
2021-03-31 19:14:21 +00:00
};
</script>
2021-03-31 22:09:39 +00:00
<style>
.suggestions-container {
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;
z-index: 10;
2021-04-06 10:10:17 +00:00
}
.suggestion {
padding: 4px 15px;
}
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>