The below given code you can see the magic of v-model
, after checking / unchecking the checkboxes the array checkedNames
will add/remove names automatically. We don't have to write anything to push/slice/filter names from the array, right?
const { ref } = Vue;
const App = {
setup () {
const checkedNames = ref([])
return { checkedNames }
}
}
Vue.createApp(App).mount("#app");
<script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>
<div id="app">
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames">
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
<label for="mike">Mike</label>
<br>
<span>Checked names: {{ checkedNames }}</span>
</div>
My question is: How can I do the same feature but with custom components?
You can see, In below given code snippets I tried but I did not like it because we are missing that magic of v-model
instead I am handeling this in my function addOrRemoveItem()
heads-up: It may seem to you that I am over explaining :) sorry for that.
I followed this tutorial for some idea but was not much helpful, then referred vue official doc here. below is the functional code but it's kinda overkill.
const { ref, createApp } = Vue;
const app = createApp({
setup() {
const itemsSelected = ref([]);
const items = ref([
{ id: '1', name: 'Jack' },
{ id: '2', name: 'John' },
{ id: '3', name: 'Mike' },
]);
const addOrRemoveItem = (itemId) => {
const exists = itemsSelected.value.includes(itemId);
if (exists) {
itemsSelected.value = itemsSelected.value.filter((id) => id !== itemId);
} else {
itemsSelected.value.push(itemId);
}
};
return {
items,
itemsSelected,
addOrRemoveItem,
};
},
});
app.component('custom-checkbox', {
props: {
item: { type: Object, default: () => ({}) },
modelValue: { type: Array, default: () => [] },
},
template: `
<div>
<input
type="checkbox" :value="item.id"
@change="$emit('update:model-value', $event.target.checked)"
> {{ item.name }}
</div>
`
})
app.mount("#app");
<script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>
<div id="app">
<div><code>itemsSelected: {{ itemsSelected }}</code></div>
<hr />
<custom-checkbox
v-for="item in items"
:key="item.id"
:item="item"
:model-value="itemsSelected"
@update:model-value="addOrRemoveItem(item.id)"
></custom-checkbox>
</div>
As mentioned above, the code was bit overkill when I follow as mentioned in doc $emit('update:model-value', ...)
which can be simply anything for eg: $emit('val-updated')
, here is bit simplified version after removing unwanted prop
and reducing the length of $emit
.
const { ref, createApp } = Vue;
const app = createApp({
setup() {
const itemsSelected = ref([]);
const items = ref([
{ id: '1', name: 'Jack' },
{ id: '2', name: 'John' },
{ id: '3', name: 'Mike' },
]);
const addOrRemoveItem = (itemId) => {
const exists = itemsSelected.value.includes(itemId);
if (exists) {
itemsSelected.value = itemsSelected.value.filter((id) => id !== itemId);
} else {
itemsSelected.value.push(itemId);
}
};
return {
items,
itemsSelected,
addOrRemoveItem,
};
},
});
app.component('custom-checkbox', {
props: {
item: { type: Object, default: () => ({}) },
},
template: `
<div>
<input
type="checkbox" :value="item.id"
@change="$emit('val-updated')"
> {{ item.name }}
</div>
`
})
app.mount("#app");
<script src="https://unpkg.com/vue@3.0.11/dist/vue.global.js"></script>
<div id="app">
<div><code>itemsSelected: {{ itemsSelected }}</code></div>
<hr />
<custom-checkbox
v-for="item in items"
:key="item.id"
:item="item"
@val-updated="addOrRemoveItem(item.id)"
></custom-checkbox>
</div>