diff --git a/readme.md b/readme.md index 9ea8538..5dbca12 100644 --- a/readme.md +++ b/readme.md @@ -52,6 +52,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu * `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`. If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port. * `groupBy`: property name of the suggestion `data` object, by which results should be grouped. + * `preserveInput`: if `true`, input value stays the same when navigating over suggestions. Default: `false`. Autocomplete instance has following methods: diff --git a/spec/autocompleteBehavior.js b/spec/autocompleteBehavior.js index 2b9f9bc..2a23f97 100644 --- a/spec/autocompleteBehavior.js +++ b/spec/autocompleteBehavior.js @@ -1,4 +1,4 @@ -/*jslint vars: true*/ +/*jslint vars: true*/ /*global describe, it, expect, waits, waitsFor, runs, afterEach, spyOn, $*/ describe('Autocomplete Async', function(){ @@ -39,7 +39,7 @@ describe('Autocomplete Async', function(){ }); }); -describe('Autocomplete Async', function() { +describe('Autocomplete Async', function () { var input = document.createElement('input'), completeQuery, mockupSuggestion = { value: 'A', data: 'A' }, @@ -688,3 +688,47 @@ describe('Autocomplete', function () { }); }); + +describe('When options.preserveInput is true', function() { + var input = $(''), + instance, + suggestionData = null; + + beforeEach(function() { + input.autocomplete({ + lookup: [{ value: 'Jamaica', data: 'J' }, { value: 'Jamaica2', data: 'J' }, { value: 'Jamaica3', data: 'J' }], + preserveInput: true, + onSelect: function (suggestion) { + suggestionData = suggestion.data; + } + }); + + input.val('J'); + instance = input.autocomplete(); + }); + + afterEach(function() { + instance.dispose(); + }); + + it('Should NOT change input value when item is selected', function() { + instance.onValueChange(); + instance.select(0); + + expect(input.val()).toEqual('J'); + }); + + it('Should NOT change input value when move down', function() { + instance.onValueChange(); + instance.moveDown(); + + expect(input.val()).toEqual('J'); + }); + + it('Should NOT change input value when move up', function() { + instance.onValueChange(); + instance.moveUp(); + + expect(input.val()).toEqual('J'); + }); +}); diff --git a/src/jquery.autocomplete.js b/src/jquery.autocomplete.js index 8be9a27..e1d9622 100644 --- a/src/jquery.autocomplete.js +++ b/src/jquery.autocomplete.js @@ -74,6 +74,7 @@ onSearchStart: noop, onSearchComplete: noop, onSearchError: noop, + preserveInput: false, containerClass: 'autocomplete-suggestions', tabDisabled: false, dataType: 'text', @@ -890,7 +891,9 @@ $(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta); } - that.el.val(that.getValue(that.suggestions[index].value)); + if (!that.options.preserveInput) { + that.el.val(that.getValue(that.suggestions[index].value)); + } that.signalHint(null); }, @@ -901,7 +904,7 @@ that.currentValue = that.getValue(suggestion.value); - if (that.currentValue !== that.el.val()) { + if (that.currentValue !== that.el.val() && !that.options.preserveInput) { that.el.val(that.currentValue); }