Skip to content

Commit

Permalink
Fixed couple of issues on components removal
Browse files Browse the repository at this point in the history
  • Loading branch information
asgvard committed Jul 31, 2023
1 parent 06a9448 commit cbb14ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions src/SpatialNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,8 @@ class SpatialNavigationService {
this.updateParentsHasFocusedChild(this.focusKey, {});
break;
}
currentComponent = this.focusableComponents[currentComponent.parentFocusKey];
currentComponent =
this.focusableComponents[currentComponent.parentFocusKey];
}
}

Expand All @@ -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;

Expand All @@ -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
);
Expand Down

0 comments on commit cbb14ed

Please sign in to comment.