diff --git a/packages/js/components/changelog/fix-40171 b/packages/js/components/changelog/fix-40171 new file mode 100644 index 00000000000..5e2452b721d --- /dev/null +++ b/packages/js/components/changelog/fix-40171 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Fix invalid focus state of the experimental select control diff --git a/packages/js/components/src/experimental-select-control/menu.scss b/packages/js/components/src/experimental-select-control/menu.scss index 1fa08495c05..0b776a80709 100644 --- a/packages/js/components/src/experimental-select-control/menu.scss +++ b/packages/js/components/src/experimental-select-control/menu.scss @@ -17,13 +17,14 @@ } .woocommerce-experimental-select-control__popover-menu { .components-popover__content { - max-height: 300px; - overflow-y: scroll; + overflow: hidden; } } .woocommerce-experimental-select-control__popover-menu-container { margin: 0; width: 100%; + max-height: 300px; + overflow-y: scroll; > .category-field-dropdown__item:not(:first-child) { .category-field-dropdown__item-content { diff --git a/packages/js/components/src/experimental-select-control/select-control.tsx b/packages/js/components/src/experimental-select-control/select-control.tsx index 691550c06b4..12dd34d9902 100644 --- a/packages/js/components/src/experimental-select-control/select-control.tsx +++ b/packages/js/components/src/experimental-select-control/select-control.tsx @@ -15,6 +15,7 @@ import { useEffect, createElement, Fragment, + useRef, } from '@wordpress/element'; import { chevronDown } from '@wordpress/icons'; @@ -138,11 +139,13 @@ function SelectControl< ItemType = DefaultItemType >( { const instanceId = useInstanceId( SelectControl, 'woocommerce-experimental-select-control' - ); + ) as string; const innerInputClassName = 'woocommerce-experimental-select-control__input'; + const selectControlWrapperRef = useRef< HTMLDivElement >( null ); + let selectedItems = selected === null ? [] : selected; selectedItems = Array.isArray( selectedItems ) ? selectedItems @@ -186,6 +189,7 @@ function SelectControl< ItemType = DefaultItemType >( { openMenu, closeMenu, } = useCombobox< ItemType | null >( { + id: instanceId, initialSelectedItem: singleSelectedItem, inputValue, items: filteredItems, @@ -246,12 +250,15 @@ function SelectControl< ItemType = DefaultItemType >( { } ); const isEventOutside = ( event: React.FocusEvent ) => { - const inputClasses = event?.target?.className; + const selectControlWrapperElement = selectControlWrapperRef.current; + const menuElement = document.getElementById( `${ instanceId }-menu` ); + const parentPopoverMenuElement = menuElement?.closest( + '.woocommerce-experimental-select-control__popover-menu' + ); + return ( - ! document - .querySelector( '.' + instanceId ) - ?.contains( event.relatedTarget ) && - ! inputClasses.includes( innerInputClassName ) + ! selectControlWrapperElement?.contains( event.relatedTarget ) && + ! parentPopoverMenuElement?.contains( event.relatedTarget ) ); }; @@ -276,10 +283,11 @@ function SelectControl< ItemType = DefaultItemType >( { return (
= ( { terms: val, } ); } } - readOnlyWhenClosed={ false } /> ) : ( { const attributeTermInputId = useRef( `woocommerce-attribute-term-field-${ ++uniqueId }`