Fix combobox listbox not scrolling up when new suggestions have loaded (#39588)

This commit is contained in:
diondiondion 2026-06-24 12:46:26 +02:00 committed by Claire
parent a090a9f515
commit ed5f3269e1

View File

@ -3,6 +3,7 @@ import {
forwardRef, forwardRef,
useCallback, useCallback,
useContext, useContext,
useEffect,
useId, useId,
useMemo, useMemo,
useRef, useRef,
@ -276,12 +277,6 @@ const ComboboxWithRef = <Item extends ComboboxItem, GroupKey extends string>(
setShouldMenuOpen(false); setShouldMenuOpen(false);
}, []); }, []);
const resetHighlight = useCallback(() => {
const firstItem = flatItems[0];
const firstItemId = firstItem ? getItemId(firstItem) : null;
setHighlightedItemId(firstItemId);
}, [getItemId, flatItems]);
const highlightItem = useCallback((id: string | null) => { const highlightItem = useCallback((id: string | null) => {
setHighlightedItemId(id); setHighlightedItemId(id);
if (id) { if (id) {
@ -294,6 +289,19 @@ const ComboboxWithRef = <Item extends ComboboxItem, GroupKey extends string>(
} }
}, []); }, []);
const resetHighlight = useCallback(() => {
const firstItem = flatItems[0];
const firstItemId = firstItem ? getItemId(firstItem) : null;
highlightItem(firstItemId);
}, [flatItems, getItemId, highlightItem]);
// Reset scroll & highlight when menu items change
useEffect(() => {
if (flatItems.length) {
resetHighlight();
}
}, [flatItems, resetHighlight]);
const handleFocus: React.FocusEventHandler<HTMLInputElement> = useCallback( const handleFocus: React.FocusEventHandler<HTMLInputElement> = useCallback(
(e) => { (e) => {
if (openOnFocus) { if (openOnFocus) {