Blame view

src/components/radio/radio.vue 3.1 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
72
              this.parent = findComponentUpward(this, 'RadioGroup');
              if (this.parent) this.group = true;
7fa943eb   梁灏   init
73
              if (!this.group) {
06322514   梁灏   support Radio
74
                  this.updateValue();
7fa943eb   梁灏   init
75
76
77
78
79
80
81
82
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
06322514   梁灏   support Radio
83
84
85
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
86
  
06322514   梁灏   support Radio
87
                  if (this.group && this.label) {
3f281d6c   梁灏   update RadioGroup
88
                      this.parent.change({
06322514   梁灏   support Radio
89
90
                          value: this.label,
                          checked: this.value
7fa943eb   梁灏   init
91
92
                      });
                  }
a804d608   梁灏   update Radio
93
94
                  if (!this.group) {
                      this.$emit('on-change', checked);
cd78c9c4   梁灏   some comps suppor...
95
                      this.dispatch('FormItem', 'on-form-change', checked);
a804d608   梁灏   update Radio
96
                  }
7fa943eb   梁灏   init
97
              },
06322514   梁灏   support Radio
98
99
              updateValue () {
                  this.currentValue = this.value;
7fa943eb   梁灏   init
100
101
102
              }
          },
          watch: {
06322514   梁灏   support Radio
103
104
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
105
106
              }
          }
b0893113   jingsam   :art: add eslint
107
108
      };
  </script>