Skip to content

Commit

Permalink
select using arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Poliak committed Jan 13, 2020
1 parent d0bc765 commit d410bec
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/VueBootstrapTypeahead.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@focus="isFocused = true"
@blur="handleBlur"
@input="handleInput($event.target.value)"
@keydown="handleKeyDown"
autocomplete="off"
/>
<div v-if="$slots.append || append" class="input-group-append">
Expand All @@ -35,6 +36,7 @@
:maxMatches="maxMatches"
:minMatchingChars="minMatchingChars"
@hit="handleHit"
@keydown="handleKeyDown"
>
<!-- pass down all scoped slots -->
<template v-for="(slot, slotName) in $scopedSlots" :slot="slotName" slot-scope="{ data, htmlText }">
Expand Down Expand Up @@ -158,6 +160,18 @@ export default {
if (typeof this.value !== 'undefined') {
this.$emit('input', newValue)
}
},
handleKeyDown(evt) {
if (! this.isFocused || this.data.length < 1) return
switch (evt.code) {
case 'ArrowUp':
case 'ArrowDown':
case 'Enter':
evt.preventDefault()
this.$emit('selectionKeyPressed', evt)
break
}
}
},
Expand Down
46 changes: 46 additions & 0 deletions src/components/VueBootstrapTypeaheadList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:background-variant="backgroundVariant"
:text-variant="textVariant"
@click.native="handleHit(item, $event)"
@keydown="$listeners.keydown"
>
<template v-if="$scopedSlots.suggestion" slot="suggestion" slot-scope="{ data, htmlText }">
<slot name="suggestion" v-bind="{ data, htmlText }" />
Expand Down Expand Up @@ -96,8 +97,53 @@ export default {
}).slice(0, this.maxMatches)
}
},
mounted() {
this.$parent.$on('selectionKeyPressed', this.handleSelectionKey.bind(this))
},
methods: {
handleSelectionKey(evt) {
if (this.data.length < 1) return
let active = this.findActiveItemIndex()
let ch = this.$children
switch (evt.code) {
case 'ArrowUp':
this.setActiveItem((active > 0) ? (active - 1) : (ch.length - 1))
break
case 'ArrowDown':
this.setActiveItem((active + 1) % ch.length)
break
case 'Enter':
this.$emit('hit', this.findActiveItem())
break
}
},
findActiveItem() {
let idx = this.findActiveItemIndex()
return (idx < 0) ? null : this.matchedItems[idx]
},
findActiveItemIndex() {
let ch = this.$children
for (let i = 0; i < ch.length; i++) {
let item = ch[i]
if (item.active) {
return i
}
}
return -1
},
setActiveItem(idx) {
this.$children.forEach((item, i) => {
if (i === idx) {
item.active = true
item.$el.scrollIntoView(false)
}
else {
item.active = false
}
})
},
handleHit(item, evt) {
this.$emit('hit', item)
evt.preventDefault()
Expand Down
1 change: 1 addition & 0 deletions src/components/VueBootstrapTypeaheadListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:class="textClasses"
@mouseover="active = true"
@mouseout="active = false"
@keydown="$listeners.keydown"
>
<slot name="suggestion" v-bind="{ data: data, htmlText: htmlText }">
<span v-html="htmlText"></span>
Expand Down

0 comments on commit d410bec

Please sign in to comment.