Blame view

src/components/radio/radio.vue 3.6 KB
7fa943eb   梁灏   init
1
2
3
4
5
6
7
8
  <template>
      <label :class="wrapClasses">
          <span :class="radioClasses">
              <span :class="innerClasses"></span>
              <input
                  type="radio"
                  :class="inputClasses"
                  :disabled="disabled"
06322514   梁灏   support Radio
9
                  :checked="currentValue"
7fa943eb   梁灏   init
10
                  @change="change">
06322514   梁灏   support Radio
11
          </span><slot>{{ label }}</slot>
7fa943eb   梁灏   init
12
13
14
      </label>
  </template>
  <script>
3f281d6c   梁灏   update RadioGroup
15
      import { findComponentUpward } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
16
17
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
18
19
20
      const prefixCls = 'ivu-radio';
  
      export default {
06322514   梁灏   support Radio
21
          name: 'Radio',
cd78c9c4   梁灏   some comps suppor...
22
          mixins: [ Emitter ],
7fa943eb   梁灏   init
23
          props: {
06322514   梁灏   support Radio
24
              value: {
d24f5082   Rijn   Support trueValue...
25
26
27
28
29
30
31
32
33
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
34
35
                  default: false
              },
06322514   梁灏   support Radio
36
37
38
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
39
40
41
              disabled: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
42
43
44
45
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
46
                  currentValue: this.value,
3f281d6c   梁灏   update RadioGroup
47
48
                  group: false,
                  parent: findComponentUpward(this, 'RadioGroup')
b0893113   jingsam   :art: add eslint
49
              };
7fa943eb   梁灏   init
50
51
52
53
54
55
56
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
57
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
58
59
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
60
                  ];
7fa943eb   梁灏   init
61
62
63
64
65
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
66
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
67
68
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
69
                  ];
7fa943eb   梁灏   init
70
71
72
73
74
75
76
77
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
78
          mounted () {
3f281d6c   梁灏   update RadioGroup
79
              if (this.parent) this.group = true;
7fa943eb   梁灏   init
80
              if (!this.group) {
06322514   梁灏   support Radio
81
                  this.updateValue();
bb1f58e2   梁灏   update Radio
82
83
              } else {
                  this.parent.updateValue();
7fa943eb   梁灏   init
84
85
86
87
88
89
90
91
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
92
93
                  const checked = event.target.checked;
                  this.currentValue = checked;
d24f5082   Rijn   Support trueValue...
94
95
96
  
                  let value = checked ? this.trueValue : this.falseValue;
                  this.$emit('input', value);
7fa943eb   梁灏   init
97
  
70d5a4ab   梁灏   fixed #425
98
                  if (this.group && this.label !== undefined) {
3f281d6c   梁灏   update RadioGroup
99
                      this.parent.change({
06322514   梁灏   support Radio
100
101
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
102
103
                      });
                  }
a804d608   梁灏   update Radio
104
                  if (!this.group) {
d24f5082   Rijn   Support trueValue...
105
106
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
a804d608   梁灏   update Radio
107
                  }
7fa943eb   梁灏   init
108
              },
06322514   梁灏   support Radio
109
              updateValue () {
d24f5082   Rijn   Support trueValue...
110
                  this.currentValue = this.value === this.trueValue;
7fa943eb   梁灏   init
111
112
113
              }
          },
          watch: {
d24f5082   Rijn   Support trueValue...
114
115
116
117
              value (val) {
                  if (val !== this.trueValue && val !== this.falseValue) {
                      throw 'Value should be trueValue or falseValue.';
                  }
06322514   梁灏   support Radio
118
                  this.updateValue();
7fa943eb   梁灏   init
119
120
              }
          }
b0893113   jingsam   :art: add eslint
121
122
      };
  </script>