Blame view

src/components/radio/radio.vue 5.17 KB
7fa943eb   梁灏   init
1
  <template>
76b2f032   Xotic750   Remove refs
2
      <label :class="wrapClasses">
7fa943eb   梁灏   init
3
          <span :class="radioClasses">
76b2f032   Xotic750   Remove refs
4
              <span :class="innerClasses"></span>
7fa943eb   梁灏   init
5
6
              <input
                  type="radio"
7fa943eb   梁灏   init
7
8
                  :class="inputClasses"
                  :disabled="disabled"
06322514   梁灏   support Radio
9
                  :checked="currentValue"
79964596   Xotic750   Use native w3c
10
                  :name="groupName"
18efb1b4   Graham Fairweather   Key space to select
11
                  @change="change"
79964596   Xotic750   Use native w3c
12
13
                  @focus="onFocus"
                  @blur="onBlur">
b2dee308   Chuanfeng   radio 组合使用,type="...
14
          </span><slot>{{ label }}</slot>
7fa943eb   梁灏   init
15
16
17
      </label>
  </template>
  <script>
4d545420   梁灏   Radio add size prop
18
      import { findComponentUpward, oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
19
20
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
21
22
23
      const prefixCls = 'ivu-radio';
  
      export default {
06322514   梁灏   support Radio
24
          name: 'Radio',
cd78c9c4   梁灏   some comps suppor...
25
          mixins: [ Emitter ],
7fa943eb   梁灏   init
26
          props: {
06322514   梁灏   support Radio
27
              value: {
d24f5082   Rijn   Support trueValue...
28
29
30
31
32
33
34
35
36
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
37
38
                  default: false
              },
06322514   梁灏   support Radio
39
40
41
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
42
43
44
              disabled: {
                  type: Boolean,
                  default: false
4d545420   梁灏   Radio add size prop
45
46
47
48
49
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
                  }
0460a1e8   梁灏   fixed #812
50
51
52
              },
              name: {
                  type: String
7fa943eb   梁灏   init
53
54
55
56
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
57
                  currentValue: this.value,
3f281d6c   梁灏   update RadioGroup
58
                  group: false,
79964596   Xotic750   Use native w3c
59
60
61
62
                  groupName: this.name,
                  parent: findComponentUpward(this, 'RadioGroup'),
                  focusWrapper: false,
                  focusInner: false
b0893113   jingsam   :art: add eslint
63
              };
7fa943eb   梁灏   init
64
65
66
67
68
69
70
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
71
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
4d545420   梁灏   Radio add size prop
72
                          [`${prefixCls}-wrapper-disabled`]: this.disabled,
79964596   Xotic750   Use native w3c
73
74
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-focus`]: this.focusWrapper
7fa943eb   梁灏   init
75
                      }
b0893113   jingsam   :art: add eslint
76
                  ];
7fa943eb   梁灏   init
77
78
79
80
81
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
82
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
83
84
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
85
                  ];
7fa943eb   梁灏   init
86
87
              },
              innerClasses () {
79964596   Xotic750   Use native w3c
88
89
90
91
92
93
                  return [
                      `${prefixCls}-inner`,
                      {
                          [`${prefixCls}-focus`]: this.focusInner
                      }
                  ];
7fa943eb   梁灏   init
94
95
96
97
98
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
99
          mounted () {
79964596   Xotic750   Use native w3c
100
101
102
              if (this.parent) {
                  this.group = true;
                  if (this.name && this.name !== this.parent.name) {
2e14b458   Xotic750   Disable lint error
103
                      /* eslint-disable no-console */
79964596   Xotic750   Use native w3c
104
105
106
                      if (console.warn) {
                          console.warn('[iview] Name does not match Radio Group name.');
                      }
2e14b458   Xotic750   Disable lint error
107
                      /* eslint-enable no-console */
79964596   Xotic750   Use native w3c
108
109
110
111
112
113
                  } else {
                      this.groupName = this.parent.name; 
                  }
              }
  
              if (this.group) {
bb1f58e2   梁灏   update Radio
114
                  this.parent.updateValue();
79964596   Xotic750   Use native w3c
115
116
              } else {
                  this.updateValue();
7fa943eb   梁灏   init
117
118
119
120
121
122
123
124
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
125
126
                  const checked = event.target.checked;
                  this.currentValue = checked;
d24f5082   Rijn   Support trueValue...
127
  
79964596   Xotic750   Use native w3c
128
                  const value = checked ? this.trueValue : this.falseValue;
d24f5082   Rijn   Support trueValue...
129
                  this.$emit('input', value);
7fa943eb   梁灏   init
130
  
79964596   Xotic750   Use native w3c
131
132
133
134
135
136
137
138
                  if (this.group) {
                      if (this.label !== undefined) {
                          this.parent.change({
                              value: this.label,
                              checked: this.value
                          });
                      }
                  } else {
d24f5082   Rijn   Support trueValue...
139
140
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
a804d608   梁灏   update Radio
141
                  }
7fa943eb   梁灏   init
142
              },
06322514   梁灏   support Radio
143
              updateValue () {
d24f5082   Rijn   Support trueValue...
144
                  this.currentValue = this.value === this.trueValue;
79964596   Xotic750   Use native w3c
145
146
147
148
149
150
151
152
153
154
155
              },
              onBlur () {
                  this.focusWrapper = false;
                  this.focusInner = false;
              },
              onFocus () {
                  if (this.group && this.parent.type === 'button') {
                      this.focusWrapper = true;
                  } else {
                      this.focusInner = true;
                  }
7fa943eb   梁灏   init
156
157
158
              }
          },
          watch: {
d24f5082   Rijn   Support trueValue...
159
              value (val) {
79964596   Xotic750   Use native w3c
160
161
162
                  if (val === this.trueValue || val === this.falseValue) {
                      this.updateValue();
                  } else {
d24f5082   Rijn   Support trueValue...
163
164
                      throw 'Value should be trueValue or falseValue.';
                  }
7fa943eb   梁灏   init
165
166
              }
          }
b0893113   jingsam   :art: add eslint
167
168
      };
  </script>