Blame view

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