add basic keyboard navigation
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div class="items">
|
||||||
<div v-for="(item, index) in items" :key="index">
|
<div
|
||||||
|
class="item"
|
||||||
|
:class="{ 'is-selected': index === selectedIndex }"
|
||||||
|
v-for="(item, index) in items"
|
||||||
|
:key="index"
|
||||||
|
>
|
||||||
{{ item }}
|
{{ item }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -14,9 +19,69 @@ export default {
|
|||||||
default: () => [],
|
default: () => [],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
selectedIndex: 0,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
items() {
|
||||||
|
this.selectedIndex = 0
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
onKeyDown({ event }) {
|
||||||
|
if (event.key === 'ArrowUp') {
|
||||||
|
this.upHandler()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === 'ArrowDown') {
|
||||||
|
this.downHandler()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
this.enterHandler()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
|
||||||
|
upHandler() {
|
||||||
|
this.selectedIndex = ((this.selectedIndex + this.items.length) - 1) % this.items.length
|
||||||
|
},
|
||||||
|
|
||||||
|
downHandler() {
|
||||||
|
this.selectedIndex = (this.selectedIndex + 1) % this.items.length
|
||||||
|
},
|
||||||
|
|
||||||
|
enterHandler() {
|
||||||
|
const item = this.items[this.selectedIndex]
|
||||||
|
|
||||||
|
if (item) {
|
||||||
|
console.log('select', item)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style lang="scss" scoped>
|
||||||
|
.items {
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
.item {
|
||||||
|
background: white;
|
||||||
|
color: black;
|
||||||
|
padding: 0.5rem;
|
||||||
|
|
||||||
|
&.is-selected {
|
||||||
|
background: #ccc;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ export default {
|
|||||||
onUpdate(props) {
|
onUpdate(props) {
|
||||||
component.updateProps(props)
|
component.updateProps(props)
|
||||||
},
|
},
|
||||||
|
onKeyDown(props) {
|
||||||
|
return component.vm.onKeyDown(props)
|
||||||
|
},
|
||||||
onExit() {
|
onExit() {
|
||||||
popup[0].destroy()
|
popup[0].destroy()
|
||||||
component.destroy()
|
component.destroy()
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ export interface SuggestionOptions {
|
|||||||
command?: () => any,
|
command?: () => any,
|
||||||
items?: (query: string) => any[],
|
items?: (query: string) => any[],
|
||||||
renderer?: () => {
|
renderer?: () => {
|
||||||
onStart?: (props: any) => any,
|
onStart?: (props: any) => void,
|
||||||
onUpdate?: (props: any) => any,
|
onUpdate?: (props: any) => void,
|
||||||
onExit?: (props: any) => any,
|
onExit?: (props: any) => void,
|
||||||
onKeyDown?: (props: any) => any,
|
onKeyDown?: (props: any) => boolean,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user