Skip to content

Commit

Permalink
Updated comments (DH-18088)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Jan 3, 2025
1 parent 92af58b commit 56ee44c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/jsapi-components/src/spectrum/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,34 @@ export function ComboBox(props: ComboBoxProps): JSX.Element {

const onInputChange = useCallback(
(value: string) => {
inputValueRef.current = value;

onInputChangeInternal?.(value);

// Clear search text when ComboBox is closed
onSearchTextChange(isOpenRef.current ? value : '');
// Only apply search text if ComboBox is open. Note that `onInputChange`
// fires before `onOpenChange`, so we have to check `isOpenRef` to see the
// last value set by `onOpenChange`.
if (isOpenRef.current) {
onSearchTextChange(value);
} else {
// If the ComboBox is closed, reset the search text but store the value
// so it can be re-applied if the ComboBox is opened by user input.
onSearchTextChange('');
inputValueRef.current = value;
}
},
[onInputChangeInternal, onSearchTextChange]
);

const onOpenChange = useCallback(
(isOpen: boolean, menuTrigger?: MenuTriggerAction) => {
isOpenRef.current = isOpen;

pickerProps.onOpenChange?.(isOpen);

// Restore search text when ComboBox is being opened if menu trigger was
// from user input.
// Restore search text when ComboBox has been opened by user input.
if (isOpen && menuTrigger === 'input') {
onSearchTextChange(inputValueRef.current);
}

// Store the open state so that `onInputChange` has access to it.
isOpenRef.current = isOpen;
},
[onSearchTextChange, pickerProps]
);
Expand Down

0 comments on commit 56ee44c

Please sign in to comment.