I'm trying to create a customCombobox component that works like a normal v-combobox with one addition - after user presses tab key, it will select the first option automatically (if there are any).
What I've done so far looks good but v-model on this field doesn't work (it's always null).
<template>
<v-combobox ref="combobox" :rules="rules"
@keydown.tab.prevent="selectFirst"
:value="innerValue" :label="label"
:items="items"
>
</v-combobox>
</template>
<script>
module.exports = {
props: ['value', 'label', 'items', 'rules'],
data() {
return {
innerValue:null,
}
},
watch:{
value(_new){
this.innerValue = _new
this.$emit('input',[_new])
this.$emit('change')
}
},
methods: {
selectFirst() {
var combobox = this.$refs.combobox
var filteredItems = combobox.filteredItems
if (filteredItems.length){
this.innerValue = filteredItems[0]
}
}
},
computed: {
}
}
</script>
Do you know why?
You can use a computed setter in the custom component to handle the
v-modelfrom the parent and pass it down to its own childv-combobox:Custom component:
Using
v-bind="$attrs"also passes down all of the props from the parent. Here's a demo: