Skip to content

Commit d110e29

Browse files
committed
Added changeImputDisabled option to not change the input value when select, move up or down a suggest.
1 parent 702bc0e commit d110e29

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

spec/autocompleteBehavior.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,46 @@ describe('Autocomplete', function () {
543543
expect(suggestionData).toBeNull();
544544
});
545545

546+
describe('options.changeInputDisabled is true', function() {
547+
var input = $('<input />'),
548+
instance,
549+
suggestionData = null;
550+
551+
beforeEach(function() {
552+
input.autocomplete({
553+
lookup: [{ value: 'Jamaica', data: 'J' }, { value: 'Jamaica2', data: 'J' }, { value: 'Jamaica3', data: 'J' }],
554+
changeInputDisabled: true,
555+
onSelect: function (suggestion) {
556+
suggestionData = suggestion.data;
557+
}
558+
});
559+
560+
input.val('J');
561+
instance = input.autocomplete();
562+
});
563+
564+
it('Should NOT change input value when item is selected', function() {
565+
instance.onValueChange();
566+
instance.select(0);
567+
568+
expect(input.val()).toEqual('J');
569+
});
570+
571+
it('Should NOT change input value when move down', function() {
572+
instance.onValueChange();
573+
instance.moveDown();
574+
575+
expect(input.val()).toEqual('J');
576+
});
577+
578+
it('Should NOT change input value when move up', function() {
579+
instance.onValueChange();
580+
instance.moveUp();
581+
582+
expect(input.val()).toEqual('J');
583+
});
584+
});
585+
546586
it('Should use serviceUrl and params as cacheKey', function () {
547587
var input = $('<input />'),
548588
instance,

src/jquery.autocomplete.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
currentRequest: null,
7878
triggerSelectOnValidInput: true,
7979
preventBadQueries: true,
80+
changeInputDisabled: false,
8081
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
8182
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
8283
},
@@ -750,7 +751,10 @@
750751
$(that.suggestionsContainer).scrollTop(offsetTop - that.options.maxHeight + heightDelta);
751752
}
752753

753-
that.el.val(that.getValue(that.suggestions[index].value));
754+
if (!that.options.changeInputDisabled) {
755+
that.el.val(that.getValue(that.suggestions[index].value));
756+
}
757+
754758
that.signalHint(null);
755759
},
756760

@@ -760,7 +764,7 @@
760764
suggestion = that.suggestions[index];
761765

762766
that.currentValue = that.getValue(suggestion.value);
763-
that.el.val(that.currentValue);
767+
if (!that.options.changeInputDisabled) { that.el.val(that.currentValue); }
764768
that.signalHint(null);
765769
that.suggestions = [];
766770
that.selection = suggestion;

0 commit comments

Comments
 (0)