Skip to content

Commit 54f2495

Browse files
author
Tomas Kirda
committed
Implement preserve input option. Closes devbridge#164.
1 parent dfd7f44 commit 54f2495

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
5252
* `orientation`: Default `bottom`. Vertical orientation of the displayed suggestions, available values are `auto`, `top`, `bottom`.
5353
If set to `auto`, the suggestions will be orientated it the way that place them closer to middle of the view port.
5454
* `groupBy`: property name of the suggestion `data` object, by which results should be grouped.
55+
* `preserveInput`: if `true`, input value stays the same when navigating over suggestions. Default: `false`.
5556

5657
Autocomplete instance has following methods:
5758

spec/autocompleteBehavior.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*jslint vars: true*/
1+
/*jslint vars: true*/
22
/*global describe, it, expect, waits, waitsFor, runs, afterEach, spyOn, $*/
33

44
describe('Autocomplete Async', function(){
@@ -39,7 +39,7 @@ describe('Autocomplete Async', function(){
3939
});
4040
});
4141

42-
describe('Autocomplete Async', function() {
42+
describe('Autocomplete Async', function () {
4343
var input = document.createElement('input'),
4444
completeQuery,
4545
mockupSuggestion = { value: 'A', data: 'A' },
@@ -688,3 +688,47 @@ describe('Autocomplete', function () {
688688
});
689689

690690
});
691+
692+
describe('When options.preserveInput is true', function() {
693+
var input = $('<input />'),
694+
instance,
695+
suggestionData = null;
696+
697+
beforeEach(function() {
698+
input.autocomplete({
699+
lookup: [{ value: 'Jamaica', data: 'J' }, { value: 'Jamaica2', data: 'J' }, { value: 'Jamaica3', data: 'J' }],
700+
preserveInput: true,
701+
onSelect: function (suggestion) {
702+
suggestionData = suggestion.data;
703+
}
704+
});
705+
706+
input.val('J');
707+
instance = input.autocomplete();
708+
});
709+
710+
afterEach(function() {
711+
instance.dispose();
712+
});
713+
714+
it('Should NOT change input value when item is selected', function() {
715+
instance.onValueChange();
716+
instance.select(0);
717+
718+
expect(input.val()).toEqual('J');
719+
});
720+
721+
it('Should NOT change input value when move down', function() {
722+
instance.onValueChange();
723+
instance.moveDown();
724+
725+
expect(input.val()).toEqual('J');
726+
});
727+
728+
it('Should NOT change input value when move up', function() {
729+
instance.onValueChange();
730+
instance.moveUp();
731+
732+
expect(input.val()).toEqual('J');
733+
});
734+
});

src/jquery.autocomplete.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
onSearchStart: noop,
7575
onSearchComplete: noop,
7676
onSearchError: noop,
77+
preserveInput: false,
7778
containerClass: 'autocomplete-suggestions',
7879
tabDisabled: false,
7980
dataType: 'text',
@@ -890,7 +891,9 @@
890891
$(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta);
891892
}
892893

893-
that.el.val(that.getValue(that.suggestions[index].value));
894+
if (!that.options.preserveInput) {
895+
that.el.val(that.getValue(that.suggestions[index].value));
896+
}
894897
that.signalHint(null);
895898
},
896899

@@ -901,7 +904,7 @@
901904

902905
that.currentValue = that.getValue(suggestion.value);
903906

904-
if (that.currentValue !== that.el.val()) {
907+
if (that.currentValue !== that.el.val() && !that.options.preserveInput) {
905908
that.el.val(that.currentValue);
906909
}
907910

0 commit comments

Comments
 (0)