Blame view

src/components/radio/radio-group.vue 4.18 KB
7fa943eb   梁灏   init
1
  <template>
36d24701   Graham Fairweather   Radio w3c keyboar...
2
3
4
5
6
7
8
      <div
          :class="classes" tabindex="0"
          @keydown.left="onLeft"
          @keydown.right="onRight"
          @keydown.up="onUp"
          @keydown.down="onDown"
          @keydown.tab="onTab">
7fa943eb   梁灏   init
9
10
11
12
          <slot></slot>
      </div>
  </template>
  <script>
3f281d6c   梁灏   update RadioGroup
13
      import { oneOf, findComponentsDownward } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
14
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
15
16
17
18
  
      const prefixCls = 'ivu-radio-group';
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
19
          name: 'RadioGroup',
cd78c9c4   梁灏   some comps suppor...
20
          mixins: [ Emitter ],
7fa943eb   梁灏   init
21
          props: {
06322514   梁灏   support Radio
22
              value: {
7fa943eb   梁灏   init
23
24
25
26
27
                  type: [String, Number],
                  default: ''
              },
              size: {
                  validator (value) {
4d545420   梁灏   Radio add size prop
28
                      return oneOf(value, ['small', 'large', 'default']);
7fa943eb   梁灏   init
29
30
31
32
33
34
                  }
              },
              type: {
                  validator (value) {
                      return oneOf(value, ['button']);
                  }
5722a6fb   梁灏   RadioGroup add ve...
35
36
37
38
              },
              vertical: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
39
40
              }
          },
06322514   梁灏   support Radio
41
42
          data () {
              return {
3f281d6c   梁灏   update RadioGroup
43
                  currentValue: this.value,
36d24701   Graham Fairweather   Radio w3c keyboar...
44
45
                  childrens: [],
                  preventDefaultTab: true
cbe03a12   梁灏   support Checkbox
46
              };
06322514   梁灏   support Radio
47
          },
7fa943eb   梁灏   init
48
49
50
51
52
53
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-${this.size}`]: !!this.size,
4d545420   梁灏   Radio add size prop
54
                          [`ivu-radio-${this.size}`]: !!this.size,
5722a6fb   梁灏   RadioGroup add ve...
55
56
                          [`${prefixCls}-${this.type}`]: !!this.type,
                          [`${prefixCls}-vertical`]: this.vertical
7fa943eb   梁灏   init
57
                      }
b0893113   jingsam   :art: add eslint
58
                  ];
7fa943eb   梁灏   init
59
60
              }
          },
06322514   梁灏   support Radio
61
62
          mounted () {
              this.updateValue();
7fa943eb   梁灏   init
63
64
          },
          methods: {
06322514   梁灏   support Radio
65
66
              updateValue () {
                  const value = this.value;
3f281d6c   梁灏   update RadioGroup
67
68
69
70
                  this.childrens = findComponentsDownward(this, 'Radio');
  
                  if (this.childrens) {
                      this.childrens.forEach(child => {
f4e25069   Graham Fairweather   Use z-index to sh...
71
                          child.currentValue = value === child.label;
3f281d6c   梁灏   update RadioGroup
72
73
74
                          child.group = true;
                      });
                  }
7fa943eb   梁灏   init
75
76
              },
              change (data) {
06322514   梁灏   support Radio
77
78
79
                  this.currentValue = data.value;
                  this.updateValue();
                  this.$emit('input', data.value);
7fa943eb   梁灏   init
80
                  this.$emit('on-change', data.value);
cd78c9c4   梁灏   some comps suppor...
81
                  this.dispatch('FormItem', 'on-form-change', data.value);
36d24701   Graham Fairweather   Radio w3c keyboar...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
              },
              findRadio(value) {
                  return this.childrens && this.childrens.length ? this.childrens.find((child) => child.value === value) : undefined;
              },
              findIndexRadio(value) {
                  return this.childrens && this.childrens.length ? this.childrens.findIndex((child) => child.value === value) : -1;
              },
              includesRadio(value) {
                  return this.childrens && this.childrens.length ? this.childrens.includes((child) => child.value === value) : false;
              },
              nextRadio() {
                if (this.includesRadio(this.currentValue)) {
                  console.log('get next');
                } else {
                  return this.childrens && this.childrens.length ? this.childrens[0] : undefined;
                }
              },
              onLeft() {
                  console.log('left', this.currentValue);
              },
              onRight() {
                  console.log('right', this.currentValue);
              },
              onUp() {
                  console.log('up', this.currentValue);
              },
              onDown() {
                  console.log('down', this.currentValue);
              },
              onTab(event) {
                  if (!this.preventDefaultTab) {
                      return;
                  }
  
                  event.preventDefault();
                  this.preventDefaultTab = false;
                  this.currentValue = this.nextRadio();
                  if (this.currentValue) {
                      this.change({
                          value: this.currentValue.label
                      });
                  }
  
                  console.log('tab', this);
  
              },
7fa943eb   梁灏   init
128
129
          },
          watch: {
06322514   梁灏   support Radio
130
131
              value () {
                  this.updateValue();
7fa943eb   梁灏   init
132
133
              }
          }
b0893113   jingsam   :art: add eslint
134
135
      };
  </script>