Blame view

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