Last active
July 7, 2018 20:35
-
-
Save jonathanharrell/a08b7ada087589f667bfab8df6dd1aba to your computer and use it in GitHub Desktop.
Vue Autocomplete Component (with scoped slots)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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