Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback on update #9

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ t.update([
])
```

### Callback

If you want to run code after autocomplete updates the input (e.g. to update a model), simply add a `callback` function into the `opts` parameter:

```javascript
var opts = {
callback:function(newValue){
console.log(newValue);
// Do code here
}
};

var t = new TypeAhead(document.getElementById('my-control'), [
'Asia', 'Africa', 'Europe', 'North America', 'South America', 'Oceania'
], opts);
```

## Contributing

Found an issue? Have a feature request? Open a [Github Issue]() and/or [fork this repo]().
Expand Down
3 changes: 3 additions & 0 deletions src/type-ahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var TypeAhead = function (element, candidates, opts) {

typeAhead.limit = opts.hasOwnProperty('limit') ? opts.limit : 5;

typeAhead.cb = opts.hasOwnProperty('callback') ? opts.callback : function(){};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's name it callback instead of cb for readability

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okiedokes :) will get that change in.


typeAhead.query = '';

typeAhead.selected = null;
Expand Down Expand Up @@ -90,6 +92,7 @@ TypeAhead.prototype.handleKeyDown = function (keyCode) {
if (keyCode === 13 && !this.list.isEmpty()) {
this.value(this.list.items[this.list.active]);
this.list.hide();
this.cb(this.list.items[this.list.active]);
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions type-ahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ var TypeAhead = function (element, candidates, opts) {

typeAhead.list = new TypeAheadList(typeAhead);

this.minLength = opts.hasOwnProperty('minLength') || 3;
this.minLength = opts.hasOwnProperty('minLength') ? opts.minLength : 3;

typeAhead.limit = opts.hasOwnProperty('limit') || 5;
typeAhead.limit = opts.hasOwnProperty('limit') ? opts.limit : 5;

typeAhead.cb = opts.hasOwnProperty('callback') ? opts.callback : function(){};

typeAhead.query = '';

Expand Down Expand Up @@ -91,6 +93,7 @@ TypeAhead.prototype.handleKeyDown = function (keyCode) {
if (keyCode === 13 && !this.list.isEmpty()) {
this.value(this.list.items[this.list.active]);
this.list.hide();
this.cb(this.list.items[this.list.active]);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion type-ahead.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.