Vue.js Quick Example
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.min.js"></script>
<div id="demo">
<o-checkbox v-model="testVal" :value="true">Label</o-checkbox>
</div>
JavaScript
const OCheckbox = {
template: `
<label>
<input type="checkbox" :checked="isChecked" @click="handleClick"/>
<slot></slot>
</label>
`,
model: {
prop: 'checked',
event: 'change',
},
props: {
checked: null,
value: null,
},
computed: {
isChecked() {
return this.checked === this.value;
}
},
methods: {
handleClick(e) {
e.preventDefault();
setTimeout(() => {
if (this.isChecked) {
this.$emit('change', false);
} else {
this.$emit('change', this.value);
}
})
},
}
};
const Demo = new Vue({
el: '#demo',
components: {
OCheckbox,
},
data: {
testVal: true,
},
watch: {
testVal() {
console.log('testVal changed', this.testVal);
}
}
});