Switch: fix @change timing bug

pull/1166/head
Leopoldthecoder 2016-11-18 16:32:00 +08:00
parent 98c9cca24c
commit 77ef3836b1
1 changed files with 16 additions and 12 deletions

View File

@ -5,7 +5,7 @@
class="el-switch__input" class="el-switch__input"
type="checkbox" type="checkbox"
@change="handleChange" @change="handleChange"
v-model="currentValue" v-model="_value"
:name="name" :name="name"
:disabled="disabled"> :disabled="disabled">
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }"> <span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
@ -79,7 +79,6 @@
}, },
data() { data() {
return { return {
currentValue: this.value,
coreWidth: this.width, coreWidth: this.width,
buttonStyle: { buttonStyle: {
transform: '' transform: ''
@ -90,18 +89,22 @@
hasText() { hasText() {
/* istanbul ignore next */ /* istanbul ignore next */
return this.onText || this.offText; return this.onText || this.offText;
},
_value: {
get() {
return this.value;
},
set(val) {
this.$emit('input', val);
}
} }
}, },
watch: { watch: {
value(val) { value() {
this.currentValue = val;
if (this.onColor || this.offColor) { if (this.onColor || this.offColor) {
this.handleCoreColor(); this.setBackgroundColor();
} }
this.handleButtonTransform(); this.handleButtonTransform();
},
currentValue(val) {
this.$emit('input', val);
} }
}, },
methods: { methods: {
@ -111,9 +114,10 @@
handleButtonTransform() { handleButtonTransform() {
this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)'; this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';
}, },
handleCoreColor() { setBackgroundColor() {
this.$refs.core.style.borderColor = this.value ? this.onColor : this.offColor; let newColor = this.value ? this.onColor : this.offColor;
this.$refs.core.style.backgroundColor = this.value ? this.onColor : this.offColor; this.$refs.core.style.borderColor = newColor;
this.$refs.core.style.backgroundColor = newColor;
} }
}, },
mounted() { mounted() {
@ -123,7 +127,7 @@
} }
this.handleButtonTransform(); this.handleButtonTransform();
if ((this.onColor || this.offColor) && !this.disabled) { if ((this.onColor || this.offColor) && !this.disabled) {
this.handleCoreColor(); this.setBackgroundColor();
} }
} }
}; };