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

Commit 03db13d

Browse files
JordanMajdThomasBurleson
authored andcommitted
fix(md-chips): appendChip disallows identical objects
* appendChip disallows appending an identical string or an exact object (duplicate object) more than once. It now disallows identical objects from being appended. * appendChips object check loop optimized: md-chips' appendChip uses a filter to loop through items to search for an identical object to chip. This is inefficient since it will keep searching even after it finds a match. Now mdchips appendChip uses a some loop, which stops execution as soon as a match is found. Fixes #4466. Closes #4479.
1 parent c577566 commit 03db13d

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/components/chips/chips.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ describe('<md-chips>', function() {
218218
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
219219
});
220220

221+
it('should disallow identical object chips', function() {
222+
var element = buildChips(CHIP_APPEND_TEMPLATE);
223+
var ctrl = element.controller('mdChips');
224+
225+
ctrl.items = [{name: 'Apple', uppername: 'APPLE'}];
226+
227+
var chipObj = function(chip) {
228+
return {
229+
name: chip,
230+
uppername: chip.toUpperCase()
231+
};
232+
};
233+
scope.appendChip = jasmine.createSpy('appendChip').and.callFake(chipObj);
234+
235+
element.scope().$apply(function() {
236+
ctrl.chipBuffer = 'Apple';
237+
simulateInputEnterKey(ctrl);
238+
});
239+
240+
expect(ctrl.items.length).toBe(1);
241+
expect(scope.appendChip).toHaveBeenCalled();
242+
expect(scope.appendChip.calls.mostRecent().args[0]).toBe('Apple');
243+
});
244+
221245
it('should prevent the default when backspace is pressed', inject(function($mdConstant) {
222246
var element = buildChips(BASIC_CHIP_TEMPLATE);
223247
var ctrl = element.controller('mdChips');

src/components/chips/js/chipsController.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,27 @@ MdChipsCtrl.prototype.getAdjacentChipIndex = function(index) {
192192
* call out to the md-on-append method, if provided
193193
* @param newChip
194194
*/
195-
MdChipsCtrl.prototype.appendChip = function(newChip) {
196-
if (this.useOnAppend && this.onAppend) {
197-
newChip = this.onAppend({'$chip': newChip});
198-
}
199-
if (this.items.indexOf(newChip) + 1) return;
200-
this.items.push(newChip);
201-
};
195+
MdChipsCtrl.prototype.appendChip = function(newChip) {
196+
197+
// If useOnAppend and onAppend function is provided call it.
198+
if (this.useOnAppend && this.onAppend) {
199+
newChip = this.onAppend({'$chip': newChip});
200+
}
201+
202+
// If items contains identical object to newChip do not append
203+
if(angular.isObject(newChip)){
204+
var identical = this.items.some(function(item){
205+
return angular.equals(newChip, item);
206+
});
207+
if(identical) return;
208+
}
209+
210+
// If items contains newChip do not append
211+
if (this.items.indexOf(newChip) + 1) return;
212+
213+
//add newChip to items
214+
this.items.push(newChip);
215+
};
202216

203217
/**
204218
* Sets whether to use the md-on-append expression. This expression is

0 commit comments

Comments
 (0)