Blame view

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