Created
April 3, 2019 20:28
-
-
Save jbieliauskas/e2c572b2b2ab4077ffe6879479fd149b to your computer and use it in GitHub Desktop.
Vue.js checkbox (single/multi) wrapper
This file contains 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
const checkboxWrap = { | |
props: ['value', 'vModelValue'], | |
model: { | |
prop: 'vModelValue', | |
event: 'checkbox-updated' | |
}, | |
data() { | |
return { | |
proxyResult: null | |
}; | |
}, | |
computed: { | |
proxy: { | |
get() { | |
return this.vModelValue; | |
}, | |
set(newProxyResult) { | |
this.proxyResult = newProxyResult; | |
} | |
} | |
}, | |
methods: { | |
emitCheckboxUpdated() { | |
this.$emit('checkbox-updated', this.proxyResult); | |
} | |
} | |
}; | |
Vue.component('single-checkbox-wrapper', { | |
mixins: [checkboxWrap], | |
template: ` | |
<div> | |
<label> | |
<input | |
type="checkbox" | |
v-model="proxy" | |
@change="emitCheckboxUpdated" | |
/> | |
{{ label }} | |
</label> | |
</div> | |
`, | |
props: { | |
label: { | |
type: String, | |
required: true | |
} | |
} | |
}); | |
Vue.component('multi-checkbox-wrapper', { | |
mixins: [checkboxWrap], | |
template: ` | |
<div> | |
<label> | |
<input | |
type="checkbox" | |
:value="value" | |
v-model="proxy" | |
@change="emitCheckboxUpdated" | |
/> | |
{{ label }} | |
</label> | |
</div> | |
`, | |
props: { | |
value: { | |
type: Array, | |
required: true | |
}, | |
label: { | |
type: String, | |
required: true | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment