Skip to content

Commit

Permalink
Add autoSelectFirst option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kirda committed Jan 20, 2013
1 parent b6482b8 commit 56437a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
* `paramName`: Default `query`. The name of the request parameter that contains the query.
* `transformResult`: `function(response) {}` called after the result of the query is ready. Converts the result into response.suggestions format.
* `autoSelectFirst`: if set to `true`, first item will be selected when showing suggestions. Default value `false`.
##Usage

Html:
Expand Down
31 changes: 29 additions & 2 deletions spec/autocompleteBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ describe('Autocomplete', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: false
tabDisabled: false,
autoSelectFirst: true
});
input.value = 'Jam';
autocomplete.onValueChange();
Expand All @@ -168,7 +169,8 @@ describe('Autocomplete', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: [{ value: 'Jamaica', data: 'B' }],
tabDisabled: true
tabDisabled: true,
autoSelectFirst: true
});
input.value = 'Jam';
autocomplete.onValueChange();
Expand All @@ -189,4 +191,29 @@ describe('Autocomplete', function () {
expect(event.preventDefault).toHaveBeenCalled();
expect(autocomplete.suggest).not.toHaveBeenCalled();
});

it('Should not autoselect first item by default', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: ['Jamaica', 'Jamaica', 'Jamaica']
});

input.value = 'Jam';
autocomplete.onValueChange();

expect(autocomplete.selectedIndex).toBe(-1);
});

it('Should autoselect first item autoSelectFirst set to true', function () {
var input = document.createElement('input'),
autocomplete = new $.Autocomplete(input, {
lookup: ['Jamaica', 'Jamaica', 'Jamaica'],
autoSelectFirst: true
});

input.value = 'Jam';
autocomplete.onValueChange();

expect(autocomplete.selectedIndex).toBe(0);
});
});
9 changes: 6 additions & 3 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
var noop = function () { },
that = this,
defaults = {
autoSelectFirst: false,
serviceUrl: null,
lookup: null,
onSelect: null,
Expand All @@ -92,7 +93,7 @@
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},
paramName: 'query',
transformResult: function(response) {
transformResult: function (response) {
return response.suggestions;
}
};
Expand Down Expand Up @@ -439,8 +440,10 @@
that.visible = true;

// Select first value by default:
that.selectedIndex = 0;
container.children().first().addClass(classSelected);
if (that.options.autoSelectFirst) {
that.selectedIndex = 0;
container.children().first().addClass(classSelected);
}
},

verifySuggestionsFormat: function (suggestions) {
Expand Down

0 comments on commit 56437a2

Please sign in to comment.