Skip to content

fix(material/chips): provide ability to edit for all screen readers with a click on already focused chip #30983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions goldens/material/chips/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ export class MatChipRow extends MatChip implements AfterViewInit {
editable: boolean;
readonly edited: EventEmitter<MatChipEditedEvent>;
// (undocumented)
_handleClick(event: MouseEvent): void;
// (undocumented)
_handleDoubleclick(event: MouseEvent): void;
_handleFocus(): void;
// (undocumented)
Expand All @@ -434,6 +436,8 @@ export class MatChipRow extends MatChip implements AfterViewInit {
// (undocumented)
_isRippleDisabled(): boolean;
// (undocumented)
ngAfterViewInit(): void;
// (undocumented)
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"], ["mat-chip-avatar, [matChipAvatar]", "[matChipEditInput]", "*", "mat-chip-trailing-icon,[matChipRemove],[matChipTrailingIcon]"], true, never>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<MatChipRow, never>;
Expand Down
85 changes: 85 additions & 0 deletions src/material/chips/chip-row.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
provideFakeDirectionality,
} from '@angular/cdk/testing/private';
import {Component, DebugElement, ElementRef, ViewChild} from '@angular/core';
Expand Down Expand Up @@ -234,6 +235,90 @@ describe('Row Chips', () => {
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeTruthy();
});

it('should not begin editing on single click', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchMouseEvent(chipNativeElement, 'click');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
});

it('should begin editing on single click when focused', fakeAsync(() => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
chipNativeElement.focus();

// Need to also simulate the mousedown as that sets the already focused flag.
dispatchMouseEvent(chipNativeElement, 'mousedown');
dispatchMouseEvent(chipNativeElement, 'click');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeTruthy();
}));

describe('when disabled', () => {
beforeEach(() => {
testComponent.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
});

it('should not begin editing on double click', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchFakeEvent(chipNativeElement, 'dblclick');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
});

it('should not begin editing on ENTER', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchKeyboardEvent(chipNativeElement, 'keydown', ENTER);
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
});

it('should not begin editing on single click when focused', fakeAsync(() => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
chipNativeElement.focus();

// Need to also simulate the mousedown as that sets the already focused flag.
dispatchMouseEvent(chipNativeElement, 'mousedown');
dispatchMouseEvent(chipNativeElement, 'click');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
}));
});

describe('when not editable', () => {
beforeEach(() => {
testComponent.editable = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
});

it('should not begin editing on double click', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchFakeEvent(chipNativeElement, 'dblclick');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
});

it('should not begin editing on ENTER', () => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
dispatchKeyboardEvent(chipNativeElement, 'keydown', ENTER);
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
});

it('should not begin editing on single click when focused', fakeAsync(() => {
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
chipNativeElement.focus();

// Need to also simulate the mousedown as that sets the already focused flag.
dispatchMouseEvent(chipNativeElement, 'mousedown');
dispatchMouseEvent(chipNativeElement, 'click');
fixture.detectChanges();
expect(chipNativeElement.querySelector('.mat-chip-edit-input')).toBeFalsy();
}));
});
});

describe('editing behavior', () => {
Expand Down
29 changes: 29 additions & 0 deletions src/material/chips/chip-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface MatChipEditedEvent extends MatChipEvent {
'[attr.aria-description]': 'null',
'[attr.role]': 'role',
'(focus)': '_handleFocus()',
'(click)': '_handleClick($event)',
'(dblclick)': '_handleDoubleclick($event)',
},
providers: [
Expand Down Expand Up @@ -92,6 +93,15 @@ export class MatChipRow extends MatChip implements AfterViewInit {
/** The projected chip edit input. */
@ContentChild(MatChipEditInput) contentEditInput?: MatChipEditInput;

/**
* Set on a mousedown when the chip is already focused via mouse or keyboard.
*
* This allows us to ensure chip is already focused when deciding whether to enter the
* edit mode on a subsequent click. Otherwise, the chip appears focused when handling the
* first click event.
*/
private _alreadyFocused = false;

_isEditing = false;

constructor(...args: unknown[]);
Expand All @@ -104,6 +114,19 @@ export class MatChipRow extends MatChip implements AfterViewInit {
if (this._isEditing && !this._editStartPending) {
this._onEditFinish();
}
this._alreadyFocused = false;
});
}

override ngAfterViewInit() {
super.ngAfterViewInit();

// Sets _alreadyFocused (ahead of click) when chip already has focus.
this._ngZone.runOutsideAngular(() => {
this._elementRef.nativeElement.addEventListener(
'mousedown',
() => (this._alreadyFocused = this._hasFocus()),
);
});
}

Expand Down Expand Up @@ -135,6 +158,12 @@ export class MatChipRow extends MatChip implements AfterViewInit {
}
}

_handleClick(event: MouseEvent) {
if (!this.disabled && this.editable && !this._isEditing && this._alreadyFocused) {
this._startEditing(event);
}
}

_handleDoubleclick(event: MouseEvent) {
if (!this.disabled && this.editable) {
this._startEditing(event);
Expand Down
Loading