1
- import React , { useEffect , useRef , useState } from "react" ;
1
+ import React , { useEffect , useMemo , useRef , useState } from "react" ;
2
2
import { twMerge } from "tailwind-merge" ;
3
3
4
4
import { ArrowDownHeadIcon } from "assets" ;
@@ -55,16 +55,16 @@ const SelectBox = React.forwardRef<HTMLDivElement, SelectBoxProps>((props, ref)
55
55
onKeyDown,
56
56
...other
57
57
} = props ;
58
+ const valueToNameMapping = useMemo ( ( ) => constructValueToNameMapping ( children ) , [ children ] ) ;
58
59
59
60
const [ selectedValue , setSelectedValue ] = useInternalState ( defaultValue , value ) ;
60
- const [ inputValue , setInputValue ] = useState ( "" ) ;
61
+ const [ inputValue , setInputValue ] = useState ( valueToNameMapping . get ( selectedValue || "" ) || "" ) ;
61
62
const [ searchQuery , setSearchQuery ] = useState ( "" ) ;
62
63
const [ isFocused , setIsFocused ] = useState ( false ) ;
63
64
64
65
const dropdownRef = useRef < HTMLDivElement > ( null ) ;
65
66
const inputRef = useRef < HTMLInputElement > ( null ) ;
66
67
const Icon = icon ;
67
- const valueToNameMapping = constructValueToNameMapping ( children ) ;
68
68
const hasSelection = hasValue ( selectedValue ) ;
69
69
70
70
useEffect ( ( ) => {
@@ -85,13 +85,17 @@ const SelectBox = React.forwardRef<HTMLDivElement, SelectBoxProps>((props, ref)
85
85
inputRef . current ?. blur ( ) ;
86
86
} else {
87
87
inputRef . current ?. focus ( ) ;
88
+ if ( inputRef . current ) {
89
+ inputRef . current . selectionStart = inputRef . current . value . length ;
90
+ inputRef . current . selectionEnd = inputRef . current . value . length ;
91
+ }
88
92
}
89
93
setIsFocused ( isFocused ) ;
90
94
} ;
91
95
92
96
const handleValueChange = ( value : string ) => {
93
97
setSearchQuery ( "" ) ;
94
- if ( selectedValue !== undefined ) setInputValue ( valueToNameMapping . get ( selectedValue ) || "" ) ;
98
+ if ( value !== undefined ) setInputValue ( valueToNameMapping . get ( value ) || "" ) ;
95
99
handleFocusChange ( false ) ;
96
100
setSelectedValue ( value ) ;
97
101
inputRef . current ?. blur ( ) ;
0 commit comments