diff --git a/doc/jquery_typeahead.md b/doc/jquery_typeahead.md index 65639d633d..b6c17f3827 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 f6bf416491..7338e057b5 100644 --- a/src/typeahead/plugin.js +++ b/src/typeahead/plugin.js @@ -84,7 +84,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 3dd9ae3183..fe40ffc1b5 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, suggestions, async, dataset) { 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); }, @@ -162,9 +170,14 @@ var Typeahead = (function() { var $selectable; if ($selectable = this.menu.getActiveSelectable()) { - if (this.select($selectable)) { - $e.preventDefault(); - $e.stopPropagation(); + if (this.select($selectable)){ + $e.preventDefault(); + $e.stopPropagation(); + } + } else if(this.autoselect) { + if (this.select(this.menu.getTopSelectable())) { + $e.preventDefault(); + $e.stopPropagation(); } } },