From 897963ba1310fad155020bdb43738730d82f1283 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 19 Dec 2024 15:17:51 -0600 Subject: [PATCH] fix(react): fix a11y active-descendant issue with controlled Listboxes (#1776) --- packages/react/src/components/Listbox/Listbox.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/react/src/components/Listbox/Listbox.tsx b/packages/react/src/components/Listbox/Listbox.tsx index def6c872e..4aeabaa8c 100644 --- a/packages/react/src/components/Listbox/Listbox.tsx +++ b/packages/react/src/components/Listbox/Listbox.tsx @@ -104,15 +104,19 @@ const Listbox = forwardRef< ) ); setSelectedOptions(matchingOptions); - setActiveOption(matchingOptions[0] || null); + if (!activeOption) { + setActiveOption(matchingOptions[0] || null); + } } else { const matchingOption = options.find((option) => optionMatchesValue(option, listboxValue) ); setSelectedOptions(matchingOption ? [matchingOption] : []); - setActiveOption(matchingOption || null); + if (!activeOption) { + setActiveOption(matchingOption || null); + } } - }, [isControlled, options, value, defaultValue]); + }, [isControlled, options, value, defaultValue, activeOption]); useEffect(() => { if (activeOption) {