Last active
April 17, 2019 20:53
-
-
Save toast38coza/2972422a1a2b8705e2d65d54fde3d882 to your computer and use it in GitHub Desktop.
Extending the v-autocomplete to support typeahead
This file contains hidden or 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
v-model: {{foo}} | |
<type-ahead | |
v-model='foo' | |
:items='["foo", "bar", "baz"]' > | |
</type-ahead> |
This file contains hidden or 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
<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