Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dropdown_keyboard_navigation.vue « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1da84df022f5b3dab98b51d084b279e6baba0fcd (plain)
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
<script>
import { UP_KEY_CODE, DOWN_KEY_CODE, TAB_KEY_CODE } from '~/lib/utils/keycodes';

export default {
  model: {
    prop: 'index',
    event: 'change',
  },
  props: {
    /* v-model property to manage location in list */
    index: {
      type: Number,
      required: true,
    },
    /* Highest index that can be navigated to */
    max: {
      type: Number,
      required: true,
    },
    /* Lowest index that can be navigated to */
    min: {
      type: Number,
      required: true,
    },
    /* Which index to set v-model to on init */
    defaultIndex: {
      type: Number,
      required: true,
    },
  },
  watch: {
    max() {
      // If the max index (list length) changes, reset the index
      this.$emit('change', this.defaultIndex);
    },
  },
  created() {
    this.$emit('change', this.defaultIndex);
    document.addEventListener('keydown', this.handleKeydown);
  },
  beforeDestroy() {
    document.removeEventListener('keydown', this.handleKeydown);
  },
  methods: {
    handleKeydown(event) {
      if (event.keyCode === DOWN_KEY_CODE) {
        // Prevents moving scrollbar
        event.preventDefault();
        event.stopPropagation();
        // Moves to next index
        this.increment(1);
      } else if (event.keyCode === UP_KEY_CODE) {
        // Prevents moving scrollbar
        event.preventDefault();
        event.stopPropagation();
        // Moves to previous index
        this.increment(-1);
      } else if (event.keyCode === TAB_KEY_CODE) {
        this.$emit('tab');
      }
    },
    increment(val) {
      if (this.max === 0) {
        return;
      }

      const nextIndex = Math.max(this.min, Math.min(this.index + val, this.max));

      // Return if the index didn't change
      if (nextIndex === this.index) {
        return;
      }

      this.$emit('change', nextIndex);
    },
  },
  render() {
    return this.$scopedSlots.default?.();
  },
};
</script>