Blame view

src/components/radio/radio.vue 3.92 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"
0460a1e8   梁灏   fixed #812
10
                  :name="name"
7fa943eb   梁灏   init
11
                  @change="change">
06322514   梁灏   support Radio
12
          </span><slot>{{ label }}</slot>
7fa943eb   梁灏   init
13
14
15
      </label>
  </template>
  <script>
4d545420   梁灏   Radio add size prop
16
      import { findComponentUpward, oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
17
18
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
19
20
21
      const prefixCls = 'ivu-radio';
  
      export default {
06322514   梁灏   support Radio
22
          name: 'Radio',
cd78c9c4   梁灏   some comps suppor...
23
          mixins: [ Emitter ],
7fa943eb   梁灏   init
24
          props: {
06322514   梁灏   support Radio
25
              value: {
d24f5082   Rijn   Support trueValue...
26
27
28
29
30
31
32
33
34
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
35
36
                  default: false
              },
06322514   梁灏   support Radio
37
38
39
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
40
41
42
              disabled: {
                  type: Boolean,
                  default: false
4d545420   梁灏   Radio add size prop
43
44
45
46
47
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
                  }
0460a1e8   梁灏   fixed #812
48
49
50
              },
              name: {
                  type: String
7fa943eb   梁灏   init
51
52
53
54
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
55
                  currentValue: this.value,
3f281d6c   梁灏   update RadioGroup
56
57
                  group: false,
                  parent: findComponentUpward(this, 'RadioGroup')
b0893113   jingsam   :art: add eslint
58
              };
7fa943eb   梁灏   init
59
60
61
62
63
64
65
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
66
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
4d545420   梁灏   Radio add size prop
67
68
                          [`${prefixCls}-wrapper-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
7fa943eb   梁灏   init
69
                      }
b0893113   jingsam   :art: add eslint
70
                  ];
7fa943eb   梁灏   init
71
72
73
74
75
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
76
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
77
78
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
79
                  ];
7fa943eb   梁灏   init
80
81
82
83
84
85
86
87
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
88
          mounted () {
3f281d6c   梁灏   update RadioGroup
89
              if (this.parent) this.group = true;
7fa943eb   梁灏   init
90
              if (!this.group) {
06322514   梁灏   support Radio
91
                  this.updateValue();
bb1f58e2   梁灏   update Radio
92
93
              } else {
                  this.parent.updateValue();
7fa943eb   梁灏   init
94
95
96
97
98
99
100
101
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
102
103
                  const checked = event.target.checked;
                  this.currentValue = checked;
d24f5082   Rijn   Support trueValue...
104
105
106
  
                  let value = checked ? this.trueValue : this.falseValue;
                  this.$emit('input', value);
7fa943eb   梁灏   init
107
  
70d5a4ab   梁灏   fixed #425
108
                  if (this.group && this.label !== undefined) {
3f281d6c   梁灏   update RadioGroup
109
                      this.parent.change({
06322514   梁灏   support Radio
110
111
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
112
113
                      });
                  }
a804d608   梁灏   update Radio
114
                  if (!this.group) {
d24f5082   Rijn   Support trueValue...
115
116
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
a804d608   梁灏   update Radio
117
                  }
7fa943eb   梁灏   init
118
              },
06322514   梁灏   support Radio
119
              updateValue () {
d24f5082   Rijn   Support trueValue...
120
                  this.currentValue = this.value === this.trueValue;
7fa943eb   梁灏   init
121
122
123
              }
          },
          watch: {
d24f5082   Rijn   Support trueValue...
124
125
126
127
              value (val) {
                  if (val !== this.trueValue && val !== this.falseValue) {
                      throw 'Value should be trueValue or falseValue.';
                  }
06322514   梁灏   support Radio
128
                  this.updateValue();
7fa943eb   梁灏   init
129
130
              }
          }
b0893113   jingsam   :art: add eslint
131
132
      };
  </script>