Blame view

src/components/radio/radio.vue 5.5 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
              },
e80b805f   chenhaodong   初始化
46
47
48
49
              readonly: {
                  type: Boolean,
                  default: false
              },
4d545420   梁灏   Radio add size prop
50
51
52
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
35f7a4ae   梁灏   Radio support glo...
53
54
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
55
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
4d545420   梁灏   Radio add size prop
56
                  }
0460a1e8   梁灏   fixed #812
57
58
59
              },
              name: {
                  type: String
7fa943eb   梁灏   init
60
61
62
63
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
64
                  currentValue: this.value,
3f281d6c   梁灏   update RadioGroup
65
                  group: false,
79964596   Xotic750   Use native w3c
66
67
68
69
                  groupName: this.name,
                  parent: findComponentUpward(this, 'RadioGroup'),
                  focusWrapper: false,
                  focusInner: false
b0893113   jingsam   :art: add eslint
70
              };
7fa943eb   梁灏   init
71
72
73
74
75
76
77
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
78
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
4d545420   梁灏   Radio add size prop
79
                          [`${prefixCls}-wrapper-disabled`]: this.disabled,
79964596   Xotic750   Use native w3c
80
81
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-focus`]: this.focusWrapper
7fa943eb   梁灏   init
82
                      }
b0893113   jingsam   :art: add eslint
83
                  ];
7fa943eb   梁灏   init
84
85
86
87
88
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
89
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
90
91
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
92
                  ];
7fa943eb   梁灏   init
93
94
              },
              innerClasses () {
79964596   Xotic750   Use native w3c
95
96
97
98
99
100
                  return [
                      `${prefixCls}-inner`,
                      {
                          [`${prefixCls}-focus`]: this.focusInner
                      }
                  ];
7fa943eb   梁灏   init
101
102
103
104
105
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
106
          mounted () {
79964596   Xotic750   Use native w3c
107
108
109
              if (this.parent) {
                  this.group = true;
                  if (this.name && this.name !== this.parent.name) {
2e14b458   Xotic750   Disable lint error
110
                      /* eslint-disable no-console */
79964596   Xotic750   Use native w3c
111
112
113
                      if (console.warn) {
                          console.warn('[iview] Name does not match Radio Group name.');
                      }
2e14b458   Xotic750   Disable lint error
114
                      /* eslint-enable no-console */
79964596   Xotic750   Use native w3c
115
116
117
118
119
120
                  } else {
                      this.groupName = this.parent.name; 
                  }
              }
  
              if (this.group) {
bb1f58e2   梁灏   update Radio
121
                  this.parent.updateValue();
79964596   Xotic750   Use native w3c
122
123
              } else {
                  this.updateValue();
7fa943eb   梁灏   init
124
125
126
127
128
129
130
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
e80b805f   chenhaodong   初始化
131
132
133
                  if (this.readonly) {
                      return false;
                  }
7fa943eb   梁灏   init
134
  
06322514   梁灏   support Radio
135
136
                  const checked = event.target.checked;
                  this.currentValue = checked;
d24f5082   Rijn   Support trueValue...
137
  
79964596   Xotic750   Use native w3c
138
                  const value = checked ? this.trueValue : this.falseValue;
d24f5082   Rijn   Support trueValue...
139
                  this.$emit('input', value);
7fa943eb   梁灏   init
140
  
79964596   Xotic750   Use native w3c
141
142
143
144
145
146
147
148
                  if (this.group) {
                      if (this.label !== undefined) {
                          this.parent.change({
                              value: this.label,
                              checked: this.value
                          });
                      }
                  } else {
d24f5082   Rijn   Support trueValue...
149
150
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
a804d608   梁灏   update Radio
151
                  }
7fa943eb   梁灏   init
152
              },
06322514   梁灏   support Radio
153
              updateValue () {
d24f5082   Rijn   Support trueValue...
154
                  this.currentValue = this.value === this.trueValue;
79964596   Xotic750   Use native w3c
155
156
157
158
159
160
161
162
163
164
165
              },
              onBlur () {
                  this.focusWrapper = false;
                  this.focusInner = false;
              },
              onFocus () {
                  if (this.group && this.parent.type === 'button') {
                      this.focusWrapper = true;
                  } else {
                      this.focusInner = true;
                  }
7fa943eb   梁灏   init
166
167
168
              }
          },
          watch: {
d24f5082   Rijn   Support trueValue...
169
              value (val) {
79964596   Xotic750   Use native w3c
170
171
172
                  if (val === this.trueValue || val === this.falseValue) {
                      this.updateValue();
                  } else {
d24f5082   Rijn   Support trueValue...
173
174
                      throw 'Value should be trueValue or falseValue.';
                  }
7fa943eb   梁灏   init
175
176
              }
          }
b0893113   jingsam   :art: add eslint
177
178
      };
  </script>