Add up-arrow key to open

Add escape key to revert
Remove automatic-closing behavior
This commit is contained in:
Andrew Croce 2018-10-19 10:55:53 -04:00
parent 17d2331322
commit 29ec31d8d8
2 changed files with 26 additions and 7 deletions

View File

@ -9,7 +9,8 @@ const SelectorInput = {
description: String, description: String,
selected: Boolean, selected: Boolean,
handleChange: Function, handleChange: Function,
handleEnter: Function handleEnter: Function,
handleEsc: Function
}, },
computed: { computed: {
@ -25,6 +26,10 @@ const SelectorInput = {
onEnter: function (e) { onEnter: function (e) {
this.handleEnter() this.handleEnter()
},
onEsc: function (e) {
this.handleEsc()
} }
} }
} }
@ -51,6 +56,7 @@ export default {
data: function () { data: function () {
return { return {
value: this.initialChoice || null, value: this.initialChoice || null,
currentChoice: this.initialChoice || null,
showError: (this.initialErrors && this.initialErrors.length) || false, showError: (this.initialErrors && this.initialErrors.length) || false,
usingKeyboard: false usingKeyboard: false
} }
@ -74,17 +80,27 @@ export default {
change: function (value) { change: function (value) {
this.value = value this.value = value
this.showError = false this.showError = false
if (!this.usingKeyboard) { },
setTimeout(() => this.$refs.popover.hide(), 300)
} onShow: function () {
setTimeout(() => { // timeout is a hack to make focus work in Chrome
this.$refs.choices.find(choice => choice.selected).$refs.input[0].focus()
}, 100)
}, },
enter: function () { enter: function () {
this.$refs.popover.hide() this.$refs.popover.hide()
}, },
esc: function () {
this.value = this.currentChoice
this.usingKeyboard = false
this.$refs.popover.hide()
},
handleEnterOption: function (e) { handleEnterOption: function (e) {
this.change(e.target.value) this.change(e.target.value)
this.currentChoice = e.target.value
this.usingKeyboard = false this.usingKeyboard = false
this.$refs.popover.hide() this.$refs.popover.hide()
}, },
@ -92,7 +108,6 @@ export default {
handleButtonArrowDown: function (e) { handleButtonArrowDown: function (e) {
this.usingKeyboard = true this.usingKeyboard = true
this.$refs.popover.show() this.$refs.popover.show()
this.$refs.choices.find(choice => choice.selected).$refs.input[0].focus()
} }
}, },
} }

View File

@ -11,7 +11,7 @@
inline-template> inline-template>
<fieldset v-bind:class="['selector usa-input', { 'usa-input--error': initialErrors }]"> <fieldset v-bind:class="['selector usa-input', { 'usa-input--error': initialErrors }]">
<v-popover v-bind:container='false' ref='popover'> <v-popover v-bind:container='false' ref='popover' v-on:show='onShow'>
<legend> <legend>
<div class="usa-input__title">{{ field.label | striptags }}</div> <div class="usa-input__title">{{ field.label | striptags }}</div>
@ -27,6 +27,7 @@
type='button' type='button'
v-html='label' v-html='label'
v-on:keydown.down.prevent.stop='handleButtonArrowDown' v-on:keydown.down.prevent.stop='handleButtonArrowDown'
v-on:keydown.up.prevent.stop='handleButtonArrowDown'
v-tooltip='{ container:false }'> v-tooltip='{ container:false }'>
</button> </button>
@ -53,6 +54,7 @@
v-bind:selected='value === choice[0]' v-bind:selected='value === choice[0]'
v-bind:handle-change='change' v-bind:handle-change='change'
v-bind:handle-enter='enter' v-bind:handle-enter='enter'
v-bind:handle-esc='esc'
inline-template> inline-template>
<div> <div>
@ -64,8 +66,10 @@
v-bind:id='id' v-bind:id='id'
v-bind:value='value' v-bind:value='value'
v-bind:checked='selected' v-bind:checked='selected'
v-bind:autofocus='selected'
v-on:change='onChange' v-on:change='onChange'
v-on:keydown.enter.prevent.stop='onEnter'/> v-on:keydown.enter.prevent.stop='onEnter'
v-on:keydown.esc.prevent.stop='onEsc'/>
<label v-bind:for='id'> <label v-bind:for='id'>
<template v-if='description'> <template v-if='description'>