Skip to content

Commit

Permalink
fix: keyboard navigation and scroll in select component
Browse files Browse the repository at this point in the history
  • Loading branch information
timwessman committed Jan 29, 2025
1 parent 071162b commit 5be7261
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion packages/react/src/components/select/components/list/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';

import styles from '../../Select.module.scss';
import { DROPDOWN_MENU_ITEM_HEIGHT, getVisibleGroupLabels } from '../../utils';
Expand Down Expand Up @@ -41,6 +41,41 @@ export const List = () => {
const hasVisibleGroupLabels = getVisibleGroupLabels(groups).length > 0;
const isMultiSelectAndHasGroupLabels = multiSelect && hasVisibleGroupLabels;

useEffect(() => {
let isListenerActive = false;

const onKeyDown = (event: KeyboardEvent) => {
if (['ArrowUp', 'ArrowDown'].includes(event.code)) {
event.preventDefault();
}
};

const onClose = () => {
if (isListenerActive) {
window.removeEventListener('keydown', onKeyDown);
isListenerActive = false;
}
};

const onOpen = () => {
if (!isListenerActive) {
window.addEventListener('keydown', onKeyDown, {
capture: true,
passive: false,
});
isListenerActive = true;
}
};

if (isVisible) {
onOpen();
} else {
onClose();
}

return onClose;
}, [isVisible]);

return (
<div className={classes} style={styleObj}>
<ListComponent
Expand Down

0 comments on commit 5be7261

Please sign in to comment.