Skip to content

Instantly share code, notes, and snippets.

@toast38coza
Last active April 17, 2019 20:53
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 toast38coza/2972422a1a2b8705e2d65d54fde3d882 to your computer and use it in GitHub Desktop.
Save toast38coza/2972422a1a2b8705e2d65d54fde3d882 to your computer and use it in GitHub Desktop.
Extending the v-autocomplete to support typeahead
v-model: {{foo}}
<type-ahead
v-model='foo'
:items='["foo", "bar", "baz"]' >
</type-ahead>
<template>
<v-autocomplete
v-model='item'
@update:searchInput='valueTyped'
:items='computedItems'
v-bind='extraProps' >
</v-autocomplete>
</template>
<script>
export default {
name: 'TypeAhead',
props: {
value: { type: String },
items: { type: Array, required: true },
extraProps: { type: Object, default: () => { return {} } }
},
data () {
return {
item: '',
searchText: ''
}
},
watch: {
value: {
immediate: true,
handler () {
if (this.value) {
this.item = this.value
this.searchText = this.value
}
}
},
item () {
this.$emit('input', this.item)
this.searchText = this.item
}
},
methods: {
valueTyped (val) {
this.searchText = val
}
},
computed: {
computedItems () {
return this.items.concat([this.searchText])
}
}
}
</script>
<style>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment