diff --git a/packages/components/src/spectrum/picker/usePickerScrollOnOpen.ts b/packages/components/src/spectrum/picker/usePickerScrollOnOpen.ts index 6e0c5bc0e6..dd0587efce 100644 --- a/packages/components/src/spectrum/picker/usePickerScrollOnOpen.ts +++ b/packages/components/src/spectrum/picker/usePickerScrollOnOpen.ts @@ -4,11 +4,12 @@ import { findSpectrumPickerScrollArea, usePopoverOnScrollRef, } from '@deephaven/react-hooks'; +import type { MenuTriggerAction } from '../comboBox'; export interface UsePickerScrollOnOpenOptions { getInitialScrollPosition?: () => Promise; onScroll: (event: Event) => void; - onOpenChange?: (isOpen: boolean) => void; + onOpenChange?: (isOpen: boolean, menuTrigger?: MenuTriggerAction) => void; } export interface UsePickerScrollOnOpenResult { @@ -37,11 +38,11 @@ export function usePickerScrollOnOpen({ ); const onOpenChangeInternal = useCallback( - (isOpen: boolean): void => { + (isOpen: boolean, menuTrigger?: MenuTriggerAction): void => { // Attach scroll event handling popoverOnOpenChange(isOpen); - onOpenChange?.(isOpen); + onOpenChange?.(isOpen, menuTrigger); }, [onOpenChange, popoverOnOpenChange] ); diff --git a/packages/jsapi-components/src/spectrum/ComboBox.tsx b/packages/jsapi-components/src/spectrum/ComboBox.tsx index f139b8b2cb..73a3168b18 100644 --- a/packages/jsapi-components/src/spectrum/ComboBox.tsx +++ b/packages/jsapi-components/src/spectrum/ComboBox.tsx @@ -19,37 +19,32 @@ export function ComboBox(props: ComboBoxProps): JSX.Element { ...pickerProps } = usePickerProps(props); - const menuTriggerActionRef = useRef(); + const isOpenRef = useRef(false); + const inputValueRef = useRef(''); const onInputChange = useCallback( (value: string) => { - onInputChangeInternal?.(value); - - onSearchTextChange(value); + inputValueRef.current = value; - // const searchText = menuTriggerActionRef.current === 'input' ? value : ''; - - console.log('[TESTING]', menuTriggerActionRef.current, value); + onInputChangeInternal?.(value); - // We want the ComboBox to show all items whenever it is initially opened, - // so keep search text set to empty string while it is closed. This is - // mostly to handle the intial state, since `onInputChange` gets called - // before the user has interacted. - // onSearchTextChange(isOpenRef.current ? value : ''); + // Clear search text when ComboBox is closed + onSearchTextChange(isOpenRef.current ? value : ''); }, [onInputChangeInternal, onSearchTextChange] ); const onOpenChange = useCallback( (isOpen: boolean, menuTrigger?: MenuTriggerAction) => { - console.log('[TESTING] onOpenChange', isOpen, menuTrigger); - menuTriggerActionRef.current = isOpen ? menuTrigger : undefined; + isOpenRef.current = isOpen; pickerProps.onOpenChange?.(isOpen); - // Clear filtering on close so that all items show on next open - if (!isOpen) { - onSearchTextChange(''); + // Restore search text when ComboBox is being opened if menu trigger was + // from user input. Otherwise we don't want to show all items on initial + // open. + if (isOpen && menuTrigger === 'input') { + onSearchTextChange(inputValueRef.current); } }, [onSearchTextChange, pickerProps]