Skip to content

Commit 69796c2

Browse files
Kyle Owsenmmalerba
Kyle Owsen
authored andcommitted
Stop arrow key navigation through matChipRemove directives from deleting mdc-chip-rows.
1 parent f92e0df commit 69796c2

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/material-experimental/mdc-chips/chip-row.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Directionality} from '@angular/cdk/bidi';
2-
import {BACKSPACE, DELETE} from '@angular/cdk/keycodes';
2+
import {BACKSPACE, DELETE, RIGHT_ARROW} from '@angular/cdk/keycodes';
33
import {
44
createKeyboardEvent,
55
createFakeEvent,
@@ -9,14 +9,15 @@ import {Component, DebugElement, ViewChild} from '@angular/core';
99
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
1010
import {By} from '@angular/platform-browser';
1111
import {Subject} from 'rxjs';
12-
import {MatChipEvent, MatChipGrid, MatChipRow, MatChipsModule} from './index';
12+
import {MatChipEvent, MatChipGrid, MatChipRemove, MatChipRow, MatChipsModule} from './index';
1313

1414

1515
describe('MDC-based Row Chips', () => {
1616
let fixture: ComponentFixture<any>;
1717
let chipDebugElement: DebugElement;
1818
let chipNativeElement: HTMLElement;
1919
let chipInstance: MatChipRow;
20+
let removeIconInstance: MatChipRemove;
2021

2122
let dir = 'ltr';
2223

@@ -46,6 +47,9 @@ describe('MDC-based Row Chips', () => {
4647
chipNativeElement = chipDebugElement.nativeElement;
4748
chipInstance = chipDebugElement.injector.get<MatChipRow>(MatChipRow);
4849
testComponent = fixture.debugElement.componentInstance;
50+
51+
const removeIconDebugElement = fixture.debugElement.query(By.directive(MatChipRemove))!;
52+
removeIconInstance = removeIconDebugElement.injector.get<MatChipRemove>(MatChipRemove);
4953
});
5054

5155
describe('basic behaviors', () => {
@@ -135,6 +139,21 @@ describe('MDC-based Row Chips', () => {
135139

136140
expect(testComponent.chipRemove).toHaveBeenCalled();
137141
});
142+
143+
it('arrow key navigation does not emit the (removed) event', () => {
144+
const ARROW_KEY_EVENT = createKeyboardEvent('keydown', RIGHT_ARROW) as KeyboardEvent;
145+
146+
spyOn(testComponent, 'chipRemove');
147+
148+
removeIconInstance.interaction.next(ARROW_KEY_EVENT);
149+
fixture.detectChanges();
150+
151+
const fakeEvent = createFakeEvent('transitionend');
152+
(fakeEvent as any).propertyName = 'width';
153+
chipNativeElement.dispatchEvent(fakeEvent);
154+
155+
expect(testComponent.chipRemove).not.toHaveBeenCalled();
156+
});
138157
});
139158

140159
describe('when removable is false', () => {
@@ -219,6 +238,7 @@ describe('MDC-based Row Chips', () => {
219238
(focus)="chipFocus($event)" (destroyed)="chipDestroy($event)"
220239
(removed)="chipRemove($event)">
221240
{{name}}
241+
<button matChipRemove>x</button>
222242
</mat-chip-row>
223243
<input matInput [matChipInputFor]="chipGrid">
224244
</div>

src/material-experimental/mdc-chips/chip-row.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ViewEncapsulation
1818
} from '@angular/core';
1919
import {MatChip} from './chip';
20-
import {GridKeyManagerRow, NAVIGATION_KEYS} from './grid-key-manager';
20+
import {GridKeyManagerRow} from './grid-key-manager';
2121

2222

2323
/**
@@ -61,9 +61,6 @@ export class MatChipRow extends MatChip implements AfterContentInit, AfterViewIn
6161
/** The focusable grid cells for this row. Implemented as part of GridKeyManagerRow. */
6262
cells!: HTMLElement[];
6363

64-
/** Key codes for which this component has a custom handler. */
65-
HANDLED_KEYS: Set<number> = new Set([...NAVIGATION_KEYS, BACKSPACE, DELETE]);
66-
6764
ngAfterContentInit() {
6865
super.ngAfterContentInit();
6966

src/material-experimental/mdc-chips/chip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
131131
/** Emits when the chip is blurred. */
132132
readonly _onBlur = new Subject<MatChipEvent>();
133133

134-
readonly HANDLED_KEYS: Set<number> = new Set([SPACE, ENTER]);
134+
readonly REMOVE_ICON_HANDLED_KEYS: Set<number> = new Set([SPACE, ENTER]);
135135

136136
/** Whether this chip is a basic (unstyled) chip. */
137137
readonly _isBasicChip: boolean;
@@ -385,7 +385,7 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
385385
const isKeyboardEvent = event.type.startsWith('key');
386386

387387
if (this.disabled || (isKeyboardEvent &&
388-
!this.HANDLED_KEYS.has((event as KeyboardEvent).keyCode))) {
388+
!this.REMOVE_ICON_HANDLED_KEYS.has((event as KeyboardEvent).keyCode))) {
389389
return;
390390
}
391391

0 commit comments

Comments
 (0)