|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <label class="d-block" v-if="showPlaceholderLabel"> </label> |
| 4 | + <b-form-group |
| 5 | + :id="id" |
| 6 | + :label="currentLabel" |
| 7 | + :label-for="`${id}-input`" |
| 8 | + content-cols="auto" |
| 9 | + > |
| 10 | + <b-input-group :class="inputGroupSize"> |
| 11 | + <c-switch |
| 12 | + :id="`${id}-input`" |
| 13 | + color="primary" |
| 14 | + v-model="innerValue" |
| 15 | + label |
| 16 | + v-bind="labelIcon" |
| 17 | + :readonly="readonly" |
| 18 | + :disabled="disabled" |
| 19 | + v-b-tooltip.hover |
| 20 | + :title="tooltip" |
| 21 | + v-on="inputListeners" |
| 22 | + /> |
| 23 | + </b-input-group> |
| 24 | + </b-form-group> |
| 25 | + </div> |
| 26 | +</template> |
| 27 | + |
| 28 | +<script> |
| 29 | +import common from '../shared/common'; |
| 30 | +import { Switch as cSwitch } from '@coreui/vue'; |
| 31 | +
|
| 32 | +export default { |
| 33 | + components: { |
| 34 | + cSwitch, |
| 35 | + }, |
| 36 | + props: { |
| 37 | + id: String, |
| 38 | + label: String, // fallback label if labelOn or labelOff not set |
| 39 | + labelOn: String, // if set will be used for "on" state |
| 40 | + labelOff: String, // if set will be used for "off" state |
| 41 | + value: Boolean, |
| 42 | + inputGroupSize: String, |
| 43 | + readonly: Boolean, |
| 44 | + disabled: Boolean, |
| 45 | + showPlaceholderLabel: Boolean, // can be used to show an empty label on top, useful to put on same row as other inputs |
| 46 | + tooltip: String, |
| 47 | + }, |
| 48 | + data() { |
| 49 | + return { |
| 50 | + labelIcon: { |
| 51 | + dataOn: '\u2713', |
| 52 | + dataOff: '\u2715', |
| 53 | + }, |
| 54 | + currentLabel: (this.value ? this.labelOn : this.labelOff) || this.label, |
| 55 | + }; |
| 56 | + }, |
| 57 | + computed: { |
| 58 | + innerValue: { |
| 59 | + get: function () { |
| 60 | + return common.toBoolean(this.value); |
| 61 | + }, |
| 62 | + set: function (newValue) { |
| 63 | + this.currentLabel = |
| 64 | + (newValue ? this.labelOn : this.labelOff) || this.label; |
| 65 | + return common.toBoolean(newValue); |
| 66 | + }, |
| 67 | + }, |
| 68 | + inputListeners: function () { |
| 69 | + const vm = this; |
| 70 | + return Object.assign({}, this.$listeners, { |
| 71 | + change: function (event) { |
| 72 | + vm.$emit('input', event); // model doesn't update otherwise? |
| 73 | + vm.$emit('change', event); |
| 74 | + }, |
| 75 | + }); |
| 76 | + }, |
| 77 | + }, |
| 78 | + methods: {}, |
| 79 | +}; |
| 80 | +</script> |
| 81 | +<style scoped> |
| 82 | +.switch { |
| 83 | + margin-right: 0; |
| 84 | + margin-top: 0.25rem; |
| 85 | +} |
| 86 | +.form-group { |
| 87 | + flex-direction: row-reverse; |
| 88 | +} |
| 89 | +.form-group >>> label.col-form-label { |
| 90 | + padding-left: 0 !important; |
| 91 | +} |
| 92 | +</style> |
0 commit comments