Skip to content

fix(material/chips): prevent default behavior on remove button #24722

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

Merged
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
3 changes: 2 additions & 1 deletion src/material-experimental/mdc-chips/chip-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ export class MatChipAction
/** Whether the action is interactive. */
@Input() isInteractive = true;

_handleClick(_event: MouseEvent) {
_handleClick(event: MouseEvent) {
// Usually these events can't happen while the chip is disabled since the browser won't
// allow them which is what MDC seems to rely on, however the event can be faked in tests.
if (!this.disabled && this.isInteractive) {
this._foundation.handleClick();
event.preventDefault();
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/material-experimental/mdc-chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component} from '@angular/core';
import {waitForAsync, ComponentFixture, TestBed, fakeAsync, flush} from '@angular/core/testing';
import {dispatchKeyboardEvent} from '@angular/cdk/testing/private';
import {dispatchKeyboardEvent, dispatchMouseEvent} from '@angular/cdk/testing/private';
import {By} from '@angular/platform-browser';
import {SPACE, ENTER} from '@angular/cdk/keycodes';
import {MDCChipAnimation, MDCChipCssClasses} from '@material/chips/chip';
Expand Down Expand Up @@ -121,6 +121,14 @@ describe('MDC-based Chip Remove', () => {
const buttonElement = chipNativeElement.querySelector('.mdc-evolution-chip__icon--trailing')!;
expect(buttonElement.classList.contains('mat-mdc-focus-indicator')).toBe(true);
}));

it('should prevent the default click action', fakeAsync(() => {
const buttonElement = chipNativeElement.querySelector('button')!;
const event = dispatchMouseEvent(buttonElement, 'click');
triggerRemoveSequence();

expect(event.defaultPrevented).toBe(true);
}));
});
});

Expand Down
9 changes: 9 additions & 0 deletions src/material/chips/chip-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {waitForAsync, ComponentFixture, TestBed} from '@angular/core/testing';
import {MatChip, MatChipsModule} from './index';
import {dispatchMouseEvent} from '@angular/cdk/testing/private';

describe('Chip Remove', () => {
let fixture: ComponentFixture<TestChip>;
Expand Down Expand Up @@ -74,6 +75,14 @@ describe('Chip Remove', () => {

expect(testChip.didRemove).not.toHaveBeenCalled();
});

it('should prevent the default click action', () => {
const buttonElement = chipNativeElement.querySelector('button')!;
const event = dispatchMouseEvent(buttonElement, 'click');
fixture.detectChanges();

expect(event.defaultPrevented).toBe(true);
});
});
});

Expand Down
1 change: 1 addition & 0 deletions src/material/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,6 @@ export class MatChipRemove {
// the parent click listener of the `MatChip` would prevent propagation, but it can happen
// that the chip is being removed before the event bubbles up.
event.stopPropagation();
event.preventDefault();
}
}