Skip to content

Commit ad1fa14

Browse files
authored
fix: key code being undefined in some enviroments (#97)
* Use keyboard event's 'code' as a fallback for 'keyCode' * correct types * set correct type for setKeyMap method
1 parent f5efd9c commit ad1fa14

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

src/SpatialNavigation.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { DebouncedFunc } from 'lodash';
2+
import debounce from 'lodash/debounce';
3+
import difference from 'lodash/difference';
24
import filter from 'lodash/filter';
3-
import first from 'lodash/first';
4-
import sortBy from 'lodash/sortBy';
55
import findKey from 'lodash/findKey';
6+
import first from 'lodash/first';
67
import forEach from 'lodash/forEach';
78
import forOwn from 'lodash/forOwn';
9+
import sortBy from 'lodash/sortBy';
810
import throttle from 'lodash/throttle';
9-
import debounce from 'lodash/debounce';
10-
import difference from 'lodash/difference';
11-
import measureLayout, { getBoundingClientRect } from './measureLayout';
1211
import VisualDebugger from './VisualDebugger';
12+
import measureLayout, { getBoundingClientRect } from './measureLayout';
1313

1414
const DIRECTION_LEFT = 'left';
1515
const DIRECTION_RIGHT = 'right';
@@ -20,11 +20,11 @@ const KEY_ENTER = 'enter';
2020
export type Direction = 'up' | 'down' | 'left' | 'right';
2121

2222
const DEFAULT_KEY_MAP = {
23-
[DIRECTION_LEFT]: [37],
24-
[DIRECTION_UP]: [38],
25-
[DIRECTION_RIGHT]: [39],
26-
[DIRECTION_DOWN]: [40],
27-
[KEY_ENTER]: [13]
23+
[DIRECTION_LEFT]: [37, 'ArrowLeft'],
24+
[DIRECTION_UP]: [38, 'ArrowUp'],
25+
[DIRECTION_RIGHT]: [39, 'ArrowRight'],
26+
[DIRECTION_DOWN]: [40, 'ArrowDown'],
27+
[KEY_ENTER]: [13, 'Enter']
2828
};
2929

3030
export const ROOT_FOCUS_KEY = 'SN:ROOT';
@@ -130,9 +130,11 @@ export interface FocusDetails {
130130
[key: string]: any;
131131
}
132132

133-
export type BackwardsCompatibleKeyMap = { [index: string]: number | number[] };
133+
export type BackwardsCompatibleKeyMap = {
134+
[index: string]: string | number | (number | string)[];
135+
};
134136

135-
export type KeyMap = { [index: string]: number[] };
137+
export type KeyMap = { [index: string]: (string | number)[] };
136138

137139
const getChildClosestToOrigin = (children: FocusableComponent[]) => {
138140
const childrenClosestToOrigin = sortBy(
@@ -151,11 +153,7 @@ const normalizeKeyMap = (keyMap: BackwardsCompatibleKeyMap) => {
151153
const newKeyMap: KeyMap = {};
152154

153155
Object.entries(keyMap).forEach(([key, value]) => {
154-
if (typeof value === 'number') {
155-
newKeyMap[key] = [value];
156-
} else if (Array.isArray(value)) {
157-
newKeyMap[key] = value;
158-
}
156+
newKeyMap[key] = Array.isArray(value) ? value : [value];
159157
});
160158

161159
return newKeyMap;
@@ -656,10 +654,14 @@ class SpatialNavigationService {
656654
}
657655
}
658656

659-
getEventType(keyCode: number) {
657+
getEventType(keyCode: number | string) {
660658
return findKey(this.getKeyMap(), (codeList) => codeList.includes(keyCode));
661659
}
662660

661+
static getKeyCode(event: KeyboardEvent) {
662+
return event.keyCode || event.code;
663+
}
664+
663665
bindEventHandlers() {
664666
// We check both because the React Native remote debugger implements window, but not window.addEventListener.
665667
if (typeof window !== 'undefined' && window.addEventListener) {
@@ -672,7 +674,8 @@ class SpatialNavigationService {
672674
this.logIndex += 1;
673675
}
674676

675-
const eventType = this.getEventType(event.keyCode);
677+
const keyCode = SpatialNavigationService.getKeyCode(event);
678+
const eventType = this.getEventType(keyCode);
676679

677680
if (!eventType) {
678681
return;
@@ -720,7 +723,8 @@ class SpatialNavigationService {
720723

721724
// When throttling then make sure to only throttle key down and cancel any queued functions in case of key up
722725
this.keyUpEventListener = (event: KeyboardEvent) => {
723-
const eventType = this.getEventType(event.keyCode);
726+
const keyCode = SpatialNavigationService.getKeyCode(event);
727+
const eventType = this.getEventType(keyCode);
724728

725729
delete this.pressedKeys[eventType];
726730

@@ -857,8 +861,9 @@ class SpatialNavigationService {
857861
this.visualDebugger.clear();
858862
}
859863

864+
const keyCode = SpatialNavigationService.getKeyCode(event);
860865
const direction = findKey(this.getKeyMap(), (codeList) =>
861-
codeList.includes(event.keyCode)
866+
codeList.includes(keyCode)
862867
);
863868

864869
this.smartNavigate(direction, null, { event });

0 commit comments

Comments
 (0)