@@ -13,6 +13,7 @@ import {
13
13
PopoverContent ,
14
14
PopoverTrigger ,
15
15
} from "@/components/ui/popover" ;
16
+ import { useDebounce } from "@/hooks/use-debounce" ;
16
17
import { cn } from "@/lib/utils" ;
17
18
import type React from "react" ;
18
19
import { useEffect , useRef , useState } from "react" ;
@@ -42,13 +43,25 @@ export const LinearCombobox = ({
42
43
const [ searchValue , setSearchValue ] = useState ( "" ) ;
43
44
const onValueChangeRef = useRef ( onValueChange ) ;
44
45
const isSearching = searchValue . length > 0 ;
45
-
46
+ const { debouncedValue } = useDebounce ( searchValue ) ;
46
47
useEffect ( ( ) => {
47
48
if ( selectedOption && onValueChangeRef . current ) {
48
49
onValueChangeRef . current ( selectedOption ) ;
49
50
}
50
51
} , [ selectedOption ] ) ;
51
52
53
+ useEffect ( ( ) => {
54
+ if ( Number . parseInt ( debouncedValue ) < options . length ) {
55
+ const possibleOption = options [ Number . parseInt ( debouncedValue ) ] ;
56
+ if ( possibleOption ) {
57
+ setSelectedOption ( possibleOption ) ;
58
+ setOpenPopover ( false ) ;
59
+ setSearchValue ( "" ) ;
60
+ return ;
61
+ }
62
+ }
63
+ } , [ debouncedValue , options ] ) ;
64
+
52
65
return (
53
66
< Popover open = { openPopover } onOpenChange = { setOpenPopover } modal >
54
67
< PopoverTrigger asChild >
@@ -85,19 +98,7 @@ export const LinearCombobox = ({
85
98
< Command className = "rounded-lg relative" >
86
99
< CommandInput
87
100
value = { searchValue }
88
- onValueChange = { ( searchValue ) => {
89
- // If the user types a number, then set as the selected option
90
- if ( Number . parseInt ( searchValue ) < options . length ) {
91
- const possibleOption = options [ Number . parseInt ( searchValue ) ] ;
92
- if ( possibleOption ) {
93
- setSelectedOption ( possibleOption ) ;
94
- setOpenPopover ( false ) ;
95
- setSearchValue ( "" ) ;
96
- return ;
97
- }
98
- }
99
- setSearchValue ( searchValue ) ;
100
- } }
101
+ onValueChange = { setSearchValue }
101
102
className = "text-[0.8125rem] leading-normal"
102
103
placeholder = "Type Option no"
103
104
/>
@@ -131,7 +132,6 @@ export const LinearCombobox = ({
131
132
</ div >
132
133
</ CommandItem >
133
134
) ) }
134
-
135
135
{ children && (
136
136
< >
137
137
< CommandSeparator />
0 commit comments