1- import { useCallback , useMemo , useState } from "react" ;
1+ import { useCallback , useMemo , useRef , useState } from "react" ;
22import { useCombobox } from "downshift" ;
33import useDropdownFiltering from "./useDropdownFiltering" ;
44import { type BaseItemData } from "../../../BaseItem" ;
@@ -22,6 +22,7 @@ function useDropdownCombobox<T extends BaseItemData<Record<string, unknown>>>(
2222 id ?: string
2323) {
2424 const [ currentSelectedItem , setCurrentSelectedItem ] = useState < T | null > ( defaultValue || null ) ;
25+ const inputRef = useRef < HTMLInputElement | null > ( null ) ;
2526
2627 // Use controlled value if provided, otherwise use internal state
2728 const selectedItem = value !== undefined ? value : currentSelectedItem ;
@@ -88,6 +89,18 @@ function useDropdownCombobox<T extends BaseItemData<Record<string, unknown>>>(
8889 } ,
8990 [ value , onOptionSelect , filterOptions , onChange ]
9091 ) ,
92+ onStateChange : useCallback (
93+ ( { type } ) => {
94+ // Blur input after selection via click or Enter key
95+ if (
96+ closeMenuOnSelect &&
97+ ( type === useCombobox . stateChangeTypes . ItemClick || type === useCombobox . stateChangeTypes . InputKeyDownEnter )
98+ ) {
99+ inputRef . current ?. blur ( ) ;
100+ }
101+ } ,
102+ [ closeMenuOnSelect ]
103+ ) ,
91104 stateReducer : ( state , actionAndChanges ) => {
92105 switch ( actionAndChanges . type ) {
93106 case useCombobox . stateChangeTypes . InputKeyDownEnter :
@@ -111,7 +124,7 @@ function useDropdownCombobox<T extends BaseItemData<Record<string, unknown>>>(
111124 getToggleButtonProps,
112125 getLabelProps,
113126 getMenuProps,
114- getInputProps,
127+ getInputProps : ( options ?: Parameters < typeof getInputProps > [ 0 ] ) => getInputProps ( { ... options , ref : inputRef } ) ,
115128 getItemProps,
116129 reset : ( ) => {
117130 if ( value === undefined ) {
0 commit comments