Skip to content

Commit

Permalink
Merge pull request #154 from JonathanSeguin/master
Browse files Browse the repository at this point in the history
Add autoselect option
  • Loading branch information
jcrben authored Aug 3, 2017
2 parents 7207531 + 034c5f6 commit f919ce8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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 @@ -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);
Expand Down
19 changes: 16 additions & 3 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, 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);
},

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

0 comments on commit f919ce8

Please sign in to comment.