Skip to content

Commit fb0251f

Browse files
committed
use mousedown instead of timer on focus
1 parent 427233d commit fb0251f

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/material/chips/chip-row.spec.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
dispatchEvent,
66
dispatchFakeEvent,
77
dispatchKeyboardEvent,
8+
dispatchMouseEvent,
89
} from '@angular/cdk/testing/private';
910
import {Component, DebugElement, ElementRef, ViewChild} from '@angular/core';
1011
import {ComponentFixture, TestBed, fakeAsync, flush, waitForAsync} from '@angular/core/testing';
@@ -248,19 +249,18 @@ describe('Row Chips', () => {
248249

249250
it('should not begin editing on single click', () => {
250251
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
251-
dispatchKeyboardEvent(chipNativeElement, 'click');
252+
dispatchMouseEvent(chipNativeElement, 'click');
252253
fixture.detectChanges();
253254
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
254255
});
255256

256257
it('should begin editing on single click when focused', fakeAsync(() => {
257258
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
258-
259259
chipNativeElement.focus();
260-
flush();
261-
fixture.detectChanges();
262260

263-
dispatchKeyboardEvent(chipNativeElement, 'click');
261+
// Need to also simulate the mousedown as that sets the already focused flag.
262+
dispatchMouseEvent(chipNativeElement, 'mousedown');
263+
dispatchMouseEvent(chipNativeElement, 'click');
264264
fixture.detectChanges();
265265
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeTruthy();
266266
}));
@@ -288,12 +288,11 @@ describe('Row Chips', () => {
288288

289289
it('should not begin editing on single click when focused', fakeAsync(() => {
290290
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
291-
292291
chipNativeElement.focus();
293-
flush();
294-
fixture.detectChanges();
295292

296-
dispatchKeyboardEvent(chipNativeElement, 'click');
293+
// Need to also simulate the mousedown as that sets the already focused flag.
294+
dispatchMouseEvent(chipNativeElement, 'mousedown');
295+
dispatchMouseEvent(chipNativeElement, 'click');
297296
fixture.detectChanges();
298297
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
299298
}));
@@ -322,12 +321,11 @@ describe('Row Chips', () => {
322321

323322
it('should not begin editing on single click when focused', fakeAsync(() => {
324323
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
325-
326324
chipNativeElement.focus();
327-
flush();
328-
fixture.detectChanges();
329325

330-
dispatchKeyboardEvent(chipNativeElement, 'click');
326+
// Need to also simulate the mousedown as that sets the already focused flag.
327+
dispatchMouseEvent(chipNativeElement, 'mousedown');
328+
dispatchMouseEvent(chipNativeElement, 'click');
331329
fixture.detectChanges();
332330
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
333331
}));

src/material/chips/chip-row.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface MatChipEditedEvent extends MatChipEvent {
6060
'[attr.aria-description]': 'null',
6161
'[attr.role]': 'role',
6262
'(focus)': '_handleFocus()',
63+
'(mousedown)': '_handleMouseDown($event)',
6364
'(click)': '_handleClick($event)',
6465
'(dblclick)': '_handleDoubleclick($event)',
6566
},
@@ -94,7 +95,7 @@ export class MatChipRow extends MatChip implements AfterViewInit {
9495
@ContentChild(MatChipEditInput) contentEditInput?: MatChipEditInput;
9596

9697
/**
97-
* Set with a timeout when the chips has been focused via mouse or keyboard.
98+
* Set on a mousedown when the chip is already focused via mouse or keyboard.
9899
*
99100
* This allows us to ensure chip is already focused when deciding whether to enter the
100101
* edit mode on a subsequent click. Otherwise, the chip appears focused when handling the
@@ -116,14 +117,6 @@ export class MatChipRow extends MatChip implements AfterViewInit {
116117
}
117118
this._alreadyFocused = false;
118119
});
119-
this._onFocus.pipe(takeUntil(this.destroyed)).subscribe(() => {
120-
if (!this._isEditing && !this.disabled) {
121-
// Need a timeout to ensure flag not set while handling the first click.
122-
this._ngZone.runOutsideAngular(() => {
123-
setTimeout(() => (this._alreadyFocused = true), 100);
124-
});
125-
}
126-
});
127120
}
128121

129122
override _hasTrailingIcon() {
@@ -154,6 +147,11 @@ export class MatChipRow extends MatChip implements AfterViewInit {
154147
}
155148
}
156149

150+
/** Sets _alreadyFocused (ahead of click) when chip already has focus. */
151+
_handleMouseDown(event: MouseEvent) {
152+
this._alreadyFocused = this._hasFocus();
153+
}
154+
157155
_handleClick(event: MouseEvent) {
158156
if (!this.disabled && this.editable && !this._isEditing && this._alreadyFocused) {
159157
this._startEditing(event);

0 commit comments

Comments
 (0)