Skip to content

Commit cbb14ed

Browse files
committed
Fixed couple of issues on components removal
1 parent 06a9448 commit cbb14ed

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [1.3.3]
8+
## Fixed
9+
- Fixed the issue where component would have kept itself in the array of `parentsHavingFocusedChild` array after removal
10+
- 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.
11+
712
# [1.3.2]
813
## Fixed
914
- Fixed a bug where parents were not updating their `hasFocusedChild` when new child is created and focused right away

src/SpatialNavigation.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,8 @@ class SpatialNavigationService {
11921192
this.updateParentsHasFocusedChild(this.focusKey, {});
11931193
break;
11941194
}
1195-
currentComponent = this.focusableComponents[currentComponent.parentFocusKey];
1195+
currentComponent =
1196+
this.focusableComponents[currentComponent.parentFocusKey];
11961197
}
11971198
}
11981199

@@ -1208,6 +1209,11 @@ class SpatialNavigationService {
12081209

12091210
delete this.focusableComponents[focusKey];
12101211

1212+
const hadFocusedChild = this.parentsHavingFocusedChild.includes(focusKey);
1213+
this.parentsHavingFocusedChild = this.parentsHavingFocusedChild.filter(
1214+
(parentWithFocusedChild) => parentWithFocusedChild !== focusKey
1215+
);
1216+
12111217
const parentComponent = this.focusableComponents[parentFocusKey];
12121218
const isFocused = focusKey === this.focusKey;
12131219

@@ -1223,11 +1229,19 @@ class SpatialNavigationService {
12231229
}
12241230

12251231
/**
1226-
* If the component was also focused at this time, focus its parent -> it will focus another child
1232+
* If the component was also focused at this time, OR had focused child, focus its parent -> it will focus another child
1233+
* Normally the order of components unmount is children -> parents, but sometimes parent can be removed before the child
1234+
* 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
12271235
*/
1228-
if (isFocused && parentComponent && parentComponent.autoRestoreFocus) {
1236+
if (
1237+
(isFocused || hadFocusedChild) &&
1238+
parentComponent &&
1239+
parentComponent.autoRestoreFocus
1240+
) {
12291241
this.log(
12301242
'removeFocusable',
1243+
'Component removed: ',
1244+
isFocused ? 'Leaf component' : 'Container component',
12311245
'Auto restoring focus to: ',
12321246
parentFocusKey
12331247
);

0 commit comments

Comments
 (0)