feat: adds arrow keys navigation on powersearch
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user