element/packages/switch/src/component.vue

145 lines
3.5 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<label class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText }">
2016-07-27 06:15:02 +00:00
<div class="el-switch__mask" v-show="disabled"></div>
<input
class="el-switch__input"
type="checkbox"
@change="handleChange"
2016-11-18 08:32:00 +00:00
v-model="_value"
:name="name"
2017-04-23 06:28:29 +00:00
:true-value="onValue"
:false-value="offValue"
:disabled="disabled">
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
<span class="el-switch__button" :style="{ transform }"></span>
2016-07-27 06:15:02 +00:00
</span>
2016-08-04 11:56:54 +00:00
<transition name="label-fade">
<div
class="el-switch__label el-switch__label--left"
2017-04-23 06:28:29 +00:00
v-show="value === onValue"
2016-08-04 11:56:54 +00:00
:style="{ 'width': coreWidth + 'px' }">
<i :class="[onIconClass]" v-if="onIconClass"></i>
<span v-if="!onIconClass && onText">{{ onText }}</span>
2016-08-04 11:56:54 +00:00
</div>
</transition>
<transition name="label-fade">
<div
class="el-switch__label el-switch__label--right"
2017-04-23 06:28:29 +00:00
v-show="value === offValue"
2016-08-04 11:56:54 +00:00
:style="{ 'width': coreWidth + 'px' }">
<i :class="[offIconClass]" v-if="offIconClass"></i>
<span v-if="!offIconClass && offText">{{ offText }}</span>
2016-08-04 11:56:54 +00:00
</div>
</transition>
</label>
2016-07-27 06:15:02 +00:00
</template>
<script>
export default {
name: 'ElSwitch',
2016-07-27 06:15:02 +00:00
props: {
value: {
2017-04-23 06:28:29 +00:00
type: [Boolean, String],
2016-07-27 06:15:02 +00:00
default: true
},
disabled: {
type: Boolean,
default: false
},
width: {
type: Number,
default: 0
},
onIconClass: {
type: String,
default: ''
},
offIconClass: {
type: String,
default: ''
},
onText: {
type: String,
default: 'ON'
},
offText: {
type: String,
default: 'OFF'
},
onColor: {
type: String,
default: ''
},
offColor: {
type: String,
default: ''
},
2017-04-23 06:28:29 +00:00
onValue: {
type: [Boolean, String],
default: true
},
offValue: {
type: [Boolean, String],
default: false
},
2016-07-27 06:15:02 +00:00
name: {
type: String,
default: ''
}
},
2016-08-04 11:56:54 +00:00
data() {
return {
coreWidth: this.width
2016-08-04 11:56:54 +00:00
};
},
2017-04-23 06:28:29 +00:00
created() {
if (!~[this.onValue, this.offValue].indexOf(this.value)) {
this.$emit('input', this.onValue);
}
},
2016-07-27 06:15:02 +00:00
computed: {
hasText() {
2016-10-19 06:53:32 +00:00
/* istanbul ignore next */
2016-07-27 06:15:02 +00:00
return this.onText || this.offText;
2016-11-18 08:32:00 +00:00
},
_value: {
get() {
return this.value;
},
set(val) {
this.$emit('input', val);
}
},
transform() {
2017-04-23 06:28:29 +00:00
return this.value === this.onValue ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';
2016-07-27 06:15:02 +00:00
}
},
watch: {
2016-11-18 08:32:00 +00:00
value() {
2016-07-27 06:15:02 +00:00
if (this.onColor || this.offColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
}
},
methods: {
handleChange(event) {
this.$emit('change', event.currentTarget.checked);
2016-07-27 06:15:02 +00:00
},
2016-11-18 08:32:00 +00:00
setBackgroundColor() {
2017-04-23 06:28:29 +00:00
let newColor = this.value === this.onValue ? this.onColor : this.offColor;
2016-11-18 08:32:00 +00:00
this.$refs.core.style.borderColor = newColor;
this.$refs.core.style.backgroundColor = newColor;
2016-07-27 06:15:02 +00:00
}
},
2016-08-04 11:56:54 +00:00
mounted() {
2016-10-19 06:53:32 +00:00
/* istanbul ignore if */
2016-07-27 06:15:02 +00:00
if (this.width === 0) {
2016-08-04 11:56:54 +00:00
this.coreWidth = this.hasText ? 58 : 46;
2016-07-27 06:15:02 +00:00
}
if (this.onColor || this.offColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
}
};
</script>