Skip to content

Instantly share code, notes, and snippets.

@jonathanharrell
Last active July 7, 2018 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanharrell/a08b7ada087589f667bfab8df6dd1aba to your computer and use it in GitHub Desktop.
Save jonathanharrell/a08b7ada087589f667bfab8df6dd1aba to your computer and use it in GitHub Desktop.
Vue Autocomplete Component (with scoped slots)
Vue.component('autocomplete', {
data () {
return {
dropdownVisible: false
}
},
methods: {
filterMethod (options, query) {
return options.filter(option => option.toLowerCase().includes(query.toLowerCase()))
},
showDropdown () {
this.dropdownVisible = true
},
hideDropdown () {
this.dropdownVisible = false
}
},
template: `
<search-select
:options="options"
:filterMethod="filterMethod"
>
<div slot-scope="{ results, searchList }">
<div class="autocomplete">
<input
type="text"
placeholder="Type to search list"
@input="searchList"
@focus="showDropdown"
@blur="hideDropdown"
/>
<div class="autocomplete-dropdown" v-if="dropdownVisible">
<ul class="autocomplete-search-results-list">
<li
class="autocomplete-search-result"
v-for="result in results"
:key="result"
>
{{ result }}
</li>
</ul>
</div>
</div>
</div>
</search-select>
`
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment