@@ -22,14 +22,14 @@ export interface TypeaheadSelectOption extends Omit<SelectOptionProps, 'content'
22
22
value : string | number ;
23
23
}
24
24
25
- export interface TypeaheadSelectProps extends Omit < SelectProps , 'toggle' > {
25
+ export interface TypeaheadSelectProps extends Omit < SelectProps , 'toggle' | 'onSelect' > {
26
26
/** @hide Forwarded ref */
27
27
innerRef ?: React . Ref < any > ;
28
28
/** Initial options of the select. */
29
29
initialOptions : TypeaheadSelectOption [ ] ;
30
30
/** Callback triggered on selection. */
31
31
onSelect ?: (
32
- _event : React . MouseEvent < Element , MouseEvent > | React . KeyboardEvent < HTMLInputElement > ,
32
+ _event : React . MouseEvent < Element , MouseEvent > | React . KeyboardEvent < HTMLInputElement > | undefined ,
33
33
selection : string | number
34
34
) => void ;
35
35
/** Callback triggered when the select opens or closes. */
@@ -154,14 +154,19 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
154
154
] ) ;
155
155
156
156
React . useEffect ( ( ) => {
157
+ // If the selected option changed and the current input value is the previously selected item, update the displayed value.
157
158
const selectedOption = initialOptions . find ( ( o ) => o . selected ) ;
158
- setInputValue ( String ( selectedOption ?. content ?? '' ) ) ;
159
+ if ( inputValue === selected && selectedOption ?. value !== selected ) {
160
+ setInputValue ( String ( selectedOption ?. content ?? '' ) ) ;
161
+ }
162
+ // Only update when options change
163
+ // eslint-disable-next-line react-hooks/exhaustive-deps
159
164
} , [ initialOptions ] ) ;
160
165
161
166
const setActiveAndFocusedItem = ( itemIndex : number ) => {
162
167
setFocusedItemIndex ( itemIndex ) ;
163
168
const focusedItem = selectOptions [ itemIndex ] ;
164
- setActiveItemId ( focusedItem . value as string ) ;
169
+ setActiveItemId ( String ( focusedItem . value ) ) ;
165
170
} ;
166
171
167
172
const resetActiveAndFocusedItem = ( ) => {
@@ -291,7 +296,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
291
296
const onToggleClick = ( ) => {
292
297
onToggle && onToggle ( ! isOpen ) ;
293
298
setIsOpen ( ! isOpen ) ;
294
- textInputRef ? .current ?. focus ( ) ;
299
+ textInputRef . current ?. focus ( ) ;
295
300
} ;
296
301
297
302
const onClearButtonClick = ( ) => {
@@ -300,7 +305,7 @@ export const TypeaheadSelectBase: React.FunctionComponent<TypeaheadSelectProps>
300
305
onInputChange && onInputChange ( '' ) ;
301
306
setFilterValue ( '' ) ;
302
307
resetActiveAndFocusedItem ( ) ;
303
- textInputRef ? .current ?. focus ( ) ;
308
+ textInputRef . current ?. focus ( ) ;
304
309
onClearSelection && onClearSelection ( ) ;
305
310
} ;
306
311
0 commit comments