Skip to content

Commit cda9784

Browse files
abarthdevoncarew
authored andcommitted
Search box shouldn't always use the first suggestion (#1335)
When you select an item from the search box dropdown, we were processing the event twice: once in typeahead:select and once on keydown. The second handler was overwriting the window.location generated by the first and always sending the user to the first suggestion even if they had selected a different suggestion. Fixes #1330
1 parent 36541ac commit cda9784

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/resources/script.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,24 @@ function initSearch() {
189189

190190
var typeaheadElement = $('#search-box.typeahead');
191191
var typeaheadElementParent = typeaheadElement.parent();
192+
var selectedSuggestion;
192193

193194
typeaheadElement.on("keydown", function (e) {
194195
if (e.keyCode === 13) { // Enter
195-
var suggestion = typeaheadElementParent.find(".tt-suggestion.tt-selectable:eq(0)");
196-
if (suggestion.length > 0) {
197-
var href = suggestion.data("href");
198-
if (href != null) {
199-
window.location = href;
196+
if (selectedSuggestion == null) {
197+
var suggestion = typeaheadElementParent.find(".tt-suggestion.tt-selectable:eq(0)");
198+
if (suggestion.length > 0) {
199+
var href = suggestion.data("href");
200+
if (href != null) {
201+
window.location = href;
202+
}
200203
}
201204
}
202-
console.log(typeaheadElement.typeahead("val"));
203205
}
204206
});
205207

206208
typeaheadElement.bind('typeahead:select', function(ev, suggestion) {
209+
selectedSuggestion = suggestion;
207210
window.location = suggestion.href;
208211
});
209212
}

testing/test_package_docs/static-assets/script.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,21 +189,24 @@ function initSearch() {
189189

190190
var typeaheadElement = $('#search-box.typeahead');
191191
var typeaheadElementParent = typeaheadElement.parent();
192+
var selectedSuggestion;
192193

193194
typeaheadElement.on("keydown", function (e) {
194195
if (e.keyCode === 13) { // Enter
195-
var suggestion = typeaheadElementParent.find(".tt-suggestion.tt-selectable:eq(0)");
196-
if (suggestion.length > 0) {
197-
var href = suggestion.data("href");
198-
if (href != null) {
199-
window.location = href;
196+
if (selectedSuggestion == null) {
197+
var suggestion = typeaheadElementParent.find(".tt-suggestion.tt-selectable:eq(0)");
198+
if (suggestion.length > 0) {
199+
var href = suggestion.data("href");
200+
if (href != null) {
201+
window.location = href;
202+
}
200203
}
201204
}
202-
console.log(typeaheadElement.typeahead("val"));
203205
}
204206
});
205207

206208
typeaheadElement.bind('typeahead:select', function(ev, suggestion) {
209+
selectedSuggestion = suggestion;
207210
window.location = suggestion.href;
208211
});
209212
}

0 commit comments

Comments
 (0)