Merge pull request #395 from dod-ccpo/dropdown-keyboard-access

Dropdown selector keyboard accessibility
This commit is contained in:
andrewdds 2018-10-23 08:08:21 -04:00 committed by GitHub
commit 9fe7d877ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 33 deletions

View File

@ -1,10 +1,46 @@
import { VPopover } from 'v-tooltip' 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 { export default {
name: 'selector', name: 'selector',
components: { components: {
VPopover VPopover,
SelectorInput
}, },
props: { props: {
@ -20,7 +56,9 @@ export default {
data: function () { data: function () {
return { return {
value: this.initialChoice || null, 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: { methods: {
change: function (e) {
this.value = e.target.value change: function (value) {
this.value = value
this.showError = false 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()
} }
}, },
} }

View File

@ -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 { .block-list {
@include block-list; @include block-list;
@ -105,24 +123,23 @@
@include block-list-item; @include block-list-item;
&.block-list__item--selectable { &.block-list__item--selectable {
> label { > div {
margin: 0;
max-width: none;
display: flex; display: flex;
flex-direction: row-reverse; flex-direction: row-reverse;
align-items: center;
justify-content: space-between; @include ie-only {
&::before { width: 100%;
flex-shrink: 0;
margin-right: 0;
margin-left: $gap * 2;
} }
&:hover { > label {
color: $color-primary; @include block-list-selectable-label;
} }
} }
> label {
@include block-list-selectable-label;
}
input:checked { input:checked {
+ label { + label {
color: $color-primary; color: $color-primary;

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>
@ -22,7 +22,14 @@
<span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span> <span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span>
</legend> </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> <span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span>
@ -35,23 +42,48 @@
<template slot='popover'> <template slot='popover'>
<div class='block-list'> <div class='block-list'>
<ul> <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] !== ""'> <template v-if='choice[0] !== ""'>
<input
type='radio' <selector-input
v-bind:id="'{{ field.name }}_' + choice[0]" name="{{ field.name }}"
ref='choices'
v-bind:value='choice[0]' v-bind:value='choice[0]'
v-bind:checked='value === choice[0]' v-bind:label='choice[1].name'
v-on:change='change'/> v-bind:description='choice[1].description'
<label v-bind:for="'{{ field.name }}_' + choice[0]"> v-bind:selected='value === choice[0]'
<template v-if='choice[1].description'> 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> <dl>
<dt v-html='choice[1].name'></dt> <dt v-html='label'></dt>
<dd v-html='choice[1].description'></dd> <dd v-html='description'></dd>
</dl> </dl>
</template> </template>
<span v-else v-html='choice[1].name'> <span v-else v-html='label'>
</label> </label>
</div>
</selector-input>
</template> </template>
</li> </li>
</ul> </ul>