Skip to content

Commit 919281f

Browse files
authored
Fix ComboBox re-opening after selection (#5354)
1 parent 4dede01 commit 919281f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/@react-stately/combobox/src/useComboBoxState.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
165165
}
166166
}, [triggerState, filteredCollection]);
167167

168-
let lastValue = useRef(inputValue);
168+
let [lastValue, setLastValue] = useState(inputValue);
169169
let resetInputValue = () => {
170170
let itemText = collection.getItem(selectedKey)?.textValue ?? '';
171-
lastValue.current = itemText;
171+
setLastValue(itemText);
172172
setInputValue(itemText);
173173
};
174174

@@ -183,7 +183,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
183183
isFocused &&
184184
(filteredCollection.size > 0 || allowsEmptyCollection) &&
185185
!triggerState.isOpen &&
186-
inputValue !== lastValue.current &&
186+
inputValue !== lastValue &&
187187
menuTrigger !== 'manual'
188188
) {
189189
open(null, 'input');
@@ -209,7 +209,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
209209
}
210210

211211
// Clear focused key when input value changes and display filtered collection again.
212-
if (inputValue !== lastValue.current) {
212+
if (inputValue !== lastValue) {
213213
selectionManager.setFocusedKey(null);
214214
setShowAllItems(false);
215215

@@ -228,8 +228,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
228228
(props.inputValue === undefined || props.selectedKey === undefined)
229229
) {
230230
resetInputValue();
231-
} else {
232-
lastValue.current = inputValue;
231+
} else if (lastValue !== inputValue) {
232+
setLastValue(inputValue);
233233
}
234234

235235
// Update the inputValue if the selected item's text changes from its last tracked value.
@@ -239,7 +239,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
239239
let selectedItemText = collection.getItem(selectedKey)?.textValue ?? '';
240240
if (!isFocused && selectedKey != null && props.inputValue === undefined && selectedKey === lastSelectedKey.current) {
241241
if (lastSelectedKeyText.current !== selectedItemText) {
242-
lastValue.current = selectedItemText;
242+
setLastValue(selectedItemText);
243243
setInputValue(selectedItemText);
244244
}
245245
}
@@ -275,7 +275,7 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
275275

276276
// Stop menu from reopening from useEffect
277277
let itemText = collection.getItem(selectedKey)?.textValue ?? '';
278-
lastValue.current = itemText;
278+
setLastValue(itemText);
279279
closeMenu();
280280
} else {
281281
// If only a single aspect of combobox is controlled, reset input value and close menu for the user

0 commit comments

Comments
 (0)