react-form-manage 1.0.8-beta.9 → 1.0.8
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.
- package/CHANGELOG.md +173 -4
- package/README.md +8 -4
- package/dist/components/Form/FormCleanUp.js +3 -3
- package/dist/components/Form/FormItem.d.ts +10 -4
- package/dist/components/Form/FormItem.js +52 -14
- package/dist/components/Form/FormList.d.ts +2 -2
- package/dist/components/Form/FormList.js +2 -2
- package/dist/constants/form.d.ts +1 -1
- package/dist/hooks/useFormItemControl.d.ts +8 -3
- package/dist/hooks/useFormItemControl.js +64 -28
- package/dist/hooks/useFormListControl.d.ts +2 -1
- package/dist/hooks/useFormListControl.js +85 -19
- package/dist/index.cjs.d.ts +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.esm.d.ts +1 -0
- package/dist/index.js +4 -2
- package/dist/providers/Form.d.ts +15 -2
- package/dist/providers/Form.js +197 -22
- package/dist/stores/formStore.d.ts +44 -4
- package/dist/stores/formStore.js +42 -7
- package/dist/test/CommonTest.d.ts +3 -0
- package/dist/test/CommonTest.js +49 -0
- package/dist/test/TestDialog.d.ts +3 -0
- package/dist/test/TestDialog.js +21 -0
- package/dist/test/TestListener.d.ts +3 -0
- package/dist/test/TestListener.js +17 -0
- package/dist/test/TestNotFormWrapper.d.ts +3 -0
- package/dist/test/TestNotFormWrapper.js +15 -0
- package/dist/test/TestSelect.d.ts +6 -0
- package/dist/test/TestSelect.js +24 -0
- package/dist/test/TestWatchNormalize.d.ts +3 -0
- package/dist/test/TestWatchNormalize.js +23 -0
- package/dist/test/TestWrapperFormItem.d.ts +3 -0
- package/dist/test/TestWrapperFormItem.js +13 -0
- package/dist/test/testSetValue/TestCase10_SetFieldValues_ComplexNested.d.ts +21 -0
- package/dist/test/testSetValue/TestCase10_SetFieldValues_ComplexNested.js +61 -0
- package/dist/test/testSetValue/TestCase1_PlainObjectToPrimitives.d.ts +16 -0
- package/dist/test/testSetValue/TestCase1_PlainObjectToPrimitives.js +18 -0
- package/dist/test/testSetValue/TestCase2_PlainObjectToFormList.d.ts +21 -0
- package/dist/test/testSetValue/TestCase2_PlainObjectToFormList.js +33 -0
- package/dist/test/testSetValue/TestCase3_ArrayNonListenerToPrimitives.d.ts +21 -0
- package/dist/test/testSetValue/TestCase3_ArrayNonListenerToPrimitives.js +26 -0
- package/dist/test/testSetValue/TestCase4_PlainObjectRemovedFields.d.ts +20 -0
- package/dist/test/testSetValue/TestCase4_PlainObjectRemovedFields.js +32 -0
- package/dist/test/testSetValue/TestCase5_FormListRemovedItems.d.ts +22 -0
- package/dist/test/testSetValue/TestCase5_FormListRemovedItems.js +29 -0
- package/dist/test/testSetValue/TestCase6_NestedFormListRemoved.d.ts +28 -0
- package/dist/test/testSetValue/TestCase6_NestedFormListRemoved.js +36 -0
- package/dist/test/testSetValue/TestCase7_SetFieldValues_MixedStructure.d.ts +17 -0
- package/dist/test/testSetValue/TestCase7_SetFieldValues_MixedStructure.js +33 -0
- package/dist/test/testSetValue/TestCase8_SetFieldValues_NestedObject.d.ts +27 -0
- package/dist/test/testSetValue/TestCase8_SetFieldValues_NestedObject.js +57 -0
- package/dist/test/testSetValue/TestCase9_SetFieldValues_MultipleArrays.d.ts +25 -0
- package/dist/test/testSetValue/TestCase9_SetFieldValues_MultipleArrays.js +46 -0
- package/dist/test/testSetValue/index.d.ts +2 -0
- package/dist/test/testSetValue/index.js +28 -0
- package/dist/types/index.d.ts +1 -1
- package/dist/types/public.d.ts +1 -1
- package/dist/utils/obj.util.d.ts +29 -1
- package/dist/utils/obj.util.js +59 -5
- package/package.json +2 -1
- package/src/App.tsx +38 -163
- package/src/DEEP_TRIGGER_LOGIC.md +573 -0
- package/src/components/Form/FormCleanUp.tsx +4 -8
- package/src/components/Form/FormItem.tsx +174 -57
- package/src/components/Form/FormList.tsx +17 -4
- package/src/constants/form.ts +1 -1
- package/src/hooks/useFormItemControl.ts +78 -32
- package/src/hooks/useFormListControl.ts +133 -43
- package/src/index.ts +25 -13
- package/src/main.tsx +6 -1
- package/src/providers/Form.tsx +451 -23
- package/src/stores/formStore.ts +363 -283
- package/src/test/CommonTest.tsx +177 -0
- package/src/test/TestDialog.tsx +52 -0
- package/src/test/TestListener.tsx +21 -0
- package/src/test/TestNotFormWrapper.tsx +43 -0
- package/src/test/TestSelect.tsx +38 -0
- package/src/test/TestWatchNormalize.tsx +32 -0
- package/src/test/TestWrapperFormItem.tsx +34 -0
- package/src/test/testSetValue/TestCase10_SetFieldValues_ComplexNested.tsx +203 -0
- package/src/test/testSetValue/TestCase1_PlainObjectToPrimitives.tsx +72 -0
- package/src/test/testSetValue/TestCase2_PlainObjectToFormList.tsx +114 -0
- package/src/test/testSetValue/TestCase3_ArrayNonListenerToPrimitives.tsx +99 -0
- package/src/test/testSetValue/TestCase4_PlainObjectRemovedFields.tsx +112 -0
- package/src/test/testSetValue/TestCase5_FormListRemovedItems.tsx +119 -0
- package/src/test/testSetValue/TestCase6_NestedFormListRemoved.tsx +185 -0
- package/src/test/testSetValue/TestCase7_SetFieldValues_MixedStructure.tsx +110 -0
- package/src/test/testSetValue/TestCase8_SetFieldValues_NestedObject.tsx +162 -0
- package/src/test/testSetValue/TestCase9_SetFieldValues_MultipleArrays.tsx +169 -0
- package/src/test/testSetValue/index.tsx +100 -0
- package/src/types/index.ts +1 -1
- package/src/types/public.ts +1 -1
- package/src/utils/obj.util.ts +153 -13
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { isNil } from "lodash";
|
|
2
|
+
import type { ComponentProps, FC, ReactElement } from "react";
|
|
3
|
+
import { cloneElement, Fragment, useEffect, useRef, useState } from "react";
|
|
3
4
|
import { v4 } from "uuid";
|
|
4
5
|
import useFormItemControl from "../../hooks/useFormItemControl";
|
|
5
6
|
import type { ValidationRule } from "../../types/public";
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
+
export type BaseFormItemProps = {
|
|
8
9
|
children: ReactElement<any>;
|
|
9
10
|
name: string;
|
|
10
11
|
formName?: string;
|
|
@@ -15,10 +16,130 @@ export interface FormItemProps {
|
|
|
15
16
|
getValueFromEvent?: (...args: any[]) => any;
|
|
16
17
|
controlAfterInit?: boolean;
|
|
17
18
|
hidden?: boolean;
|
|
19
|
+
collectOnHidden?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type FormItemProps<TCustomWrapper extends FC<any> | undefined> =
|
|
23
|
+
BaseFormItemProps &
|
|
24
|
+
(
|
|
25
|
+
| {
|
|
26
|
+
Component?: undefined;
|
|
27
|
+
}
|
|
28
|
+
| ({
|
|
29
|
+
Component: TCustomWrapper;
|
|
30
|
+
} & Omit<ComponentProps<TCustomWrapper>, keyof BaseFormItemProps>)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
function BaseFormItem({
|
|
34
|
+
children,
|
|
35
|
+
|
|
36
|
+
name,
|
|
37
|
+
elementRef: elRef,
|
|
38
|
+
|
|
39
|
+
valuePropName,
|
|
40
|
+
getValueFromEvent,
|
|
41
|
+
onChange,
|
|
42
|
+
value,
|
|
43
|
+
isDirty,
|
|
44
|
+
errors,
|
|
45
|
+
onFocus,
|
|
46
|
+
state,
|
|
47
|
+
submitState,
|
|
48
|
+
isTouched,
|
|
49
|
+
}: any) {
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
console.log("Base Form Item: ", { name, value });
|
|
52
|
+
}, [value]);
|
|
53
|
+
return cloneElement(children, {
|
|
54
|
+
name,
|
|
55
|
+
ref: elRef,
|
|
56
|
+
[valuePropName]: value,
|
|
57
|
+
onChange: (...args: any[]) => {
|
|
58
|
+
let val = args[0];
|
|
59
|
+
if (getValueFromEvent && typeof getValueFromEvent === "function") {
|
|
60
|
+
val = getValueFromEvent(...args);
|
|
61
|
+
} else {
|
|
62
|
+
const e = args[0];
|
|
63
|
+
if (e && e.target) {
|
|
64
|
+
val = e.target.value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
onChange(val);
|
|
68
|
+
},
|
|
69
|
+
// onFocus: () => {
|
|
70
|
+
// setIsTouched(true);
|
|
71
|
+
// },
|
|
72
|
+
// isTouched: isTouched,
|
|
73
|
+
isDirty: isDirty,
|
|
74
|
+
// errors: errors,
|
|
75
|
+
// formState,
|
|
76
|
+
|
|
77
|
+
errors,
|
|
78
|
+
onFocus,
|
|
79
|
+
validateState: state,
|
|
80
|
+
submitState,
|
|
81
|
+
isTouched: isTouched,
|
|
82
|
+
} as any);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function FormItemWithWrapper<TComponent extends FC<any>>({
|
|
86
|
+
children,
|
|
87
|
+
|
|
88
|
+
name,
|
|
89
|
+
elementRef: elRef,
|
|
90
|
+
|
|
91
|
+
valuePropName,
|
|
92
|
+
getValueFromEvent,
|
|
93
|
+
onChange,
|
|
94
|
+
value,
|
|
95
|
+
isDirty,
|
|
96
|
+
errors,
|
|
97
|
+
onFocus,
|
|
98
|
+
state,
|
|
99
|
+
submitState,
|
|
100
|
+
isTouched,
|
|
101
|
+
Component,
|
|
102
|
+
...props
|
|
103
|
+
}: any) {
|
|
104
|
+
return (
|
|
105
|
+
<Component {...props}>
|
|
106
|
+
{cloneElement(children, {
|
|
107
|
+
name,
|
|
108
|
+
ref: elRef,
|
|
109
|
+
[valuePropName]: value,
|
|
110
|
+
onChange: (...args: any[]) => {
|
|
111
|
+
let val = args[0];
|
|
112
|
+
if (getValueFromEvent && typeof getValueFromEvent === "function") {
|
|
113
|
+
val = getValueFromEvent(...args);
|
|
114
|
+
} else {
|
|
115
|
+
const e = args[0];
|
|
116
|
+
if (e && e.target) {
|
|
117
|
+
val = e.target.value;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
onChange(val);
|
|
121
|
+
},
|
|
122
|
+
// onFocus: () => {
|
|
123
|
+
// setIsTouched(true);
|
|
124
|
+
// },
|
|
125
|
+
// isTouched: isTouched,
|
|
126
|
+
isDirty: isDirty,
|
|
127
|
+
// errors: errors,
|
|
128
|
+
// formState,
|
|
129
|
+
|
|
130
|
+
errors,
|
|
131
|
+
onFocus,
|
|
132
|
+
validateState: state,
|
|
133
|
+
submitState,
|
|
134
|
+
isTouched: isTouched,
|
|
135
|
+
} as any)}
|
|
136
|
+
</Component>
|
|
137
|
+
);
|
|
18
138
|
}
|
|
19
139
|
|
|
20
|
-
export default function FormItem({
|
|
140
|
+
export default function FormItem<TCustomWrapper extends FC<any> | undefined>({
|
|
21
141
|
children,
|
|
142
|
+
Component,
|
|
22
143
|
name,
|
|
23
144
|
formName,
|
|
24
145
|
initialValue,
|
|
@@ -28,10 +149,13 @@ export default function FormItem({
|
|
|
28
149
|
getValueFromEvent,
|
|
29
150
|
controlAfterInit = false,
|
|
30
151
|
hidden,
|
|
31
|
-
|
|
152
|
+
collectOnHidden,
|
|
153
|
+
...props
|
|
154
|
+
}: FormItemProps<TCustomWrapper>) {
|
|
155
|
+
const [formItemId] = useState(externalFormItemId ?? v4());
|
|
156
|
+
|
|
32
157
|
const elRef = useRef<any>(null);
|
|
33
158
|
|
|
34
|
-
const [formItemId] = useState(externalFormItemId ?? v4());
|
|
35
159
|
const {
|
|
36
160
|
value,
|
|
37
161
|
onChange,
|
|
@@ -49,62 +173,55 @@ export default function FormItem({
|
|
|
49
173
|
formItemId,
|
|
50
174
|
rules,
|
|
51
175
|
elementRef: elRef,
|
|
176
|
+
hidden,
|
|
177
|
+
collectOnHidden,
|
|
52
178
|
});
|
|
53
|
-
// console.log("re-render", formName, name);
|
|
54
|
-
|
|
55
|
-
// useEffect(() => {
|
|
56
|
-
// console.log({ value });
|
|
57
|
-
// }, [value]);
|
|
58
|
-
|
|
59
|
-
// useEffect(() => {
|
|
60
|
-
// console.log("isInitied changed: ", {
|
|
61
|
-
// isInitied,
|
|
62
|
-
// name,
|
|
63
|
-
// value,
|
|
64
|
-
// controlAfterInit,
|
|
65
|
-
// });
|
|
66
|
-
// }, [isInitied]);
|
|
67
179
|
|
|
68
180
|
return (
|
|
69
181
|
<Fragment
|
|
70
|
-
key={`control-after-init-${Boolean(controlAfterInit && isInitied) ? "1" : "0"}-${
|
|
182
|
+
key={`control-after-init-${Boolean(controlAfterInit && isInitied) ? "1" : "0"}-${externalFormItemId}`}
|
|
71
183
|
>
|
|
72
|
-
{!hidden && children
|
|
73
|
-
?
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
onFocus
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
184
|
+
{!hidden && children ? (
|
|
185
|
+
isNil(Component) ? (
|
|
186
|
+
<BaseFormItem
|
|
187
|
+
elementRef={elRef}
|
|
188
|
+
name={name}
|
|
189
|
+
valuePropName={valuePropName}
|
|
190
|
+
getValueFromEvent={getValueFromEvent}
|
|
191
|
+
value={value}
|
|
192
|
+
onChange={onChange}
|
|
193
|
+
errors={errors}
|
|
194
|
+
state={state}
|
|
195
|
+
onFocus={onFocus}
|
|
196
|
+
isDirty={isDirty}
|
|
197
|
+
submitState={submitState}
|
|
198
|
+
isTouched={isTouched}
|
|
199
|
+
formItemId={formItemId}
|
|
200
|
+
>
|
|
201
|
+
{children}
|
|
202
|
+
</BaseFormItem>
|
|
203
|
+
) : (
|
|
204
|
+
<FormItemWithWrapper
|
|
205
|
+
elementRef={elRef}
|
|
206
|
+
name={name}
|
|
207
|
+
valuePropName={valuePropName}
|
|
208
|
+
getValueFromEvent={getValueFromEvent}
|
|
209
|
+
value={value}
|
|
210
|
+
onChange={onChange}
|
|
211
|
+
errors={errors}
|
|
212
|
+
state={state}
|
|
213
|
+
onFocus={onFocus}
|
|
214
|
+
isDirty={isDirty}
|
|
215
|
+
submitState={submitState}
|
|
216
|
+
isTouched={isTouched}
|
|
217
|
+
formItemId={formItemId}
|
|
218
|
+
Component={Component}
|
|
219
|
+
{...props}
|
|
220
|
+
>
|
|
221
|
+
{children}
|
|
222
|
+
</FormItemWithWrapper>
|
|
223
|
+
)
|
|
224
|
+
) : null}
|
|
108
225
|
</Fragment>
|
|
109
226
|
);
|
|
110
227
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import useFormListControl from "../../hooks/useFormListControl";
|
|
2
2
|
import type { FormInstance } from "../../stores/formStore";
|
|
3
3
|
|
|
4
4
|
export interface FormListProps<T = any> {
|
|
@@ -6,11 +6,24 @@ export interface FormListProps<T = any> {
|
|
|
6
6
|
initialValues?: T[];
|
|
7
7
|
form?: FormInstance;
|
|
8
8
|
formName?: string;
|
|
9
|
-
children: (
|
|
9
|
+
children: (
|
|
10
|
+
fields: Array<{ name: string; key: string }>,
|
|
11
|
+
operations: {
|
|
12
|
+
add: (index?: number) => void;
|
|
13
|
+
remove: (opts: { index?: number; key?: string }) => void;
|
|
14
|
+
move: (opts: { from?: number; fromKey?: string; to: number }) => void;
|
|
15
|
+
},
|
|
16
|
+
) => React.ReactNode;
|
|
10
17
|
}
|
|
11
18
|
|
|
12
|
-
const FormList = <T = any
|
|
13
|
-
|
|
19
|
+
const FormList = <T = any,>({
|
|
20
|
+
name,
|
|
21
|
+
initialValues,
|
|
22
|
+
form,
|
|
23
|
+
formName,
|
|
24
|
+
children,
|
|
25
|
+
}: FormListProps<T>) => {
|
|
26
|
+
const { listFields, ...actions } = useFormListControl({
|
|
14
27
|
name,
|
|
15
28
|
initialValues,
|
|
16
29
|
form,
|
package/src/constants/form.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { get, isNil } from "lodash";
|
|
1
|
+
import { get, has, isNil } from "lodash";
|
|
2
2
|
import { useTaskEffect } from "minh-custom-hooks-release";
|
|
3
|
-
import { useEffect, useMemo, type RefObject } from "react";
|
|
3
|
+
import { useEffect, useLayoutEffect, useMemo, type RefObject } from "react";
|
|
4
4
|
import { useShallow } from "zustand/react/shallow"; // Import useShallow
|
|
5
5
|
import {
|
|
6
6
|
IS_ALPHABET_STRING_AND_NUMBER_REGEX,
|
|
@@ -18,11 +18,7 @@ import {
|
|
|
18
18
|
IS_VIETNAMESE_PHONE_NUMBER_REGEX,
|
|
19
19
|
} from "../constants/validation";
|
|
20
20
|
import { useFormContext } from "../providers/Form";
|
|
21
|
-
import {
|
|
22
|
-
useFormCleanUp,
|
|
23
|
-
useFormListeners,
|
|
24
|
-
useFormStore,
|
|
25
|
-
} from "../stores/formStore";
|
|
21
|
+
import { useFormStore } from "../stores/formStore";
|
|
26
22
|
|
|
27
23
|
import type { FormInstance } from "../stores/formStore";
|
|
28
24
|
import type {
|
|
@@ -41,11 +37,18 @@ interface UseFormItemControlProps {
|
|
|
41
37
|
formItemId?: string;
|
|
42
38
|
rules?: ValidationRule[];
|
|
43
39
|
elementRef?: RefObject<any> | null;
|
|
40
|
+
hidden?: boolean;
|
|
41
|
+
collectOnHidden?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface OnChangeOptions {
|
|
45
|
+
notTriggerDirty?: boolean;
|
|
46
|
+
initiedData?: boolean;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
export interface UseFormItemControlReturn {
|
|
47
50
|
value: any;
|
|
48
|
-
onChange: (value: any, options?:
|
|
51
|
+
onChange: (value: any, options?: OnChangeOptions) => void;
|
|
49
52
|
state: any;
|
|
50
53
|
errors: FormFieldError[];
|
|
51
54
|
onFocus: () => void;
|
|
@@ -65,6 +68,8 @@ export default function useFormItemControl<T = any>({
|
|
|
65
68
|
formItemId,
|
|
66
69
|
rules,
|
|
67
70
|
elementRef,
|
|
71
|
+
hidden,
|
|
72
|
+
collectOnHidden,
|
|
68
73
|
}: UseFormItemControlProps): UseFormItemControlReturn {
|
|
69
74
|
const contextForm = useFormContext();
|
|
70
75
|
const {
|
|
@@ -72,9 +77,12 @@ export default function useFormItemControl<T = any>({
|
|
|
72
77
|
setData,
|
|
73
78
|
getCacheData,
|
|
74
79
|
getFormValues,
|
|
75
|
-
getFormState,
|
|
80
|
+
// getFormState,
|
|
76
81
|
isStateInitied,
|
|
77
82
|
submitState,
|
|
83
|
+
getListener,
|
|
84
|
+
revokeListener,
|
|
85
|
+
clearObjKeyItem,
|
|
78
86
|
} = useFormStore(
|
|
79
87
|
useShallow((state) => {
|
|
80
88
|
return {
|
|
@@ -85,7 +93,7 @@ export default function useFormItemControl<T = any>({
|
|
|
85
93
|
setData: state.setData,
|
|
86
94
|
getCacheData: state.getCacheData,
|
|
87
95
|
getFormValues: state.getFormValues,
|
|
88
|
-
getFormState: state.getFormState,
|
|
96
|
+
// getFormState: state.getFormState,
|
|
89
97
|
isStateInitied:
|
|
90
98
|
state.formStates?.[
|
|
91
99
|
formName || form?.formName || contextForm?.formName
|
|
@@ -94,11 +102,14 @@ export default function useFormItemControl<T = any>({
|
|
|
94
102
|
state.formStates?.[
|
|
95
103
|
formName || form?.formName || contextForm?.formName
|
|
96
104
|
]?.submitState,
|
|
105
|
+
getListener: state.getListener,
|
|
106
|
+
revokeListener: state.revokeListener,
|
|
107
|
+
clearObjKeyItem: state.clearObjKeyItem,
|
|
97
108
|
};
|
|
98
109
|
}),
|
|
99
110
|
);
|
|
100
111
|
|
|
101
|
-
const { setCleanUpStack } =
|
|
112
|
+
const { setCleanUpStack } = useFormStore(
|
|
102
113
|
useShallow((state) => ({
|
|
103
114
|
setCleanUpStack: state.setCleanUpStack,
|
|
104
115
|
})),
|
|
@@ -130,7 +141,7 @@ export default function useFormItemControl<T = any>({
|
|
|
130
141
|
}),
|
|
131
142
|
);
|
|
132
143
|
|
|
133
|
-
const { listener, setListener } =
|
|
144
|
+
const { listener, setListener } = useFormStore(
|
|
134
145
|
useShallow((state) => {
|
|
135
146
|
// console.log(
|
|
136
147
|
// "Get listener from store: ",
|
|
@@ -205,6 +216,7 @@ export default function useFormItemControl<T = any>({
|
|
|
205
216
|
formItemId,
|
|
206
217
|
isDirty: false,
|
|
207
218
|
isTouched: false,
|
|
219
|
+
isInitied: false,
|
|
208
220
|
});
|
|
209
221
|
onChange(
|
|
210
222
|
isNil(value)
|
|
@@ -219,8 +231,17 @@ export default function useFormItemControl<T = any>({
|
|
|
219
231
|
return rules || [];
|
|
220
232
|
}, [rules]);
|
|
221
233
|
|
|
234
|
+
// useEffect(() => {
|
|
235
|
+
// console.log("Rules changed: ", { name, listener });
|
|
236
|
+
// }, [value, listener]);
|
|
237
|
+
|
|
222
238
|
const { data: errors, state } = useTaskEffect({
|
|
223
239
|
async task() {
|
|
240
|
+
console.log("Trigger validate for form item: ", {
|
|
241
|
+
formName: formName || form?.formName || contextForm?.formName,
|
|
242
|
+
name,
|
|
243
|
+
value,
|
|
244
|
+
});
|
|
224
245
|
const listErrors = [];
|
|
225
246
|
|
|
226
247
|
const formValues = getFormValues(
|
|
@@ -513,7 +534,9 @@ export default function useFormItemControl<T = any>({
|
|
|
513
534
|
|
|
514
535
|
return listErrors;
|
|
515
536
|
},
|
|
516
|
-
deps: [internalRules, value],
|
|
537
|
+
deps: [internalRules, value, listener?.isInitied],
|
|
538
|
+
enabled: Boolean(listener?.isInitied),
|
|
539
|
+
|
|
517
540
|
onError(err) {
|
|
518
541
|
console.log(err);
|
|
519
542
|
},
|
|
@@ -537,40 +560,66 @@ export default function useFormItemControl<T = any>({
|
|
|
537
560
|
|
|
538
561
|
if (isNil(value)) {
|
|
539
562
|
if (isNil(internalInitValue)) {
|
|
540
|
-
|
|
541
|
-
onInitData(initialValue);
|
|
542
|
-
}
|
|
563
|
+
onInitData(initialValue);
|
|
543
564
|
} else {
|
|
544
565
|
onChange(internalInitValue, {
|
|
545
566
|
notTriggerDirty: true,
|
|
546
567
|
initiedData: true,
|
|
547
568
|
});
|
|
548
569
|
}
|
|
570
|
+
} else {
|
|
571
|
+
onChange(value, {
|
|
572
|
+
notTriggerDirty: true,
|
|
573
|
+
initiedData: true,
|
|
574
|
+
});
|
|
549
575
|
}
|
|
550
576
|
return;
|
|
551
577
|
}
|
|
552
578
|
}, [isStateInitied]);
|
|
553
579
|
|
|
554
|
-
|
|
580
|
+
useLayoutEffect(() => {
|
|
581
|
+
console.log("Component mount: ", name, formItemId, listener);
|
|
555
582
|
if (!listener) {
|
|
556
583
|
setListener({
|
|
557
584
|
onChange,
|
|
558
585
|
emitFocus,
|
|
559
586
|
isTouched: false,
|
|
560
587
|
isDirty: false,
|
|
588
|
+
isInitied: false,
|
|
561
589
|
name,
|
|
562
590
|
formName: formName || form?.formName || contextForm?.formName,
|
|
563
591
|
formItemId,
|
|
564
592
|
onReset,
|
|
593
|
+
hidden,
|
|
594
|
+
collectOnHidden,
|
|
565
595
|
});
|
|
566
596
|
}
|
|
567
597
|
|
|
598
|
+
return () => {
|
|
599
|
+
revokeListener(formItemId, (listener, same) => {
|
|
600
|
+
if (!same.length) {
|
|
601
|
+
console.log("Trigger after revoke: ", listener, same);
|
|
602
|
+
clearObjKeyItem(listener.formName, listener.name);
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
};
|
|
606
|
+
|
|
568
607
|
// return () => {
|
|
569
608
|
// console.log("Revoke listener", listener);
|
|
570
609
|
|
|
571
610
|
// };
|
|
572
611
|
}, []);
|
|
573
612
|
|
|
613
|
+
useEffect(() => {
|
|
614
|
+
if (listener) {
|
|
615
|
+
setListener({
|
|
616
|
+
formItemId,
|
|
617
|
+
hidden,
|
|
618
|
+
collectOnHidden,
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
}, [hidden, collectOnHidden]);
|
|
622
|
+
|
|
574
623
|
useEffect(() => {
|
|
575
624
|
if (listener) {
|
|
576
625
|
setListener({
|
|
@@ -590,28 +639,25 @@ export default function useFormItemControl<T = any>({
|
|
|
590
639
|
// console.log("Get cache Data after list change: ", cacheData);
|
|
591
640
|
|
|
592
641
|
if (cacheData) {
|
|
642
|
+
console.log("Cache data found when form item change: ", name, cacheData);
|
|
593
643
|
const getNewDataFromCache = get(cacheData, name);
|
|
644
|
+
const isIncludeDirectoryInCache = has(cacheData, name);
|
|
594
645
|
|
|
595
646
|
// console.log("Init data when change form ite: ", name, cacheData);
|
|
596
647
|
|
|
597
|
-
if (!getNewDataFromCache) {
|
|
598
|
-
onChange(initialValue
|
|
599
|
-
|
|
648
|
+
if (!isIncludeDirectoryInCache && isNil(getNewDataFromCache)) {
|
|
649
|
+
onChange(initialValue, {
|
|
650
|
+
notTriggerDirty: true,
|
|
651
|
+
initiedData: true,
|
|
652
|
+
});
|
|
653
|
+
} else
|
|
654
|
+
onChange(getNewDataFromCache, {
|
|
655
|
+
notTriggerDirty: true,
|
|
656
|
+
initiedData: true,
|
|
657
|
+
});
|
|
600
658
|
}
|
|
601
659
|
}, [name, formName || form?.formName || contextForm?.formName]);
|
|
602
660
|
|
|
603
|
-
useEffect(() => {
|
|
604
|
-
return () => {
|
|
605
|
-
setCleanUpStack({
|
|
606
|
-
itemKey: formItemId,
|
|
607
|
-
});
|
|
608
|
-
};
|
|
609
|
-
}, []);
|
|
610
|
-
|
|
611
|
-
useEffect(() => {
|
|
612
|
-
console.log("Submit state changed in useFormItemControl: ", submitState);
|
|
613
|
-
}, [submitState]);
|
|
614
|
-
|
|
615
661
|
return {
|
|
616
662
|
value: value as T,
|
|
617
663
|
onChange,
|