Skip to content

Commit

Permalink
refactor(vue-dropdown): simplify onKeyDown event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
devCrossNet committed Aug 28, 2021
1 parent 5166dad commit a29db17
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/components/input-and-actions/VueDropdown/VueDropdown.vue
@@ -1,5 +1,5 @@
<template>
<div ref="dropdownRef" :class="$style.vueDropdown" @keydown="onKeyDown">
<div ref="dropdownRef" :class="$style.vueDropdown" @keydown.enter.space.up.down.esc.stop.prevent="onKeyDown">
<div :class="$style.wrapper" @click.stop.prevent="onClick">
<slot>
<vue-button look="outline" :aria-expanded="show.toString()" :size="size" trailing-icon="chevron-down">
Expand All @@ -20,7 +20,7 @@
</template>

<script lang="ts">
import { defineComponent, ref } from '@vue/composition-api';
import { defineComponent, nextTick, ref } from '@vue/composition-api';
import { getDomRef } from '@/composables/get-dom-ref';
import { IItem } from '@/interfaces/IItem';
import { useOutsideClick } from '@/composables/use-outside-click';
Expand Down Expand Up @@ -53,23 +53,15 @@ export default defineComponent({
emit('item-click', item);
close();
};
const checkForPropagation = (e: KeyboardEvent) => {
if (['Enter', 'Space', 'ArrowDown', 'ArrowUp', 'Escape'].includes(e.code)) {
e.stopPropagation();
e.preventDefault();
}
};
const onKeyDown = (e: KeyboardEvent) => {
checkForPropagation(e);
if (['Enter', 'Space', 'ArrowDown', 'ArrowUp'].includes(e.code)) {
const onKeyDown = async (e: KeyboardEvent) => {
if (e.code === 'Escape') {
close();
} else {
show.value = true;
setTimeout(() => {
menuRef.value.focus();
}, 10);
} else if (e.code === 'Escape') {
close();
await nextTick();
menuRef.value.focus();
}
};
Expand Down

0 comments on commit a29db17

Please sign in to comment.