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

Commit bd3aa1d

Browse files
Splaktarjosephperrott
authored andcommitted
feat(chips): expose $event in md-on-remove (#11192)
1 parent 9086b54 commit bd3aa1d

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/components/chips/chips.spec.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('<md-chips>', function() {
99
var CHIP_ADD_TEMPLATE =
1010
'<md-chips ng-model="items" md-on-add="addChip($chip, $index)"></md-chips>';
1111
var CHIP_REMOVE_TEMPLATE =
12-
'<md-chips ng-model="items" md-on-remove="removeChip($chip, $index)"></md-chips>';
12+
'<md-chips ng-model="items" md-on-remove="removeChip($chip, $index, $event)"></md-chips>';
1313
var CHIP_SELECT_TEMPLATE =
1414
'<md-chips ng-model="items" md-on-select="selectChip($chip)"></md-chips>';
1515
var CHIP_NG_CHANGE_TEMPLATE =
@@ -194,6 +194,17 @@ describe('<md-chips>', function() {
194194
expect(scope.removeChip.calls.mostRecent().args[1]).toBe(0); // Index
195195
});
196196

197+
it('should make the event available when removing a chip', function() {
198+
var element = buildChips(CHIP_REMOVE_TEMPLATE);
199+
var chips = getChipElements(element);
200+
201+
scope.removeChip = jasmine.createSpy('removeChip');
202+
var chipButton = angular.element(chips[1]).find('button');
203+
chipButton[0].click();
204+
205+
expect(scope.removeChip).toHaveBeenCalled();
206+
expect(scope.removeChip.calls.mostRecent().args[2].type).toBe('click');
207+
});
197208

198209
it('should trigger ng-change on chip addition/removal', function() {
199210
var element = buildChips(CHIP_NG_CHANGE_TEMPLATE);
@@ -1521,16 +1532,16 @@ describe('<md-chips>', function() {
15211532
expect(chips.length).toBe(3);
15221533

15231534
// Remove 'Banana'
1524-
var db = angular.element(chips[1]).find('button');
1525-
db[0].click();
1535+
var chipButton = angular.element(chips[1]).find('button');
1536+
chipButton[0].click();
15261537

15271538
scope.$digest();
15281539
chips = getChipElements(element);
15291540
expect(chips.length).toBe(2);
15301541

15311542
// Remove 'Orange'
1532-
db = angular.element(chips[1]).find('button');
1533-
db[0].click();
1543+
chipButton = angular.element(chips[1]).find('button');
1544+
chipButton[0].click();
15341545

15351546
scope.$digest();
15361547
chips = getChipElements(element);

src/components/chips/js/chipsController.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ MdChipsCtrl.prototype.chipKeydown = function (event) {
369369
event.preventDefault();
370370
// Cancel the delete action only after the event cancel. Otherwise the page will go back.
371371
if (!this.isRemovable()) return;
372-
this.removeAndSelectAdjacentChip(this.selectedChip);
372+
this.removeAndSelectAdjacentChip(this.selectedChip, event);
373373
break;
374374
case this.$mdConstant.KEY_CODE.LEFT_ARROW:
375375
event.preventDefault();
@@ -407,17 +407,18 @@ MdChipsCtrl.prototype.getPlaceholder = function() {
407407

408408
/**
409409
* Removes chip at {@code index} and selects the adjacent chip.
410-
* @param index
410+
* @param {number} index
411+
* @param {Event=} event
411412
*/
412-
MdChipsCtrl.prototype.removeAndSelectAdjacentChip = function(index) {
413+
MdChipsCtrl.prototype.removeAndSelectAdjacentChip = function(index, event) {
413414
var self = this;
414415
var selIndex = self.getAdjacentChipIndex(index);
415416
var wrap = this.$element[0].querySelector('md-chips-wrap');
416417
var chip = this.$element[0].querySelector('md-chip[index="' + index + '"]');
417418

418-
self.removeChip(index);
419+
self.removeChip(index, event);
419420

420-
// The dobule-timeout is currently necessary to ensure that the DOM has finalized and the select()
421+
// The double-timeout is currently necessary to ensure that the DOM has finalized and the select()
421422
// will find the proper chip since the selection is index-based.
422423
//
423424
// TODO: Investigate calling from within chip $scope.$on('$destroy') to reduce/remove timeouts
@@ -592,20 +593,21 @@ MdChipsCtrl.prototype.updateNgModel = function() {
592593

593594
/**
594595
* Removes the chip at the given index.
595-
* @param index
596+
* @param {number} index
597+
* @param {Event=} event
596598
*/
597-
MdChipsCtrl.prototype.removeChip = function(index) {
599+
MdChipsCtrl.prototype.removeChip = function(index, event) {
598600
var removed = this.items.splice(index, 1);
599601

600602
this.updateNgModel();
601603

602604
if (removed && removed.length && this.useOnRemove && this.onRemove) {
603-
this.onRemove({ '$chip': removed[0], '$index': index });
605+
this.onRemove({ '$chip': removed[0], '$index': index, '$event': event });
604606
}
605607
};
606608

607-
MdChipsCtrl.prototype.removeChipAndFocusInput = function (index) {
608-
this.removeChip(index);
609+
MdChipsCtrl.prototype.removeChipAndFocusInput = function (index, $event) {
610+
this.removeChip(index, $event);
609611

610612
if (this.autocompleteCtrl) {
611613
// Always hide the autocomplete dropdown before focusing the autocomplete input.

src/components/chips/js/chipsDirective.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
*
9797
* Please refer to the documentation of this option (below) for more information.
9898
*
99-
* @param {string=|object=} ng-model A model to which the list of items will be bound.
99+
* @param {string|object=} ng-model A model to which the list of items will be bound.
100100
* @param {expression=} ng-change AngularJS expression to be executed on chip addition/removal
101101
* @param {string=} placeholder Placeholder text that will be forwarded to the input.
102102
* @param {string=} secondary-placeholder Placeholder text that will be forwarded to the input,
@@ -122,9 +122,9 @@
122122
* - `undefined` to simply add the `$chip` input string, or
123123
* - `null` to prevent the chip from being appended
124124
* @param {expression=} md-on-add An expression which will be called when a chip has been
125-
* added.
125+
* added with `$chip` and `$index` available as parameters.
126126
* @param {expression=} md-on-remove An expression which will be called when a chip has been
127-
* removed.
127+
* removed with `$chip`, `$index`, and `$event` available as parameters.
128128
* @param {expression=} md-on-select An expression which will be called when a chip is selected.
129129
* @param {boolean} md-require-match If true, and the chips template contains an autocomplete,
130130
* only allow selection of pre-defined chips (i.e. you cannot add new ones).
@@ -232,7 +232,7 @@
232232
<button\
233233
class="md-chip-remove"\
234234
ng-if="$mdChipsCtrl.isRemovable()"\
235-
ng-click="$mdChipsCtrl.removeChipAndFocusInput($$replacedScope.$index)"\
235+
ng-click="$mdChipsCtrl.removeChipAndFocusInput($$replacedScope.$index, $event)"\
236236
type="button"\
237237
tabindex="-1">\
238238
<md-icon md-svg-src="{{ $mdChipsCtrl.mdCloseIcon }}"></md-icon>\

0 commit comments

Comments
 (0)