Skip to content

Commit

Permalink
Fix highlighting of the last option when it's disabled (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
siarhei-zharnasek authored and ericgio committed Feb 1, 2019
1 parent 6b45fbb commit 4537b25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/containers/typeaheadContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function getInitialState(props) {
};
}

function skipDisabledOptions(results, activeIndex, keyCode) {
let newActiveIndex = activeIndex;

while (results[newActiveIndex] && results[newActiveIndex].disabled) {
newActiveIndex += keyCode === UP ? -1 : 1;
}

return newActiveIndex;
}

function typeaheadContainer(Component) {
const Typeahead = contextContainer(Component);

Expand Down Expand Up @@ -297,15 +307,16 @@ function typeaheadContainer(Component) {
activeIndex += e.keyCode === UP ? -1 : 1;

// Skip over any disabled options.
while (results[activeIndex] && results[activeIndex].disabled) {
activeIndex += e.keyCode === UP ? -1 : 1;
}
activeIndex = skipDisabledOptions(results, activeIndex, e.keyCode);

// If we've reached the end, go back to the beginning or vice-versa.
if (activeIndex === results.length) {
activeIndex = -1;
} else if (activeIndex === -2) {
activeIndex = results.length - 1;

// Skip over any disabled options.
activeIndex = skipDisabledOptions(results, activeIndex, e.keyCode);
}

this._handleActiveIndexChange(activeIndex);
Expand Down
18 changes: 18 additions & 0 deletions test/components/TypeaheadSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@ describe('<Typeahead>', () => {
expect(activeItem.text()).to.equal(options[0].name);
});

it(
'should not highlight disabled option which is the last in the list',
() => {
const options = [
{name: 'foo'},
{name: 'bar'},
{disabled: true, name: 'boo'},
];

typeahead = mountTypeahead({options});
focus(typeahead);

// Cycling back up should skip the last option disabled.
const activeOption = cycleThroughMenuAndGetActiveItem(typeahead, UP);
expect(activeOption.text()).to.equal(options[1].name);
}
);

describe('pagination behaviors', () => {
let maxResults, onPaginate, shownResultsCount;

Expand Down

0 comments on commit 4537b25

Please sign in to comment.