Blame view

src/components/radio/radio.vue 3.03 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: {
7fa943eb   梁灏   init
25
26
27
                  type: Boolean,
                  default: false
              },
06322514   梁灏   support Radio
28
29
30
              label: {
                  type: [String, Number]
              },
7fa943eb   梁灏   init
31
32
33
              disabled: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
34
35
36
37
              }
          },
          data () {
              return {
06322514   梁灏   support Radio
38
                  currentValue: this.value,
3f281d6c   梁灏   update RadioGroup
39
40
                  group: false,
                  parent: findComponentUpward(this, 'RadioGroup')
b0893113   jingsam   :art: add eslint
41
              };
7fa943eb   梁灏   init
42
43
44
45
46
47
48
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
06322514   梁灏   support Radio
49
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
50
51
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
52
                  ];
7fa943eb   梁灏   init
53
54
55
56
57
              },
              radioClasses () {
                  return [
                      `${prefixCls}`,
                      {
06322514   梁灏   support Radio
58
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
59
60
                          [`${prefixCls}-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
61
                  ];
7fa943eb   梁灏   init
62
63
64
65
66
67
68
69
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
06322514   梁灏   support Radio
70
          mounted () {
3f281d6c   梁灏   update RadioGroup
71
              if (this.parent) this.group = true;
7fa943eb   梁灏   init
72
              if (!this.group) {
06322514   梁灏   support Radio
73
                  this.updateValue();
7fa943eb   梁灏   init
74
75
76
77
78
79
80
81
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
82
83
84
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
85
  
06322514   梁灏   support Radio
86
                  if (this.group && this.label) {
3f281d6c   梁灏   update RadioGroup
87
                      this.parent.change({
06322514   梁灏   support Radio
88
89
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
90
91
                      });
                  }
a804d608   梁灏   update Radio
92
93
                  if (!this.group) {
                      this.$emit('on-change', checked);
cd78c9c4   梁灏   some comps suppor...
94
                      this.dispatch('FormItem', 'on-form-change', checked);
a804d608   梁灏   update Radio
95
                  }
7fa943eb   梁灏   init
96
              },
06322514   梁灏   support Radio
97
98
              updateValue () {
                  this.currentValue = this.value;
7fa943eb   梁灏   init
99
100
101
              }
          },
          watch: {
06322514   梁灏   support Radio
102
103
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
104
105
              }
          }
b0893113   jingsam   :art: add eslint
106
107
      };
  </script>