Created
September 19, 2018 13:39
-
-
Save mathieutu/3fc93a28e816ea6cd5e0fe15f1b52e2e to your computer and use it in GitHub Desktop.
vuejs checkbox like component for adding values to an array.
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> | |
| <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