feat: adds arrow keys navigation on powersearch

This commit is contained in:
franciscoemanuel
2021-10-19 02:20:30 -03:00
parent be414d8279
commit 686d8e5be7
4 changed files with 107 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
v-for="(shortcut, shortcutIndex) in searchResults"
:key="`shortcut-${shortcutIndex}`"
:ref="`item-${shortcutIndex}`"
:active="shortcutIndex === selectedEntry"
:shortcut="shortcut.item"
@action="$emit('action', shortcut.item.action)"
/>
@@ -20,8 +21,15 @@
</template>
<script setup lang="ts">
import { computed } from "@nuxtjs/composition-api"
import {
computed,
onUnmounted,
onMounted,
getCurrentInstance,
} from "@nuxtjs/composition-api"
import Fuse from "fuse.js"
import { useArrowKeysNavigation } from "~/helpers/powerSearchNavigation"
import { HoppAction } from "~/helpers/actions"
const props = defineProps<{
input: Record<string, any>[]
@@ -35,4 +43,23 @@ const options = {
const fuse = new Fuse(props.input, options)
const searchResults = computed(() => fuse.search(props.search))
const searchResultsItems = computed(() =>
searchResults.value.map((searchResult: any) => searchResult.item)
)
const currentInstance = getCurrentInstance()
const emitSearchAction = (action: HoppAction) =>
currentInstance.emit("action", action)
const { bindArrowKeysListerners, unbindArrowKeysListerners, selectedEntry } =
useArrowKeysNavigation(searchResultsItems, {
onEnter: emitSearchAction,
stopPropagation: true,
})
onMounted(bindArrowKeysListerners)
onUnmounted(unbindArrowKeysListerners)
</script>