Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/jquery_typeahead.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
3 changes: 2 additions & 1 deletion src/typeahead/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/typeahead/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
},

Expand Down Expand Up @@ -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();
}
},

Expand Down