Blame view

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