Skip to content

Commit

Permalink
Improve accessibility on OSX (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldanet authored and moroshko committed Jun 10, 2018
1 parent f594ff4 commit 7e2b9ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/Autowhatever.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,24 @@ export default class Autowhatever extends Component {
const renderedItems = multiSection ? this.renderSections() : this.renderItems();
const isOpen = (renderedItems !== null);
const ariaActivedescendant = this.getItemId(highlightedSectionIndex, highlightedItemIndex);
const containerProps = theme(
`react-autowhatever-${id}-container`,
'container',
isOpen && 'containerOpen'
);
const itemsContainerId = `react-autowhatever-${id}`;
const containerProps = {
role: 'combobox',
'aria-haspopup': 'listbox',
'aria-owns': itemsContainerId,
'aria-expanded': isOpen,
...theme(
`react-autowhatever-${id}-container`,
'container',
isOpen && 'containerOpen'
)
};
const inputComponent = renderInputComponent({
type: 'text',
value: '',
autoComplete: 'off',
role: 'combobox',
'aria-autocomplete': 'list',
'aria-owns': itemsContainerId,
'aria-expanded': isOpen,
'aria-controls': itemsContainerId,
'aria-activedescendant': ariaActivedescendant,
...theme(
`react-autowhatever-${id}-input`,
Expand All @@ -334,6 +338,7 @@ export default class Autowhatever extends Component {
const itemsContainer = renderItemsContainer({
containerProps: {
id: itemsContainerId,
role: 'listbox',
...theme(
`react-autowhatever-${id}-items-container`,
'itemsContainer',
Expand Down
1 change: 1 addition & 0 deletions src/ItemsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class ItemsList extends Component {
const itemPropsObj = isItemPropsFunction ? itemProps({ sectionIndex, itemIndex }) : itemProps;
const allItemProps = {
id: getItemId(sectionIndex, itemIndex),
'aria-selected': isHighlighted,
...theme(itemKey, 'item', isFirst && 'itemFirst', isHighlighted && 'itemHighlighted'),
...itemPropsObj
};
Expand Down
6 changes: 5 additions & 1 deletion test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import TestUtils, { Simulate } from 'react-dom/test-utils';

chai.use(sinonChai);

let app, input, itemsContainer;
let app, container, input, itemsContainer;

export const init = application => {
app = application;
container = TestUtils.findRenderedDOMComponentWithClass(app, 'react-autowhatever__container');
input = TestUtils.findRenderedDOMComponentWithTag(app, 'input');
itemsContainer = TestUtils.findRenderedDOMComponentWithClass(app, 'react-autowhatever__items-container');
};
Expand All @@ -29,6 +30,9 @@ export const getStoredInput = () => app.autowhatever.input;
export const getStoredItemsContainer = () => app.autowhatever.itemsContainer;
export const getStoredHighlightedItem = () => app.autowhatever.highlightedItem;

export const getContainerAttribute = attr =>
container.getAttribute(attr);

export const getInputAttribute = attr =>
input.getAttribute(attr);

Expand Down
20 changes: 18 additions & 2 deletions test/plain-list/Autowhatever.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
getStoredInput,
getStoredItemsContainer,
getStoredHighlightedItem,
getContainerAttribute,
getInputAttribute,
getItemsContainerAttribute,
getItems,
getItem,
mouseEnterItem,
mouseLeaveItem,
mouseDownItem,
Expand All @@ -25,8 +27,12 @@ describe('Plain List Autowhatever', () => {
});

describe('initially', () => {
it('should set input\'s `aria-owns` to items container\'s `id`', () => {
expect(getInputAttribute('aria-owns')).to.equal(getItemsContainerAttribute('id'));
it('should set container\'s `aria-owns` to items container\'s `id`', () => {
expect(getContainerAttribute('aria-owns')).to.equal(getItemsContainerAttribute('id'));
});

it('should set input\'s `aria-controls` to items container\'s `id`', () => {
expect(getInputAttribute('aria-controls')).to.equal(getItemsContainerAttribute('id'));
});

it('should render all items', () => {
Expand Down Expand Up @@ -78,6 +84,16 @@ describe('Plain List Autowhatever', () => {
expect(renderItem).to.be.calledWith({ text: 'Apple' }, { isHighlighted: false });
});

it('should set `aria-selected` to true on highlighted items', () => {
renderItem.reset();
mouseEnterItem(0);
expect(getItem(0).getAttribute('aria-selected')).to.equal('true');

renderItem.reset();
mouseLeaveItem(0);
expect(getItem(0).getAttribute('aria-selected')).to.equal('false');
});

it('should call `renderItem` once when item is left', () => {
mouseEnterItem(3);
renderItem.reset();
Expand Down

0 comments on commit 7e2b9ff

Please sign in to comment.