Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function handleKeyDown(e: KeyboardEvent) {
const filteredRefs = refs.filter(ref => ref !== null);
let refToFocus: HTMLElement;
switch (e.keyCode) {
case keyMap.ArrowDown:
refToFocus = refs[(refs.indexOf(focused!) + 1) % refs.length];
refToFocus.focus();
setFocused(refToFocus);
break;
case keyMap.ArrowUp:
refToFocus =
refs[(refs.indexOf(focused!) - 1 + refs.length) % refs.length];
setFocused(refToFocus);
refToFocus.focus();
break;
case keyMap.Tab:
if (!e.shiftKey && trapLastMenuItem(filteredRefs)) {
e.preventDefault();
setFocused(refs[0]);