Blame view

src/components/radio/radio-group.vue 2.63 KB
7fa943eb   梁灏   init
1
  <template>
79964596   Xotic750   Use native w3c
2
      <div :class="classes" :name="name">
7fa943eb   梁灏   init
3
4
5
6
          <slot></slot>
      </div>
  </template>
  <script>
3f281d6c   梁灏   update RadioGroup
7
      import { oneOf, findComponentsDownward } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
8
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
9
10
11
  
      const prefixCls = 'ivu-radio-group';
  
79964596   Xotic750   Use native w3c
12
13
14
15
      let seed = 0;
      const now = Date.now();
      const getUuid = () => `ivuRadioGroup_${now}_${seed++}`;
  
7fa943eb   梁灏   init
16
      export default {
34ee7b4a   梁灏   support Tree & ad...
17
          name: 'RadioGroup',
cd78c9c4   梁灏   some comps suppor...
18
          mixins: [ Emitter ],
7fa943eb   梁灏   init
19
          props: {
06322514   梁灏   support Radio
20
              value: {
7fa943eb   梁灏   init
21
22
23
24
25
                  type: [String, Number],
                  default: ''
              },
              size: {
                  validator (value) {
4d545420   梁灏   Radio add size prop
26
                      return oneOf(value, ['small', 'large', 'default']);
7fa943eb   梁灏   init
27
28
29
30
31
32
                  }
              },
              type: {
                  validator (value) {
                      return oneOf(value, ['button']);
                  }
5722a6fb   梁灏   RadioGroup add ve...
33
34
35
36
              },
              vertical: {
                  type: Boolean,
                  default: false
79964596   Xotic750   Use native w3c
37
38
39
40
              },
              name: {
                  type: String,
                  default: getUuid
7fa943eb   梁灏   init
41
42
              }
          },
06322514   梁灏   support Radio
43
44
          data () {
              return {
3f281d6c   梁灏   update RadioGroup
45
                  currentValue: this.value,
79964596   Xotic750   Use native w3c
46
                  childrens: []
cbe03a12   梁灏   support Checkbox
47
              };
06322514   梁灏   support Radio
48
          },
7fa943eb   梁灏   init
49
50
51
52
53
54
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
4d545420   梁灏   Radio add size prop
55
                          [`ivu-radio-${this.size}`]: !!this.size,
5722a6fb   梁灏   RadioGroup add ve...
56
57
                          [`${prefixCls}-${this.type}`]: !!this.type,
                          [`${prefixCls}-vertical`]: this.vertical
7fa943eb   梁灏   init
58
                      }
b0893113   jingsam   :art: add eslint
59
                  ];
7fa943eb   梁灏   init
60
61
              }
          },
06322514   梁灏   support Radio
62
63
          mounted () {
              this.updateValue();
7fa943eb   梁灏   init
64
65
          },
          methods: {
06322514   梁灏   support Radio
66
              updateValue () {
3f281d6c   梁灏   update RadioGroup
67
                  this.childrens = findComponentsDownward(this, 'Radio');
3f281d6c   梁灏   update RadioGroup
68
69
                  if (this.childrens) {
                      this.childrens.forEach(child => {
d44f99fd   Wu-K'ung   修复RadioGroup只能在使用...
70
                          child.currentValue = this.currentValue === child.label;
3f281d6c   梁灏   update RadioGroup
71
72
73
                          child.group = true;
                      });
                  }
7fa943eb   梁灏   init
74
75
              },
              change (data) {
06322514   梁灏   support Radio
76
77
78
                  this.currentValue = data.value;
                  this.updateValue();
                  this.$emit('input', data.value);
7fa943eb   梁灏   init
79
                  this.$emit('on-change', data.value);
cd78c9c4   梁灏   some comps suppor...
80
                  this.dispatch('FormItem', 'on-form-change', data.value);
79964596   Xotic750   Use native w3c
81
              }
7fa943eb   梁灏   init
82
83
          },
          watch: {
06322514   梁灏   support Radio
84
              value () {
0b026efe   Wu-K'ung   Update radio-grou...
85
                  if(this.currentValue !== this.value){
d44f99fd   Wu-K'ung   修复RadioGroup只能在使用...
86
87
88
                      this.currentValue = this.value;
                      this.updateValue();
                  }
7fa943eb   梁灏   init
89
90
              }
          }
b0893113   jingsam   :art: add eslint
91
92
      };
  </script>