Skip to content

Commit 74b4165

Browse files
authored
Fix missing checks related to getForcedFocusKey (#217)
1 parent 4a67d75 commit 74b4165

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'@noriginmedia/norigin-spatial-navigation-core': patch
3+
---
4+
- Fixed missing null checks for `getForcedFocusKey` in `smartNavigate` and `setFocus`

packages/core/src/SpatialNavigation.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,14 @@ class SpatialNavigationService {
835835
this.onEnterRelease();
836836
}
837837

838-
if (this.focusKey && (
839-
eventType === DIRECTION_LEFT ||
840-
eventType === DIRECTION_RIGHT ||
841-
eventType === DIRECTION_UP ||
842-
eventType === DIRECTION_DOWN)) {
843-
this.onArrowRelease(eventType)
838+
if (
839+
this.focusKey &&
840+
(eventType === DIRECTION_LEFT ||
841+
eventType === DIRECTION_RIGHT ||
842+
eventType === DIRECTION_UP ||
843+
eventType === DIRECTION_DOWN)
844+
) {
845+
this.onArrowRelease(eventType);
844846
}
845847
};
846848

@@ -1025,7 +1027,18 @@ class SpatialNavigationService {
10251027
* the Focusable Containers, that have "forceFocus" flag enabled.
10261028
*/
10271029
if (!fromParentFocusKey && !currentComponent) {
1028-
this.setFocus(this.getForcedFocusKey());
1030+
const forcedKey = this.getForcedFocusKey();
1031+
1032+
if (forcedKey) {
1033+
this.setFocus(forcedKey);
1034+
} else {
1035+
this.log(
1036+
'smartNavigate',
1037+
'Aborted due to missing current component and force-focusable key'
1038+
);
1039+
}
1040+
1041+
// Current component is null (e.g. nothing to navigate from)
10291042
return;
10301043
}
10311044

@@ -1663,10 +1676,21 @@ class SpatialNavigationService {
16631676
if (!focusKey || focusKey === ROOT_FOCUS_KEY) {
16641677
// eslint-disable-next-line no-param-reassign
16651678
focusKey = this.getForcedFocusKey();
1679+
1680+
// If there is no force-focusable key either then we abort
1681+
if (!focusKey) {
1682+
this.log('setFocus', 'Aborted due to missing force-focusable key');
1683+
return;
1684+
}
16661685
}
16671686

16681687
const newFocusKey = this.getNextFocusKey(focusKey);
16691688

1689+
if (!newFocusKey) {
1690+
this.log('setFocus', 'Aborted due to missing next focus key');
1691+
return;
1692+
}
1693+
16701694
this.log('setFocus', 'newFocusKey', newFocusKey);
16711695

16721696
this.setCurrentFocusedKey(newFocusKey, focusDetails);

0 commit comments

Comments
 (0)