Skip to content
Prev Previous commit
Next Next commit
Move keyDown handler into useCallback and use optional chaining
  • Loading branch information
opr committed Jan 11, 2022
commit 2b1dc164f22642d83e6fd48f1b1a3d7ff2ed3939
19 changes: 12 additions & 7 deletions packages/components/src/custom-select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import classnames from 'classnames';
*/
import { Icon, check, chevronDown } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -98,6 +100,15 @@ export default function CustomSelectControl( {
className: 'components-custom-select-control__menu',
'aria-hidden': ! isOpen,
} );

const onKeyDownHandler = useCallback(
( e ) => {
e.stopPropagation();
menuProps?.onKeyDown?.( e );
},
[ menuProps ]
);

// We need this here, because the null active descendant is not fully ARIA compliant.
if (
menuProps[ 'aria-activedescendant' ]?.startsWith( 'downshift-null' )
Expand Down Expand Up @@ -142,13 +153,7 @@ export default function CustomSelectControl( {
/>
</Button>
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
<ul
{ ...menuProps }
onKeyDown={ ( e ) => {
e.stopPropagation();
menuProps.onKeyDown( e );
} }
>
<ul { ...menuProps } onKeyDown={ onKeyDownHandler }>
{ isOpen &&
items.map( ( item, index ) => (
// eslint-disable-next-line react/jsx-key
Expand Down