Blame view

node_modules/element-ui/packages/radio/src/radio-group.vue 2.67 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
  <template>
    <div
      class="el-radio-group"
      role="radiogroup"
      @keydown="handleKeydown"
    >
      <slot></slot>
    </div>
  </template>
  <script>
    import Emitter from 'element-ui/src/mixins/emitter';
  
    const keyCode = Object.freeze({
      LEFT: 37,
      UP: 38,
      RIGHT: 39,
      DOWN: 40
    });
    export default {
      name: 'ElRadioGroup',
  
      componentName: 'ElRadioGroup',
  
      inject: {
        elFormItem: {
          default: ''
        }
      },
  
      mixins: [Emitter],
  
      props: {
        value: {},
        size: String,
        fill: String,
        textColor: String,
        disabled: Boolean
      },
  
      computed: {
        _elFormItemSize() {
          return (this.elFormItem || {}).elFormItemSize;
        },
        radioGroupSize() {
          return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
        }
      },
  
      created() {
        this.$on('handleChange', value => {
          this.$emit('change', value);
        });
      },
      mounted() {
        // 当radioGroup没有默认选项时,第一个可以选中Tab导航
        const radios = this.$el.querySelectorAll('[type=radio]');
        const firstLabel = this.$el.querySelectorAll('[role=radio]')[0];
        if (![].some.call(radios, radio => radio.checked) && firstLabel) {
          firstLabel.tabIndex = 0;
        }
      },
      methods: {
        handleKeydown(e) { // 左右上下按键 可以在radio组内切换不同选项
          const target = e.target;
          const className = target.nodeName === 'INPUT' ? '[type=radio]' : '[role=radio]';
          const radios = this.$el.querySelectorAll(className);
          const length = radios.length;
          const index = [].indexOf.call(radios, target);
          const roleRadios = this.$el.querySelectorAll('[role=radio]');
          switch (e.keyCode) {
            case keyCode.LEFT:
            case keyCode.UP:
              e.stopPropagation();
              e.preventDefault();
              if (index === 0) {
                roleRadios[length - 1].click();
                roleRadios[length - 1].focus();
              } else {
                roleRadios[index - 1].click();
                roleRadios[index - 1].focus();
              }
              break;
            case keyCode.RIGHT:
            case keyCode.DOWN:
              if (index === (length - 1)) {
                e.stopPropagation();
                e.preventDefault();
                roleRadios[0].click();
                roleRadios[0].focus();
              } else {
                roleRadios[index + 1].click();
                roleRadios[index + 1].focus();
              }
              break;
            default:
              break;
          }
        }
      },
      watch: {
        value(value) {
          this.dispatch('ElFormItem', 'el.form.change', [this.value]);
        }
      }
    };
  </script>