From 081fa8be39678b2e948b05c3926d49616a25300b Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 22 Sep 2017 17:12:44 -0400 Subject: [PATCH] Autocomplete: Use combobox role with ARIA attributes --- components/autocomplete/index.js | 19 ++++++++++++++----- components/autocomplete/test/index.js | 14 +++++++++++--- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/components/autocomplete/index.js b/components/autocomplete/index.js index 9247198130ce7..495ed19b5b831 100644 --- a/components/autocomplete/index.js +++ b/components/autocomplete/index.js @@ -16,10 +16,11 @@ import { keycodes } from '@wordpress/utils'; import './style.scss'; import Button from '../button'; import Popover from '../popover'; +import withInstanceId from '../higher-order/with-instance-id'; const { ENTER, ESCAPE, UP, DOWN } = keycodes; -class Autocomplete extends Component { +export class Autocomplete extends Component { static getInitialState() { return { isOpen: false, @@ -180,10 +181,12 @@ class Autocomplete extends Component { } render() { - const { children, className } = this.props; + const { children, className, instanceId } = this.props; const { isOpen, selectedIndex } = this.state; const classes = classnames( 'components-autocomplete__popover', className ); const filteredOptions = this.getFilteredOptions(); + const listBoxId = `components-autocomplete-listbox-${ instanceId }`; + const activeId = `components-autocomplete-item-${ instanceId }-${ selectedIndex }`; // Blur is applied to the wrapper node, since if the child is Editable, // the event will not have `relatedTarget` assigned. @@ -196,6 +199,10 @@ class Autocomplete extends Component { { cloneElement( Children.only( children ), { onInput: this.search, onKeyDown: this.setSelectedIndex, + role: 'combobox', + 'aria-expanded': isOpen, + 'aria-activedescendant': isOpen ? activeId : null, + 'aria-owns': isOpen ? listBoxId : null, } ) } 0 } @@ -204,13 +211,15 @@ class Autocomplete extends Component { className={ classes } >