Skip to content

Commit

Permalink
Add aria-activedescendent as replacement for focusable suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Terje Røstum committed Feb 23, 2017
1 parent 6c769e7 commit 9e5a6e3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/selectors/base-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BaseSelector extends Component {
this.state = {
showSuggestions: false,
highlightedSuggestionIndex: -1,
suggestionListId: "account-selector-suggestion-list"
};

/*
Expand Down Expand Up @@ -172,14 +173,23 @@ class BaseSelector extends Component {
}
}

getFocusedOptionId() {
const filteredResults = this.filterSuggestions();
const {highlightedSuggestionIndex} = this.state;
if (filteredResults.length > 0 && highlightedSuggestionIndex > -1 && filteredResults.length > highlightedSuggestionIndex) {
return `option-${filteredResults[highlightedSuggestionIndex].id}`;
}
return null;
}

render() {
const {
value,
placeholder,
suggestionsHeightMax,
id,
} = this.props;
const {showSuggestions, highlightedSuggestionIndex} = this.state;
const {showSuggestions, highlightedSuggestionIndex, suggestionListId} = this.state;
return (
<div
className='base-selector'
Expand All @@ -198,6 +208,8 @@ class BaseSelector extends Component {
placeholder={placeholder}
onBlur={this.onBlur}
onFocus={this.onFocus}
highlightedId={highlightedSuggestionIndex}
suggestionListId={suggestionListId}
/>
{showSuggestions &&
<SuggestionsList
Expand All @@ -209,6 +221,7 @@ class BaseSelector extends Component {
suggestions={this.filterSuggestions()}
heightMax={suggestionsHeightMax}
onSelect={this.onSuggestionClick}
id={this.state.suggestionListId}
/>}
</div>
);
Expand Down
8 changes: 7 additions & 1 deletion src/selectors/input-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ class Input extends Component {
resetLabel,
onBlur,
onReset,
inputFieldRef
inputFieldRef,
highlightedId,
suggestionListId
} = this.props;
return (
<div
role='combobox'
aria-expanded={ isSuggestionsShowing }
onFocus={ onFocus }
onBlur={ onBlur }
aria-activedescendant={highlightedId > -1 ? `suggestion-option-${highlightedId}` : null}
aria-owns={suggestionListId}
>
<input
onChange={ (e) => {onChange(e.target.value);}}
Expand Down Expand Up @@ -70,6 +74,8 @@ Input.propTypes = {
onFocus: PropTypes.func,
ariaInvalid: PropTypes.bool,
inputFieldRef: PropTypes.func,
highlightedId: PropTypes.number,
suggestionListId: PropTypes.string,
};

Input.defaultProps = {
Expand Down
4 changes: 3 additions & 1 deletion src/suggestion/suggestion-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';

class SuggestionItem extends Component {
render() {
const {item, isHighlighted, render, onSelect, refHighlightedSuggestion} = this.props;
const {item, id, isHighlighted, render, onSelect, refHighlightedSuggestion} = this.props;
return (
<li
ref={(itemRef) => {
Expand All @@ -12,6 +12,7 @@ class SuggestionItem extends Component {
}
}}
role='option'
id={id}
onMouseDown={() => {onSelect(item);}}
className={classNames('account-suggestion', {'account-suggestion__highlighted' : isHighlighted})}
tabIndex={-1}
Expand All @@ -23,6 +24,7 @@ class SuggestionItem extends Component {

SuggestionItem.propTypes = {
item: PropTypes.object.isRequired,
id: PropTypes.string.isRequired,
isHighlighted: PropTypes.bool.isRequired,
render: PropTypes.func.isRequired,
onSelect: PropTypes.func.isRequired,
Expand Down
4 changes: 4 additions & 0 deletions src/suggestion/suggestion-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ export default function SuggestionList(props) {
highlightedIndex,
renderSuggestion,
renderNoMatches,
id,
} = props;
return (
<ul
className='container-suggestion-list'
role='listbox'
id={id}
>
{ suggestions.length > 0 ?
suggestions.map((item, index) => {
Expand All @@ -20,6 +22,7 @@ export default function SuggestionList(props) {
{...props}
key={index}
item={item}
id={`suggestion-option-${index}`}
isHighlighted={index === highlightedIndex}
render={renderSuggestion}
/>);
Expand All @@ -35,6 +38,7 @@ SuggestionList.propTypes = {
highlightedIndex: PropTypes.number.isRequired,
renderSuggestion: PropTypes.func.isRequired,
renderNoMatches: PropTypes.func,
id: PropTypes.string.isRequired
};

SuggestionList.defaultProps = {
Expand Down

0 comments on commit 9e5a6e3

Please sign in to comment.