react-form-manage 1.0.8-beta.26 → 1.0.8-beta.27
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 +7 -0
- package/dist/providers/Form.d.ts +6 -0
- package/dist/providers/Form.js +15 -2
- package/dist/test/TestWatchNormalize.d.ts +3 -0
- package/dist/test/TestWatchNormalize.js +23 -0
- package/package.json +1 -1
- package/src/App.tsx +4 -2
- package/src/providers/Form.tsx +26 -1
- package/src/test/TestWatchNormalize.tsx +32 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.0.8-beta.27] - 2026-02-04
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
- Add `useWatchNormalize` hook for watching and normalizing form data
|
|
9
|
+
- Support custom normalization functions for automatic value transformation
|
|
10
|
+
- Simplifies data format conversion during form value watching
|
|
11
|
+
|
|
5
12
|
## [1.0.8-beta.25] - 2026-02-04
|
|
6
13
|
|
|
7
14
|
### Changes
|
package/dist/providers/Form.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ declare namespace Form {
|
|
|
22
22
|
var useForm: typeof import("./Form").useForm;
|
|
23
23
|
var Item: typeof FormItem;
|
|
24
24
|
var useWatch: typeof import("./Form").useWatch;
|
|
25
|
+
var useWatchNormalized: typeof import("./Form").useWatchNormalized;
|
|
25
26
|
var useSubmitDataWatch: typeof import("./Form").useSubmitDataWatch;
|
|
26
27
|
var useFormStateWatch: <T = any>(formNameOrFormInstance?: string | PublicFormInstance<T>) => any;
|
|
27
28
|
var useFormItemStateWatch: <T = any>(nameOrFormItemId: string, formNameOrFormInstance?: string | PublicFormInstance<T>) => UseFormItemStateWatchReturn;
|
|
@@ -30,6 +31,11 @@ export default Form;
|
|
|
30
31
|
export declare function useFormContext(): any;
|
|
31
32
|
export declare function useForm<T = any>(formNameOrFormInstance?: string | PublicFormInstance<T>): PublicFormInstance<T>[];
|
|
32
33
|
export declare function useWatch<T = any>(name: keyof T & string, formNameOrFormInstance?: string | PublicFormInstance<T>): T[keyof T] | undefined;
|
|
34
|
+
export declare function useWatchNormalized<T, TFn extends (v: any) => any>({ name, normalizeFn, formNameOrFormInstance, }: {
|
|
35
|
+
name: keyof T & string;
|
|
36
|
+
normalizeFn: TFn;
|
|
37
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>;
|
|
38
|
+
}): ReturnType<TFn>;
|
|
33
39
|
export declare function useSubmitDataWatch<T = any>({ formNameOrFormInstance, triggerWhenChange, mapFn, }: {
|
|
34
40
|
formNameOrFormInstance?: string | PublicFormInstance<T>;
|
|
35
41
|
triggerWhenChange?: boolean;
|
package/dist/providers/Form.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { cloneDeep, filter, get, isEqual, isNil, isPlainObject, last, set, uniqBy } from "lodash";
|
|
3
3
|
import { useTask } from "minh-custom-hooks-release";
|
|
4
|
-
import { createContext, useContext, useEffect, useState } from "react";
|
|
4
|
+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
|
|
5
5
|
import { flushSync } from "react-dom";
|
|
6
6
|
import { useShallow } from "zustand/react/shallow";
|
|
7
7
|
import FormItem from "../components/Form/FormItem";
|
|
@@ -333,6 +333,17 @@ function useWatch(name, formNameOrFormInstance) {
|
|
|
333
333
|
});
|
|
334
334
|
return value;
|
|
335
335
|
}
|
|
336
|
+
function useWatchNormalized({ name, normalizeFn, formNameOrFormInstance }) {
|
|
337
|
+
const [formInstance] = useForm(formNameOrFormInstance);
|
|
338
|
+
const rawValue = useFormStore((state) => {
|
|
339
|
+
var _a;
|
|
340
|
+
return state.getFormItemValue((_a = formInstance == null ? void 0 : formInstance.formName) != null ? _a : formNameOrFormInstance, name);
|
|
341
|
+
});
|
|
342
|
+
const normalizedValue = useMemo(() => {
|
|
343
|
+
return normalizeFn(rawValue);
|
|
344
|
+
}, [rawValue, normalizeFn]);
|
|
345
|
+
return normalizedValue;
|
|
346
|
+
}
|
|
336
347
|
function useSubmitDataWatch({ formNameOrFormInstance, triggerWhenChange = false, mapFn }) {
|
|
337
348
|
const [formInstance] = useForm(formNameOrFormInstance);
|
|
338
349
|
const value = useFormStore((state) => {
|
|
@@ -368,6 +379,7 @@ const useFormItemStateWatch = (nameOrFormItemId, formNameOrFormInstance) => {
|
|
|
368
379
|
Form.useForm = useForm;
|
|
369
380
|
Form.Item = FormItem;
|
|
370
381
|
Form.useWatch = useWatch;
|
|
382
|
+
Form.useWatchNormalized = useWatchNormalized;
|
|
371
383
|
Form.useSubmitDataWatch = useSubmitDataWatch;
|
|
372
384
|
Form.useFormStateWatch = useFormStateWatch;
|
|
373
385
|
Form.useFormItemStateWatch = useFormItemStateWatch;
|
|
@@ -379,5 +391,6 @@ export {
|
|
|
379
391
|
useFormItemStateWatch,
|
|
380
392
|
useFormStateWatch,
|
|
381
393
|
useSubmitDataWatch,
|
|
382
|
-
useWatch
|
|
394
|
+
useWatch,
|
|
395
|
+
useWatchNormalized
|
|
383
396
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Input } from "antd";
|
|
3
|
+
import React from "react";
|
|
4
|
+
import Form from "../providers/Form";
|
|
5
|
+
function TestWatchNormalize({}) {
|
|
6
|
+
const normalizeData = Form.useWatchNormalized({
|
|
7
|
+
name: "normalizeItem",
|
|
8
|
+
formNameOrFormInstance: "testNormalize",
|
|
9
|
+
normalizeFn: (value) => {
|
|
10
|
+
if (typeof value === "string") {
|
|
11
|
+
return value.toUpperCase();
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
}, [normalizeData]);
|
|
18
|
+
return _jsxs(Form, { formName: "testNormalize", children: ["Test Watch Normalize", _jsx(Form.Item, { name: "normalizeItem", children: _jsx(Input, {}) })] });
|
|
19
|
+
}
|
|
20
|
+
var stdin_default = TestWatchNormalize;
|
|
21
|
+
export {
|
|
22
|
+
stdin_default as default
|
|
23
|
+
};
|
package/package.json
CHANGED
package/src/App.tsx
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Input } from "antd";
|
|
2
2
|
import { motion } from "framer-motion";
|
|
3
3
|
import Form from "./providers/Form";
|
|
4
|
-
import CommonTest from "./test/CommonTest";
|
|
5
4
|
import TestListener from "./test/TestListener";
|
|
6
5
|
import TestWrapperFormItem from "./test/TestWrapperFormItem";
|
|
7
6
|
|
|
8
7
|
import { Form as AntdForm } from "antd";
|
|
8
|
+
import TestWatchNormalize from "./test/TestWatchNormalize";
|
|
9
9
|
|
|
10
10
|
function TestFormWatch() {
|
|
11
11
|
const watchValue = Form.useWatch("numericCode");
|
|
@@ -30,11 +30,13 @@ const App = () => {
|
|
|
30
30
|
</AntdForm>
|
|
31
31
|
|
|
32
32
|
{/* Hidden Test */}
|
|
33
|
-
<CommonTest />
|
|
33
|
+
{/* <CommonTest /> */}
|
|
34
34
|
{/* <Form formName="TestDialog" >
|
|
35
35
|
<TestDialog />
|
|
36
36
|
</Form> */}
|
|
37
37
|
|
|
38
|
+
<TestWatchNormalize />
|
|
39
|
+
|
|
38
40
|
<TestListener />
|
|
39
41
|
<TestWrapperFormItem />
|
|
40
42
|
</div>
|
package/src/providers/Form.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from "lodash";
|
|
12
12
|
import { useTask } from "minh-custom-hooks-release";
|
|
13
13
|
import type { ComponentType, FormHTMLAttributes, ReactNode } from "react";
|
|
14
|
-
import { createContext, useContext, useEffect, useState } from "react";
|
|
14
|
+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
|
|
15
15
|
import { flushSync } from "react-dom";
|
|
16
16
|
import { useShallow } from "zustand/react/shallow"; // Import useShallow
|
|
17
17
|
import FormItem from "../components/Form/FormItem";
|
|
@@ -537,6 +537,30 @@ export function useWatch<T = any>(
|
|
|
537
537
|
return value as T[keyof T] | undefined;
|
|
538
538
|
}
|
|
539
539
|
|
|
540
|
+
export function useWatchNormalized<T, TFn extends (v: any) => any>({
|
|
541
|
+
name,
|
|
542
|
+
normalizeFn,
|
|
543
|
+
formNameOrFormInstance,
|
|
544
|
+
}: {
|
|
545
|
+
name: keyof T & string;
|
|
546
|
+
normalizeFn: TFn;
|
|
547
|
+
formNameOrFormInstance?: string | PublicFormInstance<T>;
|
|
548
|
+
}) {
|
|
549
|
+
const [formInstance] = useForm<T>(formNameOrFormInstance as any);
|
|
550
|
+
const rawValue = useFormStore((state) => {
|
|
551
|
+
return state.getFormItemValue(
|
|
552
|
+
formInstance?.formName ?? (formNameOrFormInstance as any),
|
|
553
|
+
name as string,
|
|
554
|
+
);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
const normalizedValue = useMemo(() => {
|
|
558
|
+
return normalizeFn(rawValue);
|
|
559
|
+
}, [rawValue, normalizeFn]);
|
|
560
|
+
|
|
561
|
+
return normalizedValue as ReturnType<TFn>;
|
|
562
|
+
}
|
|
563
|
+
|
|
540
564
|
export function useSubmitDataWatch<T = any>({
|
|
541
565
|
formNameOrFormInstance,
|
|
542
566
|
triggerWhenChange = false,
|
|
@@ -605,6 +629,7 @@ export const useFormItemStateWatch = <T = any,>(
|
|
|
605
629
|
Form.useForm = useForm;
|
|
606
630
|
Form.Item = FormItem;
|
|
607
631
|
Form.useWatch = useWatch;
|
|
632
|
+
Form.useWatchNormalized = useWatchNormalized;
|
|
608
633
|
Form.useSubmitDataWatch = useSubmitDataWatch;
|
|
609
634
|
Form.useFormStateWatch = useFormStateWatch;
|
|
610
635
|
Form.useFormItemStateWatch = useFormItemStateWatch;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Input } from "antd";
|
|
2
|
+
import React from "react";
|
|
3
|
+
import Form from "../providers/Form";
|
|
4
|
+
|
|
5
|
+
type Props = {};
|
|
6
|
+
|
|
7
|
+
function TestWatchNormalize({}: Props) {
|
|
8
|
+
const normalizeData = Form.useWatchNormalized({
|
|
9
|
+
name: "normalizeItem",
|
|
10
|
+
formNameOrFormInstance: "testNormalize",
|
|
11
|
+
normalizeFn: (value) => {
|
|
12
|
+
if (typeof value === "string") {
|
|
13
|
+
return value.toUpperCase();
|
|
14
|
+
}
|
|
15
|
+
return value;
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
console.log("Normalized Data: ", normalizeData);
|
|
21
|
+
}, [normalizeData]);
|
|
22
|
+
return (
|
|
23
|
+
<Form formName="testNormalize">
|
|
24
|
+
Test Watch Normalize
|
|
25
|
+
<Form.Item name="normalizeItem">
|
|
26
|
+
<Input />
|
|
27
|
+
</Form.Item>
|
|
28
|
+
</Form>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default TestWatchNormalize;
|