Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes TAB and RETURN keys behaviour #676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
that.suggestions = [];
that.badQueries = [];
that.selectedIndex = -1;
that.selectedBy = 'other'; // what changed selectedIndex ? possible values: 'keyboard','mouse' or 'other'
that.currentValue = that.element.value;
that.timeoutId = null;
that.cachedResponse = {};
Expand Down Expand Up @@ -183,6 +184,7 @@

// Listen for mouse over event on suggestions list:
container.on('mouseover.autocomplete', suggestionSelector, function () {
that.selectedBy = 'mouse';
that.activate($(this).data('index'));
});

Expand Down Expand Up @@ -392,11 +394,13 @@
}
return;
case keys.TAB:
// select suggestion only if it was selected by keyboard, not by mouse.
// Hint is an exception from this rule:
if (that.hint && that.options.onHint) {
that.selectHint();
return;
}
if (that.selectedIndex === -1) {
if (that.selectedIndex === -1 || that.selectedBy !== 'keyboard') {
that.hide();
return;
}
Expand All @@ -406,7 +410,8 @@
}
break;
case keys.RETURN:
if (that.selectedIndex === -1) {
// select suggestion only if it was selected by keyboard, not by mouse.
if (that.selectedIndex === -1 || that.selectedBy !== 'keyboard') {
that.hide();
return;
}
Expand Down Expand Up @@ -857,6 +862,8 @@
return;
}

that.selectedBy = 'keyboard';

if (that.selectedIndex === 0) {
$(that.suggestionsContainer).children().first().removeClass(that.classes.selected);
that.selectedIndex = -1;
Expand All @@ -872,6 +879,8 @@
moveDown: function () {
var that = this;

that.selectedBy = 'keyboard';

if (that.selectedIndex === (that.suggestions.length - 1)) {
return;
}
Expand Down