|
@@ -1,15 +1,20 @@
|
|
|
<template>
|
|
|
- <div class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText }">
|
|
|
+ <label class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText }">
|
|
|
<div class="el-switch__mask" v-show="disabled"></div>
|
|
|
- <input class="el-switch__input" type="checkbox" :checked="value" :name="name" :disabled="disabled" style="display: none;">
|
|
|
- <span class="el-switch__core" ref="core" @click="handleMiscClick" :style="{ 'width': coreWidth + 'px' }">
|
|
|
+ <input
|
|
|
+ class="el-switch__input"
|
|
|
+ type="checkbox"
|
|
|
+ @change="handleChange"
|
|
|
+ v-model="currentValue"
|
|
|
+ :name="name"
|
|
|
+ :disabled="disabled">
|
|
|
+ <span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
|
|
|
<span class="el-switch__button" :style="buttonStyle"></span>
|
|
|
</span>
|
|
|
<transition name="label-fade">
|
|
|
<div
|
|
|
class="el-switch__label el-switch__label--left"
|
|
|
v-show="value"
|
|
|
- @click="handleMiscClick"
|
|
|
:style="{ 'width': coreWidth + 'px' }">
|
|
|
<i :class="[onIconClass]" v-if="onIconClass"></i>
|
|
|
<span v-if="!onIconClass && onText">{{ onText }}</span>
|
|
@@ -19,13 +24,12 @@
|
|
|
<div
|
|
|
class="el-switch__label el-switch__label--right"
|
|
|
v-show="!value"
|
|
|
- @click="handleMiscClick"
|
|
|
:style="{ 'width': coreWidth + 'px' }">
|
|
|
<i :class="[offIconClass]" v-if="offIconClass"></i>
|
|
|
<span v-if="!offIconClass && offText">{{ offText }}</span>
|
|
|
</div>
|
|
|
</transition>
|
|
|
- </div>
|
|
|
+ </label>
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
@@ -75,6 +79,7 @@
|
|
|
},
|
|
|
data() {
|
|
|
return {
|
|
|
+ currentValue: this.value,
|
|
|
coreWidth: this.width,
|
|
|
buttonStyle: {}
|
|
|
};
|
|
@@ -87,18 +92,19 @@
|
|
|
},
|
|
|
watch: {
|
|
|
value(val) {
|
|
|
+ this.currentValue = val;
|
|
|
if (this.onColor || this.offColor) {
|
|
|
this.handleCoreColor();
|
|
|
}
|
|
|
this.handleButtonTransform();
|
|
|
- this.$emit('change', val);
|
|
|
+ },
|
|
|
+ currentValue(val) {
|
|
|
+ this.$emit('input', val);
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
- handleMiscClick() {
|
|
|
- if (!this.disabled) {
|
|
|
- this.$emit('input', !this.value);
|
|
|
- }
|
|
|
+ handleChange(event) {
|
|
|
+ this.$emit('change', event.currentTarget.checked);
|
|
|
},
|
|
|
handleButtonTransform() {
|
|
|
this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';
|