@@ -61,6 +61,14 @@ const defaultIsActiveElementInListbox = (listboxRef) =>
61
61
62
62
const MULTIPLE_DEFAULT_VALUE = [ ] ;
63
63
64
+ function getInputValue ( value , multiple , getOptionLabel ) {
65
+ if ( multiple || value == null ) {
66
+ return '' ;
67
+ }
68
+ const optionLabel = getOptionLabel ( value ) ;
69
+ return typeof optionLabel === 'string' ? optionLabel : '' ;
70
+ }
71
+
64
72
function useAutocomplete ( props ) {
65
73
const {
66
74
// eslint-disable-next-line @typescript-eslint/naming-convention
@@ -137,14 +145,20 @@ function useAutocomplete(props) {
137
145
const defaultHighlighted = autoHighlight ? 0 : - 1 ;
138
146
const highlightedIndexRef = React . useRef ( defaultHighlighted ) ;
139
147
148
+ // Calculate the initial inputValue on mount only.
149
+ // Using useRef since defaultValue doesn't need to update inputValue dynamically.
150
+ const initialInputValue = React . useRef (
151
+ getInputValue ( defaultValue , multiple , getOptionLabel ) ,
152
+ ) . current ;
153
+
140
154
const [ value , setValueState ] = useControlled ( {
141
155
controlled : valueProp ,
142
156
default : defaultValue ,
143
157
name : componentName ,
144
158
} ) ;
145
159
const [ inputValue , setInputValueState ] = useControlled ( {
146
160
controlled : inputValueProp ,
147
- default : '' ,
161
+ default : initialInputValue ,
148
162
name : componentName ,
149
163
state : 'inputValue' ,
150
164
} ) ;
@@ -159,15 +173,7 @@ function useAutocomplete(props) {
159
173
if ( ! isOptionSelected && ! clearOnBlur ) {
160
174
return ;
161
175
}
162
- let newInputValue ;
163
- if ( multiple ) {
164
- newInputValue = '' ;
165
- } else if ( newValue == null ) {
166
- newInputValue = '' ;
167
- } else {
168
- const optionLabel = getOptionLabel ( newValue ) ;
169
- newInputValue = typeof optionLabel === 'string' ? optionLabel : '' ;
170
- }
176
+ const newInputValue = getInputValue ( newValue , multiple , getOptionLabel ) ;
171
177
172
178
if ( inputValue === newInputValue ) {
173
179
return ;
0 commit comments