Blame view

src/components/radio/radio.vue 2.95 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向上查找
e6c0b158   梁灏   update
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
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
81
  
06322514   梁灏   support Radio
82
                  if (this.group && this.label) {
7fa943eb   梁灏   init
83
                      this.$parent.change({
06322514   梁灏   support Radio
84
85
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
86
87
                      });
                  }
a804d608   梁灏   update Radio
88
89
90
91
92
                  if (!this.group) {
                      this.$emit('on-change', checked);
                      // todo 事件
  //                    this.$dispatch('on-form-change', checked);
                  }
7fa943eb   梁灏   init
93
              },
06322514   梁灏   support Radio
94
95
              updateValue () {
                  this.currentValue = this.value;
7fa943eb   梁灏   init
96
97
98
              }
          },
          watch: {
06322514   梁灏   support Radio
99
100
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
101
102
              }
          }
b0893113   jingsam   :art: add eslint
103
104
      };
  </script>