Skip to content

Commit

Permalink
Fix ComboBox re-opening after selection (#5354)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Nov 3, 2023
1 parent 4dede01 commit 919281f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/@react-stately/combobox/src/useComboBoxState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
}
}, [triggerState, filteredCollection]);

let lastValue = useRef(inputValue);
let [lastValue, setLastValue] = useState(inputValue);
let resetInputValue = () => {
let itemText = collection.getItem(selectedKey)?.textValue ?? '';
lastValue.current = itemText;
setLastValue(itemText);
setInputValue(itemText);
};

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

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

Expand All @@ -228,8 +228,8 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
(props.inputValue === undefined || props.selectedKey === undefined)
) {
resetInputValue();
} else {
lastValue.current = inputValue;
} else if (lastValue !== inputValue) {
setLastValue(inputValue);
}

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

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

1 comment on commit 919281f

@rspbot
Copy link

@rspbot rspbot commented on 919281f Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.