Skip to content

Commit 4ceac36

Browse files
committed
refactor(material/chips): run event through renderer
Fixes an event in the chip row that wasn't registered through the renderer. (cherry picked from commit e36cf43)
1 parent 7832307 commit 4ceac36

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

goldens/material/chips/index.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ export class MatChipRemove extends MatChipAction {
435435
}
436436

437437
// @public
438-
export class MatChipRow extends MatChip implements AfterViewInit {
438+
export class MatChipRow extends MatChip implements AfterViewInit, OnDestroy {
439439
constructor(...args: unknown[]);
440440
// (undocumented)
441441
protected basicChipAttrName: string;
@@ -464,6 +464,8 @@ export class MatChipRow extends MatChip implements AfterViewInit {
464464
// (undocumented)
465465
ngAfterViewInit(): void;
466466
// (undocumented)
467+
ngOnDestroy(): void;
468+
// (undocumented)
467469
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipRow, "mat-chip-row, [mat-chip-row], mat-basic-chip-row, [mat-basic-chip-row]", never, { "editable": { "alias": "editable"; "required": false; }; }, { "edited": "edited"; }, ["contentEditInput"], ["[matChipEdit]", "mat-chip-avatar, [matChipAvatar]", "[matChipEditInput]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
468470
// (undocumented)
469471
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipRow, never>;

src/material/chips/chip-row.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ import {
1414
ContentChild,
1515
EventEmitter,
1616
Input,
17+
OnDestroy,
1718
Output,
19+
Renderer2,
1820
ViewChild,
1921
ViewEncapsulation,
2022
afterNextRender,
23+
inject,
2124
} from '@angular/core';
2225
import {takeUntil} from 'rxjs/operators';
2326
import {MatChip, MatChipEvent} from './chip';
@@ -72,8 +75,10 @@ export interface MatChipEditedEvent extends MatChipEvent {
7275
changeDetection: ChangeDetectionStrategy.OnPush,
7376
imports: [MatChipAction, MatChipEditInput],
7477
})
75-
export class MatChipRow extends MatChip implements AfterViewInit {
78+
export class MatChipRow extends MatChip implements AfterViewInit, OnDestroy {
7679
protected override basicChipAttrName = 'mat-basic-chip-row';
80+
private _renderer = inject(Renderer2);
81+
private _cleanupMousedown: (() => void) | undefined;
7782

7883
/**
7984
* The editing action has to be triggered in a timeout. While we're waiting on it, a blur
@@ -123,12 +128,16 @@ export class MatChipRow extends MatChip implements AfterViewInit {
123128
super.ngAfterViewInit();
124129

125130
// Sets _alreadyFocused (ahead of click) when chip already has focus.
126-
this._ngZone.runOutsideAngular(() => {
127-
this._elementRef.nativeElement.addEventListener(
128-
'mousedown',
129-
() => (this._alreadyFocused = this._hasFocus()),
130-
);
131-
});
131+
this._cleanupMousedown = this._ngZone.runOutsideAngular(() =>
132+
this._renderer.listen(this._elementRef.nativeElement, 'mousedown', () => {
133+
this._alreadyFocused = this._hasFocus();
134+
}),
135+
);
136+
}
137+
138+
override ngOnDestroy(): void {
139+
super.ngOnDestroy();
140+
this._cleanupMousedown?.();
132141
}
133142

134143
protected _hasLeadingActionIcon() {

0 commit comments

Comments
 (0)