Blame view

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