Pārlūkot izejas kodu

fix radio dynamic init bug (#1514)

baiyaaaaa 8 gadi atpakaļ
vecāks
revīzija
e56f52ff26

+ 0 - 6
packages/checkbox/src/checkbox.vue

@@ -103,12 +103,6 @@
       falseLabel: [String, Number]
     },
 
-    data() {
-      return {
-        isGroup: false
-      };
-    },
-
     methods: {
       addToStore() {
         if (Array.isArray(this.model)) {

+ 21 - 15
packages/radio/src/radio-button.vue

@@ -3,33 +3,39 @@
     name: 'ElRadioButton',
 
     props: {
-      label: {
-        type: [String, Number],
-        required: true
-      },
+      label: {},
       disabled: Boolean,
       name: String
     },
-    data() {
-      return {
-        size: this.$parent.size
-      };
-    },
     computed: {
       value: {
         get() {
-          return this.$parent.value;
+          return this._radioGroup.value;
         },
-        set(newValue) {
-          this.$parent.$emit('input', newValue);
+        set(value) {
+          this._radioGroup.$emit('input', value);
         }
       },
+      _radioGroup() {
+        let parent = this.$parent;
+        while (parent) {
+          if (parent.$options.componentName !== 'ElRadioGroup') {
+            parent = parent.$parent;
+          } else {
+            return parent;
+          }
+        }
+        return false;
+      },
       activeStyle() {
         return {
-          backgroundColor: this.$parent.fill,
-          borderColor: this.$parent.fill,
-          color: this.$parent.textColor
+          backgroundColor: this._radioGroup.fill,
+          borderColor: this._radioGroup.fill,
+          color: this._radioGroup.textColor
         };
+      },
+      size() {
+        return this._radioGroup.size;
       }
     }
   };

+ 0 - 4
packages/radio/src/radio-group.vue

@@ -23,12 +23,8 @@
     watch: {
       value(value) {
         this.$emit('change', value);
-        this.broadcast('ElRadio', 'initData', value);
         this.dispatch('ElFormItem', 'el.form.change', [this.value]);
       }
-    },
-    mounted() {
-      this.broadcast('ElRadio', 'initData', this.value);
     }
   };
 </script>

+ 28 - 25
packages/radio/src/radio.vue

@@ -4,14 +4,14 @@
       <span class="el-radio__inner"
         :class="{
         'is-disabled': disabled,
-        'is-checked': store === label,
+        'is-checked': model === label,
         'is-focus': focus
       }"></span>
       <input
         class="el-radio__original"
         :value="label"
         type="radio"
-        v-model="store"
+        v-model="model"
         @focus="focus = true"
         @blur="focus = false"
         :name="name"
@@ -34,42 +34,45 @@
     componentName: 'ElRadio',
 
     props: {
-      value: [String, Number],
-      label: {
-        type: [String, Number],
-        required: true
-      },
+      value: {},
+      label: {},
       disabled: Boolean,
       name: String
     },
 
     data() {
       return {
-        focus: false,
-        isGroup: false,
-        store: this.value
+        focus: false
       };
     },
 
-    watch: {
-      store(store) {
-        if (this.isGroup) {
-          this.dispatch('ElRadioGroup', 'input', store);
-        } else {
-          this.$emit('input', store);
+    computed: {
+      isGroup() {
+        let parent = this.$parent;
+        while (parent) {
+          if (parent.$options.componentName !== 'ElRadioGroup') {
+            parent = parent.$parent;
+          } else {
+            this._radioGroup = parent;
+            return true;
+          }
         }
+        return false;
       },
 
-      value(val) {
-        this.store = val;
-      }
-    },
+      model: {
+        get() {
+          return this.isGroup ? this._radioGroup.value : this.value;
+        },
 
-    created() {
-      this.$on('initData', data => {
-        this.store = data;
-        this.isGroup = true;
-      });
+        set(val) {
+          if (this.isGroup) {
+            this.dispatch('ElRadioGroup', 'input', [val]);
+          } else {
+            this.$emit('input', val);
+          }
+        }
+      }
     }
   };
 </script>

+ 1 - 1
test/unit/index.js

@@ -4,7 +4,7 @@ Function.prototype.bind = require('function-bind');
 require('packages/theme-default/src/index.css');
 
 // require all test files (files that ends with .spec.js)
-const testsContext = require.context('./specs', true, /tabs\.spec$/);
+const testsContext = require.context('./specs', true, /\.spec$/);
 testsContext.keys().forEach(testsContext);
 
 // require all src files except main.js for coverage.