Skip to content

Commit d0d3169

Browse files
added debounce for text inputs to avoid glitch
1 parent f81eb8a commit d0d3169

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

client/packages/lowcoder/src/comps/comps/textInputComp/textInputConstants.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { stringExposingStateControl } from "comps/controls/codeStateControl";
1111
import { LabelControl } from "comps/controls/labelControl";
1212
import { InputLikeStyleType, LabelStyleType, heightCalculator, widthCalculator } from "comps/controls/styleControlConstants";
1313
import { Section, sectionNames, ValueFromOption } from "lowcoder-design";
14-
import { fromPairs } from "lodash";
14+
import { debounce, fromPairs } from "lodash";
1515
import { css } from "styled-components";
1616
import { EMAIL_PATTERN, URL_PATTERN } from "util/stringUtils";
1717
import { MultiBaseComp, RecordConstructorToComp, RecordConstructorToView } from "lowcoder-core";
@@ -33,7 +33,7 @@ import {
3333
showDataLoadingIndicatorsPropertyView,
3434
} from "comps/utils/propertyUtils";
3535
import { trans } from "i18n";
36-
import { ChangeEvent, useEffect, useRef, useState } from "react";
36+
import { ChangeEvent, useEffect, useMemo, useRef, useState } from "react";
3737
import { refMethods } from "comps/generators/withMethodExposing";
3838
import { InputRef } from "antd/es/input";
3939
import {
@@ -199,7 +199,6 @@ export const useTextInputProps = (props: RecordConstructorToView<typeof textInpu
199199
},
200200
})
201201
);
202-
propsRef.current.onEvent("change");
203202
changeRef.current = false;
204203
}, [localInputValue]);
205204

@@ -216,19 +215,21 @@ export const useTextInputProps = (props: RecordConstructorToView<typeof textInpu
216215
);
217216
}, [props.customRule])
218217

219-
const onChangeRef = useRef(
220-
(value: string) => {
221-
props.value.onChange(value);
222-
}
218+
const debouncedOnChangeRef = useRef(
219+
debounce(function (value: string, valueCtx: any) {
220+
propsRef.current.value.onChange(value);
221+
propsRef.current.onEvent("change");
222+
}, 1000)
223223
);
224+
224225

225226
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
226227
const value = e.target.value;
227228
setLocalInputValue(value);
228229

229230
changeRef.current = true;
230231
touchRef.current = true;
231-
onChangeRef.current?.(value);
232+
debouncedOnChangeRef.current?.(value, propsRef.current.value);
232233
};
233234

234235
// Cleanup refs on unmount
@@ -237,7 +238,7 @@ export const useTextInputProps = (props: RecordConstructorToView<typeof textInpu
237238
changeRef.current = false;
238239
touchRef.current = false;
239240
propsRef.current = null as any;
240-
onChangeRef.current = null as any;
241+
debouncedOnChangeRef.current.cancel();
241242
};
242243
}, []);
243244

0 commit comments

Comments
 (0)