Broken checkbox
by Thorsten
HTML
<script src="https://unpkg.com/vue@2.6.0"></script>
<!-- <script src="https://unpkg.com/vue@2.5.22"></script> -->
<div id="app">
<div>
<toggle :value="async" @input="doStuff"></toggle>
{{ async }} Async state
</div>
<div>
<toggle v-model="basic"></toggle>
{{ basic }} Basic state
</div>
<div>
<input type="text" :value="text" @input="textInput">
</div>
</div>
JavaScript
const toggle = Vue.component('toggle', {
template: `
<input type="checkbox" :checked="value" @input.prevent="input">
`,
props: {
value: {
type: Boolean,
default() {
return false;
}
}
},
methods: {
input(e) {
this.$emit("input", e.target.checked)
e.target.checked = !e.target.checked
}
}
})
new Vue({
el: '#app',
data: {
basic: false,
async: false,
text: "",
},
methods: {
doStuff(v) {
setTimeout(() => {
this.async = v;
}, 1000);
},
textInput(e) {
this.text = e.target.value.slice(-5);
}
},
})