From 9280ad3948a52e737bc23abc94ed098ed311afd9 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 26 Sep 2024 09:11:19 +0200 Subject: [PATCH] fix(material/chips): chip grid not re-focusing first item There was some logic that assumed that if a chip is the active item in the key manager, it must have focus. That's incorrect since the active item isn't reset on blur. This prevented the chip grid from re-focusing the first chip when the user tabs into it a second time. These changes add a `focus` call whenever the grid receives focus. Fixes #29785. (cherry picked from commit 2861a306aebc54eafe4a72b4a53860d8612af5d4) --- src/material/chips/chip-grid.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/material/chips/chip-grid.ts b/src/material/chips/chip-grid.ts index 60cb8f6af3bd..176e8100d592 100644 --- a/src/material/chips/chip-grid.ts +++ b/src/material/chips/chip-grid.ts @@ -340,8 +340,14 @@ export class MatChipGrid // Delay until the next tick, because this can cause a "changed after checked" // error if the input does something on focus (e.g. opens an autocomplete). Promise.resolve().then(() => this._chipInput.focus()); - } else if (this._chips.length && this._keyManager.activeItemIndex !== 0) { - this._keyManager.setFirstItemActive(); + } else { + const activeItem = this._keyManager.activeItem; + + if (activeItem) { + activeItem.focus(); + } else { + this._keyManager.setFirstItemActive(); + } } this.stateChanges.next();