Skip to content
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
26 changes: 26 additions & 0 deletions src/material/chips/chip-grid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,32 @@ describe('MatChipGrid', () => {
expect(chipGridNativeElement.getAttribute('tabindex')).toBe('-1');
});


it('should silently update the active item without stealing document focus when a focused chip is destroyed', fakeAsync(() => {
const fixture = createComponent(StandardChipGrid);
fixture.detectChanges();


chips.first.focus();
fixture.detectChanges();


const activeElementBeforeDeletion = document.activeElement;


fixture.componentInstance.foods.splice(0, 1);
fixture.detectChanges();
flush();


expect(chipGridInstance._keyManager.activeItemIndex).toBe(0);


expect(document.activeElement).not.toBe(chips.toArray()[1].nativeElement);
}));


});
describe('on chip destroy', () => {
it('should focus the next item', () => {
const fixture = createComponent(StandardChipGrid);
Expand Down
28 changes: 28 additions & 0 deletions src/material/chips/chip-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,34 @@ export class MatChipGrid
this.stateChanges.next();
}

protected override _redirectDestroyedChipFocus() {
if (this._lastDestroyedFocusedChipIndex === null) {
return;
}

if (this._chips.length) {
const newIndex =
Math.min(this._lastDestroyedFocusedChipIndex, this._chips.length - 1);
const chipToFocus = this._chips.toArray()[newIndex];

if (chipToFocus.disabled) {
if (this._chips.length === 1) {
this.focus();
} else {
this._keyManager.setPreviousItemActive();
}
} else {
// SILENT UPDATE: Fixes stale reference without stealing browser focus
this._keyManager.updateActiveItem(chipToFocus._getActions()[0]);
}
} else {
// No chips left. Clear the active item silently.
this._keyManager.updateActiveItem(-1);
}

this._lastDestroyedFocusedChipIndex = null;
}

_focusLastChip() {
if (this._chips.length) {
this._chips.last.focus();
Expand Down
4 changes: 2 additions & 2 deletions src/material/chips/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class MatChipSet implements AfterViewInit, OnDestroy {
private _dir = inject(Directionality, {optional: true});

/** Index of the last destroyed chip that had focus. */
private _lastDestroyedFocusedChipIndex: number | null = null;
protected _lastDestroyedFocusedChipIndex: number | null = null;

/** Used to manage focus within the chip list. */
protected _keyManager!: FocusKeyManager<MatChipAction>;
Expand Down Expand Up @@ -310,7 +310,7 @@ export class MatChipSet implements AfterViewInit, OnDestroy {
* Finds the next appropriate chip to move focus to,
* if the currently-focused chip is destroyed.
*/
private _redirectDestroyedChipFocus() {
protected _redirectDestroyedChipFocus() {
if (this._lastDestroyedFocusedChipIndex == null) {
return;
}
Expand Down
Loading