react-form-manage 1.0.6-beta.2 → 1.0.6-beta.4
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 +12 -0
- package/README.md +44 -17
- package/dist/components/Form/FormItem.d.ts +2 -1
- package/dist/hooks/useFormItemControl.d.ts +2 -1
- package/dist/hooks/useFormItemControl.js +25 -10
- package/dist/index.d.ts +2 -2
- package/dist/providers/Form.d.ts +3 -2
- package/dist/providers/Form.js +21 -4
- package/dist/stores/formStore.d.ts +12 -9
- package/dist/stores/formStore.js +1 -1
- package/dist/types/public.d.ts +37 -9
- package/package.json +1 -1
- package/src/App.tsx +74 -3
- package/src/components/Form/FormItem.tsx +2 -1
- package/src/hooks/useFormItemControl.ts +39 -10
- package/src/index.ts +2 -0
- package/src/providers/Form.tsx +48 -10
- package/src/stores/formStore.ts +286 -284
- package/src/types/public.ts +37 -9
package/src/providers/Form.tsx
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
import { cloneDeep, get, isEqual, last, set, uniq } from "lodash";
|
|
2
2
|
import { useTask } from "minh-custom-hooks-release";
|
|
3
|
+
import type { ComponentType, FormHTMLAttributes, ReactNode } from "react";
|
|
3
4
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
4
|
-
import type { ReactNode, ComponentType, FormHTMLAttributes } from "react";
|
|
5
5
|
import { flushSync } from "react-dom";
|
|
6
6
|
import { useShallow } from "zustand/react/shallow"; // Import useShallow
|
|
7
7
|
import FormCleanUp from "../components/Form/FormCleanUp";
|
|
8
8
|
import { useFormListeners, useFormStore } from "../stores/formStore";
|
|
9
9
|
import type { PublicFormInstance } from "../types/public";
|
|
10
10
|
import { getAllNoneObjStringPath } from "../utils/obj.util";
|
|
11
|
+
export type { ValidationRule } from "../types/public";
|
|
11
12
|
|
|
12
13
|
export const FormContext = createContext(null);
|
|
13
14
|
|
|
14
|
-
export interface FormProps<T = any> extends Omit<
|
|
15
|
+
export interface FormProps<T = any> extends Omit<
|
|
16
|
+
FormHTMLAttributes<HTMLFormElement>,
|
|
17
|
+
"onSubmit"
|
|
18
|
+
> {
|
|
15
19
|
children: ReactNode;
|
|
16
20
|
formName: string;
|
|
17
21
|
initialValues?: T;
|
|
18
22
|
onFinish?: (values: T, allValues?: any) => void | Promise<void>;
|
|
19
23
|
onReject?: (errorFields: any[]) => void | Promise<void>;
|
|
20
|
-
onFinally?: (result: {
|
|
24
|
+
onFinally?: (result: {
|
|
25
|
+
errorsField: any[];
|
|
26
|
+
values: T;
|
|
27
|
+
withUnRegisteredValues: any;
|
|
28
|
+
}) => void | Promise<void>;
|
|
21
29
|
FormElement?: ComponentType<any>;
|
|
22
30
|
}
|
|
23
31
|
|
|
@@ -32,6 +40,7 @@ export default function Form<T = any>({
|
|
|
32
40
|
...props
|
|
33
41
|
}: FormProps<T>) {
|
|
34
42
|
const {
|
|
43
|
+
appInitValue,
|
|
35
44
|
getFormItemValue,
|
|
36
45
|
setInitData,
|
|
37
46
|
setData,
|
|
@@ -45,7 +54,8 @@ export default function Form<T = any>({
|
|
|
45
54
|
clearFormState,
|
|
46
55
|
} = useFormStore(
|
|
47
56
|
useShallow((state) => ({
|
|
48
|
-
|
|
57
|
+
appInitValue: state.initialValues,
|
|
58
|
+
setInitData: state.setInitData,
|
|
49
59
|
getFormValues: state.getFormValues,
|
|
50
60
|
setFormState: state.setFormState,
|
|
51
61
|
setFormInstance: state.setFormInstance,
|
|
@@ -303,7 +313,19 @@ export default function Form<T = any>({
|
|
|
303
313
|
};
|
|
304
314
|
|
|
305
315
|
useEffect(() => {
|
|
306
|
-
|
|
316
|
+
// set initial values
|
|
317
|
+
if (initialValues) {
|
|
318
|
+
const allStringPath = getAllNoneObjStringPath(initialValues);
|
|
319
|
+
allStringPath.forEach((p) => {
|
|
320
|
+
// console.log(
|
|
321
|
+
// "Set initial value for form",
|
|
322
|
+
// formName,
|
|
323
|
+
// p,
|
|
324
|
+
// get(initialValues, p),
|
|
325
|
+
// );
|
|
326
|
+
setInitData(formName, p, get(initialValues, p));
|
|
327
|
+
});
|
|
328
|
+
}
|
|
307
329
|
setFormState({
|
|
308
330
|
formName,
|
|
309
331
|
isInitied: true,
|
|
@@ -330,6 +352,10 @@ export default function Form<T = any>({
|
|
|
330
352
|
};
|
|
331
353
|
}, []);
|
|
332
354
|
|
|
355
|
+
// useEffect(() => {
|
|
356
|
+
// // console.log("Form appInitValue changed", appInitValue);
|
|
357
|
+
// }, [appInitValue]);
|
|
358
|
+
|
|
333
359
|
return (
|
|
334
360
|
<FormContext.Provider
|
|
335
361
|
value={{
|
|
@@ -374,9 +400,12 @@ export function useFormContext() {
|
|
|
374
400
|
return c;
|
|
375
401
|
}
|
|
376
402
|
|
|
377
|
-
export function useForm<T = any>(
|
|
403
|
+
export function useForm<T = any>(
|
|
404
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>,
|
|
405
|
+
) {
|
|
378
406
|
const targetFormName =
|
|
379
|
-
typeof formNameOrFormInstance === "object" &&
|
|
407
|
+
typeof formNameOrFormInstance === "object" &&
|
|
408
|
+
formNameOrFormInstance !== null
|
|
380
409
|
? (formNameOrFormInstance as PublicFormInstance<T>).formName
|
|
381
410
|
: (formNameOrFormInstance as string | undefined);
|
|
382
411
|
|
|
@@ -387,7 +416,10 @@ export function useForm<T = any>(formNameOrFormInstance?: string | PublicFormIns
|
|
|
387
416
|
return [formInstance];
|
|
388
417
|
}
|
|
389
418
|
|
|
390
|
-
export function useWatch<T = any>(
|
|
419
|
+
export function useWatch<T = any>(
|
|
420
|
+
name: keyof T & string,
|
|
421
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>,
|
|
422
|
+
) {
|
|
391
423
|
const [formInstance] = useForm<T>(formNameOrFormInstance as any);
|
|
392
424
|
|
|
393
425
|
const value = useFormStore((state) => {
|
|
@@ -404,7 +436,11 @@ export function useSubmitDataWatch<T = any>({
|
|
|
404
436
|
formNameOrFormInstance,
|
|
405
437
|
triggerWhenChange = false,
|
|
406
438
|
mapFn,
|
|
407
|
-
}: {
|
|
439
|
+
}: {
|
|
440
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>;
|
|
441
|
+
triggerWhenChange?: boolean;
|
|
442
|
+
mapFn?: (v: any, prev: any) => any;
|
|
443
|
+
}) {
|
|
408
444
|
const [formInstance] = useForm<T>(formNameOrFormInstance as any);
|
|
409
445
|
|
|
410
446
|
const value = useFormStore((state) => {
|
|
@@ -424,7 +460,9 @@ export function useSubmitDataWatch<T = any>({
|
|
|
424
460
|
return submitData as T | undefined;
|
|
425
461
|
}
|
|
426
462
|
|
|
427
|
-
export const useFormStateWatch = <T = any
|
|
463
|
+
export const useFormStateWatch = <T = any,>(
|
|
464
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>,
|
|
465
|
+
) => {
|
|
428
466
|
const [formInstance] = useForm<T>(formNameOrFormInstance as any);
|
|
429
467
|
|
|
430
468
|
const formState = useFormStore((state) => {
|