Skip to content

Instantly share code, notes, and snippets.

@mathieutu
Created September 19, 2018 13:39
Show Gist options
  • Save mathieutu/3fc93a28e816ea6cd5e0fe15f1b52e2e to your computer and use it in GitHub Desktop.
Save mathieutu/3fc93a28e816ea6cd5e0fe15f1b52e2e to your computer and use it in GitHub Desktop.
vuejs checkbox like component for adding values to an array.
<template>
<div @click="onClick">
<span :class="{checked}" class="icon icon--checkbox"/>
</div>
</template>
<script>
export default {
model: {
prop: 'allSelected',
},
props: {
value: { type: String, required: true },
allSelected: { type: Array, required: true },
},
computed: {
checked() {
return this.allSelected.includes(this.value);
},
},
methods: {
onClick() {
this.$emit(
'input',
this.checked
? this.allSelected.filter(place => place !== this.value)
: [...this.allSelected, this.value],
);
},
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment