react-form-manage 1.0.6-beta.0 → 1.0.6-beta.2
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 +52 -0
- package/dist/components/Form/FormCleanUp.js +46 -0
- package/dist/components/Form/FormItem.d.ts +11 -17
- package/dist/components/Form/FormItem.js +46 -0
- package/dist/components/Form/FormList.d.ts +23 -7
- package/dist/components/Form/FormList.js +11 -0
- package/dist/components/Form/InputWrapper.d.ts +10 -5
- package/dist/components/Form/InputWrapper.js +7 -0
- package/dist/components/Input.d.ts +4 -5
- package/dist/components/Input.js +8 -0
- package/dist/constants/validation.js +33 -0
- package/dist/contexts/FormContext.js +2 -0
- package/dist/hooks/useFormItemControl.js +390 -0
- package/dist/hooks/useFormListControl.js +280 -0
- package/dist/index.d.ts +8 -185
- package/dist/index.js +9 -0
- package/dist/providers/Form.d.ts +15 -10
- package/dist/providers/Form.js +322 -0
- package/dist/stores/formStore.js +304 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/types/public.d.ts +51 -0
- package/dist/utils/obj.util.js +24 -0
- package/package.json +30 -45
- package/src/App.css +49 -0
- package/src/App.tsx +36 -0
- package/src/assets/react.svg +1 -0
- package/src/components/Form/FormCleanUp.tsx +57 -0
- package/src/components/Form/FormItem.tsx +72 -0
- package/src/components/Form/FormList.tsx +22 -0
- package/src/components/Form/InputWrapper.tsx +24 -0
- package/src/components/Input.jsx +13 -0
- package/src/components/Input.tsx +21 -0
- package/src/constants/validation.ts +63 -0
- package/src/contexts/FormContext.ts +3 -0
- package/src/hooks/useFormItemControl.ts +558 -0
- package/src/hooks/useFormListControl.ts +378 -0
- package/src/index.css +68 -0
- package/src/index.ts +35 -0
- package/src/main.tsx +10 -0
- package/src/providers/Form.tsx +440 -0
- package/src/stores/formStore.ts +477 -0
- package/src/types/index.ts +1 -0
- package/src/types/public.ts +50 -0
- package/src/utils/obj.util.ts +42 -0
- package/dist/App.d.ts +0 -2
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.d.ts +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.esm.d.ts +0 -1
- package/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +0 -1
- package/src/types/components.d.ts +0 -135
- /package/dist/{main.d.ts → types/public.js} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,186 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
setFieldValues?: (values: Partial<T>) => void;
|
|
10
|
-
getFieldValue?: (name: keyof T & string) => any;
|
|
11
|
-
getFieldValues?: () => T;
|
|
12
|
-
getFieldErrors?: () => Record<string, any>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface UseFormItemProps<T = any> {
|
|
16
|
-
formName?: string;
|
|
17
|
-
form?: PublicFormInstance<T>;
|
|
18
|
-
name?: keyof T & string;
|
|
19
|
-
initialValue?: any;
|
|
20
|
-
formItemId?: string;
|
|
21
|
-
rules?: any[];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface UseFormItemReturn<T = any> {
|
|
25
|
-
value: T | undefined;
|
|
26
|
-
onChange: (value: T, options?: any) => void;
|
|
27
|
-
state: any;
|
|
28
|
-
errors: any;
|
|
29
|
-
onFocus: () => void;
|
|
30
|
-
isDirty?: boolean;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface UseFormListProps<T = any> {
|
|
34
|
-
name?: string;
|
|
35
|
-
form?: PublicFormInstance<T>;
|
|
36
|
-
initialValues?: T[];
|
|
37
|
-
formName?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface ListField {
|
|
41
|
-
name: string;
|
|
42
|
-
key: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface UseFormListReturn {
|
|
46
|
-
listFields: ListField[];
|
|
47
|
-
move: (opts: { from?: number; fromKey?: string; to: number }) => void;
|
|
48
|
-
add: (index: number) => void;
|
|
49
|
-
remove: (opts: { index?: number; key?: string }) => void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
import * as React from "react";
|
|
53
|
-
|
|
54
|
-
// Generic types for top-level Form provider
|
|
55
|
-
export interface FormProps<TValues = Record<string, any>> {
|
|
56
|
-
children?: React.ReactNode;
|
|
57
|
-
formName?: string;
|
|
58
|
-
initialValues?: Partial<TValues>;
|
|
59
|
-
onFinish?: (
|
|
60
|
-
values: Partial<TValues>,
|
|
61
|
-
allValues?: TValues,
|
|
62
|
-
) => void | Promise<void>;
|
|
63
|
-
onReject?: (errors: any) => void | Promise<void>;
|
|
64
|
-
onFinally?: (payload: {
|
|
65
|
-
errorsField: any;
|
|
66
|
-
values: Partial<TValues>;
|
|
67
|
-
withUnRegisteredValues: TValues;
|
|
68
|
-
}) => void | Promise<void>;
|
|
69
|
-
FormElement?: React.ComponentType<any>;
|
|
70
|
-
[key: string]: any;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
declare function Form<TValues = Record<string, any>>(
|
|
74
|
-
props: FormProps<TValues>,
|
|
75
|
-
): JSX.Element;
|
|
76
|
-
|
|
77
|
-
// Standalone generic hooks (also attached to Form as static properties at runtime)
|
|
78
|
-
export function useForm<TValues = Record<string, any>>(
|
|
79
|
-
formNameOrFormInstance?: any,
|
|
80
|
-
): [any];
|
|
81
|
-
|
|
82
|
-
export function useWatch<
|
|
83
|
-
TValues = Record<string, any>,
|
|
84
|
-
K extends keyof TValues = string & keyof TValues,
|
|
85
|
-
>(
|
|
86
|
-
name: K,
|
|
87
|
-
formNameOrFormInstance?: any,
|
|
88
|
-
): K extends keyof TValues ? TValues[K] : any;
|
|
89
|
-
|
|
90
|
-
export function useSubmitDataWatch<TValues = any>(opts: {
|
|
91
|
-
formNameOrFormInstance?: any;
|
|
92
|
-
triggerWhenChange?: boolean;
|
|
93
|
-
mapFn?: (v: any, prev: any) => any;
|
|
94
|
-
}): any;
|
|
95
|
-
|
|
96
|
-
export function useFormStateWatch<TValues = any>(
|
|
97
|
-
formNameOrFormInstance?: any,
|
|
98
|
-
): any;
|
|
99
|
-
|
|
100
|
-
declare namespace Form {
|
|
101
|
-
// expose same hook names on the Form object
|
|
102
|
-
export { useForm, useWatch, useSubmitDataWatch, useFormStateWatch };
|
|
103
|
-
}
|
|
104
|
-
|
|
1
|
+
import Form, { useForm, useWatch, useSubmitDataWatch, useFormStateWatch, type FormProps } from "./providers/Form";
|
|
2
|
+
import FormItem, { type FormItemProps } from "./components/Form/FormItem";
|
|
3
|
+
import FormList, { type FormListProps } from "./components/Form/FormList";
|
|
4
|
+
import Input from "./components/Input";
|
|
5
|
+
import InputWrapper, { type InputWrapperProps } from "./components/Form/InputWrapper";
|
|
6
|
+
import useFormItemControl from "./hooks/useFormItemControl";
|
|
7
|
+
import useFormListControl from "./hooks/useFormListControl";
|
|
8
|
+
export { Form, FormItem, FormList, Input, InputWrapper, useFormItemControl, useFormListControl, useForm, useWatch, useSubmitDataWatch, useFormStateWatch, type FormProps, type FormItemProps, type FormListProps, type InputWrapperProps, };
|
|
105
9
|
export default Form;
|
|
106
|
-
export { Form };
|
|
107
|
-
|
|
108
|
-
// FormItem (generic for the field value type)
|
|
109
|
-
export interface FormItemProps<T = any> {
|
|
110
|
-
children: React.ReactElement;
|
|
111
|
-
name: string;
|
|
112
|
-
formName?: string;
|
|
113
|
-
initialValue?: T;
|
|
114
|
-
formItemId?: string;
|
|
115
|
-
rules?: any[];
|
|
116
|
-
valuePropName?: string;
|
|
117
|
-
getValueFromEvent?: (e: any) => T;
|
|
118
|
-
[key: string]: any;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function FormItem<T = any>(props: FormItemProps<T>): JSX.Element;
|
|
122
|
-
|
|
123
|
-
// FormList (generic for item shape)
|
|
124
|
-
export interface FormListProps<TItem = any> {
|
|
125
|
-
name: string;
|
|
126
|
-
initialValues?: TItem[];
|
|
127
|
-
form?: any;
|
|
128
|
-
formName?: string;
|
|
129
|
-
children: (listFields: any[], actions: any) => React.ReactNode;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function FormList<TItem = any>(props: FormListProps<TItem>): JSX.Element;
|
|
133
|
-
|
|
134
|
-
// Input components
|
|
135
|
-
export interface BasicInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
136
|
-
onChange?: (value: any, ...args: any[]) => void;
|
|
137
|
-
isValidating?: boolean;
|
|
138
|
-
[key: string]: any;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// InputWrapper
|
|
142
|
-
export interface InputWrapperProps {
|
|
143
|
-
children: React.ReactElement;
|
|
144
|
-
errors?: Array<{ ruleName?: string; message?: string }>;
|
|
145
|
-
isDirty?: boolean;
|
|
146
|
-
formState?: any;
|
|
147
|
-
name?: string;
|
|
148
|
-
formItemId?: string;
|
|
149
|
-
[key: string]: any;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export function InputWrapper(props: InputWrapperProps): JSX.Element;
|
|
153
|
-
|
|
154
|
-
// Cleanup component (internal)
|
|
155
|
-
export function FormCleanUp(): JSX.Element;
|
|
156
|
-
|
|
157
|
-
// Hooks in src/hooks
|
|
158
|
-
export function useFormItemControl<T = any>(opts: {
|
|
159
|
-
formName?: string;
|
|
160
|
-
form?: any;
|
|
161
|
-
name: string;
|
|
162
|
-
initialValue?: T;
|
|
163
|
-
formItemId?: string;
|
|
164
|
-
rules?: any[];
|
|
165
|
-
elementRef?: React.RefObject<any>;
|
|
166
|
-
}): {
|
|
167
|
-
value: T | undefined;
|
|
168
|
-
onChange: (value: any, options?: any) => void;
|
|
169
|
-
state: any;
|
|
170
|
-
errors: any;
|
|
171
|
-
onFocus: () => void;
|
|
172
|
-
isDirty?: boolean;
|
|
173
|
-
onReset?: (value?: any) => void;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export function useFormListControl<TItem = any>(opts: {
|
|
177
|
-
name: string;
|
|
178
|
-
form?: any;
|
|
179
|
-
initialValues?: TItem[];
|
|
180
|
-
formName?: string;
|
|
181
|
-
}): {
|
|
182
|
-
listFields: Array<{ name: string; key: string }>;
|
|
183
|
-
add: (index?: number) => void;
|
|
184
|
-
remove: (opts: { index?: number; key?: string }) => void;
|
|
185
|
-
move: (opts: { from?: number; fromKey?: string; to: number }) => void;
|
|
186
|
-
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import Form, { useForm, useWatch, useSubmitDataWatch, useFormStateWatch, } from "./providers/Form";
|
|
2
|
+
import FormItem from "./components/Form/FormItem";
|
|
3
|
+
import FormList from "./components/Form/FormList";
|
|
4
|
+
import Input from "./components/Input";
|
|
5
|
+
import InputWrapper from "./components/Form/InputWrapper";
|
|
6
|
+
import useFormItemControl from "./hooks/useFormItemControl";
|
|
7
|
+
import useFormListControl from "./hooks/useFormListControl";
|
|
8
|
+
export { Form, FormItem, FormList, Input, InputWrapper, useFormItemControl, useFormListControl, useForm, useWatch, useSubmitDataWatch, useFormStateWatch, };
|
|
9
|
+
export default Form;
|
package/dist/providers/Form.d.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
+
import type { ReactNode, ComponentType, FormHTMLAttributes } from "react";
|
|
1
2
|
import type { PublicFormInstance } from "../types/public";
|
|
2
3
|
export declare const FormContext: import("react").Context<any>;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
export interface FormProps<T = any> extends Omit<FormHTMLAttributes<HTMLFormElement>, 'onSubmit'> {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
formName: string;
|
|
7
|
+
initialValues?: T;
|
|
8
|
+
onFinish?: (values: T, allValues?: any) => void | Promise<void>;
|
|
9
|
+
onReject?: (errorFields: any[]) => void | Promise<void>;
|
|
10
|
+
onFinally?: (result: {
|
|
11
|
+
errorsField: any[];
|
|
12
|
+
values: T;
|
|
13
|
+
withUnRegisteredValues: any;
|
|
14
|
+
}) => void | Promise<void>;
|
|
15
|
+
FormElement?: ComponentType<any>;
|
|
16
|
+
}
|
|
17
|
+
declare function Form<T = any>({ children, formName, initialValues, onFinish, onReject, onFinally, FormElement, ...props }: FormProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
13
18
|
declare namespace Form {
|
|
14
19
|
var useForm: typeof import("./Form").useForm;
|
|
15
20
|
var useWatch: typeof import("./Form").useWatch;
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { cloneDeep, get, isEqual, last, set, uniq } from "lodash";
|
|
3
|
+
import { useTask } from "minh-custom-hooks-release";
|
|
4
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
5
|
+
import { flushSync } from "react-dom";
|
|
6
|
+
import { useShallow } from "zustand/react/shallow"; // Import useShallow
|
|
7
|
+
import FormCleanUp from "../components/Form/FormCleanUp";
|
|
8
|
+
import { useFormListeners, useFormStore } from "../stores/formStore";
|
|
9
|
+
import { getAllNoneObjStringPath } from "../utils/obj.util";
|
|
10
|
+
export const FormContext = createContext(null);
|
|
11
|
+
export default function Form({ children, formName, initialValues, onFinish, onReject, onFinally, FormElement, ...props }) {
|
|
12
|
+
const { getFormItemValue, setInitData, setData, getFormValues, setFormState, setFormInstance, revokeFormInstance, setSubmitHistory, clearFormValues, clearFormInitialValues, clearFormState, } = useFormStore(useShallow((state) => ({
|
|
13
|
+
setInitData: state.setFormInitData,
|
|
14
|
+
getFormValues: state.getFormValues,
|
|
15
|
+
setFormState: state.setFormState,
|
|
16
|
+
setFormInstance: state.setFormInstance,
|
|
17
|
+
revokeFormInstance: state.revokeFormInstance,
|
|
18
|
+
setData: state.setData,
|
|
19
|
+
setSubmitHistory: state.setSubmitHistory,
|
|
20
|
+
getFormItemValue: state.getFormItemValue,
|
|
21
|
+
clearFormValues: state.clearFormValues,
|
|
22
|
+
clearFormInitialValues: state.clearFormInitialValues,
|
|
23
|
+
clearFormState: state.clearFormState,
|
|
24
|
+
// Test, nhớ xóa sau khi xong
|
|
25
|
+
formStates: state.formStates?.[formName],
|
|
26
|
+
})));
|
|
27
|
+
const { getListeners } = useFormListeners(useShallow((state) => {
|
|
28
|
+
return {
|
|
29
|
+
getListeners: state.getListeners,
|
|
30
|
+
};
|
|
31
|
+
}));
|
|
32
|
+
const setFieldValue = (name, value, options) => {
|
|
33
|
+
const listener = getListeners().find((l) => l.name === name && l.formName === formName);
|
|
34
|
+
if (listener) {
|
|
35
|
+
listener.onChange(value, options);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
setData(formName, name, value);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const setFieldValues = (values, options = { notTriggerDirty: false }) => {
|
|
42
|
+
const allStringPath = getAllNoneObjStringPath(values);
|
|
43
|
+
allStringPath.forEach((p) => {
|
|
44
|
+
const listener = getListeners().find((l) => l.name === p && l.formName === formName);
|
|
45
|
+
if (listener) {
|
|
46
|
+
listener.onChange(get(values, listener.name), options);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
setData(formName, p, get(values, p));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
const getFieldValue = (name) => {
|
|
54
|
+
return getFormItemValue(formName, name);
|
|
55
|
+
};
|
|
56
|
+
const getFieldValues = (names = []) => {
|
|
57
|
+
return names.map((name) => ({
|
|
58
|
+
name,
|
|
59
|
+
value: getFormItemValue(formName, name),
|
|
60
|
+
}));
|
|
61
|
+
};
|
|
62
|
+
const setFieldFocus = (name) => {
|
|
63
|
+
const listener = getListeners().find((l) => l.name === name && l.formName === formName);
|
|
64
|
+
if (listener && typeof listener.emitFocus === "function") {
|
|
65
|
+
listener.emitFocus();
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const getFieldErrors = (names) => {
|
|
69
|
+
const listeners = getListeners().filter((l) => names.includes(l.name) && l.formName === formName);
|
|
70
|
+
return listeners.map((l) => ({
|
|
71
|
+
name: l.name,
|
|
72
|
+
errors: l.internalErrors || [],
|
|
73
|
+
}));
|
|
74
|
+
};
|
|
75
|
+
const getAllFieldErrors = () => {
|
|
76
|
+
return getListeners()
|
|
77
|
+
?.filter((l) => {
|
|
78
|
+
return l.internalErrors?.length && l.formName === formName;
|
|
79
|
+
})
|
|
80
|
+
.map((l) => ({
|
|
81
|
+
name: l.name,
|
|
82
|
+
formName: l.formName,
|
|
83
|
+
formItemId: l.formItemId,
|
|
84
|
+
errors: l.internalErrors,
|
|
85
|
+
}));
|
|
86
|
+
};
|
|
87
|
+
// Submit data
|
|
88
|
+
const { run: runSubmit, runAsync: runSubmitAsync, state: _, reset, } = useTask({
|
|
89
|
+
async task(props) {
|
|
90
|
+
try {
|
|
91
|
+
flushSync(setFormState({ formName, submitState: "submitting" }));
|
|
92
|
+
const errorFields = getAllFieldErrors();
|
|
93
|
+
const listeners = getListeners().filter((l) => l.formName === formName);
|
|
94
|
+
const formValues = getFormValues(formName);
|
|
95
|
+
const resultValues = cloneDeep(formValues);
|
|
96
|
+
const cleanValues = {};
|
|
97
|
+
uniq(listeners, (l) => l.name).forEach((l) => {
|
|
98
|
+
set(cleanValues, l.name, get(resultValues, l.name));
|
|
99
|
+
});
|
|
100
|
+
const handleIsolateCase = async () => {
|
|
101
|
+
if (!errorFields?.length) {
|
|
102
|
+
if (typeof props?.externalFinishCallback !== "function") {
|
|
103
|
+
onFinish && (await onFinish(cleanValues, resultValues));
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
if (props?.callBothFinish) {
|
|
107
|
+
onFinish && (await onFinish(cleanValues, resultValues));
|
|
108
|
+
await props?.externalFinishCallback(cleanValues, resultValues);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
await props?.externalFinishCallback(cleanValues, resultValues);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// if (!isEqual(cleanValues, last(getSubmitHistory(formName)))) {
|
|
115
|
+
// setSubmitHistory(formName, cleanValues);
|
|
116
|
+
// }
|
|
117
|
+
setSubmitHistory(formName, cleanValues);
|
|
118
|
+
// if (
|
|
119
|
+
// !isEqual(
|
|
120
|
+
// resultValues,
|
|
121
|
+
// last(submitHistoryWithUnRegisteredValues.current)
|
|
122
|
+
// )
|
|
123
|
+
// ) {
|
|
124
|
+
// submitHistoryWithUnRegisteredValues.current = [
|
|
125
|
+
// ...submitHistoryWithUnRegisteredValues.current,
|
|
126
|
+
// resultValues,
|
|
127
|
+
// ].slice(-10, 10);
|
|
128
|
+
// }
|
|
129
|
+
// resultWatchQueue.current.forEach((l) => {
|
|
130
|
+
// l.emitSubmitTrigger(cleanValues);
|
|
131
|
+
// l.emitDataChange(cleanValues);
|
|
132
|
+
// l.emitDataWithUnRegisteredValuesChange(resultValues);
|
|
133
|
+
// l.emitSubmitTriggerWithUnRegisteredValues(resultValues);
|
|
134
|
+
// });
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
if (typeof props?.externalRejectCallback !== "function") {
|
|
138
|
+
onReject && (await onReject(errorFields));
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
if (props?.callBothReject) {
|
|
142
|
+
onReject && (await onReject(errorFields));
|
|
143
|
+
await props?.externalRejectCallback(errorFields);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
await props?.externalRejectCallback(errorFields);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const handleFinallyCase = async () => {
|
|
152
|
+
if (typeof props?.externalFinallyCallback !== "function") {
|
|
153
|
+
onFinally &&
|
|
154
|
+
(await onFinally({
|
|
155
|
+
errorsField: errorFields,
|
|
156
|
+
values: cleanValues,
|
|
157
|
+
withUnRegisteredValues: resultValues,
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
if (props?.callBothFinally) {
|
|
162
|
+
onFinally &&
|
|
163
|
+
(await onFinally({
|
|
164
|
+
errorsField: errorFields,
|
|
165
|
+
values: cleanValues,
|
|
166
|
+
withUnRegisteredValues: resultValues,
|
|
167
|
+
}));
|
|
168
|
+
await props?.externalFinallyCallback({
|
|
169
|
+
errorsField: errorFields,
|
|
170
|
+
values: cleanValues,
|
|
171
|
+
withUnRegisteredValues: resultValues,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
await props?.externalFinallyCallback({
|
|
176
|
+
errorsField: errorFields,
|
|
177
|
+
values: cleanValues,
|
|
178
|
+
withUnRegisteredValues: resultValues,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
await Promise.allSettled([handleIsolateCase(), handleFinallyCase()]);
|
|
184
|
+
if (errorFields?.length) {
|
|
185
|
+
setFormState({ formName, submitState: "rejected" });
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
setFormState({ formName, submitState: "submitted" });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
console.error("Error in form submit: ", error);
|
|
193
|
+
setFormState({ formName, submitState: "rejected" });
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
const resetFields = (resetOptions) => {
|
|
198
|
+
reset();
|
|
199
|
+
flushSync(setFormState({
|
|
200
|
+
formName,
|
|
201
|
+
isInitied: false,
|
|
202
|
+
}));
|
|
203
|
+
const totalListenerFields = getListeners();
|
|
204
|
+
if (Array.isArray(resetOptions)) {
|
|
205
|
+
totalListenerFields.forEach((l) => {
|
|
206
|
+
const findListenerByName = resetOptions.find((o) => o.name === l.name);
|
|
207
|
+
if (findListenerByName) {
|
|
208
|
+
l.onReset(findListenerByName?.value);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
totalListenerFields
|
|
212
|
+
.filter((l) => resetOptions.find((o) => o.name !== l.name))
|
|
213
|
+
.forEach((l) => {
|
|
214
|
+
l?.onReset?.();
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
else if (typeof resetOptions === "object") {
|
|
218
|
+
const allStringPath = getAllNoneObjStringPath(resetOptions);
|
|
219
|
+
allStringPath.forEach((p) => {
|
|
220
|
+
const listener = totalListenerFields.find((l) => l.name === p && l.formName === formName);
|
|
221
|
+
if (listener) {
|
|
222
|
+
listener.onChange(get(resetOptions, listener.name));
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
setData(formName, p, get(resetOptions, p));
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
totalListenerFields.forEach((l) => {
|
|
231
|
+
l?.onReset?.();
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
setFormState({
|
|
235
|
+
formName,
|
|
236
|
+
isInitied: true,
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
useEffect(() => {
|
|
240
|
+
setInitData(formName, initialValues);
|
|
241
|
+
setFormState({
|
|
242
|
+
formName,
|
|
243
|
+
isInitied: true,
|
|
244
|
+
submitState: "idle",
|
|
245
|
+
});
|
|
246
|
+
setFormInstance({
|
|
247
|
+
formName,
|
|
248
|
+
resetFields,
|
|
249
|
+
submit: runSubmit,
|
|
250
|
+
submitAsync: runSubmitAsync,
|
|
251
|
+
setFieldValue,
|
|
252
|
+
setFieldValues,
|
|
253
|
+
getFieldValue,
|
|
254
|
+
getFieldValues,
|
|
255
|
+
setFieldFocus,
|
|
256
|
+
getFieldErrors,
|
|
257
|
+
});
|
|
258
|
+
return () => {
|
|
259
|
+
revokeFormInstance({ formName });
|
|
260
|
+
clearFormInitialValues(formName);
|
|
261
|
+
clearFormValues(formName);
|
|
262
|
+
clearFormState(formName);
|
|
263
|
+
};
|
|
264
|
+
}, []);
|
|
265
|
+
return (_jsxs(FormContext.Provider, { value: {
|
|
266
|
+
formName,
|
|
267
|
+
}, children: [FormElement ? (_jsx(FormElement, { onSubmit: (e) => {
|
|
268
|
+
e.preventDefault();
|
|
269
|
+
e.stopPropagation();
|
|
270
|
+
runSubmit(undefined);
|
|
271
|
+
}, ...props, children: children })) : (_jsx("form", { onSubmit: (e) => {
|
|
272
|
+
e.preventDefault();
|
|
273
|
+
e.stopPropagation();
|
|
274
|
+
runSubmit(undefined);
|
|
275
|
+
}, ...props, children: children })), _jsx(FormCleanUp, {})] }));
|
|
276
|
+
}
|
|
277
|
+
export function useFormContext() {
|
|
278
|
+
const c = useContext(FormContext);
|
|
279
|
+
if (!c)
|
|
280
|
+
throw new Error("Must be use inside FormProvider");
|
|
281
|
+
return c;
|
|
282
|
+
}
|
|
283
|
+
export function useForm(formNameOrFormInstance) {
|
|
284
|
+
const targetFormName = typeof formNameOrFormInstance === "object" && formNameOrFormInstance !== null
|
|
285
|
+
? formNameOrFormInstance.formName
|
|
286
|
+
: formNameOrFormInstance;
|
|
287
|
+
const formInstance = useFormStore((state) => {
|
|
288
|
+
return state.formInstances.find((i) => i.formName === targetFormName);
|
|
289
|
+
});
|
|
290
|
+
return [formInstance];
|
|
291
|
+
}
|
|
292
|
+
export function useWatch(name, formNameOrFormInstance) {
|
|
293
|
+
const [formInstance] = useForm(formNameOrFormInstance);
|
|
294
|
+
const value = useFormStore((state) => {
|
|
295
|
+
return state.getFormItemValue(formInstance?.formName ?? formNameOrFormInstance, name);
|
|
296
|
+
});
|
|
297
|
+
return value;
|
|
298
|
+
}
|
|
299
|
+
export function useSubmitDataWatch({ formNameOrFormInstance, triggerWhenChange = false, mapFn, }) {
|
|
300
|
+
const [formInstance] = useForm(formNameOrFormInstance);
|
|
301
|
+
const value = useFormStore((state) => {
|
|
302
|
+
return last(get(state.submitHistory, formInstance?.formName));
|
|
303
|
+
});
|
|
304
|
+
const [submitData, setSubmitData] = useState(mapFn ? mapFn(value, null) : value);
|
|
305
|
+
useEffect(() => {
|
|
306
|
+
if (!triggerWhenChange || !isEqual(submitData, value)) {
|
|
307
|
+
setSubmitData(mapFn ? mapFn(value, submitData) : value);
|
|
308
|
+
}
|
|
309
|
+
}, [value, triggerWhenChange]);
|
|
310
|
+
return submitData;
|
|
311
|
+
}
|
|
312
|
+
export const useFormStateWatch = (formNameOrFormInstance) => {
|
|
313
|
+
const [formInstance] = useForm(formNameOrFormInstance);
|
|
314
|
+
const formState = useFormStore((state) => {
|
|
315
|
+
return state.formStates?.[formInstance?.formName];
|
|
316
|
+
});
|
|
317
|
+
return formState;
|
|
318
|
+
};
|
|
319
|
+
Form.useForm = useForm;
|
|
320
|
+
Form.useWatch = useWatch;
|
|
321
|
+
Form.useSubmitDataWatch = useSubmitDataWatch;
|
|
322
|
+
Form.useFormStateWatch = useFormStateWatch;
|