Browse Source

Switch: fix @change timing bug

Leopoldthecoder 8 năm trước cách đây
mục cha
commit
69b65037eb
1 tập tin đã thay đổi với 16 bổ sung12 xóa
  1. 16 12
      packages/switch/src/component.vue

+ 16 - 12
packages/switch/src/component.vue

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