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`. 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..686e19f3ed 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(); @@ -133,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); }, @@ -163,6 +171,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(); } },