From 23b1fb8984461e5a62739298186de3bc1228f841 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Wed, 12 Jul 2023 11:19:55 -0400 Subject: [PATCH] unset last focused item; #1436 --- src/components/tree/tree.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/components/tree/tree.ts b/src/components/tree/tree.ts index b0dc010b..7522bc10 100644 --- a/src/components/tree/tree.ts +++ b/src/components/tree/tree.ts @@ -87,7 +87,7 @@ export default class SlTree extends ShoelaceElement { // A collection of all the items in the tree, in the order they appear. The collection is live, meaning it is // automatically updated when the underlying document is changed. // - private lastFocusedItem: SlTreeItem; + private lastFocusedItem: SlTreeItem | null; private readonly localize = new LocalizeController(this); private mutationObserver: MutationObserver; private clickTarget: SlTreeItem | null = null; @@ -159,8 +159,13 @@ export default class SlTree extends ShoelaceElement { private handleTreeChanged = (mutations: MutationRecord[]) => { for (const mutation of mutations) { const addedNodes: SlTreeItem[] = [...mutation.addedNodes].filter(SlTreeItem.isTreeItem) as SlTreeItem[]; + const removedNodes = [...mutation.removedNodes].filter(SlTreeItem.isTreeItem) as SlTreeItem[]; addedNodes.forEach(this.initTreeItem); + + if (this.lastFocusedItem && removedNodes.includes(this.lastFocusedItem)) { + this.lastFocusedItem = null; + } } };