Skip to content

Instantly share code, notes, and snippets.

@micaz
Last active April 20, 2021 04:48
Show Gist options
  • Save micaz/2e1c58fde3f0847f817f77a5e970b2f5 to your computer and use it in GitHub Desktop.
Save micaz/2e1c58fde3f0847f817f77a5e970b2f5 to your computer and use it in GitHub Desktop.
simple chebox input component for vue.js
<template>
<span :class="propClass">
<input
:id="inputId"
v-model="selected"
type="checkbox"
:name="inputName"
:value="value"
/>
<label :for="inputId" @click.stop="$emit('click')">
{{ labelText }}
</label>
</span>
</template>
<script>
export default {
name: 'InputCheck',
model: {
prop: 'modelValue',
event: 'change',
},
props: {
modelValue: { type: [Array, Boolean], default: () => [] },
value: { type: String, default: '' },
inputId: { type: String, default: '' },
inputName: { type: String, default: '' },
labelText: { type: String, default: '' },
propClass: { type: String, default: 'chkA' },
disabled: { type: Boolean, default: false },
},
data() {
return {}
},
computed: {
checked() {
return this.options.includes(this.value)
},
selected: {
get() {
return this.modelValue
},
set(val) {
console.log(val)
this.$emit('change', val)
},
},
},
methods: {
updateValue(e) {
// const isChecked = e.target.checked
// if (Array.isArray(this.modelValue)) {
// const newValue = [...this.modelValue]
// if (isChecked) {
// newValue.push(this.value)
// } else {
// newValue.splice(newValue.indexOf(this.value), 1)
// }
// console.log('change ', newValue)
// this.$emit('change', newValue)
// } else {
// this.$emit('change', !!isChecked)
// }
},
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment