Skip to content

Instantly share code, notes, and snippets.

@jbieliauskas
Created April 3, 2019 20:28
Show Gist options
  • Save jbieliauskas/e2c572b2b2ab4077ffe6879479fd149b to your computer and use it in GitHub Desktop.
Save jbieliauskas/e2c572b2b2ab4077ffe6879479fd149b to your computer and use it in GitHub Desktop.
Vue.js checkbox (single/multi) wrapper
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