Blame view

src/components/radio/radio-group.vue 2.76 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']);
35f7a4ae   梁灏   Radio support glo...
27
28
29
                  },
                  default () {
                      return this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
30
31
32
33
34
35
                  }
              },
              type: {
                  validator (value) {
                      return oneOf(value, ['button']);
                  }
5722a6fb   梁灏   RadioGroup add ve...
36
37
38
39
              },
              vertical: {
                  type: Boolean,
                  default: false
79964596   Xotic750   Use native w3c
40
41
42
43
              },
              name: {
                  type: String,
                  default: getUuid
7fa943eb   梁灏   init
44
45
              }
          },
06322514   梁灏   support Radio
46
47
          data () {
              return {
3f281d6c   梁灏   update RadioGroup
48
                  currentValue: this.value,
79964596   Xotic750   Use native w3c
49
                  childrens: []
cbe03a12   梁灏   support Checkbox
50
              };
06322514   梁灏   support Radio
51
          },
7fa943eb   梁灏   init
52
53
54
55
56
57
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
4d545420   梁灏   Radio add size prop
58
                          [`ivu-radio-${this.size}`]: !!this.size,
5722a6fb   梁灏   RadioGroup add ve...
59
60
                          [`${prefixCls}-${this.type}`]: !!this.type,
                          [`${prefixCls}-vertical`]: this.vertical
7fa943eb   梁灏   init
61
                      }
b0893113   jingsam   :art: add eslint
62
                  ];
7fa943eb   梁灏   init
63
64
              }
          },
06322514   梁灏   support Radio
65
66
          mounted () {
              this.updateValue();
7fa943eb   梁灏   init
67
68
          },
          methods: {
06322514   梁灏   support Radio
69
              updateValue () {
3f281d6c   梁灏   update RadioGroup
70
                  this.childrens = findComponentsDownward(this, 'Radio');
3f281d6c   梁灏   update RadioGroup
71
72
                  if (this.childrens) {
                      this.childrens.forEach(child => {
d44f99fd   Wu-K'ung   修复RadioGroup只能在使用...
73
                          child.currentValue = this.currentValue === child.label;
3f281d6c   梁灏   update RadioGroup
74
75
76
                          child.group = true;
                      });
                  }
7fa943eb   梁灏   init
77
78
              },
              change (data) {
06322514   梁灏   support Radio
79
80
81
                  this.currentValue = data.value;
                  this.updateValue();
                  this.$emit('input', data.value);
7fa943eb   梁灏   init
82
                  this.$emit('on-change', data.value);
cd78c9c4   梁灏   some comps suppor...
83
                  this.dispatch('FormItem', 'on-form-change', data.value);
79964596   Xotic750   Use native w3c
84
              }
7fa943eb   梁灏   init
85
86
          },
          watch: {
06322514   梁灏   support Radio
87
              value () {
0b026efe   Wu-K'ung   Update radio-grou...
88
                  if(this.currentValue !== this.value){
d44f99fd   Wu-K'ung   修复RadioGroup只能在使用...
89
90
91
                      this.currentValue = this.value;
                      this.updateValue();
                  }
7fa943eb   梁灏   init
92
93
              }
          }
b0893113   jingsam   :art: add eslint
94
95
      };
  </script>