Skip to content

Commit d5053dd

Browse files
authored
fix(react-templates): fix TypeaheadSelect state synchronization issue (#12147)
The closeMenu() function was overwriting inputValue with stale state when called from selectOption(), causing displayed value to lag one selection behind. Additionally, filterValue was not being cleared when closing without selecting, causing the filter to persist and hide options on subsequent opens. Modified closeMenu() to accept shouldRestoreInput parameter to distinguish between: - Selection flow: Skip restoration (value already set correctly) - Cancellation flow: Restore to previous selected value and clear filter Fixes #12146
1 parent cf8be69 commit d5053dd

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/react-templates/src/components/Select/TypeaheadSelect.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,20 @@ export const TypeaheadSelectBase: FunctionComponent<TypeaheadSelectProps> = ({
195195
}
196196
};
197197

198-
const closeMenu = () => {
198+
const closeMenu = (shouldRestoreInput = true) => {
199199
onToggle && onToggle(false);
200200
setIsOpen(false);
201201
resetActiveAndFocusedItem();
202-
const option = initialOptions.find((o) => o.value === selected);
203-
if (option) {
204-
setInputValue(String(option.content));
202+
203+
// Only restore input value when user cancels (doesn't select)
204+
// Don't restore when explicitly selecting an option (handled by selectOption)
205+
if (shouldRestoreInput) {
206+
const option = initialOptions.find((o) => o.value === selected);
207+
if (option) {
208+
setInputValue(String(option.content));
209+
}
210+
// Clear filter when closing without selecting to show all options on next open
211+
setFilterValue('');
205212
}
206213
};
207214

@@ -223,7 +230,7 @@ export const TypeaheadSelectBase: FunctionComponent<TypeaheadSelectProps> = ({
223230
setFilterValue('');
224231
setSelected(String(option.value));
225232

226-
closeMenu();
233+
closeMenu(false); // Don't restore - we just set the correct value above
227234
};
228235

229236
const _onSelect = (_event: ReactMouseEvent<Element, MouseEvent> | undefined, value: string | number | undefined) => {

0 commit comments

Comments
 (0)