react-form-manage 1.0.5 → 1.0.6-beta.1
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 +44 -0
- package/dist/components/Form/FormCleanUp.d.ts +2 -0
- package/dist/components/Form/FormCleanUp.js +46 -0
- package/dist/components/Form/FormItem.d.ts +18 -0
- package/dist/components/Form/FormItem.js +46 -0
- package/dist/components/Form/FormList.d.ts +8 -0
- package/dist/components/Form/FormList.js +11 -0
- package/dist/components/Form/InputWrapper.d.ts +6 -0
- package/dist/components/Form/InputWrapper.js +7 -0
- package/dist/components/Input.d.ts +5 -0
- package/dist/components/Input.js +8 -0
- package/dist/constants/validation.d.ts +31 -0
- package/dist/constants/validation.js +33 -0
- package/dist/contexts/FormContext.d.ts +2 -0
- package/dist/contexts/FormContext.js +2 -0
- package/dist/hooks/useFormItemControl.d.ts +22 -0
- package/dist/hooks/useFormItemControl.js +390 -0
- package/dist/hooks/useFormListControl.d.ts +26 -0
- package/dist/hooks/useFormListControl.js +280 -0
- package/dist/index.d.ts +8 -134
- package/dist/index.js +9 -0
- package/dist/providers/Form.d.ts +28 -0
- package/dist/providers/Form.js +322 -0
- package/dist/stores/formStore.d.ts +25 -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/types/public.js +1 -0
- package/dist/utils/obj.util.d.ts +2 -0
- package/dist/utils/obj.util.js +24 -0
- package/package.json +30 -47
- 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 +60 -0
- package/src/components/Form/FormList.tsx +13 -0
- package/src/components/Form/InputWrapper.tsx +17 -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 +30 -0
- package/src/main.tsx +10 -0
- package/src/providers/Form.tsx +429 -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/index.cjs +0 -1937
- 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 -1926
- package/dist/index.esm.js.map +0 -1
- package/src/types/components.d.ts +0 -135
package/dist/index.d.ts
CHANGED
|
@@ -1,135 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
values: Partial<TValues>,
|
|
10
|
-
allValues?: TValues,
|
|
11
|
-
) => void | Promise<void>;
|
|
12
|
-
onReject?: (errors: any) => void | Promise<void>;
|
|
13
|
-
onFinally?: (payload: {
|
|
14
|
-
errorsField: any;
|
|
15
|
-
values: Partial<TValues>;
|
|
16
|
-
withUnRegisteredValues: TValues;
|
|
17
|
-
}) => void | Promise<void>;
|
|
18
|
-
FormElement?: React.ComponentType<any>;
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
declare function Form<TValues = Record<string, any>>(
|
|
23
|
-
props: FormProps<TValues>,
|
|
24
|
-
): JSX.Element;
|
|
25
|
-
|
|
26
|
-
// Standalone generic hooks (also attached to Form as static properties at runtime)
|
|
27
|
-
export function useForm<TValues = Record<string, any>>(
|
|
28
|
-
formNameOrFormInstance?: any,
|
|
29
|
-
): [any];
|
|
30
|
-
|
|
31
|
-
export function useWatch<
|
|
32
|
-
TValues = Record<string, any>,
|
|
33
|
-
K extends keyof TValues = string & keyof TValues,
|
|
34
|
-
>(
|
|
35
|
-
name: K,
|
|
36
|
-
formNameOrFormInstance?: any,
|
|
37
|
-
): K extends keyof TValues ? TValues[K] : any;
|
|
38
|
-
|
|
39
|
-
export function useSubmitDataWatch<TValues = any>(opts: {
|
|
40
|
-
formNameOrFormInstance?: any;
|
|
41
|
-
triggerWhenChange?: boolean;
|
|
42
|
-
mapFn?: (v: any, prev: any) => any;
|
|
43
|
-
}): any;
|
|
44
|
-
|
|
45
|
-
export function useFormStateWatch<TValues = any>(
|
|
46
|
-
formNameOrFormInstance?: any,
|
|
47
|
-
): any;
|
|
48
|
-
|
|
49
|
-
declare namespace Form {
|
|
50
|
-
// expose same hook names on the Form object
|
|
51
|
-
export { useForm, useWatch, useSubmitDataWatch, useFormStateWatch };
|
|
52
|
-
}
|
|
53
|
-
|
|
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, };
|
|
54
9
|
export default Form;
|
|
55
|
-
export { Form };
|
|
56
|
-
|
|
57
|
-
// FormItem (generic for the field value type)
|
|
58
|
-
export interface FormItemProps<T = any> {
|
|
59
|
-
children: React.ReactElement;
|
|
60
|
-
name: string;
|
|
61
|
-
formName?: string;
|
|
62
|
-
initialValue?: T;
|
|
63
|
-
formItemId?: string;
|
|
64
|
-
rules?: any[];
|
|
65
|
-
valuePropName?: string;
|
|
66
|
-
getValueFromEvent?: (e: any) => T;
|
|
67
|
-
[key: string]: any;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function FormItem<T = any>(props: FormItemProps<T>): JSX.Element;
|
|
71
|
-
|
|
72
|
-
// FormList (generic for item shape)
|
|
73
|
-
export interface FormListProps<TItem = any> {
|
|
74
|
-
name: string;
|
|
75
|
-
initialValues?: TItem[];
|
|
76
|
-
form?: any;
|
|
77
|
-
formName?: string;
|
|
78
|
-
children: (listFields: any[], actions: any) => React.ReactNode;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function FormList<TItem = any>(props: FormListProps<TItem>): JSX.Element;
|
|
82
|
-
|
|
83
|
-
// Input components
|
|
84
|
-
export interface BasicInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
85
|
-
onChange?: (value: any, ...args: any[]) => void;
|
|
86
|
-
isValidating?: boolean;
|
|
87
|
-
[key: string]: any;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// InputWrapper
|
|
91
|
-
export interface InputWrapperProps {
|
|
92
|
-
children: React.ReactElement;
|
|
93
|
-
errors?: Array<{ ruleName?: string; message?: string }>;
|
|
94
|
-
isDirty?: boolean;
|
|
95
|
-
formState?: any;
|
|
96
|
-
name?: string;
|
|
97
|
-
formItemId?: string;
|
|
98
|
-
[key: string]: any;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function InputWrapper(props: InputWrapperProps): JSX.Element;
|
|
102
|
-
|
|
103
|
-
// Cleanup component (internal)
|
|
104
|
-
export function FormCleanUp(): JSX.Element;
|
|
105
|
-
|
|
106
|
-
// Hooks in src/hooks
|
|
107
|
-
export function useFormItemControl<T = any>(opts: {
|
|
108
|
-
formName?: string;
|
|
109
|
-
form?: any;
|
|
110
|
-
name: string;
|
|
111
|
-
initialValue?: T;
|
|
112
|
-
formItemId?: string;
|
|
113
|
-
rules?: any[];
|
|
114
|
-
elementRef?: React.RefObject<any>;
|
|
115
|
-
}): {
|
|
116
|
-
value: T | undefined;
|
|
117
|
-
onChange: (value: any, options?: any) => void;
|
|
118
|
-
state: any;
|
|
119
|
-
errors: any;
|
|
120
|
-
onFocus: () => void;
|
|
121
|
-
isDirty?: boolean;
|
|
122
|
-
onReset?: (value?: any) => void;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
export function useFormListControl<TItem = any>(opts: {
|
|
126
|
-
name: string;
|
|
127
|
-
form?: any;
|
|
128
|
-
initialValues?: TItem[];
|
|
129
|
-
formName?: string;
|
|
130
|
-
}): {
|
|
131
|
-
listFields: Array<{ name: string; key: string }>;
|
|
132
|
-
add: (index?: number) => void;
|
|
133
|
-
remove: (opts: { index?: number; key?: string }) => void;
|
|
134
|
-
move: (opts: { from?: number; fromKey?: string; to: number }) => void;
|
|
135
|
-
};
|
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;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PublicFormInstance } from "../types/public";
|
|
2
|
+
export declare const FormContext: import("react").Context<any>;
|
|
3
|
+
declare function Form({ children, formName, initialValues, onFinish, onReject, onFinally, FormElement, ...props }: {
|
|
4
|
+
[x: string]: any;
|
|
5
|
+
children: any;
|
|
6
|
+
formName: any;
|
|
7
|
+
initialValues: any;
|
|
8
|
+
onFinish: any;
|
|
9
|
+
onReject: any;
|
|
10
|
+
onFinally: any;
|
|
11
|
+
FormElement: any;
|
|
12
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
declare namespace Form {
|
|
14
|
+
var useForm: typeof import("./Form").useForm;
|
|
15
|
+
var useWatch: typeof import("./Form").useWatch;
|
|
16
|
+
var useSubmitDataWatch: typeof import("./Form").useSubmitDataWatch;
|
|
17
|
+
var useFormStateWatch: <T = any>(formNameOrFormInstance?: string | PublicFormInstance<T>) => any;
|
|
18
|
+
}
|
|
19
|
+
export default Form;
|
|
20
|
+
export declare function useFormContext(): any;
|
|
21
|
+
export declare function useForm<T = any>(formNameOrFormInstance?: string | PublicFormInstance<T>): PublicFormInstance<T>[];
|
|
22
|
+
export declare function useWatch<T = any>(name: keyof T & string, formNameOrFormInstance?: string | PublicFormInstance<T>): T[keyof T] | undefined;
|
|
23
|
+
export declare function useSubmitDataWatch<T = any>({ formNameOrFormInstance, triggerWhenChange, mapFn, }: {
|
|
24
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>;
|
|
25
|
+
triggerWhenChange?: boolean;
|
|
26
|
+
mapFn?: (v: any, prev: any) => any;
|
|
27
|
+
}): T | undefined;
|
|
28
|
+
export declare const useFormStateWatch: <T = any>(formNameOrFormInstance?: string | PublicFormInstance<T>) => any;
|
|
@@ -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;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface FormInstance {
|
|
2
|
+
formName: string;
|
|
3
|
+
resetFields?: any;
|
|
4
|
+
submit?: any;
|
|
5
|
+
submitAsync?: any;
|
|
6
|
+
setFieldValue?: any;
|
|
7
|
+
setFieldValues?: any;
|
|
8
|
+
getFieldValue?: any;
|
|
9
|
+
getFieldValues?: any;
|
|
10
|
+
getFieldErrors?: any;
|
|
11
|
+
setFieldFocus?: any;
|
|
12
|
+
}
|
|
13
|
+
interface FormStoreState {
|
|
14
|
+
forms: Record<string, any>;
|
|
15
|
+
initialValues: Record<string, any>;
|
|
16
|
+
formStates: Record<string, any>;
|
|
17
|
+
cacheData: Record<string, any>;
|
|
18
|
+
formInstances: FormInstance[];
|
|
19
|
+
submitHistory: Record<string, any[]>;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
}
|
|
22
|
+
export declare const useFormStore: import("zustand").UseBoundStore<import("zustand").StoreApi<FormStoreState>>;
|
|
23
|
+
export declare const useFormListeners: import("zustand").UseBoundStore<import("zustand").StoreApi<any>>;
|
|
24
|
+
export declare const useFormCleanUp: import("zustand").UseBoundStore<import("zustand").StoreApi<any>>;
|
|
25
|
+
export {};
|