Blame view

src/components/radio/radio.vue 3 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
      </label>
  </template>
  <script>
cd78c9c4   梁灏   some comps suppor...
15
16
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
17
18
19
      const prefixCls = 'ivu-radio';
  
      export default {
06322514   梁灏   support Radio
20
          name: 'Radio',
cd78c9c4   梁灏   some comps suppor...
21
          mixins: [ Emitter ],
7fa943eb   梁灏   init
22
          props: {
06322514   梁灏   support Radio
23
              value: {
7fa943eb   梁灏   init
24
25
26
                  type: Boolean,
                  default: false
              },
06322514   梁灏   support Radio
27
28
29
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
30
31
32
              disabled: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
33
34
35
36
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
37
                  currentValue: this.value,
7fa943eb   梁灏   init
38
                  group: false
b0893113   jingsam   :art: add eslint
39
              };
7fa943eb   梁灏   init
40
41
42
43
44
45
46
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
47
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
48
49
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
50
                  ];
7fa943eb   梁灏   init
51
52
53
54
55
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
56
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
57
58
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
59
                  ];
7fa943eb   梁灏   init
60
61
62
63
64
65
66
67
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
68
          mounted () {
cbe03a12   梁灏   support Checkbox
69
              // todo 使用 while向上查找
e6c0b158   梁灏   update
70
              if (this.$parent && this.$parent.$options.name === 'RadioGroup') this.group = true;
7fa943eb   梁灏   init
71
              if (!this.group) {
06322514   梁灏   support Radio
72
                  this.updateValue();
7fa943eb   梁灏   init
73
74
75
76
77
78
79
80
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
81
82
83
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
84
  
06322514   梁灏   support Radio
85
                  if (this.group && this.label) {
7fa943eb   梁灏   init
86
                      this.$parent.change({
06322514   梁灏   support Radio
87
88
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
89
90
                      });
                  }
a804d608   梁灏   update Radio
91
92
                  if (!this.group) {
                      this.$emit('on-change', checked);
cd78c9c4   梁灏   some comps suppor...
93
                      this.dispatch('FormItem', 'on-form-change', checked);
a804d608   梁灏   update Radio
94
                  }
7fa943eb   梁灏   init
95
              },
06322514   梁灏   support Radio
96
97
              updateValue () {
                  this.currentValue = this.value;
7fa943eb   梁灏   init
98
99
100
              }
          },
          watch: {
06322514   梁灏   support Radio
101
102
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
103
104
              }
          }
b0893113   jingsam   :art: add eslint
105
106
      };
  </script>