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

Commit 051474e

Browse files
devversionThomasBurleson
authored andcommitted
fix(chips): fix md-max-chips for autocomplete watcher
The chips didn't check for the maxmium, when using an autocomplete. That's why the chip always got appended, when reached the chips max. Fixes #7549. Closes #7550
1 parent c3b498f commit 051474e

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/components/chips/chips.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,56 @@ describe('<md-chips>', function() {
556556
expect(ctrl.chipBuffer).toBe('Test 2');
557557
expect(scope.items.length).not.toBe(2);
558558
});
559+
560+
it('should not append the chip when maximum is reached and using an autocomplete', function() {
561+
var template =
562+
'<md-chips ng-model="items" md-max-chips="1">' +
563+
'<md-autocomplete ' +
564+
'md-selected-item="selectedItem" ' +
565+
'md-search-text="searchText" ' +
566+
'md-items="item in querySearch(searchText)" ' +
567+
'md-item-text="item">' +
568+
'<span md-highlight-text="searchText">{{itemtype}}</span>' +
569+
'</md-autocomplete>' +
570+
'</md-chips>';
571+
572+
setupScopeForAutocomplete();
573+
var element = buildChips(template);
574+
var ctrl = element.controller('mdChips');
575+
576+
// Flush the autocompletes init timeout.
577+
$timeout.flush();
578+
579+
var autocompleteCtrl = element.find('md-autocomplete').controller('mdAutocomplete');
580+
581+
element.scope().$apply(function() {
582+
autocompleteCtrl.scope.searchText = 'K';
583+
});
584+
585+
element.scope().$apply(function() {
586+
autocompleteCtrl.select(0);
587+
});
588+
589+
$timeout.flush();
590+
591+
expect(scope.items.length).toBe(1);
592+
expect(scope.items[0]).toBe('Kiwi');
593+
expect(element.find('input').val()).toBe('');
594+
595+
element.scope().$apply(function() {
596+
autocompleteCtrl.scope.searchText = 'O';
597+
});
598+
599+
element.scope().$apply(function() {
600+
autocompleteCtrl.select(0);
601+
});
602+
603+
$timeout.flush();
604+
605+
expect(scope.items.length).toBe(1);
606+
expect(element.find('input').val()).toBe('Orange');
607+
});
608+
559609
});
560610

561611
describe('focus functionality', function() {

src/components/chips/js/chipsController.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ MdChipsCtrl.prototype.inputKeydown = function(event) {
139139
event.preventDefault();
140140

141141
// Only append the chip and reset the chip buffer if the max chips limit isn't reached.
142-
if (this.items.length >= this.maxChips) return;
142+
if (this.hasMaxChipsReached()) return;
143143

144144
this.appendChip(chipBuffer);
145145
this.resetChipBuffer();
@@ -358,7 +358,7 @@ MdChipsCtrl.prototype.resetChipBuffer = function() {
358358
}
359359
};
360360

361-
MdChipsCtrl.prototype.hasMaxChips = function() {
361+
MdChipsCtrl.prototype.hasMaxChipsReached = function() {
362362
if (angular.isString(this.maxChips)) this.maxChips = parseInt(this.maxChips, 10) || 0;
363363

364364
return this.maxChips > 0 && this.items.length >= this.maxChips;
@@ -368,7 +368,7 @@ MdChipsCtrl.prototype.hasMaxChips = function() {
368368
* Updates the validity properties for the ngModel.
369369
*/
370370
MdChipsCtrl.prototype.validateModel = function() {
371-
this.ngModelCtrl.$setValidity('md-max-chips', !this.hasMaxChips());
371+
this.ngModelCtrl.$setValidity('md-max-chips', !this.hasMaxChipsReached());
372372
};
373373

374374
/**
@@ -504,10 +504,14 @@ MdChipsCtrl.prototype.configureUserInput = function(inputElement) {
504504
};
505505

506506
MdChipsCtrl.prototype.configureAutocomplete = function(ctrl) {
507-
if ( ctrl ){
507+
if ( ctrl ) {
508508
this.hasAutocomplete = true;
509+
509510
ctrl.registerSelectedItemWatcher(angular.bind(this, function (item) {
510511
if (item) {
512+
// Only append the chip and reset the chip buffer if the max chips limit isn't reached.
513+
if (this.hasMaxChipsReached()) return;
514+
511515
this.appendChip(item);
512516
this.resetChipBuffer();
513517
}

0 commit comments

Comments
 (0)