Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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