I'm trying to implement Autocomplete from scratch in Vue, however I have issue with selecting option in dropdown. I'm enabling (shownig) this dropdown on input click or if user types something. Then, on focus outside of input I want to get rid of dropdown. However, it means that when I select something inside dropdown, I'm closing dropdown instead of triggering selectOption
. How to keep both v-on:focusout
on input
and @click="selectOption(option)"
on dropdown option? So far, the only way to make selectOption
work was to remove focusout
, which isn't something I want...
<template>
<div>
<input
type="text"
v-on:input="enableDropdown"
v-on:click="enableDropdown"
v-on:focusout="disableDropdown"
/>
<div class="options-dropdown" v-if="showDropdown">
<div
class="option"
v-for="option in options"
:key="option"
@click="selectOption(option)"
>
{{ option }}
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
options: [
"java",
"c#",
"c++"
],
showDropdown: false,
};
},
methods: {
enableDropdown() {
this.showDropdown = true;
},
disableDropdown() {
this.showDropdown = false;
},
selectOption($event) {
console.log($event);
},
},
};
</script>