@@ -22,7 +22,7 @@ import type { Option } from './useCombobox';
22
22
import { useCombobox } from './useCombobox' ;
23
23
import { useComboboxKeyboard } from './useComboboxKeyboard' ;
24
24
import { useFloatingCombobox } from './useFloatingCombobox' ;
25
- import { prefix , removePrefix } from './utilities' ;
25
+ import { prefix , removePrefix , setReactInputValue } from './utilities' ;
26
26
27
27
export type ComboboxProps = {
28
28
/**
@@ -161,6 +161,12 @@ export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
161
161
162
162
const [ inputValue , setInputValue ] = useState < string > ( rest . inputValue || '' ) ;
163
163
164
+ useEffect ( ( ) => {
165
+ if ( typeof rest . inputValue === 'string' ) {
166
+ setInputValue ( rest . inputValue ) ;
167
+ }
168
+ } , [ rest . inputValue ] ) ;
169
+
164
170
const {
165
171
selectedOptions,
166
172
options,
@@ -207,8 +213,9 @@ export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
207
213
// if value is set, set input value to the label of the value
208
214
useEffect ( ( ) => {
209
215
if ( value && value . length > 0 && ! multiple ) {
210
- const option = options [ value [ 0 ] ] ;
211
- setInputValue ( option ?. label || '' ) ;
216
+ const option = options [ prefix ( value [ 0 ] ) ] ;
217
+ inputRef . current &&
218
+ setReactInputValue ( inputRef . current , option ?. label || '' ) ;
212
219
}
213
220
} , [ multiple , value , options ] ) ;
214
221
@@ -239,7 +246,7 @@ export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
239
246
const { option, clear, remove } = args ;
240
247
if ( clear ) {
241
248
setSelectedOptions ( { } ) ;
242
- setInputValue ( '' ) ;
249
+ inputRef . current && setReactInputValue ( inputRef . current , '' ) ;
243
250
onValueChange ?.( [ ] ) ;
244
251
return ;
245
252
}
@@ -262,15 +269,16 @@ export const ComboboxComponent = forwardRef<HTMLInputElement, ComboboxProps>(
262
269
} else {
263
270
newSelectedOptions [ option . value ] = option ;
264
271
}
265
- setInputValue ( '' ) ;
272
+ inputRef . current && setReactInputValue ( inputRef . current , '' ) ;
266
273
inputRef . current ?. focus ( ) ;
267
274
} else {
268
275
/* clear newSelectedOptions */
269
276
for ( const key of Object . keys ( newSelectedOptions ) ) {
270
277
delete newSelectedOptions [ key ] ;
271
278
}
272
279
newSelectedOptions [ prefix ( option . value ) ] = option ;
273
- setInputValue ( option ?. label || '' ) ;
280
+ inputRef . current &&
281
+ setReactInputValue ( inputRef . current , option ?. label || '' ) ;
274
282
// move cursor to the end of the input
275
283
setTimeout ( ( ) => {
276
284
inputRef . current ?. setSelectionRange (
0 commit comments