Created
February 3, 2020 12:17
-
-
Save fLipE23/7f0e9614677a73650c3516de985fd1af to your computer and use it in GitHub Desktop.
custom vue-input :: btn-group
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
<template> | |
<div class="btn-group " role="group"> | |
<button type="button" :class="getClassForOption(option)" | |
v-for="option in options" | |
@click="setValue(option)">{{option.title}}</button> | |
</div> | |
</template> | |
<script> | |
export default { | |
mounted() { | |
}, | |
props: { | |
// варианты выбора, [{title: 'Example title', value: 'Example value',}, ... ] | |
options: { | |
type: Array, | |
required: true, | |
default() { | |
return [] | |
}, | |
}, | |
// возвращаемое значение | |
value: { | |
// type: Number, | |
required: false, | |
default() { | |
return false | |
}, | |
}, | |
active_class: { | |
type: String, | |
required: false, | |
default() { | |
return 'btn-primary' | |
}, | |
}, | |
not_active_class: { | |
type: String, | |
required: false, | |
default() { | |
return 'btn-default' | |
}, | |
}, | |
}, | |
data() { | |
return { | |
// текущее значение - по умолчанию передано из родителя | |
current_value: this.value, | |
} | |
}, | |
methods: { | |
setValue(option) { | |
this.current_value = option.value | |
this.$emit('input', this.current_value) | |
}, | |
getClassForOption(option) { | |
return [ | |
'btn', | |
(option.value === this.current_value ? 'btn-primary' : 'btn-default'), | |
] | |
}, | |
}, | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment