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
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { cloneElement, useRef, useState } from "react";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
3
|
+
import { v4 } from "uuid";
|
|
4
|
+
import useFormItemControl from "../../hooks/useFormItemControl";
|
|
5
|
+
|
|
6
|
+
export interface FormItemProps {
|
|
7
|
+
children: ReactElement;
|
|
8
|
+
name: string;
|
|
9
|
+
formName?: string;
|
|
10
|
+
initialValue?: any;
|
|
11
|
+
formItemId?: string;
|
|
12
|
+
rules?: any[];
|
|
13
|
+
valuePropName?: string;
|
|
14
|
+
getValueFromEvent?: (event: any) => any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function FormItem({
|
|
18
|
+
children,
|
|
19
|
+
name,
|
|
20
|
+
formName,
|
|
21
|
+
initialValue,
|
|
22
|
+
formItemId: externalFormItemId,
|
|
23
|
+
rules,
|
|
24
|
+
valuePropName = "value",
|
|
25
|
+
getValueFromEvent,
|
|
26
|
+
}: FormItemProps) {
|
|
27
|
+
const elRef = useRef<any>(null);
|
|
28
|
+
|
|
29
|
+
const [formItemId] = useState(externalFormItemId ?? v4());
|
|
30
|
+
const { value, onChange, errors, state, onFocus, isDirty } =
|
|
31
|
+
useFormItemControl({
|
|
32
|
+
formName,
|
|
33
|
+
name,
|
|
34
|
+
initialValue,
|
|
35
|
+
formItemId,
|
|
36
|
+
rules,
|
|
37
|
+
elementRef: elRef,
|
|
38
|
+
});
|
|
39
|
+
// console.log("re-render", formName, name);
|
|
40
|
+
|
|
41
|
+
// useEffect(() => {
|
|
42
|
+
// console.log({ value });
|
|
43
|
+
// }, [value]);
|
|
44
|
+
|
|
45
|
+
return cloneElement(children, {
|
|
46
|
+
// ref: inputRef,
|
|
47
|
+
[valuePropName]: value,
|
|
48
|
+
onChange: (e: any) => {
|
|
49
|
+
let val = e;
|
|
50
|
+
if (getValueFromEvent && typeof getValueFromEvent === "function") {
|
|
51
|
+
val = getValueFromEvent(e);
|
|
52
|
+
} else {
|
|
53
|
+
if (e && e.target) {
|
|
54
|
+
val = e.target.value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
onChange(val);
|
|
58
|
+
},
|
|
59
|
+
// onFocus: () => {
|
|
60
|
+
// setIsTouched(true);
|
|
61
|
+
// },
|
|
62
|
+
// isTouched: isTouched,
|
|
63
|
+
isDirty: isDirty,
|
|
64
|
+
// errors: errors,
|
|
65
|
+
// formState,
|
|
66
|
+
|
|
67
|
+
errors,
|
|
68
|
+
onFocus,
|
|
69
|
+
validateState: state,
|
|
70
|
+
ref: elRef,
|
|
71
|
+
} as any);
|
|
72
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import useTestRrenderListControl from "../../hooks/useFormListControl";
|
|
2
|
+
import type { FormInstance } from "../../stores/formStore";
|
|
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>) => {
|
|
13
|
+
const { listFields, ...actions } = useTestRrenderListControl({
|
|
14
|
+
name,
|
|
15
|
+
initialValues,
|
|
16
|
+
form,
|
|
17
|
+
formName,
|
|
18
|
+
});
|
|
19
|
+
return children(listFields, { ...actions });
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default FormList;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { cloneElement } from "react";
|
|
2
|
+
import type { ReactElement } from "react";
|
|
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) => {
|
|
11
|
+
return (
|
|
12
|
+
<div>
|
|
13
|
+
{cloneElement(children, props)}
|
|
14
|
+
{(props.isDirty || props.formState === "submitted") &&
|
|
15
|
+
errors?.map((r) => (
|
|
16
|
+
<div key={`${props.name}-${props.formItemId}-${r.ruleName}`}>
|
|
17
|
+
{r?.message}
|
|
18
|
+
</div>
|
|
19
|
+
))}
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default InputWrapper;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { forwardRef, type InputHTMLAttributes } from "react";
|
|
2
|
+
|
|
3
|
+
type InputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "onChange"> & {
|
|
4
|
+
onChange?: (value: any) => void;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const Input = forwardRef<HTMLInputElement, InputProps>(
|
|
8
|
+
({ onChange, ...props }, ref) => {
|
|
9
|
+
return (
|
|
10
|
+
<input
|
|
11
|
+
ref={ref}
|
|
12
|
+
onChange={(e) => {
|
|
13
|
+
onChange?.(e.target.value);
|
|
14
|
+
}}
|
|
15
|
+
{...props}
|
|
16
|
+
/>
|
|
17
|
+
);
|
|
18
|
+
},
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
export default Input;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Rules regex
|
|
2
|
+
export const IS_STRING_NUMBER_REGEX = /^\d+$/;
|
|
3
|
+
|
|
4
|
+
export const IS_USERNAME_REGEX = /^[a-zA-Z0-9._]{3,16}$/;
|
|
5
|
+
|
|
6
|
+
export const IS_PASSWORD_REGEX =
|
|
7
|
+
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
|
|
8
|
+
|
|
9
|
+
export const IS_POSITIVE_STRING_NUMBER_REGEX = /^[1-9]\d*$/;
|
|
10
|
+
|
|
11
|
+
export const IS_POSITIVE_INTEGER_STRING_NUMBER_REGEX =
|
|
12
|
+
/^(?:0|[1-9]\d*)(?:\.\d+)?$/;
|
|
13
|
+
|
|
14
|
+
export const IS_VIETNAMESE_PHONE_NUMBER_REGEX =
|
|
15
|
+
/^(?:\+84|0)[\s\-\.]?(?:3[2-9]|5[6|8|9]|7[06-9]|8[1-5]|9[0-9])[\s\-\.]?\d[\d\s\-\.]{6,8}$/;
|
|
16
|
+
|
|
17
|
+
export const IS_EMAIL_REGEX =
|
|
18
|
+
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
19
|
+
|
|
20
|
+
export const IS_NAME_REGEX =
|
|
21
|
+
/^[A-Za-zÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂÂÊÔƠƯăâêôơưÇçÝýỳỵỷỹ\s]{2,50}$/;
|
|
22
|
+
|
|
23
|
+
export const IS_NOSPACE_STRING_AND_NUMBER_REGEX = /^[A-Za-zÀ-ỹ0-9]+$/;
|
|
24
|
+
|
|
25
|
+
export const IS_STRING_AND_NUMBER_REGEX = /^[A-Za-zÀ-ỹ0-9][A-Za-zÀ-ỹ0-9\s]*$/;
|
|
26
|
+
|
|
27
|
+
export const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_REGEX = /^[A-Za-z0-9]+$/;
|
|
28
|
+
|
|
29
|
+
export const IS_ALPHABET_STRING_AND_NUMBER_REGEX =
|
|
30
|
+
/^[A-Za-z0-9]+(?:\s[A-Za-z0-9]+)*$/;
|
|
31
|
+
|
|
32
|
+
export const IS_NO_SPACE_ALPHABET_STRING_REGEX = /^[A-Za-z]+$/;
|
|
33
|
+
|
|
34
|
+
// default message
|
|
35
|
+
export const HANDLER_INVALID_MESSAGE = "Invalid value!";
|
|
36
|
+
|
|
37
|
+
export const MIN_INVALID_MESSAGE = "Min value invalid!";
|
|
38
|
+
export const MAX_INVALID_MESSAGE = "Max value invalid!";
|
|
39
|
+
export const MIN_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
40
|
+
export const MAX_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
41
|
+
|
|
42
|
+
export const IS_STRING_NUMBER_INVALID_MESSAGE = "Must be number!";
|
|
43
|
+
export const IS_USERNAME_INVALID_MESSAGE = `Username must be containes string, number, '.' or '_'!`;
|
|
44
|
+
export const IS_PASSWORD_INVALID_MESSAGE =
|
|
45
|
+
"Password must be contains Minimum 8 characters, with uppercase, lowercase, number, special character!";
|
|
46
|
+
export const IS_POSITIVE_STRING_NUMBER_INVALID_MESSAGE =
|
|
47
|
+
"Must be positive number!";
|
|
48
|
+
export const IS_POSITIVE_INTEGER_STRING_NUMBER_INVALID_MESSAGE =
|
|
49
|
+
"Must be positive integer number!";
|
|
50
|
+
export const IS_VIETNAMESE_PHONE_NUMBER_INVALID_MESSAGE =
|
|
51
|
+
"Invalid phone number format!";
|
|
52
|
+
export const IS_EMAIL_INVALID_MESSAGE = "Invalid email format!";
|
|
53
|
+
export const IS_NAME_INVALID_MESSAGE = "Invalid name format!";
|
|
54
|
+
export const IS_NOSPACE_STRING_AND_NUMBER_MESSAGE =
|
|
55
|
+
"String must only includes string and number without whitespace!";
|
|
56
|
+
export const IS_STRING_AND_NUMBER_MESSAGE =
|
|
57
|
+
"String must only includes string, number and whitespace!";
|
|
58
|
+
export const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_MESSAGE =
|
|
59
|
+
"String must only includes alphabet characters and numbers!";
|
|
60
|
+
export const IS_ALPHABET_STRING_AND_NUMBER_MESSAGE =
|
|
61
|
+
"String must only includes alphabet charaters, numbers and whitespace";
|
|
62
|
+
export const IS_NO_SPACE_ALPHABET_STRING_MESSAGE =
|
|
63
|
+
"String must only includes alphabet";
|