Blame view

src/components/radio/radio.vue 2.9 KB
7fa943eb   梁灏   init
1
2
3
4
5
6
7
8
  <template>
      <label :class="wrapClasses">
          <span :class="radioClasses">
              <span :class="innerClasses"></span>
              <input
                  type="radio"
                  :class="inputClasses"
                  :disabled="disabled"
06322514   梁灏   support Radio
9
                  :checked="currentValue"
7fa943eb   梁灏   init
10
                  @change="change">
06322514   梁灏   support Radio
11
          </span><slot>{{ label }}</slot>
7fa943eb   梁灏   init
12
13
14
15
16
17
      </label>
  </template>
  <script>
      const prefixCls = 'ivu-radio';
  
      export default {
06322514   梁灏   support Radio
18
          name: 'Radio',
7fa943eb   梁灏   init
19
          props: {
06322514   梁灏   support Radio
20
              value: {
7fa943eb   梁灏   init
21
22
23
                  type: Boolean,
                  default: false
              },
06322514   梁灏   support Radio
24
25
26
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
27
28
29
              disabled: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
30
31
32
33
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
34
                  currentValue: this.value,
7fa943eb   梁灏   init
35
                  group: false
b0893113   jingsam   :art: add eslint
36
              };
7fa943eb   梁灏   init
37
38
39
40
41
42
43
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
44
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
45
46
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
47
                  ];
7fa943eb   梁灏   init
48
49
50
51
52
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
53
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
54
55
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
56
                  ];
7fa943eb   梁灏   init
57
58
59
60
61
62
63
64
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
65
          mounted () {
cbe03a12   梁灏   support Checkbox
66
              // todo 使用 while向上查找
578ca325   梁灏   fixed Radio bug
67
              if (this.$parent && this.$parent.$options.name === 'radioGroup') this.group = true;
7fa943eb   梁灏   init
68
              if (!this.group) {
06322514   梁灏   support Radio
69
                  this.updateValue();
7fa943eb   梁灏   init
70
71
72
73
74
75
76
77
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
78
79
80
81
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
                  this.$emit('on-change', checked);
7fa943eb   梁灏   init
82
  
06322514   梁灏   support Radio
83
                  if (this.group && this.label) {
7fa943eb   梁灏   init
84
                      this.$parent.change({
06322514   梁灏   support Radio
85
86
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
87
88
                      });
                  }
06322514   梁灏   support Radio
89
90
                  // todo 事件
  //                if (!this.group) this.$dispatch('on-form-change', checked);
7fa943eb   梁灏   init
91
              },
06322514   梁灏   support Radio
92
93
              updateValue () {
                  this.currentValue = this.value;
7fa943eb   梁灏   init
94
95
96
              }
          },
          watch: {
06322514   梁灏   support Radio
97
98
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
99
100
              }
          }
b0893113   jingsam   :art: add eslint
101
102
      };
  </script>