2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2017-05-19 17:20:04 +00:00
|
|
|
<label class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText, 'is-checked': checked }">
|
2016-07-27 06:15:02 +00:00
|
|
|
<div class="el-switch__mask" v-show="disabled"></div>
|
2016-11-13 03:45:53 +00:00
|
|
|
<input
|
|
|
|
class="el-switch__input"
|
|
|
|
type="checkbox"
|
|
|
|
@change="handleChange"
|
2017-05-19 17:20:04 +00:00
|
|
|
ref="input"
|
2016-11-13 03:45:53 +00:00
|
|
|
:name="name"
|
2017-04-23 06:28:29 +00:00
|
|
|
:true-value="onValue"
|
|
|
|
:false-value="offValue"
|
2016-11-13 03:45:53 +00:00
|
|
|
:disabled="disabled">
|
|
|
|
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
|
2017-03-28 07:06:34 +00:00
|
|
|
<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:43:50 +00:00
|
|
|
v-show="checked"
|
2016-08-04 11:56:54 +00:00
|
|
|
:style="{ 'width': coreWidth + 'px' }">
|
|
|
|
<i :class="[onIconClass]" v-if="onIconClass"></i>
|
2016-08-25 10:24:51 +00:00
|
|
|
<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:43:50 +00:00
|
|
|
v-show="!checked"
|
2016-08-04 11:56:54 +00:00
|
|
|
:style="{ 'width': coreWidth + 'px' }">
|
|
|
|
<i :class="[offIconClass]" v-if="offIconClass"></i>
|
2016-08-25 10:24:51 +00:00
|
|
|
<span v-if="!offIconClass && offText">{{ offText }}</span>
|
2016-08-04 11:56:54 +00:00
|
|
|
</div>
|
|
|
|
</transition>
|
2016-11-13 03:45:53 +00:00
|
|
|
</label>
|
2016-07-27 06:15:02 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
2016-12-31 15:33:51 +00:00
|
|
|
name: 'ElSwitch',
|
2016-07-27 06:15:02 +00:00
|
|
|
props: {
|
|
|
|
value: {
|
2017-04-23 06:43:50 +00:00
|
|
|
type: [Boolean, String, Number],
|
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: {
|
2017-04-23 06:43:50 +00:00
|
|
|
type: [Boolean, String, Number],
|
2017-04-23 06:28:29 +00:00
|
|
|
default: true
|
|
|
|
},
|
|
|
|
offValue: {
|
2017-04-23 06:43:50 +00:00
|
|
|
type: [Boolean, String, Number],
|
2017-04-23 06:28:29 +00:00
|
|
|
default: false
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
}
|
|
|
|
},
|
2016-08-04 11:56:54 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2017-03-28 07:06:34 +00:00
|
|
|
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)) {
|
2017-06-03 12:55:17 +00:00
|
|
|
this.$emit('input', this.offValue);
|
2017-04-23 06:28:29 +00:00
|
|
|
}
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
computed: {
|
2017-04-23 06:43:50 +00:00
|
|
|
checked() {
|
|
|
|
return this.value === this.onValue;
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
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
|
|
|
},
|
2017-03-28 07:06:34 +00:00
|
|
|
transform() {
|
2017-04-23 06:43:50 +00:00
|
|
|
return this.checked ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: {
|
2017-05-19 17:20:04 +00:00
|
|
|
checked() {
|
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: {
|
2016-11-13 03:45:53 +00:00
|
|
|
handleChange(event) {
|
2017-05-19 17:20:04 +00:00
|
|
|
this.$emit('change', !this.checked ? this.onValue : this.offValue);
|
|
|
|
this.$emit('input', !this.checked ? this.onValue : this.offValue);
|
|
|
|
this.$nextTick(() => {
|
|
|
|
// set input's checked property
|
|
|
|
// in case parent refuses to change component's value
|
|
|
|
this.$refs.input.checked = this.checked;
|
|
|
|
});
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
2016-11-18 08:32:00 +00:00
|
|
|
setBackgroundColor() {
|
2017-04-23 06:43:50 +00:00
|
|
|
let newColor = this.checked ? 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
|
|
|
}
|
2016-12-08 07:23:21 +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
|
|
|
}
|
2017-05-19 17:20:04 +00:00
|
|
|
this.$refs.input.checked = this.checked;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|