@react5/ui 1.0.3 → 1.0.5

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.
@@ -1,4 +1,5 @@
1
1
  export * from './use-click-outside';
2
2
  export * from './use-keys-enteresc';
3
3
  export * from './use-set-startup-focus';
4
+ export * from './use-form';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.tsx"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.tsx"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAA;AACnC,cAAc,qBAAqB,CAAA;AACnC,cAAc,yBAAyB,CAAA;AACvC,cAAc,YAAY,CAAA"}
@@ -0,0 +1,26 @@
1
+ import { ChangeEvent, FormEvent } from 'react';
2
+ type FormFields = Record<string, any>;
3
+ type Validator<T> = (values: T) => Partial<Record<keyof T, string>>;
4
+ type ChEv = ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>;
5
+ interface IRegisterInfo {
6
+ name: string;
7
+ onChange: (e: ChEv) => (void);
8
+ value?: any;
9
+ checked?: boolean;
10
+ error?: string;
11
+ }
12
+ export declare function useForm<T extends FormFields>(initialValues: T, validate?: Validator<T>): {
13
+ values: T;
14
+ errors: Partial<Record<keyof T, string>>;
15
+ handleChange: (e: ChEv) => void;
16
+ setFieldValue: <K extends keyof T>(field: K, value: T[K]) => void;
17
+ reset: (newValues?: T) => void;
18
+ setValues: import("react").Dispatch<import("react").SetStateAction<T>>;
19
+ validateForm: () => Partial<Record<keyof T, string>>;
20
+ register: (name: string, isCustomComponent?: boolean) => IRegisterInfo;
21
+ registerForm: (onUserSubmit?: () => void) => {
22
+ onSubmit: (e: FormEvent<HTMLFormElement>) => void;
23
+ };
24
+ };
25
+ export {};
26
+ //# sourceMappingURL=use-form.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-form.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-form.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEzD,KAAK,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACtC,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC;AAEpE,KAAK,IAAI,GAAG,WAAW,CAAC,gBAAgB,GAAG,iBAAiB,GAAG,mBAAmB,CAAC,CAAC;AACpF,UAAU,aAAa;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,CAAC,CAAC,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC;IAC9B,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AACD,wBAAgB,OAAO,CAAC,CAAC,SAAS,UAAU,EAC1C,aAAa,EAAE,CAAC,EAChB,QAAQ,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC;;;sBAOlB,IAAI;oBA6Bc,CAAC,SAAS,MAAM,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;;;wBAWtC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;qBAcjC,MAAM,sBAAqB,OAAO,KAAW,aAAa;kCAiB7C,MAAM,IAAI;sBAPoB,SAAS,CAAC,eAAe,CAAC;;EAY9F"}
@@ -1,3 +1,4 @@
1
1
  export * from './use-click-outside'
2
2
  export * from './use-keys-enteresc'
3
- export * from './use-set-startup-focus'
3
+ export * from './use-set-startup-focus'
4
+ export * from './use-form'
@@ -0,0 +1,99 @@
1
+ import { useState, ChangeEvent, FormEvent } from 'react';
2
+
3
+ type FormFields = Record<string, any>;
4
+ type Validator<T> = (values: T) => Partial<Record<keyof T, string>>;
5
+
6
+ type ChEv = ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>;
7
+ interface IRegisterInfo {
8
+ name: string,
9
+ onChange: (e: ChEv) => (void),
10
+ value?: any,
11
+ checked?: boolean,
12
+ error?: string
13
+ };
14
+ export function useForm<T extends FormFields>(
15
+ initialValues: T,
16
+ validate?: Validator<T>
17
+ ) {
18
+ const [values, setValues] = useState<T>(initialValues);
19
+ const [errors, setErrors] = useState<Partial<Record<keyof T, string>>>({});
20
+
21
+ // Standard change handler for native elements
22
+ const handleChange = (
23
+ e: ChEv
24
+ ) => {
25
+ const { name, type, value } = e.target;
26
+ const fieldValue = type === 'checkbox' ? (e.target as HTMLInputElement).checked : value;
27
+
28
+ setValues((prevValues) => {
29
+ const newValues = { ...prevValues, [name]: fieldValue };
30
+ if (validate) {
31
+ const newErrors = validate(newValues);
32
+ setErrors(newErrors);
33
+ }
34
+ return newValues;
35
+ });
36
+ };
37
+
38
+ const handleCustomChange = (name: string) => (v: any) => {
39
+ const fieldValue = v;
40
+
41
+ setValues((prevValues) => {
42
+ const newValues = { ...prevValues, [name]: fieldValue };
43
+ if (validate) {
44
+ const newErrors = validate(newValues);
45
+ setErrors(newErrors);
46
+ }
47
+ return newValues;
48
+ });
49
+ }
50
+
51
+ // New helper: update a specific field directly.
52
+ const setFieldValue = <K extends keyof T>(field: K, value: T[K]) => {
53
+ setValues((prevValues) => {
54
+ const newValues = { ...prevValues, [field]: value };
55
+ if (validate) {
56
+ const newErrors = validate(newValues);
57
+ setErrors(newErrors);
58
+ }
59
+ return newValues;
60
+ });
61
+ };
62
+
63
+ const validateForm = (): Partial<Record<keyof T, string>> => {
64
+ if (validate) {
65
+ const newErrors = validate(values);
66
+ setErrors(newErrors);
67
+ return newErrors;
68
+ }
69
+ return {};
70
+ };
71
+
72
+ const reset = (newValues = initialValues) => {
73
+ setValues(newValues);
74
+ setErrors({});
75
+ };
76
+
77
+ const register = (name: string, isCustomComponent: boolean = false): IRegisterInfo => {
78
+ const cm = {name, value: values[name] || '', checked: values[name] || false, error: errors[name]};
79
+ if (isCustomComponent) {
80
+ return {...cm, onChange: handleCustomChange(name) };
81
+ }
82
+ else {
83
+ return {...cm, onChange: handleChange };
84
+ }
85
+ }
86
+
87
+ const handleFormSubmit = (onUserSubmit?: (data: T) => void) => (e: FormEvent<HTMLFormElement>) => {
88
+ e.preventDefault();
89
+ onUserSubmit?.(values);
90
+
91
+ reset();
92
+ }
93
+
94
+ const registerForm = (onUserSubmit?: () => void) => {
95
+ return { onSubmit: handleFormSubmit(onUserSubmit) };
96
+ }
97
+
98
+ return { values, errors, handleChange, setFieldValue, reset, setValues, validateForm, register, registerForm };
99
+ }