Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit a1a0deb

Browse files
fix(chips): Fix browser back nav in FF when removing chip with… (#5537)
PiperOrigin-RevId: 292049726 Co-authored-by: Material Web Copybara Robot <[email protected]>
1 parent e41ed61 commit a1a0deb

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

packages/mdc-chips/chip/foundation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ export class MDCChipFoundation extends MDCFoundation<MDCChipAdapter> {
310310

311311
private removeChip_(evt: MouseEvent|KeyboardEvent) {
312312
evt.stopPropagation();
313+
// Prevent default behavior for backspace on Firefox which causes a page
314+
// navigation.
315+
evt.preventDefault();
313316
if (this.shouldRemoveOnTrailingIconClick_) {
314317
this.beginExit();
315318
}

packages/mdc-chips/chip/test/foundation.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,27 +379,31 @@ describe('MDCChipFoundation', () => {
379379
const mockEvt = {
380380
type: 'click',
381381
stopPropagation: jasmine.createSpy('stopPropagation'),
382+
preventDefault: jasmine.createSpy('preventDefault'),
382383
key: '',
383384
};
384385

385386
foundation.handleTrailingIconInteraction(mockEvt);
386387
expect(mockAdapter.notifyTrailingIconInteraction)
387388
.toHaveBeenCalledTimes(1);
388389
expect(mockEvt.stopPropagation).toHaveBeenCalledTimes(1);
390+
expect(mockEvt.preventDefault).toHaveBeenCalledTimes(1);
389391

390392
mockEvt.type = 'keydown';
391393
mockEvt.key = ' ';
392394
foundation.handleTrailingIconInteraction(mockEvt);
393395
expect(mockAdapter.notifyTrailingIconInteraction)
394396
.toHaveBeenCalledTimes(2);
395397
expect(mockEvt.stopPropagation).toHaveBeenCalledTimes(2);
398+
expect(mockEvt.preventDefault).toHaveBeenCalledTimes(2);
396399

397400
mockEvt.type = 'keydown';
398401
mockEvt.key = 'Enter';
399402
foundation.handleTrailingIconInteraction(mockEvt);
400403
expect(mockAdapter.notifyTrailingIconInteraction)
401404
.toHaveBeenCalledTimes(3);
402405
expect(mockEvt.stopPropagation).toHaveBeenCalledTimes(3);
406+
expect(mockEvt.preventDefault).toHaveBeenCalledTimes(3);
403407
});
404408

405409
it(`#handleTrailingIconInteraction adds ${
@@ -409,13 +413,15 @@ describe('MDCChipFoundation', () => {
409413
const mockEvt = {
410414
type: 'click',
411415
stopPropagation: jasmine.createSpy('stopPropagation'),
416+
preventDefault: jasmine.createSpy('preventDefault'),
412417
};
413418

414419
foundation.handleTrailingIconInteraction(mockEvt);
415420

416421
expect(foundation.getShouldRemoveOnTrailingIconClick()).toBe(true);
417422
expect(mockAdapter.addClass).toHaveBeenCalledWith(cssClasses.CHIP_EXIT);
418423
expect(mockEvt.stopPropagation).toHaveBeenCalled();
424+
expect(mockEvt.preventDefault).toHaveBeenCalled();
419425
});
420426

421427
it(`#handleTrailingIconInteraction does not add ${
@@ -426,6 +432,7 @@ describe('MDCChipFoundation', () => {
426432
const mockEvt = {
427433
type: 'click',
428434
stopPropagation: jasmine.createSpy('stopPropagation'),
435+
preventDefault: jasmine.createSpy('preventDefault'),
429436
};
430437

431438
foundation.setShouldRemoveOnTrailingIconClick(false);
@@ -435,6 +442,7 @@ describe('MDCChipFoundation', () => {
435442
expect(mockAdapter.addClass)
436443
.not.toHaveBeenCalledWith(cssClasses.CHIP_EXIT);
437444
expect(mockEvt.stopPropagation).toHaveBeenCalled();
445+
expect(mockEvt.preventDefault).toHaveBeenCalled();
438446
});
439447

440448
it('#handleKeydown emits custom event with appropriate keys', () => {

0 commit comments

Comments
 (0)