1
- import { ReactNode , useEffect , useState , useCallback } from "react" ;
1
+ import { ReactNode , useEffect , useState , useCallback , useRef } from "react" ;
2
2
import { Input , Section , sectionNames } from "lowcoder-design" ;
3
3
import { BoolControl } from "comps/controls/boolControl" ;
4
4
import { styleControl } from "comps/controls/styleControl" ;
@@ -148,12 +148,19 @@ let AutoCompleteCompBase = (function () {
148
148
const [ activationFlag , setActivationFlag ] = useState ( false ) ;
149
149
const [ searchtext , setsearchtext ] = useState < string > ( props . value . value ) ;
150
150
const [ validateState , setvalidateState ] = useState ( { } ) ;
151
+
152
+ // Use simple refs like text input components
153
+ const changeRef = useRef ( false ) ;
154
+ const touchRef = useRef ( false ) ;
151
155
152
156
// 是否中文环境
153
157
const [ chineseEnv , setChineseEnv ] = useState ( getDayJSLocale ( ) === "zh-cn" ) ;
154
158
155
159
useEffect ( ( ) => {
156
- setsearchtext ( props . value . value ) ;
160
+ // Only update local state from props if user hasn't touched the input
161
+ if ( ! touchRef . current ) {
162
+ setsearchtext ( props . value . value ) ;
163
+ }
157
164
activationFlag &&
158
165
setvalidateState ( textInputValidate ( getTextInputValidate ( ) ) ) ;
159
166
} , [
@@ -247,19 +254,27 @@ let AutoCompleteCompBase = (function () {
247
254
props . valueInItems . onChange ( false ) ;
248
255
setvalidateState ( textInputValidate ( getTextInputValidate ( ) ) ) ;
249
256
setsearchtext ( value ) ;
257
+ changeRef . current = true ;
258
+ touchRef . current = true ;
259
+
260
+ // Update parent value immediately to prevent sync issues
250
261
props . value . onChange ( value ) ;
251
262
props . onEvent ( "change" ) ;
263
+
252
264
if ( ! Boolean ( value ) ) {
253
265
props . selectedOption . onChange ( { } ) ;
254
266
}
255
267
} , [ props . valueInItems , getTextInputValidate , props . value , props . onEvent , props . selectedOption ] ) ;
256
268
257
269
const handleSelect = useCallback ( ( data :string , option :any ) => {
258
- setsearchtext ( option [ valueOrLabel ] ) ;
270
+ const selectedValue = option [ valueOrLabel ] ;
271
+ setsearchtext ( selectedValue ) ;
259
272
props . valueInItems . onChange ( true ) ;
260
- props . value . onChange ( option [ valueOrLabel ] ) ;
273
+ props . value . onChange ( selectedValue ) ;
261
274
props . selectedOption . onChange ( option ) ;
262
275
props . onEvent ( "submit" ) ;
276
+ changeRef . current = true ;
277
+ touchRef . current = true ;
263
278
} , [ valueOrLabel , props . valueInItems , props . value , props . onEvent , props . selectedOption ] ) ;
264
279
265
280
const handleFocus = useCallback ( ( ) => {
@@ -268,6 +283,7 @@ let AutoCompleteCompBase = (function () {
268
283
} , [ props . onEvent ] ) ;
269
284
270
285
const handleBlur = useCallback ( ( ) => {
286
+ touchRef . current = false ;
271
287
props . onEvent ( "blur" ) ;
272
288
} , [ props . onEvent ] ) ;
273
289