From cbb14ed43a84f4540154db07a2fbcff096640e31 Mon Sep 17 00:00:00 2001 From: asgvard Date: Mon, 31 Jul 2023 14:59:58 +0200 Subject: [PATCH] Fixed couple of issues on components removal --- CHANGELOG.md | 5 +++++ src/SpatialNavigation.ts | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5e55ff..d8bb588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# [1.3.3] +## Fixed +- Fixed the issue where component would have kept itself in the array of `parentsHavingFocusedChild` array after removal +- Further improvements to `autoRestoreFocus` logic to trigger not only on Lead components, but also on Parents that had focused child when being removed. Edge case, normally children are removed first. + # [1.3.2] ## Fixed - Fixed a bug where parents were not updating their `hasFocusedChild` when new child is created and focused right away diff --git a/src/SpatialNavigation.ts b/src/SpatialNavigation.ts index ac510ad..763e190 100644 --- a/src/SpatialNavigation.ts +++ b/src/SpatialNavigation.ts @@ -1192,7 +1192,8 @@ class SpatialNavigationService { this.updateParentsHasFocusedChild(this.focusKey, {}); break; } - currentComponent = this.focusableComponents[currentComponent.parentFocusKey]; + currentComponent = + this.focusableComponents[currentComponent.parentFocusKey]; } } @@ -1208,6 +1209,11 @@ class SpatialNavigationService { delete this.focusableComponents[focusKey]; + const hadFocusedChild = this.parentsHavingFocusedChild.includes(focusKey); + this.parentsHavingFocusedChild = this.parentsHavingFocusedChild.filter( + (parentWithFocusedChild) => parentWithFocusedChild !== focusKey + ); + const parentComponent = this.focusableComponents[parentFocusKey]; const isFocused = focusKey === this.focusKey; @@ -1223,11 +1229,19 @@ class SpatialNavigationService { } /** - * If the component was also focused at this time, focus its parent -> it will focus another child + * If the component was also focused at this time, OR had focused child, focus its parent -> it will focus another child + * Normally the order of components unmount is children -> parents, but sometimes parent can be removed before the child + * So we need to check not only for the current Leaf component focus state, but also if it was a Parent that had focused child */ - if (isFocused && parentComponent && parentComponent.autoRestoreFocus) { + if ( + (isFocused || hadFocusedChild) && + parentComponent && + parentComponent.autoRestoreFocus + ) { this.log( 'removeFocusable', + 'Component removed: ', + isFocused ? 'Leaf component' : 'Container component', 'Auto restoring focus to: ', parentFocusKey );