Skip to content

Commit

Permalink
fix(material/chips): emitting end event multiple times when holding d…
Browse files Browse the repository at this point in the history
…own key (#29894)

Fixes that that the chip input was emitting the `matChipEnd` event while the user is holding down the separator key.

Fixes #29883.

(cherry picked from commit 5153a5c)
  • Loading branch information
crisbeto committed Oct 18, 2024
1 parent 474f0c1 commit 501faa9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/material/chips/chip-input.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {Directionality} from '@angular/cdk/bidi';
import {COMMA, ENTER, TAB} from '@angular/cdk/keycodes';
import {PlatformModule} from '@angular/cdk/platform';
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
import {
createKeyboardEvent,
dispatchKeyboardEvent,
dispatchEvent,
} from '@angular/cdk/testing/private';
import {Component, DebugElement, ViewChild} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
import {MatFormFieldModule} from '@angular/material/form-field';
Expand Down Expand Up @@ -248,6 +252,20 @@ describe('MatChipInput', () => {

expect(inputNativeElement.getAttribute('aria-describedby')).toBeNull();
}));

it('should not emit chipEnd if the key is repeated', () => {
spyOn(testChipInput, 'add');

chipInputDirective.separatorKeyCodes = [COMMA];
fixture.detectChanges();

const event = createKeyboardEvent('keydown', COMMA);
Object.defineProperty(event, 'repeat', {get: () => true});
dispatchEvent(inputNativeElement, event);
fixture.detectChanges();

expect(testChipInput.add).not.toHaveBeenCalled();
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/material/chips/chip-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class MatChipInput implements MatChipTextControl, OnChanges, OnDestroy {

/** Checks to see if the (chipEnd) event needs to be emitted. */
_emitChipEnd(event?: KeyboardEvent) {
if (!event || this._isSeparatorKey(event)) {
if (!event || (this._isSeparatorKey(event) && !event.repeat)) {
this.chipEnd.emit({
input: this.inputElement,
value: this.inputElement.value,
Expand Down

0 comments on commit 501faa9

Please sign in to comment.