Skip to content

Commit 08da7d6

Browse files
astashovdevoncarew
authored andcommitted
Go to the first suggestion in the search field on Enter [#1149] (#1307)
Unfortunately, typeahead.js doesn't give us any API for fetching current suggesions, so we workaround that by adding `href` of a suggesion into a data attribute of HTML for that suggestion, and then fetching suggestions from DOM, choosing the first one, and using it's `data-href`. Quite dirty though :( I'd prefer to do something like `typeAheadElement.typeadead('suggestions')[0].href` instead. Testing: Checked that functionality in latest Chrome, Firefox and Safari Fixes #1149
1 parent 3c653b4 commit 08da7d6

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/resources/script.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function initSearch() {
173173
templates: {
174174
suggestion: function(match) {
175175
return [
176-
'<div>',
176+
'<div data-href="' + match.href + '">',
177177
match.name,
178178
' ',
179179
match.type.toLowerCase(),
@@ -187,7 +187,23 @@ function initSearch() {
187187
}
188188
});
189189

190-
$('#search-box.typeahead').bind('typeahead:select', function(ev, suggestion) {
190+
var typeaheadElement = $('#search-box.typeahead');
191+
var typeaheadElementParent = typeaheadElement.parent();
192+
193+
typeaheadElement.on("keydown", function (e) {
194+
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;
200+
}
201+
}
202+
console.log(typeaheadElement.typeahead("val"));
203+
}
204+
});
205+
206+
typeaheadElement.bind('typeahead:select', function(ev, suggestion) {
191207
window.location = suggestion.href;
192208
});
193209
}

testing/test_package_docs/static-assets/script.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function initSearch() {
173173
templates: {
174174
suggestion: function(match) {
175175
return [
176-
'<div>',
176+
'<div data-href="' + match.href + '">',
177177
match.name,
178178
' ',
179179
match.type.toLowerCase(),
@@ -187,7 +187,23 @@ function initSearch() {
187187
}
188188
});
189189

190-
$('#search-box.typeahead').bind('typeahead:select', function(ev, suggestion) {
190+
var typeaheadElement = $('#search-box.typeahead');
191+
var typeaheadElementParent = typeaheadElement.parent();
192+
193+
typeaheadElement.on("keydown", function (e) {
194+
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;
200+
}
201+
}
202+
console.log(typeaheadElement.typeahead("val"));
203+
}
204+
});
205+
206+
typeaheadElement.bind('typeahead:select', function(ev, suggestion) {
191207
window.location = suggestion.href;
192208
});
193209
}

0 commit comments

Comments
 (0)