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

Commit

Permalink
fix(mdChips): Run duplicate check after mdOnAppend.
Browse files Browse the repository at this point in the history
The duplicate check was firing before the user's onAppend
method had a chance to run, so the check would always
compare a string against an object and fail.

fixes #2748. closes #4113.
  • Loading branch information
topherfangio authored and ThomasBurleson committed Aug 13, 2015
1 parent 6726eca commit 44ab072
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
30 changes: 27 additions & 3 deletions src/components/chips/chips.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ describe('<md-chips>', function() {
expect(scope.items[3].name).toBe('Grape');
expect(scope.items[3].uppername).toBe('GRAPE');
});

it('should disallow duplicate object chips', function() {
var element = buildChips(CHIP_APPEND_TEMPLATE);
var ctrl = element.controller('mdChips');

// Manually set the items
ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];

// Make our custom appendChip function return our existing item
var chipObj = function(chip) {
return ctrl.items[0];
};

scope.appendChip = jasmine.createSpy('appendChip').and.callFake(chipObj);

element.scope().$apply(function() {
ctrl.chipBuffer = 'Apple';
simulateInputEnterKey(ctrl);
});

expect(ctrl.items.length).toBe(1);
expect(scope.appendChip).toHaveBeenCalled();
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
});
});

describe('custom inputs', function() {
Expand Down Expand Up @@ -309,9 +333,9 @@ describe('<md-chips>', function() {
});
});

// *******************************
// Internal helper methods
// *******************************
// *******************************
// Internal helper methods
// *******************************

function buildChips(str) {
var container;
Expand Down
2 changes: 1 addition & 1 deletion src/components/chips/js/chipsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
* @param newChip
*/
MdChipsCtrl.prototype.appendChip = function(newChip) {
if (this.items.indexOf(newChip) + 1) return;
if (this.useMdOnAppend && this.mdOnAppend) {
newChip = this.mdOnAppend({'$chip': newChip});
}
if (this.items.indexOf(newChip) + 1) return;
this.items.push(newChip);
};

Expand Down

0 comments on commit 44ab072

Please sign in to comment.