Skip to content

Commit 0efaa08

Browse files
committed
fix(material/chips): don't prevent default mousedown action
The chip row was calling `preventDefault` on `mousedown` events in order to avoid sending focus to the chip grid which in turn moves it to the first chip. This is problematic, because preventing the default `mousedown` action also stops native drag&drop and `click` events. These changes resolve to original issue by making the chip focusable on clicks and forwarding focus through there.
1 parent 1eb83e5 commit 0efaa08

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@ describe('MDC-based Row Chips', () => {
9696
expect(testComponent.chipRemove).toHaveBeenCalledWith({chip: chipInstance});
9797
});
9898

99-
it('should prevent the default click action', () => {
100-
const event = dispatchFakeEvent(chipNativeElement, 'mousedown');
101-
fixture.detectChanges();
102-
103-
expect(event.defaultPrevented).toBe(true);
99+
it('should have a tabidex', () => {
100+
expect(chipNativeElement.getAttribute('tabindex')).toBe('-1');
104101
});
105102

106103
it('should have the correct role', () => {
@@ -189,8 +186,8 @@ describe('MDC-based Row Chips', () => {
189186
});
190187

191188
describe('focus management', () => {
192-
it('sends focus to first grid cell on mousedown', () => {
193-
dispatchFakeEvent(chipNativeElement, 'mousedown');
189+
it('sends focus to first grid cell on root chip focus', () => {
190+
dispatchFakeEvent(chipNativeElement, 'focus');
194191
fixture.detectChanges();
195192

196193
expect(document.activeElement).toHaveClass('mdc-evolution-chip__action--primary');

src/material/chips/chip-row.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,14 @@ export interface MatChipEditedEvent extends MatChipEvent {
6262
'[class.mat-mdc-chip-highlighted]': 'highlighted',
6363
'[class.mat-mdc-chip-with-trailing-icon]': '_hasTrailingIcon()',
6464
'[id]': 'id',
65-
'[attr.tabindex]': 'null',
65+
// Has to have a negative tabindex in order to capture
66+
// focus and redirect it to the primary action.
67+
'[attr.tabindex]': 'disabled ? null : -1',
6668
'[attr.aria-label]': 'null',
6769
'[attr.aria-description]': 'null',
6870
'[attr.role]': 'role',
69-
'(mousedown)': '_mousedown($event)',
70-
'(dblclick)': '_doubleclick($event)',
71+
'(focus)': '_handleFocus($event)',
72+
'(dblclick)': '_handleDoubleclick($event)',
7173
},
7274
providers: [
7375
{provide: MatChip, useExisting: MatChipRow},
@@ -137,13 +139,9 @@ export class MatChipRow extends MatChip implements AfterViewInit {
137139
}
138140

139141
/** Sends focus to the first gridcell when the user clicks anywhere inside the chip. */
140-
_mousedown(event: MouseEvent) {
141-
if (!this._isEditing) {
142-
if (!this.disabled) {
143-
this.focus();
144-
}
145-
146-
event.preventDefault();
142+
_handleFocus() {
143+
if (!this._isEditing && !this.disabled) {
144+
this.focus();
147145
}
148146
}
149147

@@ -163,7 +161,7 @@ export class MatChipRow extends MatChip implements AfterViewInit {
163161
}
164162
}
165163

166-
_doubleclick(event: MouseEvent) {
164+
_handleDoubleclick(event: MouseEvent) {
167165
if (!this.disabled && this.editable) {
168166
this._startEditing(event);
169167
}

tools/public_api_guard/material/chips.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,17 @@ export class MatChipRow extends MatChip implements AfterViewInit {
392392
contentEditInput?: MatChipEditInput;
393393
defaultEditInput?: MatChipEditInput;
394394
// (undocumented)
395-
_doubleclick(event: MouseEvent): void;
396-
// (undocumented)
397395
editable: boolean;
398396
readonly edited: EventEmitter<MatChipEditedEvent>;
399397
// (undocumented)
398+
_handleDoubleclick(event: MouseEvent): void;
399+
_handleFocus(): void;
400+
// (undocumented)
400401
_handleKeydown(event: KeyboardEvent): void;
401402
// (undocumented)
402403
_hasTrailingIcon(): boolean;
403404
// (undocumented)
404405
_isEditing: boolean;
405-
_mousedown(event: MouseEvent): void;
406406
// (undocumented)
407407
static ɵcmp: i0.ɵɵComponentDeclaration<MatChipRow, "mat-chip-row, [mat-chip-row], mat-basic-chip-row, [mat-basic-chip-row]", never, { "color": "color"; "disabled": "disabled"; "disableRipple": "disableRipple"; "tabIndex": "tabIndex"; "editable": "editable"; }, { "edited": "edited"; }, ["contentEditInput"], ["mat-chip-avatar, [matChipAvatar]", "*", "[matChipEditInput]", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], false, never>;
408408
// (undocumented)

0 commit comments

Comments
 (0)