x-ui-design 0.6.12 → 0.6.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,6 @@ import React, {
4
4
  Children,
5
5
  isValidElement,
6
6
  ReactElement,
7
- useCallback,
8
7
  useContext,
9
8
  useEffect,
10
9
  useMemo,
@@ -24,6 +23,20 @@ import { prefixClsFormItem } from '../../../utils';
24
23
  import { FormContext } from '../Form';
25
24
  import './style.css';
26
25
 
26
+ const debounce = (func: (...args: any[]) => void, delay: number) => {
27
+ let timeoutId: ReturnType<typeof setTimeout> | null = null;
28
+
29
+ return (...args: any[]) => {
30
+ if (timeoutId) {
31
+ clearTimeout(timeoutId);
32
+ }
33
+
34
+ timeoutId = setTimeout(() => {
35
+ func(...args);
36
+ }, delay);
37
+ };
38
+ };
39
+
27
40
  const FormItem = ({
28
41
  prefixCls = prefixClsFormItem,
29
42
  name,
@@ -198,12 +211,17 @@ const FormItemChildComponent = ({
198
211
  }: FormItemChildComponentProps) => {
199
212
  const formContext = useContext(FormContext);
200
213
 
201
- const animationRef = useRef<number | null>(null);
202
214
  const [wasNormalize, setWasNormalize] = useState(false);
203
215
 
204
216
  const { getFieldsValue } = formContext || {};
205
217
 
206
- const handleChange = useCallback((e: SyntheticBaseEvent, option?: OptionProps) => {
218
+ const debouncedSetFieldValue = useRef(
219
+ debounce((name: string, value: any) => {
220
+ setFieldValue(name, value, undefined, undefined, true);
221
+ }, 200)
222
+ ).current;
223
+
224
+ const handleChange = (e: SyntheticBaseEvent, option?: OptionProps) => {
207
225
  let rawValue: RuleType | SyntheticBaseEvent = e?.target
208
226
  ? e.target.value
209
227
  : e;
@@ -218,20 +236,21 @@ const FormItemChildComponent = ({
218
236
  e.target.value = rawValue;
219
237
  setWasNormalize(prev => !prev);
220
238
 
221
- if (animationRef.current) {
222
- cancelAnimationFrame(animationRef.current);
223
- }
239
+ const timeout = setTimeout(() => {
240
+ (
241
+ document.querySelector(`[name='${name}']`) as HTMLInputElement
242
+ )?.focus();
243
+
244
+ clearTimeout(timeout);
245
+ }, 0);
224
246
 
225
- animationRef.current = requestAnimationFrame(() => {
226
- (document.querySelector(`[name='${name}']`) as HTMLInputElement)?.focus();
227
- });
228
247
  return;
229
248
  }
230
249
  }
231
250
 
232
- setFieldValue(name, rawValue, undefined, undefined, true);
251
+ debouncedSetFieldValue(name, rawValue);
233
252
  onChange?.(e, option);
234
- }, [fieldValue, props.value, name]);
253
+ };
235
254
 
236
255
  const injectPropsIntoFinalLeaf = (child: ReactElement): ReactElement => {
237
256
  if (!isValidElement(child)) {
@@ -259,7 +278,7 @@ const FormItemChildComponent = ({
259
278
  if (childProps?.__injected) {
260
279
  return child;
261
280
  }
262
-
281
+
263
282
  return <child.type
264
283
  {...props}
265
284
  ref={ref}
@@ -284,4 +303,4 @@ const FormItemChildComponent = ({
284
303
 
285
304
  FormItem.displayName = 'FormItem';
286
305
 
287
- export default FormItem;
306
+ export default FormItem;
@@ -119,13 +119,7 @@ const InputComponent = ({
119
119
  }
120
120
  };
121
121
 
122
- if (animationRef.current) {
123
- cancelAnimationFrame(animationRef.current)
124
- }
125
-
126
- animationRef.current = requestAnimationFrame(() => {
127
- props.onChange?.(eventWithMaskedValue as RuleType);
128
- })
122
+ props.onChange?.(eventWithMaskedValue as RuleType);
129
123
  };
130
124
 
131
125
  const handleClear = (e: MouseEvent<HTMLSpanElement> & TargetProps) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-ui-design",
3
- "version": "0.6.12",
3
+ "version": "0.6.13",
4
4
  "license": "ISC",
5
5
  "author": "Gabriel Boyajyan",
6
6
  "main": "dist/index.js",