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

Commit 72264af

Browse files
devversionmmalerba
authored andcommitted
fix(chips): add-on-blur with autocomplete (#9949)
* Properly adds the selected item of the autocomplete to the chips when `md-add-on-blur` is enabled * No longer adds chips on blur when `md-require-match` is enabled (previously user could add non-matches to the chips) * Moves logic into an extra function to keep blur listener clear. Fixes #9582.
1 parent eac3bfb commit 72264af

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed

src/components/chips/chips.spec.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,66 @@ describe('<md-chips>', function() {
310310
expect(scope.items.length).toBe(4);
311311
});
312312

313+
it('should not append a new chip when requireMatch is enabled', function() {
314+
var template =
315+
'<md-chips ng-model="items" md-add-on-blur="true" md-require-match="true">' +
316+
'<md-autocomplete ' +
317+
'md-selected-item="selectedItem" ' +
318+
'md-search-text="searchText" ' +
319+
'md-items="item in querySearch(searchText)" ' +
320+
'md-item-text="item">' +
321+
'<span md-highlight-text="searchText">{{item}}</span>' +
322+
'</md-autocomplete>' +
323+
'</md-chips>';
324+
325+
setupScopeForAutocomplete();
326+
327+
var element = buildChips(template);
328+
var ctrl = element.controller('mdChips');
329+
var input = element.find('input');
330+
331+
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
332+
333+
// Flush the initial timeout of the md-autocomplete.
334+
$timeout.flush();
335+
336+
scope.$apply('searchText = "Hello"');
337+
338+
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
339+
});
340+
341+
it('should not append a new chip on blur when the autocomplete is showing', function() {
342+
var template =
343+
'<md-chips ng-model="items" md-add-on-blur="true">' +
344+
'<md-autocomplete ' +
345+
'md-selected-item="selectedItem" ' +
346+
'md-search-text="searchText" ' +
347+
'md-items="item in querySearch(searchText)" ' +
348+
'md-item-text="item">' +
349+
'<span md-highlight-text="searchText">{{item}}</span>' +
350+
'</md-autocomplete>' +
351+
'</md-chips>';
352+
353+
setupScopeForAutocomplete();
354+
355+
var element = buildChips(template);
356+
var ctrl = element.controller('mdChips');
357+
var input = element.find('input');
358+
359+
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
360+
361+
// Flush the initial timeout of the md-autocomplete.
362+
$timeout.flush();
363+
364+
var autocompleteCtrl = element.find('md-autocomplete').controller('mdAutocomplete');
365+
366+
// Open the dropdown by searching for a possible item and focusing the input.
367+
scope.$apply('searchText = "Ki"');
368+
autocompleteCtrl.focus();
369+
370+
expect(ctrl.shouldAddOnBlur()).toBeFalsy();
371+
});
372+
313373
});
314374

315375
describe('when removable', function() {

src/components/chips/js/chipsController.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,20 +534,8 @@ MdChipsCtrl.prototype.onInputFocus = function () {
534534
MdChipsCtrl.prototype.onInputBlur = function () {
535535
this.inputHasFocus = false;
536536

537-
var chipBuffer = this.getChipBuffer().trim();
538-
539-
// Update the custom chip validators.
540-
this.validateModel();
541-
542-
var isModelValid = this.ngModelCtrl.$valid;
543-
544-
if (this.userInputNgModelCtrl) {
545-
isModelValid &= this.userInputNgModelCtrl.$valid;
546-
}
547-
548-
// Only append the chip and reset the chip buffer if the chips and input ngModel is valid.
549-
if (this.addOnBlur && chipBuffer && isModelValid) {
550-
this.appendChip(chipBuffer);
537+
if (this.shouldAddOnBlur()) {
538+
this.appendChip(this.getChipBuffer().trim());
551539
this.resetChipBuffer();
552540
}
553541
};
@@ -602,6 +590,26 @@ MdChipsCtrl.prototype.configureAutocomplete = function(ctrl) {
602590
}
603591
};
604592

593+
/**
594+
* Whether the current chip buffer should be added on input blur or not.
595+
* @returns {boolean}
596+
*/
597+
MdChipsCtrl.prototype.shouldAddOnBlur = function() {
598+
599+
// Update the custom ngModel validators from the chips component.
600+
this.validateModel();
601+
602+
var chipBuffer = this.getChipBuffer().trim();
603+
var isModelValid = this.ngModelCtrl.$valid;
604+
var isAutocompleteShowing = this.autocompleteCtrl && !this.autocompleteCtrl.hidden;
605+
606+
if (this.userInputNgModelCtrl) {
607+
isModelValid = isModelValid && this.userInputNgModelCtrl.$valid;
608+
}
609+
610+
return this.addOnBlur && !this.requireMatch && chipBuffer && isModelValid && !isAutocompleteShowing;
611+
};
612+
605613
MdChipsCtrl.prototype.hasFocus = function () {
606614
return this.inputHasFocus || this.selectedChip >= 0;
607615
};

0 commit comments

Comments
 (0)