Merge pull request #395 from dod-ccpo/dropdown-keyboard-access
Dropdown selector keyboard accessibility
This commit is contained in:
commit
9fe7d877ba
@ -1,10 +1,46 @@
|
||||
import { VPopover } from 'v-tooltip'
|
||||
|
||||
const SelectorInput = {
|
||||
name: 'SelectorInput',
|
||||
props: {
|
||||
name: String,
|
||||
value: String,
|
||||
label: String,
|
||||
description: String,
|
||||
selected: Boolean,
|
||||
handleChange: Function,
|
||||
handleEnter: Function,
|
||||
handleEsc: Function
|
||||
},
|
||||
|
||||
computed: {
|
||||
id: function () {
|
||||
return `${this.name}_${this.value}`
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onChange: function (e) {
|
||||
this.handleChange(this.value)
|
||||
},
|
||||
|
||||
onEnter: function (e) {
|
||||
this.handleEnter()
|
||||
},
|
||||
|
||||
onEsc: function (e) {
|
||||
this.handleEsc()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default {
|
||||
name: 'selector',
|
||||
|
||||
components: {
|
||||
VPopover
|
||||
VPopover,
|
||||
SelectorInput
|
||||
},
|
||||
|
||||
props: {
|
||||
@ -20,7 +56,9 @@ export default {
|
||||
data: function () {
|
||||
return {
|
||||
value: this.initialChoice || null,
|
||||
showError: (this.initialErrors && this.initialErrors.length) || false
|
||||
currentChoice: this.initialChoice || null,
|
||||
showError: (this.initialErrors && this.initialErrors.length) || false,
|
||||
usingKeyboard: false
|
||||
}
|
||||
},
|
||||
|
||||
@ -38,10 +76,38 @@ export default {
|
||||
},
|
||||
|
||||
methods: {
|
||||
change: function (e) {
|
||||
this.value = e.target.value
|
||||
|
||||
change: function (value) {
|
||||
this.value = value
|
||||
this.showError = false
|
||||
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 () {
|
||||
this.$refs.popover.hide()
|
||||
},
|
||||
|
||||
esc: function () {
|
||||
this.value = this.currentChoice
|
||||
this.usingKeyboard = false
|
||||
this.$refs.popover.hide()
|
||||
},
|
||||
|
||||
handleEnterOption: function (e) {
|
||||
this.change(e.target.value)
|
||||
this.currentChoice = e.target.value
|
||||
this.usingKeyboard = false
|
||||
this.$refs.popover.hide()
|
||||
},
|
||||
|
||||
handleButtonArrowDown: function (e) {
|
||||
this.usingKeyboard = true
|
||||
this.$refs.popover.show()
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -68,6 +68,24 @@
|
||||
}
|
||||
}
|
||||
|
||||
@mixin block-list-selectable-label {
|
||||
margin: 0;
|
||||
max-width: 100%;
|
||||
display: flex;
|
||||
flex-direction: row-reverse;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
&::before {
|
||||
flex-shrink: 0;
|
||||
margin-right: 0;
|
||||
margin-left: $gap * 2;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
color: $color-primary;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.block-list {
|
||||
@include block-list;
|
||||
@ -105,24 +123,23 @@
|
||||
@include block-list-item;
|
||||
|
||||
&.block-list__item--selectable {
|
||||
> label {
|
||||
margin: 0;
|
||||
max-width: none;
|
||||
> div {
|
||||
display: flex;
|
||||
flex-direction: row-reverse;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
&::before {
|
||||
flex-shrink: 0;
|
||||
margin-right: 0;
|
||||
margin-left: $gap * 2;
|
||||
|
||||
@include ie-only {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
color: $color-primary;
|
||||
> label {
|
||||
@include block-list-selectable-label;
|
||||
}
|
||||
}
|
||||
|
||||
> label {
|
||||
@include block-list-selectable-label;
|
||||
}
|
||||
|
||||
input:checked {
|
||||
+ label {
|
||||
color: $color-primary;
|
||||
|
@ -11,7 +11,7 @@
|
||||
inline-template>
|
||||
|
||||
<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>
|
||||
<div class="usa-input__title">{{ field.label | striptags }}</div>
|
||||
|
||||
@ -22,7 +22,14 @@
|
||||
<span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span>
|
||||
</legend>
|
||||
|
||||
<button class='selector__button' type='button' v-html='label'></button>
|
||||
<button
|
||||
class='selector__button'
|
||||
type='button'
|
||||
v-html='label'
|
||||
v-on:keydown.down.prevent.stop='handleButtonArrowDown'
|
||||
v-on:keydown.up.prevent.stop='handleButtonArrowDown'
|
||||
v-tooltip='{ container:false }'>
|
||||
</button>
|
||||
|
||||
<span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span>
|
||||
|
||||
@ -35,23 +42,48 @@
|
||||
<template slot='popover'>
|
||||
<div class='block-list'>
|
||||
<ul>
|
||||
<li v-for='choice in choices' class='block-list__item block-list__item--selectable'>
|
||||
<li v-for='(choice, index) in choices' class='block-list__item block-list__item--selectable'>
|
||||
<template v-if='choice[0] !== ""'>
|
||||
<input
|
||||
type='radio'
|
||||
v-bind:id="'{{ field.name }}_' + choice[0]"
|
||||
|
||||
<selector-input
|
||||
name="{{ field.name }}"
|
||||
ref='choices'
|
||||
v-bind:value='choice[0]'
|
||||
v-bind:checked='value === choice[0]'
|
||||
v-on:change='change'/>
|
||||
<label v-bind:for="'{{ field.name }}_' + choice[0]">
|
||||
<template v-if='choice[1].description'>
|
||||
v-bind:label='choice[1].name'
|
||||
v-bind:description='choice[1].description'
|
||||
v-bind:selected='value === choice[0]'
|
||||
v-bind:handle-change='change'
|
||||
v-bind:handle-enter='enter'
|
||||
v-bind:handle-esc='esc'
|
||||
inline-template>
|
||||
|
||||
<div>
|
||||
<input
|
||||
ref='input'
|
||||
tabindex='0'
|
||||
type='radio'
|
||||
v-bind:name='name'
|
||||
v-bind:id='id'
|
||||
v-bind:value='value'
|
||||
v-bind:checked='selected'
|
||||
v-bind:autofocus='selected'
|
||||
v-on:change='onChange'
|
||||
v-on:keydown.enter.prevent.stop='onEnter'
|
||||
v-on:keydown.esc.prevent.stop='onEsc'/>
|
||||
|
||||
<label v-bind:for='id'>
|
||||
<template v-if='description'>
|
||||
<dl>
|
||||
<dt v-html='choice[1].name'></dt>
|
||||
<dd v-html='choice[1].description'></dd>
|
||||
<dt v-html='label'></dt>
|
||||
<dd v-html='description'></dd>
|
||||
</dl>
|
||||
</template>
|
||||
<span v-else v-html='choice[1].name'>
|
||||
<span v-else v-html='label'>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
</selector-input>
|
||||
|
||||
</template>
|
||||
</li>
|
||||
</ul>
|
||||
|
Loading…
x
Reference in New Issue
Block a user