diff --git a/readme.md b/readme.md index 5d1e7e1..edd545b 100644 --- a/readme.md +++ b/readme.md @@ -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: diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index 62e9e07..9ab5ef6 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -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(); @@ -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(); @@ -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); + }); }); \ No newline at end of file diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 99e2957..7b6be77 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -71,6 +71,7 @@ var noop = function () { }, that = this, defaults = { + autoSelectFirst: false, serviceUrl: null, lookup: null, onSelect: null, @@ -92,7 +93,7 @@ return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; }, paramName: 'query', - transformResult: function(response) { + transformResult: function (response) { return response.suggestions; } }; @@ -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) {