Last active
April 12, 2023 16:09
-
-
Save sr8e/2e8ec1b8249279fa208e090fd72f2090 to your computer and use it in GitHub Desktop.
sample of two-way binding using v-model on multiple checkbox component (for Vue 3.x)
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
<script> | |
import MultiCheckbox from "./MultiCheckbox.vue" | |
import MultiCheckboxProp from "./MultiCheckboxProp.vue" | |
export default { | |
components: { MultiCheckbox, MultiCheckboxProp }, | |
data(){ | |
return{ | |
checkList: [], | |
checkListProp: [] | |
} | |
}, | |
} | |
</script> | |
<template> | |
<ul> | |
<li><multi-checkbox v-model="checkList" :id="0" :thisValue="0">hoge</multi-checkbox></li> | |
<li><multi-checkbox v-model="checkList" :id="1" :thisValue="1">fuga</multi-checkbox></li> | |
<li><multi-checkbox v-model="checkList" :id="2" :thisValue="2">piyo</multi-checkbox></li> | |
</ul> | |
<div> | |
checked entries: {{ checkList }} | |
</div> | |
<ul> | |
<li><multi-checkbox-prop v-model="checkListProp" :id="10" :thisValue="10">foo</multi-checkbox-prop></li> | |
<li><multi-checkbox-prop v-model="checkListProp" :id="11" :thisValue="11">bar</multi-checkbox-prop></li> | |
<li><multi-checkbox-prop v-model="checkListProp" :id="12" :thisValue="12">baz</multi-checkbox-prop></li> | |
</ul> | |
<div> | |
checked entries: {{ checkListProp }} | |
</div> | |
</template> |
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
<template> | |
<div class="multi-checkbox"> | |
<input | |
type="checkbox" | |
:checked="modelValue.includes(thisValue)" | |
@input="(event) =>{ | |
$emit('update:modelValue', modelValue.includes(event.target.value) ? | |
modelValue.filter(v => v !== event.target.value) : | |
modelValue.concat(event.target.value) | |
) | |
}" | |
:id="id" | |
:value="thisValue" | |
> | |
<slot /> | |
</div> | |
</template> | |
<script> | |
export default { | |
props: ['modelValue', 'id', 'thisValue'], | |
emits: ['update:modelValue'] | |
} | |
</script> |
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
<template> | |
<div class="multi-checkbox-prop"> | |
<input | |
type="checkbox" | |
v-model="bindProp" | |
:id="id" | |
:value="thisValue" | |
> | |
<slot /> | |
</div> | |
</template> | |
<script> | |
export default { | |
props: ['modelValue', 'id', 'thisValue'], | |
emits: ['update:modelValue'], | |
computed:{ | |
bindProp:{ | |
get(){ return this.modelValue }, | |
set(newValue){ this.$emit('update:modelValue', newValue) } | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment