rfhook 1.0.0 → 1.1.0
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/README.md +8 -7
- package/dist/index.cjs +57 -4
- package/dist/index.js +57 -4
- package/dist/useForm.hook.d.ts +49 -6
- package/package.json +1 -1
- package/src/useForm.hook.ts +94 -8
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rfhook
|
|
2
2
|
|
|
3
3
|
A lightweight React hook for handling form submissions with advanced form data parsing capabilities. Supports nested objects, arrays, and dot notation for complex form structures.
|
|
4
4
|
|
|
@@ -8,28 +8,29 @@ A lightweight React hook for handling form submissions with advanced form data p
|
|
|
8
8
|
- 🎯 **TypeScript Support** - Fully typed with generic support
|
|
9
9
|
- 🔧 **Nested Objects** - Parse nested form data with dot notation (`user.name`)
|
|
10
10
|
- 📋 **Array Support** - Handle arrays with indexed notation (`items[0]`)
|
|
11
|
-
-
|
|
11
|
+
- �️ **Automatic Prevention** - Prevents default form submission behavior by default
|
|
12
|
+
- �📦 **Lightweight** - Zero dependencies (except React)
|
|
12
13
|
- 🎨 **Framework Agnostic** - Works with any form structure
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
15
16
|
|
|
16
17
|
```bash
|
|
17
|
-
npm install
|
|
18
|
+
npm install rfhook
|
|
18
19
|
```
|
|
19
20
|
|
|
20
21
|
```bash
|
|
21
|
-
pnpm add
|
|
22
|
+
pnpm add rfhook
|
|
22
23
|
```
|
|
23
24
|
|
|
24
25
|
```bash
|
|
25
|
-
yarn add
|
|
26
|
+
yarn add rfhook
|
|
26
27
|
```
|
|
27
28
|
|
|
28
29
|
## Quick Start
|
|
29
30
|
|
|
30
31
|
```tsx
|
|
31
32
|
import React from 'react';
|
|
32
|
-
import { useForm } from '
|
|
33
|
+
import { useForm } from 'rfhook';
|
|
33
34
|
|
|
34
35
|
interface FormData {
|
|
35
36
|
email: string;
|
|
@@ -169,7 +170,7 @@ The `parseFormData` utility converts FormData into structured objects using thes
|
|
|
169
170
|
### Contact Form
|
|
170
171
|
|
|
171
172
|
```tsx
|
|
172
|
-
import { useForm } from '
|
|
173
|
+
import { useForm } from 'rfhook';
|
|
173
174
|
|
|
174
175
|
interface ContactData {
|
|
175
176
|
name: string;
|
package/dist/index.cjs
CHANGED
|
@@ -49,17 +49,70 @@ function parseFormData(formData) {
|
|
|
49
49
|
return result;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
|
+
* A React hook for handling form state and submission
|
|
54
|
+
*
|
|
55
|
+
* @template T - The expected shape of the parsed form data
|
|
56
|
+
* @param options - Configuration options for the form
|
|
57
|
+
* @returns Object containing form ref, handlers, and utility functions
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* interface LoginData {
|
|
62
|
+
* email: string;
|
|
63
|
+
* password: string;
|
|
64
|
+
* }
|
|
65
|
+
*
|
|
66
|
+
* const form = useForm<LoginData>({
|
|
67
|
+
* submit: (data) => console.log(data.email, data.password)
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* return (
|
|
71
|
+
* <form ref={form.ref} onSubmit={form.onSubmit}>
|
|
72
|
+
* <input name="email" type="email" />
|
|
73
|
+
* <input name="password" type="password" />
|
|
74
|
+
* <button type="submit">Login</button>
|
|
75
|
+
* </form>
|
|
76
|
+
* );
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function useForm({ submit }) {
|
|
53
80
|
const ref = react.useRef(null);
|
|
54
|
-
const
|
|
81
|
+
const onSubmit = (event) => {
|
|
55
82
|
event.preventDefault();
|
|
56
83
|
if (!ref.current)
|
|
57
84
|
return;
|
|
58
85
|
const formData = new FormData(ref.current);
|
|
59
86
|
const data = parseFormData(formData);
|
|
60
|
-
|
|
87
|
+
submit(data);
|
|
88
|
+
};
|
|
89
|
+
const reset = () => {
|
|
90
|
+
if (!ref.current)
|
|
91
|
+
return;
|
|
92
|
+
ref.current.reset();
|
|
93
|
+
};
|
|
94
|
+
const getFormData = () => {
|
|
95
|
+
if (!ref.current)
|
|
96
|
+
return null;
|
|
97
|
+
const formData = new FormData(ref.current);
|
|
98
|
+
const data = parseFormData(formData);
|
|
99
|
+
return data;
|
|
100
|
+
};
|
|
101
|
+
const setValue = (name, value) => {
|
|
102
|
+
if (!ref.current)
|
|
103
|
+
return;
|
|
104
|
+
const element = ref.current.elements.namedItem(name);
|
|
105
|
+
if (element && 'value' in element) {
|
|
106
|
+
element.value = value;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
ref,
|
|
111
|
+
onSubmit,
|
|
112
|
+
reset,
|
|
113
|
+
getFormData,
|
|
114
|
+
setValue
|
|
61
115
|
};
|
|
62
|
-
return { ref, submit };
|
|
63
116
|
}
|
|
64
117
|
|
|
65
118
|
exports.useForm = useForm;
|
package/dist/index.js
CHANGED
|
@@ -47,17 +47,70 @@ function parseFormData(formData) {
|
|
|
47
47
|
return result;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
/**
|
|
51
|
+
* A React hook for handling form state and submission
|
|
52
|
+
*
|
|
53
|
+
* @template T - The expected shape of the parsed form data
|
|
54
|
+
* @param options - Configuration options for the form
|
|
55
|
+
* @returns Object containing form ref, handlers, and utility functions
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* interface LoginData {
|
|
60
|
+
* email: string;
|
|
61
|
+
* password: string;
|
|
62
|
+
* }
|
|
63
|
+
*
|
|
64
|
+
* const form = useForm<LoginData>({
|
|
65
|
+
* submit: (data) => console.log(data.email, data.password)
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* return (
|
|
69
|
+
* <form ref={form.ref} onSubmit={form.onSubmit}>
|
|
70
|
+
* <input name="email" type="email" />
|
|
71
|
+
* <input name="password" type="password" />
|
|
72
|
+
* <button type="submit">Login</button>
|
|
73
|
+
* </form>
|
|
74
|
+
* );
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function useForm({ submit }) {
|
|
51
78
|
const ref = useRef(null);
|
|
52
|
-
const
|
|
79
|
+
const onSubmit = (event) => {
|
|
53
80
|
event.preventDefault();
|
|
54
81
|
if (!ref.current)
|
|
55
82
|
return;
|
|
56
83
|
const formData = new FormData(ref.current);
|
|
57
84
|
const data = parseFormData(formData);
|
|
58
|
-
|
|
85
|
+
submit(data);
|
|
86
|
+
};
|
|
87
|
+
const reset = () => {
|
|
88
|
+
if (!ref.current)
|
|
89
|
+
return;
|
|
90
|
+
ref.current.reset();
|
|
91
|
+
};
|
|
92
|
+
const getFormData = () => {
|
|
93
|
+
if (!ref.current)
|
|
94
|
+
return null;
|
|
95
|
+
const formData = new FormData(ref.current);
|
|
96
|
+
const data = parseFormData(formData);
|
|
97
|
+
return data;
|
|
98
|
+
};
|
|
99
|
+
const setValue = (name, value) => {
|
|
100
|
+
if (!ref.current)
|
|
101
|
+
return;
|
|
102
|
+
const element = ref.current.elements.namedItem(name);
|
|
103
|
+
if (element && 'value' in element) {
|
|
104
|
+
element.value = value;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
ref,
|
|
109
|
+
onSubmit,
|
|
110
|
+
reset,
|
|
111
|
+
getFormData,
|
|
112
|
+
setValue
|
|
59
113
|
};
|
|
60
|
-
return { ref, submit };
|
|
61
114
|
}
|
|
62
115
|
|
|
63
116
|
export { useForm };
|
package/dist/useForm.hook.d.ts
CHANGED
|
@@ -1,8 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for the useForm hook
|
|
3
|
+
*/
|
|
4
|
+
interface UseFormOptions<T> {
|
|
5
|
+
/** Callback function called when form is submitted with parsed form data */
|
|
6
|
+
submit: (data: T) => void;
|
|
3
7
|
}
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Return type of the useForm hook
|
|
10
|
+
*/
|
|
11
|
+
interface UseFormReturn<T> {
|
|
12
|
+
/** React ref to be attached to the form element */
|
|
13
|
+
ref: React.RefObject<HTMLFormElement | null>;
|
|
14
|
+
/** Form submission handler - prevents default and calls submit with parsed data */
|
|
15
|
+
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
16
|
+
/** Resets the form to its initial state */
|
|
17
|
+
reset: () => void;
|
|
18
|
+
/** Gets current form data without triggering submission */
|
|
19
|
+
getFormData: () => T | null;
|
|
20
|
+
/** Sets the value of a specific form field by name */
|
|
21
|
+
setValue: (name: string, value: string) => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* A React hook for handling form state and submission
|
|
25
|
+
*
|
|
26
|
+
* @template T - The expected shape of the parsed form data
|
|
27
|
+
* @param options - Configuration options for the form
|
|
28
|
+
* @returns Object containing form ref, handlers, and utility functions
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* interface LoginData {
|
|
33
|
+
* email: string;
|
|
34
|
+
* password: string;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* const form = useForm<LoginData>({
|
|
38
|
+
* submit: (data) => console.log(data.email, data.password)
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* return (
|
|
42
|
+
* <form ref={form.ref} onSubmit={form.onSubmit}>
|
|
43
|
+
* <input name="email" type="email" />
|
|
44
|
+
* <input name="password" type="password" />
|
|
45
|
+
* <button type="submit">Login</button>
|
|
46
|
+
* </form>
|
|
47
|
+
* );
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function useForm<T = Record<string, unknown>>({ submit }: UseFormOptions<T>): UseFormReturn<T>;
|
|
8
51
|
export {};
|
package/package.json
CHANGED
package/src/useForm.hook.ts
CHANGED
|
@@ -2,24 +2,110 @@ import { useRef } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { parseFormData } from './utils/parseFormData';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the useForm hook
|
|
7
|
+
*/
|
|
8
|
+
interface UseFormOptions<T> {
|
|
9
|
+
/** Callback function called when form is submitted with parsed form data */
|
|
10
|
+
submit: (data: T) => void;
|
|
7
11
|
}
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Form element types that can have their values set programmatically
|
|
15
|
+
*/
|
|
16
|
+
type FormElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Return type of the useForm hook
|
|
20
|
+
*/
|
|
21
|
+
interface UseFormReturn<T> {
|
|
22
|
+
/** React ref to be attached to the form element */
|
|
23
|
+
ref: React.RefObject<HTMLFormElement | null>;
|
|
24
|
+
/** Form submission handler - prevents default and calls submit with parsed data */
|
|
25
|
+
onSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
26
|
+
/** Resets the form to its initial state */
|
|
27
|
+
reset: () => void;
|
|
28
|
+
/** Gets current form data without triggering submission */
|
|
29
|
+
getFormData: () => T | null;
|
|
30
|
+
/** Sets the value of a specific form field by name */
|
|
31
|
+
setValue: (name: string, value: string) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A React hook for handling form state and submission
|
|
36
|
+
*
|
|
37
|
+
* @template T - The expected shape of the parsed form data
|
|
38
|
+
* @param options - Configuration options for the form
|
|
39
|
+
* @returns Object containing form ref, handlers, and utility functions
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* interface LoginData {
|
|
44
|
+
* email: string;
|
|
45
|
+
* password: string;
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* const form = useForm<LoginData>({
|
|
49
|
+
* submit: (data) => console.log(data.email, data.password)
|
|
50
|
+
* });
|
|
51
|
+
*
|
|
52
|
+
* return (
|
|
53
|
+
* <form ref={form.ref} onSubmit={form.onSubmit}>
|
|
54
|
+
* <input name="email" type="email" />
|
|
55
|
+
* <input name="password" type="password" />
|
|
56
|
+
* <button type="submit">Login</button>
|
|
57
|
+
* </form>
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function useForm<T = Record<string, unknown>>(
|
|
62
|
+
{ submit }: UseFormOptions<T>
|
|
63
|
+
): UseFormReturn<T> {
|
|
10
64
|
const ref = useRef<HTMLFormElement>(null);
|
|
11
65
|
|
|
12
|
-
const
|
|
66
|
+
const onSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
13
67
|
event.preventDefault();
|
|
14
68
|
|
|
15
|
-
if (!ref.current) return
|
|
69
|
+
if (!ref.current) return;
|
|
16
70
|
|
|
17
71
|
const formData = new FormData(ref.current);
|
|
18
72
|
|
|
19
|
-
const data
|
|
73
|
+
const data = parseFormData(formData) as T;
|
|
74
|
+
|
|
75
|
+
submit(data);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const reset = () => {
|
|
79
|
+
if (!ref.current) return;
|
|
80
|
+
|
|
81
|
+
ref.current.reset();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const getFormData = (): T | null => {
|
|
85
|
+
if (!ref.current) return null;
|
|
86
|
+
|
|
87
|
+
const formData = new FormData(ref.current);
|
|
88
|
+
|
|
89
|
+
const data = parseFormData(formData) as T;
|
|
90
|
+
|
|
91
|
+
return data;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const setValue = (name: string, value: string) => {
|
|
95
|
+
if (!ref.current) return;
|
|
96
|
+
|
|
97
|
+
const element = ref.current.elements.namedItem(name) as FormElement | null;
|
|
20
98
|
|
|
21
|
-
|
|
99
|
+
if (element && 'value' in element) {
|
|
100
|
+
element.value = value;
|
|
22
101
|
}
|
|
102
|
+
};
|
|
23
103
|
|
|
24
|
-
return {
|
|
104
|
+
return {
|
|
105
|
+
ref,
|
|
106
|
+
onSubmit,
|
|
107
|
+
reset,
|
|
108
|
+
getFormData,
|
|
109
|
+
setValue
|
|
110
|
+
};
|
|
25
111
|
}
|