Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 5d0d498

Browse files
devversionThomasBurleson
authored andcommitted
fix(autocomplete): autocompletes input should not clear the view value.
* Currently when specifying attributes like `md-input-maxlength` and the limit as reached, the ngModelCtrl will clear the `$viewValue`, which causes the input to keep the entered text, but the autocomplete already knows that the searchText is empty. - This causes the dropdown to open, even if the input "should" not show up with all results. - Also the input value will be cleared, which is not correct. BREAKING CHANGE Fixes #8947. Closes #8977
1 parent 3e2bdc0 commit 5d0d498

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/components/autocomplete/autocomplete.spec.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,75 @@ describe('<md-autocomplete>', function() {
320320
element.remove();
321321
}));
322322

323+
describe('md-input-maxlength', function() {
324+
325+
it('should correctly set the form to invalid', inject(function($timeout) {
326+
var scope = createScope(null, {inputId: 'custom-input-id'});
327+
var template =
328+
'<form name="testForm">' +
329+
'<md-autocomplete ' +
330+
'md-input-id="{{inputId}}" ' +
331+
'md-input-maxlength="2" ' +
332+
'md-input-name="testAutocomplete" ' +
333+
'md-selected-item="selectedItem" ' +
334+
'md-search-text="searchText" ' +
335+
'md-items="item in match(searchText)" ' +
336+
'md-item-text="item.display" ' +
337+
'tabindex="3"' +
338+
'md-floating-label="Favorite state">' +
339+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
340+
'</md-autocomplete>' +
341+
'</form>';
342+
343+
var element = compile(template, scope);
344+
var input = element.find('input');
345+
346+
expect(scope.searchText).toBe('');
347+
expect(scope.testForm.$valid).toBe(true);
348+
349+
scope.$apply('searchText = "Exceeded"');
350+
351+
expect(scope.testForm.$valid).toBe(false);
352+
353+
element.remove();
354+
}));
355+
356+
it('should not clear the view value if the input is invalid', inject(function($timeout) {
357+
var scope = createScope(null, {inputId: 'custom-input-id'});
358+
var template =
359+
'<form name="testForm">' +
360+
'<md-autocomplete ' +
361+
'md-input-id="{{inputId}}" ' +
362+
'md-input-maxlength="2" ' +
363+
'md-input-name="testAutocomplete" ' +
364+
'md-selected-item="selectedItem" ' +
365+
'md-search-text="searchText" ' +
366+
'md-items="item in match(searchText)" ' +
367+
'md-item-text="item.display" ' +
368+
'tabindex="3"' +
369+
'md-floating-label="Favorite state">' +
370+
'<span md-highlight-text="searchText">{{item.display}}</span>' +
371+
'</md-autocomplete>' +
372+
'</form>';
373+
374+
var element = compile(template, scope);
375+
var input = element.find('input');
376+
377+
expect(scope.searchText).toBe('');
378+
expect(scope.testForm.$valid).toBe(true);
379+
380+
input.val('Exceeded');
381+
input.triggerHandler('change');
382+
scope.$digest();
383+
384+
expect(scope.testForm.$valid).toBe(false);
385+
expect(scope.searchText).toBe('Exceeded');
386+
387+
element.remove();
388+
}));
389+
390+
});
391+
323392
describe('md-escape-options checks', function() {
324393
var scope, ctrl, element;
325394
var template = '\

src/components/autocomplete/js/autocompleteDirective.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ function MdAutocomplete ($$mdSvgRegistry) {
245245
ng-maxlength="inputMaxlength"\
246246
ng-disabled="$mdAutocompleteCtrl.isDisabled"\
247247
ng-model="$mdAutocompleteCtrl.scope.searchText"\
248+
ng-model-options="{ allowInvalid: true }"\
248249
ng-keydown="$mdAutocompleteCtrl.keydown($event)"\
249250
ng-blur="$mdAutocompleteCtrl.blur()"\
250251
' + (attr.mdNoAsterisk != null ? 'md-no-asterisk="' + attr.mdNoAsterisk + '"' : '') + '\

0 commit comments

Comments
 (0)