rfhook 1.1.0 → 1.1.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.
Files changed (2) hide show
  1. package/README.md +73 -22
  2. package/package.json +5 -1
package/README.md CHANGED
@@ -4,13 +4,13 @@ A lightweight React hook for handling form submissions with advanced form data p
4
4
 
5
5
  ## Features
6
6
 
7
- - 🚀 **Simple API** - Just one hook to handle all your form needs
8
- - 🎯 **TypeScript Support** - Fully typed with generic support
9
- - 🔧 **Nested Objects** - Parse nested form data with dot notation (`user.name`)
10
- - 📋 **Array Support** - Handle arrays with indexed notation (`items[0]`)
11
- - �️ **Automatic Prevention** - Prevents default form submission behavior by default
12
- - �📦 **Lightweight** - Zero dependencies (except React)
13
- - 🎨 **Framework Agnostic** - Works with any form structure
7
+ - :rocket: **Simple API** - Just one hook to handle all your form needs
8
+ - :dart: **TypeScript Support** - Fully typed with generic support
9
+ - :wrench: **Nested Objects** - Parse nested form data with dot notation (`user.name`)
10
+ - :clipboard: **Array Support** - Handle arrays with indexed notation (`items[0]`)
11
+ - :floppy_disk: **Automatic Prevention** - Prevents default form submission behavior by default
12
+ - :money_with_wings: **Lightweight** - Zero dependencies (except React)
13
+ - :memo: **Framework Agnostic** - Works with any form structure
14
14
 
15
15
  ## Installation
16
16
 
@@ -38,19 +38,23 @@ interface FormData {
38
38
  }
39
39
 
40
40
  function LoginForm() {
41
- const { ref, submit } = useForm<FormData>({
42
- handleSubmit: (data) => {
41
+ const { ref, onSubmit } = useForm<FormData>({
42
+ submit: (data) => {
43
43
  console.log(data); // { email: "user@example.com", password: "secret" }
44
44
  }
45
45
  });
46
46
 
47
47
  return (
48
- <form ref={ref} onSubmit={submit}>
48
+ <form ref={ref} onSubmit={onSubmit}>
49
49
  <input name="email" type="email" />
50
50
  <input name="password" type="password" />
51
51
  <button type="submit">Login</button>
52
52
  </form>
53
53
  );
54
+ }
55
+ <button type="submit">Login</button>
56
+ </form>
57
+ );
54
58
  }
55
59
  ```
56
60
 
@@ -74,8 +78,8 @@ interface UserForm {
74
78
  }
75
79
 
76
80
  function UserForm() {
77
- const { ref, submit } = useForm<UserForm>({
78
- handleSubmit: (data) => {
81
+ const { ref, onSubmit } = useForm<UserForm>({
82
+ submit: (data) => {
79
83
  console.log(data);
80
84
  // {
81
85
  // user: {
@@ -92,7 +96,7 @@ function UserForm() {
92
96
  });
93
97
 
94
98
  return (
95
- <form ref={ref} onSubmit={submit}>
99
+ <form ref={ref} onSubmit={onSubmit}>
96
100
  <input name="user.profile.name" placeholder="Name" />
97
101
  <input name="user.profile.age" type="number" placeholder="Age" />
98
102
  <select name="user.preferences.theme">
@@ -118,8 +122,8 @@ interface TodoForm {
118
122
  }
119
123
 
120
124
  function TodoForm() {
121
- const { ref, submit } = useForm<TodoForm>({
122
- handleSubmit: (data) => {
125
+ const { ref, onSubmit } = useForm<TodoForm>({
126
+ submit: (data) => {
123
127
  console.log(data);
124
128
  // {
125
129
  // todos: [
@@ -131,7 +135,7 @@ function TodoForm() {
131
135
  });
132
136
 
133
137
  return (
134
- <form ref={ref} onSubmit={submit}>
138
+ <form ref={ref} onSubmit={onSubmit}>
135
139
  <input name="todos[0].title" placeholder="First todo" />
136
140
  <input name="todos[0].completed" type="checkbox" />
137
141
 
@@ -150,12 +154,59 @@ function TodoForm() {
150
154
 
151
155
  #### Parameters
152
156
 
153
- - `options.handleSubmit: (data: T) => void` - Callback function called when form is submitted
157
+ - `options.submit: (data: T) => void` - Callback function called when form is submitted with parsed form data
154
158
 
155
159
  #### Returns
156
160
 
157
- - `ref: RefObject<HTMLFormElement>` - React ref to attach to your form element
158
- - `submit: (event: React.FormEvent) => void` - Submit handler to attach to form's `onSubmit`
161
+ - `ref: React.RefObject<HTMLFormElement>` - React ref to attach to your form element
162
+ - `onSubmit: (event: React.FormEvent<HTMLFormElement>) => void` - Form submission handler that prevents default behavior and calls submit with parsed data
163
+ - `reset: () => void` - Resets the form to its initial state
164
+ - `getFormData: () => T | null` - Gets current form data without triggering submission (returns null if form ref is not available)
165
+ - `setValue: (name: string, value: string) => void` - Sets the value of a specific form field by name
166
+
167
+ ### Additional Usage Examples
168
+
169
+ #### Using Form Utilities
170
+
171
+ ```tsx
172
+ function MyForm() {
173
+ const form = useForm<FormData>({
174
+ submit: (data) => console.log('Submitted:', data)
175
+ });
176
+
177
+ const handleReset = () => {
178
+ form.reset(); // Reset form to initial state
179
+ };
180
+
181
+ const handlePreview = () => {
182
+ const currentData = form.getFormData();
183
+ if (currentData) {
184
+ console.log('Current form data:', currentData);
185
+ }
186
+ };
187
+
188
+ const handlePrefill = () => {
189
+ form.setValue('email', 'user@example.com');
190
+ form.setValue('name', 'John Doe');
191
+ };
192
+
193
+ return (
194
+ <>
195
+ <form ref={form.ref} onSubmit={form.onSubmit}>
196
+ <input name="name" placeholder="Name" />
197
+ <input name="email" type="email" placeholder="Email" />
198
+ <button type="submit">Submit</button>
199
+ </form>
200
+
201
+ <div>
202
+ <button onClick={handleReset}>Reset Form</button>
203
+ <button onClick={handlePreview}>Preview Data</button>
204
+ <button onClick={handlePrefill}>Prefill Form</button>
205
+ </div>
206
+ </>
207
+ );
208
+ }
209
+ ```
159
210
 
160
211
  ### Form Data Parsing Rules
161
212
 
@@ -183,8 +234,8 @@ interface ContactData {
183
234
  }
184
235
 
185
236
  function ContactForm() {
186
- const { ref, submit } = useForm<ContactData>({
187
- handleSubmit: async (data) => {
237
+ const { ref, onSubmit } = useForm<ContactData>({
238
+ submit: async (data) => {
188
239
  try {
189
240
  const response = await fetch('/api/contact', {
190
241
  method: 'POST',
@@ -202,7 +253,7 @@ function ContactForm() {
202
253
  });
203
254
 
204
255
  return (
205
- <form ref={ref} onSubmit={submit}>
256
+ <form ref={ref} onSubmit={onSubmit}>
206
257
  <input name="name" placeholder="Your Name" required />
207
258
  <input name="email" type="email" placeholder="Your Email" required />
208
259
  <textarea name="message" placeholder="Your Message" required />
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "rfhook",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/evandroishikawa/rfhook.git"
10
+ },
7
11
  "type": "module",
8
12
  "types": "dist/index.d.ts",
9
13
  "keywords": [],