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/CHANGELOG.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [1.0.6-beta.1] - 2026-01-22
|
|
6
|
+
|
|
7
|
+
- Full TypeScript migration: Convert all source files from JS to TS
|
|
8
|
+
- Replace JS entrypoints (main.jsx, Input.jsx) with TypeScript versions
|
|
9
|
+
- Add proper type declarations generated from source
|
|
10
|
+
- Update build to emit both JS and .d.ts files
|
|
11
|
+
- Add @types/node for Node.js type support
|
|
12
|
+
- Remove legacy copy-types scripts
|
|
13
|
+
- Add antd, react, react-dom to devDependencies for local development
|
|
14
|
+
|
|
15
|
+
## [1.0.1] - 2026-01-21
|
|
16
|
+
|
|
17
|
+
- Publish patch release v1.0.1
|
|
18
|
+
- Minor packaging and release metadata updates
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## [1.0.0] - initial publish
|
|
23
|
+
|
|
24
|
+
- Initial public release `react-form-manage@1.0.0`.
|
|
25
|
+
|
|
26
|
+
## [1.0.2] - 2026-01-21
|
|
27
|
+
|
|
28
|
+
- Add distributed TypeScript declaration `dist/index.d.ts` and update `package.json` `types` field.
|
|
29
|
+
|
|
30
|
+
## [1.0.3] - 2026-01-21
|
|
31
|
+
|
|
32
|
+
- Provide `dist/index.esm.d.ts` and `dist/index.cjs.d.ts` re-exports so Node-style module resolution finds typings correctly.
|
|
33
|
+
|
|
34
|
+
## [1.0.4] - 2026-01-21
|
|
35
|
+
|
|
36
|
+
- Add `build:types` script and `scripts/copy-types.js` to automatically copy `src/types/components.d.ts` into `dist/` during publish builds.
|
|
37
|
+
|
|
38
|
+
## [1.0.5] - 2026-01-21
|
|
39
|
+
|
|
40
|
+
- Export `Form` as a named export in addition to default to improve import ergonomics for consumers.
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { useShallow } from "zustand/react/shallow";
|
|
4
|
+
import { useFormCleanUp, useFormListeners, useFormStore, } from "../../stores/formStore";
|
|
5
|
+
const FormCleanUp = () => {
|
|
6
|
+
const { cleanUpStack, clearCleanUpStack } = useFormCleanUp(useShallow((state) => ({
|
|
7
|
+
cleanUpStack: state.cleanUpStack,
|
|
8
|
+
clearCleanUpStack: state.clearCleanUpStack,
|
|
9
|
+
})));
|
|
10
|
+
const { revokeListener } = useFormListeners(useShallow((state) => ({
|
|
11
|
+
revokeListener: state.revokeListener,
|
|
12
|
+
})));
|
|
13
|
+
const { clearObjKeyItem, clearArrItem } = useFormStore(useShallow((state) => {
|
|
14
|
+
return {
|
|
15
|
+
clearObjKeyItem: state.clearObjKeyItem,
|
|
16
|
+
clearArrItem: state.clearArrItem,
|
|
17
|
+
};
|
|
18
|
+
}));
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (Array.isArray(cleanUpStack) && cleanUpStack?.length) {
|
|
21
|
+
// console.log("Trigger clean up: ", cleanUpStack);
|
|
22
|
+
cleanUpStack.forEach((c) => {
|
|
23
|
+
if (c.itemKey) {
|
|
24
|
+
console.log("Revoke in Clean up: ", c);
|
|
25
|
+
revokeListener(c.itemKey, (listener, same) => {
|
|
26
|
+
if (!same.length) {
|
|
27
|
+
console.log("Trigger after revoke: ", listener, same);
|
|
28
|
+
clearObjKeyItem(listener.formName, listener.name);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (c.type === "array") {
|
|
34
|
+
console.log(c);
|
|
35
|
+
clearArrItem(c.formName, c.name);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
clearCleanUpStack();
|
|
42
|
+
}
|
|
43
|
+
}, [cleanUpStack]);
|
|
44
|
+
return _jsx(_Fragment, {});
|
|
45
|
+
};
|
|
46
|
+
export default FormCleanUp;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export default function FormItem({ children, name, formName, initialValue, formItemId: externalFormItemId, rules, valuePropName, getValueFromEvent, }: {
|
|
2
|
+
children: any;
|
|
3
|
+
name: any;
|
|
4
|
+
formName: any;
|
|
5
|
+
initialValue: any;
|
|
6
|
+
formItemId: any;
|
|
7
|
+
rules: any;
|
|
8
|
+
valuePropName?: string;
|
|
9
|
+
getValueFromEvent: any;
|
|
10
|
+
}): import("react").DetailedReactHTMLElement<{
|
|
11
|
+
[valuePropName]: any;
|
|
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>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { cloneElement, useRef, useState } from "react";
|
|
2
|
+
import { v4 } from "uuid";
|
|
3
|
+
import useFormItemControl from "../../hooks/useFormItemControl";
|
|
4
|
+
export default function FormItem({ children, name, formName, initialValue, formItemId: externalFormItemId, rules, valuePropName = "value", getValueFromEvent, }) {
|
|
5
|
+
const elRef = useRef(null);
|
|
6
|
+
const [formItemId] = useState(externalFormItemId ?? v4());
|
|
7
|
+
const { value, onChange, errors, state, onFocus, isDirty } = useFormItemControl({
|
|
8
|
+
formName,
|
|
9
|
+
name,
|
|
10
|
+
initialValue,
|
|
11
|
+
formItemId,
|
|
12
|
+
rules,
|
|
13
|
+
elementRef: elRef,
|
|
14
|
+
});
|
|
15
|
+
// console.log("re-render", formName, name);
|
|
16
|
+
// useEffect(() => {
|
|
17
|
+
// console.log({ value });
|
|
18
|
+
// }, [value]);
|
|
19
|
+
return cloneElement(children, {
|
|
20
|
+
// ref: inputRef,
|
|
21
|
+
[valuePropName]: value,
|
|
22
|
+
onChange: (e) => {
|
|
23
|
+
let val = e;
|
|
24
|
+
if (getValueFromEvent && typeof getValueFromEvent === "function") {
|
|
25
|
+
val = getValueFromEvent(e);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
if (e && e.target) {
|
|
29
|
+
val = e.target.value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
onChange(val);
|
|
33
|
+
},
|
|
34
|
+
// onFocus: () => {
|
|
35
|
+
// setIsTouched(true);
|
|
36
|
+
// },
|
|
37
|
+
// isTouched: isTouched,
|
|
38
|
+
isDirty: isDirty,
|
|
39
|
+
// errors: errors,
|
|
40
|
+
// formState,
|
|
41
|
+
errors,
|
|
42
|
+
onFocus,
|
|
43
|
+
validateState: state,
|
|
44
|
+
ref: elRef,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import useTestRrenderListControl from "../../hooks/useFormListControl";
|
|
2
|
+
const FormList = ({ name, initialValues, form, formName, children }) => {
|
|
3
|
+
const { listFields, ...actions } = useTestRrenderListControl({
|
|
4
|
+
name,
|
|
5
|
+
initialValues,
|
|
6
|
+
form,
|
|
7
|
+
formName,
|
|
8
|
+
});
|
|
9
|
+
return children(listFields, { ...actions });
|
|
10
|
+
};
|
|
11
|
+
export default FormList;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { cloneElement } from "react";
|
|
3
|
+
const InputWrapper = ({ children, errors = [], ...props }) => {
|
|
4
|
+
return (_jsxs("div", { children: [cloneElement(children, props), (props.isDirty || props.formState === "submitted") &&
|
|
5
|
+
errors?.map((r) => (_jsx("div", { children: r?.message }, `${props.name}-${props.formItemId}-${r.ruleName}`)))] }));
|
|
6
|
+
};
|
|
7
|
+
export default InputWrapper;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type InputHTMLAttributes } from "react";
|
|
2
|
+
declare const Input: import("react").ForwardRefExoticComponent<Omit<InputHTMLAttributes<HTMLInputElement>, "onChange"> & {
|
|
3
|
+
onChange?: (value: any) => void;
|
|
4
|
+
} & import("react").RefAttributes<HTMLInputElement>>;
|
|
5
|
+
export default Input;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { forwardRef } from "react";
|
|
3
|
+
const Input = forwardRef(({ onChange, ...props }, ref) => {
|
|
4
|
+
return (_jsx("input", { ref: ref, onChange: (e) => {
|
|
5
|
+
onChange?.(e.target.value);
|
|
6
|
+
}, ...props }));
|
|
7
|
+
});
|
|
8
|
+
export default Input;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const IS_STRING_NUMBER_REGEX: RegExp;
|
|
2
|
+
export declare const IS_USERNAME_REGEX: RegExp;
|
|
3
|
+
export declare const IS_PASSWORD_REGEX: RegExp;
|
|
4
|
+
export declare const IS_POSITIVE_STRING_NUMBER_REGEX: RegExp;
|
|
5
|
+
export declare const IS_POSITIVE_INTEGER_STRING_NUMBER_REGEX: RegExp;
|
|
6
|
+
export declare const IS_VIETNAMESE_PHONE_NUMBER_REGEX: RegExp;
|
|
7
|
+
export declare const IS_EMAIL_REGEX: RegExp;
|
|
8
|
+
export declare const IS_NAME_REGEX: RegExp;
|
|
9
|
+
export declare const IS_NOSPACE_STRING_AND_NUMBER_REGEX: RegExp;
|
|
10
|
+
export declare const IS_STRING_AND_NUMBER_REGEX: RegExp;
|
|
11
|
+
export declare const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_REGEX: RegExp;
|
|
12
|
+
export declare const IS_ALPHABET_STRING_AND_NUMBER_REGEX: RegExp;
|
|
13
|
+
export declare const IS_NO_SPACE_ALPHABET_STRING_REGEX: RegExp;
|
|
14
|
+
export declare const HANDLER_INVALID_MESSAGE = "Invalid value!";
|
|
15
|
+
export declare const MIN_INVALID_MESSAGE = "Min value invalid!";
|
|
16
|
+
export declare const MAX_INVALID_MESSAGE = "Max value invalid!";
|
|
17
|
+
export declare const MIN_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
18
|
+
export declare const MAX_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
19
|
+
export declare const IS_STRING_NUMBER_INVALID_MESSAGE = "Must be number!";
|
|
20
|
+
export declare const IS_USERNAME_INVALID_MESSAGE = "Username must be containes string, number, '.' or '_'!";
|
|
21
|
+
export declare const IS_PASSWORD_INVALID_MESSAGE = "Password must be contains Minimum 8 characters, with uppercase, lowercase, number, special character!";
|
|
22
|
+
export declare const IS_POSITIVE_STRING_NUMBER_INVALID_MESSAGE = "Must be positive number!";
|
|
23
|
+
export declare const IS_POSITIVE_INTEGER_STRING_NUMBER_INVALID_MESSAGE = "Must be positive integer number!";
|
|
24
|
+
export declare const IS_VIETNAMESE_PHONE_NUMBER_INVALID_MESSAGE = "Invalid phone number format!";
|
|
25
|
+
export declare const IS_EMAIL_INVALID_MESSAGE = "Invalid email format!";
|
|
26
|
+
export declare const IS_NAME_INVALID_MESSAGE = "Invalid name format!";
|
|
27
|
+
export declare const IS_NOSPACE_STRING_AND_NUMBER_MESSAGE = "String must only includes string and number without whitespace!";
|
|
28
|
+
export declare const IS_STRING_AND_NUMBER_MESSAGE = "String must only includes string, number and whitespace!";
|
|
29
|
+
export declare const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_MESSAGE = "String must only includes alphabet characters and numbers!";
|
|
30
|
+
export declare const IS_ALPHABET_STRING_AND_NUMBER_MESSAGE = "String must only includes alphabet charaters, numbers and whitespace";
|
|
31
|
+
export declare const IS_NO_SPACE_ALPHABET_STRING_MESSAGE = "String must only includes alphabet";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Rules regex
|
|
2
|
+
export const IS_STRING_NUMBER_REGEX = /^\d+$/;
|
|
3
|
+
export const IS_USERNAME_REGEX = /^[a-zA-Z0-9._]{3,16}$/;
|
|
4
|
+
export const IS_PASSWORD_REGEX = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
|
|
5
|
+
export const IS_POSITIVE_STRING_NUMBER_REGEX = /^[1-9]\d*$/;
|
|
6
|
+
export const IS_POSITIVE_INTEGER_STRING_NUMBER_REGEX = /^(?:0|[1-9]\d*)(?:\.\d+)?$/;
|
|
7
|
+
export const IS_VIETNAMESE_PHONE_NUMBER_REGEX = /^(?:\+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}$/;
|
|
8
|
+
export const IS_EMAIL_REGEX = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
9
|
+
export const IS_NAME_REGEX = /^[A-Za-zÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚĂĐĨŨƠàáâãèéêìíòóôõùúăđĩũơƯĂÂÊÔƠƯăâêôơưÇçÝýỳỵỷỹ\s]{2,50}$/;
|
|
10
|
+
export const IS_NOSPACE_STRING_AND_NUMBER_REGEX = /^[A-Za-zÀ-ỹ0-9]+$/;
|
|
11
|
+
export const IS_STRING_AND_NUMBER_REGEX = /^[A-Za-zÀ-ỹ0-9][A-Za-zÀ-ỹ0-9\s]*$/;
|
|
12
|
+
export const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_REGEX = /^[A-Za-z0-9]+$/;
|
|
13
|
+
export const IS_ALPHABET_STRING_AND_NUMBER_REGEX = /^[A-Za-z0-9]+(?:\s[A-Za-z0-9]+)*$/;
|
|
14
|
+
export const IS_NO_SPACE_ALPHABET_STRING_REGEX = /^[A-Za-z]+$/;
|
|
15
|
+
// default message
|
|
16
|
+
export const HANDLER_INVALID_MESSAGE = "Invalid value!";
|
|
17
|
+
export const MIN_INVALID_MESSAGE = "Min value invalid!";
|
|
18
|
+
export const MAX_INVALID_MESSAGE = "Max value invalid!";
|
|
19
|
+
export const MIN_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
20
|
+
export const MAX_INVALID_LENGTH_MESSAGE = "Max length invalid!";
|
|
21
|
+
export const IS_STRING_NUMBER_INVALID_MESSAGE = "Must be number!";
|
|
22
|
+
export const IS_USERNAME_INVALID_MESSAGE = `Username must be containes string, number, '.' or '_'!`;
|
|
23
|
+
export const IS_PASSWORD_INVALID_MESSAGE = "Password must be contains Minimum 8 characters, with uppercase, lowercase, number, special character!";
|
|
24
|
+
export const IS_POSITIVE_STRING_NUMBER_INVALID_MESSAGE = "Must be positive number!";
|
|
25
|
+
export const IS_POSITIVE_INTEGER_STRING_NUMBER_INVALID_MESSAGE = "Must be positive integer number!";
|
|
26
|
+
export const IS_VIETNAMESE_PHONE_NUMBER_INVALID_MESSAGE = "Invalid phone number format!";
|
|
27
|
+
export const IS_EMAIL_INVALID_MESSAGE = "Invalid email format!";
|
|
28
|
+
export const IS_NAME_INVALID_MESSAGE = "Invalid name format!";
|
|
29
|
+
export const IS_NOSPACE_STRING_AND_NUMBER_MESSAGE = "String must only includes string and number without whitespace!";
|
|
30
|
+
export const IS_STRING_AND_NUMBER_MESSAGE = "String must only includes string, number and whitespace!";
|
|
31
|
+
export const IS_NO_SPACE_ALPHABET_STRING_AND_NUMBER_MESSAGE = "String must only includes alphabet characters and numbers!";
|
|
32
|
+
export const IS_ALPHABET_STRING_AND_NUMBER_MESSAGE = "String must only includes alphabet charaters, numbers and whitespace";
|
|
33
|
+
export const IS_NO_SPACE_ALPHABET_STRING_MESSAGE = "String must only includes alphabet";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
import type { FormInstance } from "../stores/formStore";
|
|
3
|
+
type AnyObject = Record<string, any>;
|
|
4
|
+
interface UseFormItemControlProps {
|
|
5
|
+
formName?: string;
|
|
6
|
+
form?: FormInstance;
|
|
7
|
+
name?: string;
|
|
8
|
+
initialValue?: any;
|
|
9
|
+
formItemId?: string;
|
|
10
|
+
rules?: any[];
|
|
11
|
+
elementRef?: RefObject<any> | null;
|
|
12
|
+
}
|
|
13
|
+
interface UseFormItemControlReturn {
|
|
14
|
+
value: any;
|
|
15
|
+
onChange: (value: any, options?: AnyObject) => void;
|
|
16
|
+
state: any;
|
|
17
|
+
errors: any;
|
|
18
|
+
onFocus: () => void;
|
|
19
|
+
isDirty?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export default function useFormItemControl<T = any>({ formName, form, name, initialValue, formItemId, rules, elementRef, }: UseFormItemControlProps): UseFormItemControlReturn;
|
|
22
|
+
export {};
|