From d94cadfc976d08f86c8b3e1dc8093353ea8fa2e4 Mon Sep 17 00:00:00 2001 From: DanielApt Date: Mon, 18 Jan 2016 13:22:27 +0000 Subject: [PATCH 1/5] Add autoselect option --- src/typeahead/plugin.js | 3 ++- src/typeahead/typeahead.js | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/typeahead/plugin.js b/src/typeahead/plugin.js index 0ecaca7ccb..ae78c35bc6 100644 --- a/src/typeahead/plugin.js +++ b/src/typeahead/plugin.js @@ -79,7 +79,8 @@ input: input, menu: menu, eventBus: eventBus, - minLength: o.minLength + minLength: o.minLength, + autoselect: o.autoselect }, www); $input.data(keys.www, www); diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index faeed27c88..f4b7fcbb98 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -39,6 +39,8 @@ var Typeahead = (function() { this.enabled = true; + this.autoselect = !!o.autoselect; + // activate the typeahead on init if the input has focus this.active = false; this.input.hasFocus() && this.activate(); @@ -163,6 +165,8 @@ var Typeahead = (function() { if ($selectable = this.menu.getActiveSelectable()) { this.select($selectable) && $e.preventDefault(); + } else if(this.autoselect) { + this.select(this.menu.getTopSelectable()) && $e.preventDefault(); } }, From 3bcc67f572d7fa73e1662d209ac6f0401f9f4922 Mon Sep 17 00:00:00 2001 From: DanielApt Date: Mon, 18 Jan 2016 13:35:32 +0000 Subject: [PATCH 2/5] Set the cursor on the first suggestion when using autoselect --- src/typeahead/typeahead.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index f4b7fcbb98..686e19f3ed 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -135,6 +135,12 @@ var Typeahead = (function() { _onDatasetRendered: function onDatasetRendered(type, dataset, suggestions, async) { this._updateHint(); + + if(this.autoselect) { + var cursorClass = this.selectors.cursor.substr(1); + this.menu.$node.find(this.selectors.suggestion).first().addClass(cursorClass); + } + this.eventBus.trigger('render', suggestions, async, dataset); }, From 93e030191c51eb3e6b0196451f97650b4e117ec8 Mon Sep 17 00:00:00 2001 From: DanielApt Date: Mon, 18 Jan 2016 13:45:58 +0000 Subject: [PATCH 3/5] Update typeahead docs --- doc/jquery_typeahead.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/jquery_typeahead.md b/doc/jquery_typeahead.md index f6402607c7..5fd1264568 100644 --- a/doc/jquery_typeahead.md +++ b/doc/jquery_typeahead.md @@ -117,6 +117,8 @@ When initializing a typeahead, there are a number of options you can configure. * `hint` – If `false`, the typeahead will not show a hint. Defaults to `true`. +* `autoselect` – If `true`, the first suggestion will be selected when pressing the Enter key. + * `minLength` – The minimum character length needed before suggestions start getting rendered. Defaults to `1`. From c4f789cf09c8c95a605ed8304bd78b7d2e5ff8dd Mon Sep 17 00:00:00 2001 From: Jonathan Seguin Date: Tue, 4 Jul 2017 10:36:53 -0400 Subject: [PATCH 4/5] Fix typo --- src/typeahead/typeahead.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 38df399ff8..47c6a166fb 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -170,7 +170,7 @@ var Typeahead = (function() { var $selectable; if ($selectable = this.menu.getActiveSelectable()) { - this.select($selectable)) && $e.preventDefault() && $e.stopPropagation(); + this.select($selectable) && $e.preventDefault() && $e.stopPropagation(); } else if(this.autoselect) { this.select(this.menu.getTopSelectable()) && $e.preventDefault() && $e.stopPropagation(); } From 4a2d06763774b6d22bd0297248560e0fb1a545aa Mon Sep 17 00:00:00 2001 From: Jonathan Seguin Date: Tue, 4 Jul 2017 10:54:42 -0400 Subject: [PATCH 5/5] Fix CI test error --- src/typeahead/typeahead.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/typeahead/typeahead.js b/src/typeahead/typeahead.js index 47c6a166fb..fe40ffc1b5 100644 --- a/src/typeahead/typeahead.js +++ b/src/typeahead/typeahead.js @@ -170,9 +170,15 @@ var Typeahead = (function() { var $selectable; if ($selectable = this.menu.getActiveSelectable()) { - this.select($selectable) && $e.preventDefault() && $e.stopPropagation(); + if (this.select($selectable)){ + $e.preventDefault(); + $e.stopPropagation(); + } } else if(this.autoselect) { - this.select(this.menu.getTopSelectable()) && $e.preventDefault() && $e.stopPropagation(); + if (this.select(this.menu.getTopSelectable())) { + $e.preventDefault(); + $e.stopPropagation(); + } } },