Skip to content

Commit

Permalink
Handle keyboard focus
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <[email protected]>
  • Loading branch information
Pytal committed Jan 27, 2023
1 parent 61f4211 commit 164ac68
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/components/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@
isOptionDeselectable(option) && index === typeAheadPointer,
'vs__dropdown-option--selected': isOptionSelected(option),
'vs__dropdown-option--highlight': index === typeAheadPointer,
'vs__dropdown-option--kb-focus': hasKeyboardFocusBorder(index),
'vs__dropdown-option--disabled': !selectable(option),
}"
:aria-selected="optionAriaSelected(option)"
@mouseover="selectable(option) ? (typeAheadPointer = index) : null"
@mousemove="onMouseMove(option, index)"
@click.prevent.stop="selectable(option) ? select(option) : null"
>
<slot name="option" v-bind="normalizeOptionForSlot(option)">
Expand Down Expand Up @@ -661,6 +662,15 @@ export default {
},
},
/**
* Display a visible border around dropdown options
* which have keyboard focus.
*/
keyboardFocusBorder: {
type: Boolean,
default: false,
},
/**
* A unique identifier used to generate IDs in HTML.
* Must be unique for every instance of the component.
Expand All @@ -676,6 +686,7 @@ export default {
search: '',
open: false,
isComposing: false,
isKeyboardNavigation: false,
pushedTags: [],
// eslint-disable-next-line vue/no-reserved-keys
_value: [], // Internal value managed by Vue Select if no `value` prop is passed
Expand Down Expand Up @@ -1135,6 +1146,19 @@ export default {
return this.isOptionSelected(option) && this.deselectFromDropdown
},
/**
* Check if the option at the given index should display a
* keyboard focus border.
* @param {Number} index
* @return {Boolean}
*/
hasKeyboardFocusBorder(index) {
if (this.keyboardFocusBorder && this.isKeyboardNavigation) {
return index === this.typeAheadPointer
}
return false
},
/**
* Determine if two option objects are matching.
*
Expand Down Expand Up @@ -1324,6 +1348,20 @@ export default {
this.mousedown = false
},
/**
* Event-Handler for option mousemove
* @param {Object|String} option
* @param {Number} index
* @return {void}
*/
onMouseMove(option, index) {
this.isKeyboardNavigation = false
if (!this.selectable(option)) {
return
}
this.typeAheadPointer = index
},
/**
* Search <input> KeyBoardEvent handler.
* @param {KeyboardEvent} e
Expand All @@ -1349,6 +1387,7 @@ export default {
// up.prevent
38: (e) => {
e.preventDefault()
this.isKeyboardNavigation = true
if (!this.open) {
this.open = true
return
Expand All @@ -1358,6 +1397,7 @@ export default {
// down.prevent
40: (e) => {
e.preventDefault()
this.isKeyboardNavigation = true
if (!this.open) {
this.open = true
return
Expand Down

0 comments on commit 164ac68

Please sign in to comment.