2021-04-01 00:44:21 +05:30
|
|
|
<template>
|
2021-12-28 05:34:20 +05:30
|
|
|
<div class="absolute suggestions-container">
|
2021-12-27 21:06:18 +05:30
|
|
|
<ul>
|
2021-04-06 15:40:17 +05:30
|
|
|
<li
|
|
|
|
v-for="(suggestion, i) in searchSuggestions"
|
|
|
|
:key="i"
|
2021-12-27 21:06:18 +05:30
|
|
|
class="suggestion"
|
2021-12-28 05:34:20 +05:30
|
|
|
:class="{ 'suggestion-selected': selected === i }"
|
2021-04-07 14:52:55 +05:30
|
|
|
@mouseover="onMouseOver(i)"
|
|
|
|
@mousedown.stop="onClick(i)"
|
2021-12-27 21:59:25 +05:30
|
|
|
v-text="suggestion"
|
|
|
|
/>
|
2021-04-01 03:39:39 +05:30
|
|
|
</ul>
|
|
|
|
</div>
|
2021-04-01 00:44:21 +05:30
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
2021-10-09 00:22:51 +05:30
|
|
|
searchText: { type: String, default: "" },
|
2021-04-06 15:40:17 +05:30
|
|
|
},
|
2021-10-09 00:22:51 +05:30
|
|
|
emits: ["searchchange"],
|
2021-04-06 15:40:17 +05:30
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
selected: 0,
|
2021-05-10 23:44:28 +05:30
|
|
|
searchSuggestions: [],
|
2021-04-06 15:40:17 +05:30
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onKeyUp(e) {
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
if (this.selected <= 0) {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(this.searchSuggestions.length - 1);
|
2021-04-06 15:40:17 +05:30
|
|
|
} else {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(this.selected - 1);
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
if (this.selected >= this.searchSuggestions.length - 1) {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(0);
|
2021-04-06 15:40:17 +05:30
|
|
|
} else {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(this.selected + 1);
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
this.refreshSuggestions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async refreshSuggestions() {
|
2022-07-02 18:04:45 +05:30
|
|
|
this.searchSuggestions = (
|
|
|
|
await this.fetchJson(this.apiUrl() + "/opensearch/suggestions", {
|
|
|
|
query: this.searchText,
|
|
|
|
})
|
|
|
|
)?.[1];
|
2021-04-06 15:40:17 +05:30
|
|
|
this.searchSuggestions.unshift(this.searchText);
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(0);
|
|
|
|
},
|
|
|
|
onMouseOver(i) {
|
|
|
|
if (i !== this.selected) {
|
|
|
|
this.selected = i;
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
2021-04-07 14:52:55 +05:30
|
|
|
},
|
|
|
|
onClick(i) {
|
|
|
|
this.setSelected(i);
|
|
|
|
this.$router.push({
|
|
|
|
name: "SearchResults",
|
2021-05-10 23:44:28 +05:30
|
|
|
query: { search_query: this.searchSuggestions[i] },
|
2021-04-07 14:52:55 +05:30
|
|
|
});
|
|
|
|
},
|
|
|
|
setSelected(val) {
|
|
|
|
this.selected = val;
|
|
|
|
this.$emit("searchchange", this.searchSuggestions[this.selected]);
|
2021-05-10 23:44:28 +05:30
|
|
|
},
|
|
|
|
},
|
2021-04-01 00:44:21 +05:30
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2021-04-01 03:39:39 +05:30
|
|
|
<style>
|
|
|
|
.suggestions-container {
|
2021-12-28 05:34:20 +05:30
|
|
|
@apply left-1/2 translate-x-[-50%] transform-gpu max-w-3xl w-full box-border p-y-1.25 z-10 <md:max-w-[calc(100%-0.5rem)] bg-gray-300;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dark .suggestions-container {
|
|
|
|
@apply bg-dark-400;
|
|
|
|
}
|
|
|
|
|
|
|
|
.auto .suggestions-container {
|
|
|
|
@apply dark:bg-dark-400;
|
|
|
|
}
|
|
|
|
|
|
|
|
.suggestion-selected {
|
|
|
|
@apply bg-gray-200;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dark .suggestion-selected {
|
|
|
|
@apply bg-dark-100;
|
|
|
|
}
|
|
|
|
|
|
|
|
.auto .suggestion-selected {
|
|
|
|
@apply dark:bg-dark-100;
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
2021-12-27 21:06:18 +05:30
|
|
|
|
2021-04-06 15:40:17 +05:30
|
|
|
.suggestion {
|
2021-12-27 21:06:18 +05:30
|
|
|
@apply p-y-1;
|
2021-04-01 03:39:39 +05:30
|
|
|
}
|
|
|
|
</style>
|