react-form-manage 1.0.6-beta.1 → 1.0.6-beta.3
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 +15 -0
- package/dist/components/Form/FormItem.d.ts +12 -17
- package/dist/components/Form/FormList.d.ts +23 -7
- package/dist/components/Form/InputWrapper.d.ts +10 -5
- package/dist/hooks/useFormItemControl.d.ts +2 -1
- package/dist/hooks/useFormItemControl.js +25 -10
- package/dist/index.d.ts +5 -5
- package/dist/providers/Form.d.ts +16 -10
- 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 +15 -2
- package/src/components/Form/FormList.tsx +10 -1
- package/src/components/Form/InputWrapper.tsx +8 -1
- package/src/hooks/useFormItemControl.ts +39 -10
- package/src/index.ts +10 -3
- package/src/providers/Form.tsx +59 -10
- package/src/stores/formStore.ts +286 -284
- package/src/types/public.ts +37 -9
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [1.0.6-beta.3] - 2026-01-22
|
|
6
|
+
|
|
7
|
+
- Add `ValidationRule` typing and export
|
|
8
|
+
- Support custom regex `pattern` in validation
|
|
9
|
+
- Update usage docs with numeric-only example
|
|
10
|
+
- Add demo field `numericCode` using pattern rule
|
|
11
|
+
|
|
12
|
+
## [1.0.6-beta.2] - 2026-01-22
|
|
13
|
+
|
|
14
|
+
- Fix TypeScript prop types for all components
|
|
15
|
+
- Add FormProps interface with proper optional fields
|
|
16
|
+
- Add FormItemProps, FormListProps, InputWrapperProps interfaces
|
|
17
|
+
- Export type interfaces from package
|
|
18
|
+
- Fix type error where Form component required all props
|
|
19
|
+
|
|
5
20
|
## [1.0.6-beta.1] - 2026-01-22
|
|
6
21
|
|
|
7
22
|
- Full TypeScript migration: Convert all source files from JS to TS
|
|
@@ -1,18 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import type { ValidationRule } from "../../types/public";
|
|
3
|
+
export interface FormItemProps {
|
|
4
|
+
children: ReactElement;
|
|
5
|
+
name: string;
|
|
6
|
+
formName?: string;
|
|
7
|
+
initialValue?: any;
|
|
8
|
+
formItemId?: string;
|
|
9
|
+
rules?: ValidationRule[];
|
|
8
10
|
valuePropName?: string;
|
|
9
|
-
getValueFromEvent: any;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
onChange: (e: any) => void;
|
|
13
|
-
isDirty: boolean;
|
|
14
|
-
errors: any;
|
|
15
|
-
onFocus: () => void;
|
|
16
|
-
validateState: any;
|
|
17
|
-
ref: import("react").RefObject<any>;
|
|
18
|
-
}, HTMLElement>;
|
|
11
|
+
getValueFromEvent?: (event: any) => any;
|
|
12
|
+
}
|
|
13
|
+
export default function FormItem({ children, name, formName, initialValue, formItemId: externalFormItemId, rules, valuePropName, getValueFromEvent, }: FormItemProps): ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
@@ -1,8 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { FormInstance } from "../../stores/formStore";
|
|
2
|
+
export interface FormListProps<T = any> {
|
|
3
|
+
name: string;
|
|
4
|
+
initialValues?: T[];
|
|
5
|
+
form?: FormInstance;
|
|
6
|
+
formName?: string;
|
|
7
|
+
children: (fields: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
key: string;
|
|
10
|
+
}>, operations: {
|
|
11
|
+
add: (index: number) => void;
|
|
12
|
+
remove: (opts: {
|
|
13
|
+
index?: number;
|
|
14
|
+
key?: string;
|
|
15
|
+
}) => void;
|
|
16
|
+
move: (opts: {
|
|
17
|
+
from?: number;
|
|
18
|
+
fromKey?: string;
|
|
19
|
+
to: number;
|
|
20
|
+
}) => void;
|
|
21
|
+
}) => React.ReactNode;
|
|
22
|
+
}
|
|
23
|
+
declare const FormList: <T = any>({ name, initialValues, form, formName, children }: FormListProps<T>) => import("react").ReactNode;
|
|
8
24
|
export default FormList;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
children:
|
|
4
|
-
errors?:
|
|
5
|
-
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
export interface InputWrapperProps {
|
|
3
|
+
children: ReactElement;
|
|
4
|
+
errors?: Array<{
|
|
5
|
+
ruleName: string;
|
|
6
|
+
message: string;
|
|
7
|
+
}>;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
declare const InputWrapper: ({ children, errors, ...props }: InputWrapperProps) => import("react/jsx-runtime").JSX.Element;
|
|
6
11
|
export default InputWrapper;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type RefObject } from "react";
|
|
2
2
|
import type { FormInstance } from "../stores/formStore";
|
|
3
|
+
import type { ValidationRule } from "../types/public";
|
|
3
4
|
type AnyObject = Record<string, any>;
|
|
4
5
|
interface UseFormItemControlProps {
|
|
5
6
|
formName?: string;
|
|
@@ -7,7 +8,7 @@ interface UseFormItemControlProps {
|
|
|
7
8
|
name?: string;
|
|
8
9
|
initialValue?: any;
|
|
9
10
|
formItemId?: string;
|
|
10
|
-
rules?:
|
|
11
|
+
rules?: ValidationRule[];
|
|
11
12
|
elementRef?: RefObject<any> | null;
|
|
12
13
|
}
|
|
13
14
|
interface UseFormItemControlReturn {
|
|
@@ -8,12 +8,14 @@ import { useFormCleanUp, useFormListeners, useFormStore, } from "../stores/formS
|
|
|
8
8
|
const VALID_PREMITIVE_TYPE = ["string", "number", "undefined"];
|
|
9
9
|
export default function useFormItemControl({ formName, form, name, initialValue, formItemId, rules, elementRef, }) {
|
|
10
10
|
const contextForm = useFormContext();
|
|
11
|
-
const { value, setData, getCacheData, getFormValues } = useFormStore(useShallow((state) => {
|
|
11
|
+
const { value, setData, getCacheData, getFormValues, getFormState, isStateInitied, } = useFormStore(useShallow((state) => {
|
|
12
12
|
return {
|
|
13
13
|
value: get(state.forms, `${formName || form?.formName || contextForm?.formName}.${name}`),
|
|
14
14
|
setData: state.setData,
|
|
15
15
|
getCacheData: state.getCacheData,
|
|
16
16
|
getFormValues: state.getFormValues,
|
|
17
|
+
getFormState: state.getFormState,
|
|
18
|
+
isStateInitied: state.formStates?.[formName || form?.formName || contextForm?.formName]?.isInitied,
|
|
17
19
|
};
|
|
18
20
|
}));
|
|
19
21
|
const { setCleanUpStack } = useFormCleanUp(useShallow((state) => ({
|
|
@@ -53,6 +55,7 @@ export default function useFormItemControl({ formName, form, name, initialValue,
|
|
|
53
55
|
// value
|
|
54
56
|
// );
|
|
55
57
|
setInitData(formName || form?.formName || contextForm?.formName, name, value);
|
|
58
|
+
onChange(value, { notTriggerDirty: true });
|
|
56
59
|
};
|
|
57
60
|
const onFocus = () => {
|
|
58
61
|
setListener({
|
|
@@ -171,6 +174,14 @@ export default function useFormItemControl({ formName, form, name, initialValue,
|
|
|
171
174
|
}
|
|
172
175
|
}
|
|
173
176
|
}
|
|
177
|
+
// Custom pattern
|
|
178
|
+
if (r.pattern && !r.pattern.test(fieldValue || "")) {
|
|
179
|
+
listErrors.push({
|
|
180
|
+
ruleName,
|
|
181
|
+
message: r.message,
|
|
182
|
+
});
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
174
185
|
// Password
|
|
175
186
|
if (r.isPassword && !IS_PASSWORD_REGEX.test(fieldValue)) {
|
|
176
187
|
// tempMessage.push(IS_PASSWORD_INVALID_MESSAGE);
|
|
@@ -319,18 +330,22 @@ export default function useFormItemControl({ formName, form, name, initialValue,
|
|
|
319
330
|
// console.log({ internalInitValue, value, initialValue });
|
|
320
331
|
// console.log("Init item value: ", name, value, internalInitValue);
|
|
321
332
|
// console.log("internalInitValue: ", internalInitValue);
|
|
322
|
-
if (
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
333
|
+
if (isStateInitied) {
|
|
334
|
+
// console.log("Skip set init data when form is resetting");
|
|
335
|
+
if (isNil(value)) {
|
|
336
|
+
if (isNil(internalInitValue)) {
|
|
337
|
+
if (isNil(initialValue)) {
|
|
338
|
+
console.log("On init data", initialValue);
|
|
339
|
+
onInitData(initialValue);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
onChange(internalInitValue, { notTriggerDirty: true });
|
|
327
344
|
}
|
|
328
345
|
}
|
|
329
|
-
|
|
330
|
-
onChange(internalInitValue);
|
|
331
|
-
}
|
|
346
|
+
return;
|
|
332
347
|
}
|
|
333
|
-
}, [
|
|
348
|
+
}, [isStateInitied]);
|
|
334
349
|
useEffect(() => {
|
|
335
350
|
if (!listener) {
|
|
336
351
|
setListener({
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +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";
|
|
1
|
+
import Form, { useForm, useWatch, useSubmitDataWatch, useFormStateWatch, type FormProps, type ValidationRule } from "./providers/Form";
|
|
2
|
+
import FormItem, { type FormItemProps } from "./components/Form/FormItem";
|
|
3
|
+
import FormList, { type FormListProps } from "./components/Form/FormList";
|
|
4
4
|
import Input from "./components/Input";
|
|
5
|
-
import InputWrapper from "./components/Form/InputWrapper";
|
|
5
|
+
import InputWrapper, { type InputWrapperProps } from "./components/Form/InputWrapper";
|
|
6
6
|
import useFormItemControl from "./hooks/useFormItemControl";
|
|
7
7
|
import useFormListControl from "./hooks/useFormListControl";
|
|
8
|
-
export { Form, FormItem, FormList, Input, InputWrapper, useFormItemControl, useFormListControl, useForm, useWatch, useSubmitDataWatch, useFormStateWatch, };
|
|
8
|
+
export { Form, FormItem, FormList, Input, InputWrapper, useFormItemControl, useFormListControl, useForm, useWatch, useSubmitDataWatch, useFormStateWatch, type FormProps, type FormItemProps, type FormListProps, type InputWrapperProps, type ValidationRule, };
|
|
9
9
|
export default Form;
|
package/dist/providers/Form.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
import type { ComponentType, FormHTMLAttributes, ReactNode } from "react";
|
|
1
2
|
import type { PublicFormInstance } from "../types/public";
|
|
3
|
+
export type { ValidationRule } from "../types/public";
|
|
2
4
|
export declare const FormContext: import("react").Context<any>;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export interface FormProps<T = any> extends Omit<FormHTMLAttributes<HTMLFormElement>, "onSubmit"> {
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
formName: string;
|
|
8
|
+
initialValues?: T;
|
|
9
|
+
onFinish?: (values: T, allValues?: any) => void | Promise<void>;
|
|
10
|
+
onReject?: (errorFields: any[]) => void | Promise<void>;
|
|
11
|
+
onFinally?: (result: {
|
|
12
|
+
errorsField: any[];
|
|
13
|
+
values: T;
|
|
14
|
+
withUnRegisteredValues: any;
|
|
15
|
+
}) => void | Promise<void>;
|
|
16
|
+
FormElement?: ComponentType<any>;
|
|
17
|
+
}
|
|
18
|
+
declare function Form<T = any>({ children, formName, initialValues, onFinish, onReject, onFinally, FormElement, ...props }: FormProps<T>): import("react/jsx-runtime").JSX.Element;
|
|
13
19
|
declare namespace Form {
|
|
14
20
|
var useForm: typeof import("./Form").useForm;
|
|
15
21
|
var useWatch: typeof import("./Form").useWatch;
|
package/dist/providers/Form.js
CHANGED
|
@@ -9,8 +9,9 @@ import { useFormListeners, useFormStore } from "../stores/formStore";
|
|
|
9
9
|
import { getAllNoneObjStringPath } from "../utils/obj.util";
|
|
10
10
|
export const FormContext = createContext(null);
|
|
11
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
|
-
|
|
12
|
+
const { appInitValue, getFormItemValue, setInitData, setData, getFormValues, setFormState, setFormInstance, revokeFormInstance, setSubmitHistory, clearFormValues, clearFormInitialValues, clearFormState, } = useFormStore(useShallow((state) => ({
|
|
13
|
+
appInitValue: state.initialValues,
|
|
14
|
+
setInitData: state.setInitData,
|
|
14
15
|
getFormValues: state.getFormValues,
|
|
15
16
|
setFormState: state.setFormState,
|
|
16
17
|
setFormInstance: state.setFormInstance,
|
|
@@ -237,7 +238,19 @@ export default function Form({ children, formName, initialValues, onFinish, onRe
|
|
|
237
238
|
});
|
|
238
239
|
};
|
|
239
240
|
useEffect(() => {
|
|
240
|
-
|
|
241
|
+
// set initial values
|
|
242
|
+
if (initialValues) {
|
|
243
|
+
const allStringPath = getAllNoneObjStringPath(initialValues);
|
|
244
|
+
allStringPath.forEach((p) => {
|
|
245
|
+
// console.log(
|
|
246
|
+
// "Set initial value for form",
|
|
247
|
+
// formName,
|
|
248
|
+
// p,
|
|
249
|
+
// get(initialValues, p),
|
|
250
|
+
// );
|
|
251
|
+
setInitData(formName, p, get(initialValues, p));
|
|
252
|
+
});
|
|
253
|
+
}
|
|
241
254
|
setFormState({
|
|
242
255
|
formName,
|
|
243
256
|
isInitied: true,
|
|
@@ -262,6 +275,9 @@ export default function Form({ children, formName, initialValues, onFinish, onRe
|
|
|
262
275
|
clearFormState(formName);
|
|
263
276
|
};
|
|
264
277
|
}, []);
|
|
278
|
+
// useEffect(() => {
|
|
279
|
+
// // console.log("Form appInitValue changed", appInitValue);
|
|
280
|
+
// }, [appInitValue]);
|
|
265
281
|
return (_jsxs(FormContext.Provider, { value: {
|
|
266
282
|
formName,
|
|
267
283
|
}, children: [FormElement ? (_jsx(FormElement, { onSubmit: (e) => {
|
|
@@ -281,7 +297,8 @@ export function useFormContext() {
|
|
|
281
297
|
return c;
|
|
282
298
|
}
|
|
283
299
|
export function useForm(formNameOrFormInstance) {
|
|
284
|
-
const targetFormName = typeof formNameOrFormInstance === "object" &&
|
|
300
|
+
const targetFormName = typeof formNameOrFormInstance === "object" &&
|
|
301
|
+
formNameOrFormInstance !== null
|
|
285
302
|
? formNameOrFormInstance.formName
|
|
286
303
|
: formNameOrFormInstance;
|
|
287
304
|
const formInstance = useFormStore((state) => {
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
export interface FormInstance {
|
|
2
2
|
formName: string;
|
|
3
|
-
resetFields?: any;
|
|
4
|
-
submit?: any;
|
|
5
|
-
submitAsync?: any
|
|
6
|
-
setFieldValue?: any;
|
|
7
|
-
setFieldValues?: any;
|
|
8
|
-
getFieldValue
|
|
9
|
-
getFieldValues?:
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
resetFields: (values?: any) => void;
|
|
4
|
+
submit: (values?: any) => void;
|
|
5
|
+
submitAsync: (values?: any) => Promise<any>;
|
|
6
|
+
setFieldValue: (name: string, value: any, options?: any) => void;
|
|
7
|
+
setFieldValues: (values: Record<string, any>, options?: any) => void;
|
|
8
|
+
getFieldValue: (name: string) => any;
|
|
9
|
+
getFieldValues: (names?: string[]) => Array<{
|
|
10
|
+
name: string;
|
|
11
|
+
value: any;
|
|
12
|
+
}>;
|
|
13
|
+
getFieldErrors: () => Record<string, any>;
|
|
14
|
+
setFieldFocus: (name: string) => void;
|
|
12
15
|
}
|
|
13
16
|
interface FormStoreState {
|
|
14
17
|
forms: Record<string, any>;
|
package/dist/stores/formStore.js
CHANGED
|
@@ -126,7 +126,7 @@ export const useFormStore = create((storeSet, storeGet) => ({
|
|
|
126
126
|
setInitData(formName, name, value) {
|
|
127
127
|
return storeSet(produce((state) => {
|
|
128
128
|
const oldValues = state.initialValues;
|
|
129
|
-
|
|
129
|
+
console.log("Inner init: ", `${formName}.${name}`, value);
|
|
130
130
|
set(oldValues, `${formName}.${name}`, value);
|
|
131
131
|
}));
|
|
132
132
|
},
|
package/dist/types/public.d.ts
CHANGED
|
@@ -1,14 +1,42 @@
|
|
|
1
1
|
export type FormValues<T = any> = T;
|
|
2
|
+
export interface ValidationRule<T = any, TValues = any> {
|
|
3
|
+
name?: string;
|
|
4
|
+
message: string;
|
|
5
|
+
handler?: (value: T, formValues: TValues) => boolean | Promise<boolean>;
|
|
6
|
+
required?: boolean;
|
|
7
|
+
min?: number;
|
|
8
|
+
max?: number;
|
|
9
|
+
minLength?: number;
|
|
10
|
+
maxLength?: number;
|
|
11
|
+
isPassword?: boolean;
|
|
12
|
+
isUsername?: boolean;
|
|
13
|
+
isPhoneNumber?: boolean;
|
|
14
|
+
isStringNumber?: boolean;
|
|
15
|
+
isPositiveStringNumber?: boolean;
|
|
16
|
+
isPositiveIntegerStringNumber?: boolean;
|
|
17
|
+
isEmail?: boolean;
|
|
18
|
+
isVietnameseName?: boolean;
|
|
19
|
+
isNoSpaceStringAndNumber?: boolean;
|
|
20
|
+
isStringAndNumber?: boolean;
|
|
21
|
+
isNoSpaceOnlyAlphabetStringAndNumber?: boolean;
|
|
22
|
+
isOnlyAlphabetStringAndNumber?: boolean;
|
|
23
|
+
isNoSpaceAlphabetString?: boolean;
|
|
24
|
+
pattern?: RegExp;
|
|
25
|
+
}
|
|
2
26
|
export interface PublicFormInstance<T = any> {
|
|
3
27
|
formName: string;
|
|
4
|
-
resetFields
|
|
5
|
-
submit
|
|
6
|
-
submitAsync
|
|
7
|
-
setFieldValue
|
|
8
|
-
setFieldValues
|
|
9
|
-
getFieldValue
|
|
10
|
-
getFieldValues?:
|
|
11
|
-
|
|
28
|
+
resetFields: (values?: Partial<T>) => void;
|
|
29
|
+
submit: (values?: T) => void;
|
|
30
|
+
submitAsync: (values?: T) => Promise<any>;
|
|
31
|
+
setFieldValue: (name: keyof T & string, value: any, options?: any) => void;
|
|
32
|
+
setFieldValues: (values: Partial<T>, options?: any) => void;
|
|
33
|
+
getFieldValue: (name: keyof T & string) => any;
|
|
34
|
+
getFieldValues: (names?: Array<keyof T & string>) => Array<{
|
|
35
|
+
name: string;
|
|
36
|
+
value: any;
|
|
37
|
+
}>;
|
|
38
|
+
getFieldErrors: () => Record<string, any>;
|
|
39
|
+
setFieldFocus: (name: keyof T & string) => void;
|
|
12
40
|
}
|
|
13
41
|
export interface UseFormItemProps<T = any> {
|
|
14
42
|
formName?: string;
|
|
@@ -16,7 +44,7 @@ export interface UseFormItemProps<T = any> {
|
|
|
16
44
|
name?: keyof T & string;
|
|
17
45
|
initialValue?: any;
|
|
18
46
|
formItemId?: string;
|
|
19
|
-
rules?: any[];
|
|
47
|
+
rules?: ValidationRule<any, T>[];
|
|
20
48
|
}
|
|
21
49
|
export interface UseFormItemReturn<T = any> {
|
|
22
50
|
value: T | undefined;
|
package/package.json
CHANGED
package/src/App.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Button, Input } from "antd";
|
|
2
2
|
import { useEffect } from "react";
|
|
3
3
|
import FormItem from "./components/Form/FormItem";
|
|
4
|
+
import FormList from "./components/Form/FormList";
|
|
4
5
|
import InputWrapper from "./components/Form/InputWrapper";
|
|
5
6
|
import Form, { useForm } from "./providers/Form";
|
|
6
7
|
|
|
@@ -9,14 +10,19 @@ const App = () => {
|
|
|
9
10
|
|
|
10
11
|
useEffect(() => {
|
|
11
12
|
if (form) {
|
|
12
|
-
setTimeout(() => {
|
|
13
|
-
|
|
14
|
-
}, 500);
|
|
13
|
+
// setTimeout(() => {
|
|
14
|
+
// form.setFieldValue("TestData", "HelloWorld");
|
|
15
|
+
// }, 500);
|
|
15
16
|
}
|
|
16
17
|
}, [form]);
|
|
17
18
|
return (
|
|
18
19
|
<div>
|
|
19
20
|
<Form
|
|
21
|
+
initialValues={{
|
|
22
|
+
TestData: "",
|
|
23
|
+
numericCode: "",
|
|
24
|
+
arr: [{ el: "Item 1" }, { el: "Item 2" }],
|
|
25
|
+
}}
|
|
20
26
|
onFinish={(values) => {
|
|
21
27
|
console.log(values);
|
|
22
28
|
}}
|
|
@@ -27,7 +33,72 @@ const App = () => {
|
|
|
27
33
|
<Input />
|
|
28
34
|
</InputWrapper>
|
|
29
35
|
</FormItem>
|
|
36
|
+
|
|
37
|
+
<FormItem
|
|
38
|
+
name={"numericCode"}
|
|
39
|
+
rules={[
|
|
40
|
+
{ required: true, message: "Vui lòng nhập mã" },
|
|
41
|
+
{
|
|
42
|
+
pattern: /^\d+$/,
|
|
43
|
+
message: "Chỉ cho phép chuỗi số",
|
|
44
|
+
},
|
|
45
|
+
]}
|
|
46
|
+
>
|
|
47
|
+
<InputWrapper>
|
|
48
|
+
<Input placeholder="Mã chỉ gồm số" style={{ width: 200 }} />
|
|
49
|
+
</InputWrapper>
|
|
50
|
+
</FormItem>
|
|
51
|
+
<FormList
|
|
52
|
+
initialValues={[
|
|
53
|
+
{
|
|
54
|
+
el: "",
|
|
55
|
+
},
|
|
56
|
+
]}
|
|
57
|
+
name="arr"
|
|
58
|
+
>
|
|
59
|
+
{(fields, { add, remove, move }) => (
|
|
60
|
+
<div>
|
|
61
|
+
{fields.map((field, index) => (
|
|
62
|
+
<div key={field.key} style={{ marginBottom: 8 }}>
|
|
63
|
+
<FormItem name={`${field.name}.el`}>
|
|
64
|
+
<InputWrapper>
|
|
65
|
+
<Input placeholder="Item value" style={{ width: 200 }} />
|
|
66
|
+
</InputWrapper>
|
|
67
|
+
</FormItem>
|
|
68
|
+
<Button
|
|
69
|
+
onClick={() => remove({ index })}
|
|
70
|
+
style={{ marginLeft: 8 }}
|
|
71
|
+
>
|
|
72
|
+
Remove
|
|
73
|
+
</Button>
|
|
74
|
+
{index > 0 && (
|
|
75
|
+
<Button
|
|
76
|
+
onClick={() => move({ from: index, to: index - 1 })}
|
|
77
|
+
style={{ marginLeft: 8 }}
|
|
78
|
+
>
|
|
79
|
+
Move Up
|
|
80
|
+
</Button>
|
|
81
|
+
)}
|
|
82
|
+
{index < fields.length - 1 && (
|
|
83
|
+
<Button
|
|
84
|
+
onClick={() => move({ from: index, to: index + 1 })}
|
|
85
|
+
style={{ marginLeft: 8 }}
|
|
86
|
+
>
|
|
87
|
+
Move Down
|
|
88
|
+
</Button>
|
|
89
|
+
)}
|
|
90
|
+
</div>
|
|
91
|
+
))}
|
|
92
|
+
<Button onClick={() => add(fields.length)}>Add Item</Button>
|
|
93
|
+
</div>
|
|
94
|
+
)}
|
|
95
|
+
</FormList>
|
|
30
96
|
<Button htmlType="submit">Submit</Button>
|
|
97
|
+
<Button
|
|
98
|
+
onClick={() => {
|
|
99
|
+
form?.resetFields?.();
|
|
100
|
+
}}
|
|
101
|
+
></Button>
|
|
31
102
|
</Form>
|
|
32
103
|
</div>
|
|
33
104
|
);
|
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { cloneElement, useRef, useState } from "react";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
3
|
+
import type { ValidationRule } from "../../types/public";
|
|
2
4
|
import { v4 } from "uuid";
|
|
3
5
|
import useFormItemControl from "../../hooks/useFormItemControl";
|
|
4
6
|
|
|
7
|
+
export interface FormItemProps {
|
|
8
|
+
children: ReactElement;
|
|
9
|
+
name: string;
|
|
10
|
+
formName?: string;
|
|
11
|
+
initialValue?: any;
|
|
12
|
+
formItemId?: string;
|
|
13
|
+
rules?: ValidationRule[];
|
|
14
|
+
valuePropName?: string;
|
|
15
|
+
getValueFromEvent?: (event: any) => any;
|
|
16
|
+
}
|
|
17
|
+
|
|
5
18
|
export default function FormItem({
|
|
6
19
|
children,
|
|
7
20
|
name,
|
|
@@ -11,7 +24,7 @@ export default function FormItem({
|
|
|
11
24
|
rules,
|
|
12
25
|
valuePropName = "value",
|
|
13
26
|
getValueFromEvent,
|
|
14
|
-
}) {
|
|
27
|
+
}: FormItemProps) {
|
|
15
28
|
const elRef = useRef<any>(null);
|
|
16
29
|
|
|
17
30
|
const [formItemId] = useState(externalFormItemId ?? v4());
|
|
@@ -56,5 +69,5 @@ export default function FormItem({
|
|
|
56
69
|
onFocus,
|
|
57
70
|
validateState: state,
|
|
58
71
|
ref: elRef,
|
|
59
|
-
});
|
|
72
|
+
} as any);
|
|
60
73
|
}
|
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import useTestRrenderListControl from "../../hooks/useFormListControl";
|
|
2
|
+
import type { FormInstance } from "../../stores/formStore";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
export interface FormListProps<T = any> {
|
|
5
|
+
name: string;
|
|
6
|
+
initialValues?: T[];
|
|
7
|
+
form?: FormInstance;
|
|
8
|
+
formName?: string;
|
|
9
|
+
children: (fields: Array<{ name: string; key: string }>, operations: { add: (index: number) => void; remove: (opts: { index?: number; key?: string }) => void; move: (opts: { from?: number; fromKey?: string; to: number }) => void }) => React.ReactNode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const FormList = <T = any>({ name, initialValues, form, formName, children }: FormListProps<T>) => {
|
|
4
13
|
const { listFields, ...actions } = useTestRrenderListControl({
|
|
5
14
|
name,
|
|
6
15
|
initialValues,
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { cloneElement } from "react";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
export interface InputWrapperProps {
|
|
5
|
+
children: ReactElement;
|
|
6
|
+
errors?: Array<{ ruleName: string; message: string }>;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const InputWrapper = ({ children, errors = [], ...props }: InputWrapperProps) => {
|
|
4
11
|
return (
|
|
5
12
|
<div>
|
|
6
13
|
{cloneElement(children, props)}
|