Skip to content

Instantly share code, notes, and snippets.

@Bane83
Created November 17, 2017 14:34
Show Gist options
  • Select an option

  • Save Bane83/3b5ef7a9d8bc7c8b872a9a2887b17d1f to your computer and use it in GitHub Desktop.

Select an option

Save Bane83/3b5ef7a9d8bc7c8b872a9a2887b17d1f to your computer and use it in GitHub Desktop.
Vue.component('vue-switch',{
template: '<label :class="classObject"><span class="vue-switcher__label" v-if="shouldShowLabel"><span v-if="label" v-text="label"></span><span v-if="!label && value" v-text="textEnabled"></span> <span v-if="!label && !value" v-text="textDisabled"></span> </span> <input type="checkbox" :disabled="disabled" @change="trigger" :checked="value"><div></div> </label>',
props: {
typeBold: {
default: false
},
value: {
default: false
},
disabled: {
default: false
},
label: {
default: ''
},
textEnabled: {
default: ''
},
textDisabled: {
default: ''
},
color: {
default: 'default'
},
theme: {
default: 'default'
},
emitOnMount: {
default: true
}
},
mounted: function () {
if(this.emitOnMount) {
this.$emit('input', this.value)
}
},
methods: {
trigger: function (e) {
this.$emit('input', e.target.checked)
}
},
computed: {
classObject: function () {
const { color, value, theme, typeBold, disabled } = this;
return {
'vue-switcher' : true,
['vue-switcher--unchecked'] : !value,
['vue-switcher--disabled'] : disabled,
['vue-switcher--bold']: typeBold,
['vue-switcher--bold--unchecked']: typeBold && !value,
[`vue-switcher-theme--${theme}`] : color,
[`vue-switcher-color--${color}`] : color,
};
},
shouldShowLabel: function () {
return this.label !== '' || this.textEnabled !== '' || this.textDisabled !== '';
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment