mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
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:
committed by
Jeremy Elbourn
parent
0c62798296
commit
20a09301e9
@@ -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() {}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user