Blame view

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