Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/chips): emitting end event multiple times when holding down key #29894

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -183,7 +183,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
Loading