fix(chips): able to remove disabled chip via remove button (#15732)

Fixes users being able to remove disabled chips by clicking on their remove button.

Fixes #15708.
This commit is contained in:
Kristiyan Kostadinov
2019-05-14 18:57:51 +02:00
committed by Jeremy Elbourn
parent 0c62798296
commit 20a09301e9
2 changed files with 30 additions and 7 deletions
+26 -5
View File
@@ -31,33 +31,54 @@ describe('Chip Remove', () => {
describe('basic behavior', () => {
it('should applies the `mat-chip-remove` CSS class', () => {
let hrefElement = chipNativeElement.querySelector('a')!;
let buttonElement = chipNativeElement.querySelector('button')!;
expect(hrefElement.classList).toContain('mat-chip-remove');
expect(buttonElement.classList).toContain('mat-chip-remove');
});
it('should emits (removed) on click', () => {
let hrefElement = chipNativeElement.querySelector('a')!;
let buttonElement = chipNativeElement.querySelector('button')!;
testChip.removable = true;
fixture.detectChanges();
spyOn(testChip, 'didRemove');
hrefElement.click();
buttonElement.click();
fixture.detectChanges();
expect(testChip.didRemove).toHaveBeenCalled();
});
it('should not remove if parent chip is disabled', () => {
let buttonElement = chipNativeElement.querySelector('button')!;
testChip.disabled = true;
testChip.removable = true;
fixture.detectChanges();
spyOn(testChip, 'didRemove');
buttonElement.click();
fixture.detectChanges();
expect(testChip.didRemove).not.toHaveBeenCalled();
});
});
});
@Component({
template: `
<mat-chip [removable]="removable" (removed)="didRemove()"><a matChipRemove></a></mat-chip>
<mat-chip
[removable]="removable"
[disabled]="disabled"
(removed)="didRemove()"><button matChipRemove></button></mat-chip>
`
})
class TestChip {
removable: boolean;
disabled = false;
didRemove() {}
}
+4 -2
View File
@@ -394,8 +394,10 @@ export class MatChipRemove {
/** Calls the parent chip's public `remove()` method if applicable. */
_handleClick(event: Event): void {
if (this._parentChip.removable) {
this._parentChip.remove();
const parentChip = this._parentChip;
if (parentChip.removable && !parentChip.disabled) {
parentChip.remove();
}
// We need to stop event propagation because otherwise the event will bubble up to the