You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library works with both **Javascript** and **Typescript**. **Typescript** is certainly preferred because of the enforced type-checking!
14
+
This library works with both **Javascript** and **Typescript**. Typescript is certainly preferred because of the enforced type-checking!
15
15
16
16
## Step 2: Creating a form
17
17
@@ -34,7 +34,8 @@ function MyForm() {
34
34
35
35
### Creating the submit handler
36
36
37
-
**typed-react-form** does not expose any submit events or helpers, you must implement your own submitting procedure. This is typically done by using the `<form>` html element with the `onSubmit` event and using a submit button.
37
+
Pass `form.handleSubmit` to the form's `onSubmit` prop to validate before calling your callback function.
38
+
You can also just use a `<button>` and submitting in the button's `onClick` handler, but this event does not fire when pressing return/enter in a text input!
38
39
39
40
✔️ **`<form>` element with `onSubmit` event**
40
41
@@ -43,29 +44,27 @@ import { useForm } from "typed-react-form";
43
44
44
45
function MyForm() {
45
46
const form =useForm({ email: "" });
46
-
// Use the html form element, which exposes the onSubmit event.
47
+
// Use the standard html form element, which exposes the onSubmit event.
47
48
return (
48
49
<form
49
-
onSubmit={(ev) => {
50
-
// IMPORTANT: Prevent the onSubmit event from reloading the page!
51
-
ev.preventDefault();
52
-
// Do not submit if there is an error! Use form.validate to validate when validateOnChange is disabled.
53
-
if (form.error) return;
54
-
// form.values contains the modified values, form.defaultValues contains the initial values
50
+
onSubmit={form.handleSubmit(() => {
51
+
// The form.handleSubmit validates the form before calling your callback
52
+
// Do your submit logic here...
55
53
console.log("submit", form.values);
56
-
}}
54
+
})}
57
55
>
58
-
<button>Submit!</button>
56
+
{/* Make sure to add type="submit" */}
57
+
<buttontype="submit">Submit!</button>
59
58
</form>
60
59
);
61
60
}
62
61
```
63
62
64
-
You can also just use a `<button>` and submitting in the button's `onClick` handler, but this event does not fire when pressing enter in a text input!
63
+
`form.handleSubmit()`just returns a helper function that wraps `ev.preventDefault()`, `form.validate()` and `form.setState()`.
65
64
66
65
### Creating inputs
67
66
68
-
This library is build upon the fact that only the things that change should rerender (~refresh), for example: when the _name_ field changes, only the inputs that use _name_ field will rerender.
67
+
This library is build upon the fact that only the things that change should rerender (~refresh), for example: when the _name_ field changes, only the inputs that use the _name_ field will rerender.
69
68
70
69
The built-in form elements ([`FormInput`](/docs/FormInput), [`FormSelect`](/docs/FormSelect) ...) implement this by listening for changes only on their specified field. You can also use multiple inputs on the same field (they will the synchronized) and listen for changes on a field by using the [`useListener`](/docs/useListener) hook or [`Listener`](/docs/Listener) component.
71
70
@@ -87,25 +86,19 @@ function MyForm() {
87
86
const form =useForm({ email: "", password: "" });
88
87
return (
89
88
<form
90
-
onSubmit={async (ev) => {
91
-
ev.preventDefault();
92
-
if (form.error) return;
93
-
94
-
// Notify every input that the form is submitting. This will disable them.
95
-
form.setState({ isSubmitting: true });
89
+
onSubmit={form.handleSubmit(async (ev) => {
90
+
// Implement your submit logic here...
91
+
console.log("submitting", form.values);
96
92
// Fake fetch, by waiting for 500ms
97
-
console.log("submit", form.values);
98
93
awaitnewPromise((res) =>setTimeout(res, 500));
99
-
// Notify every input that the form is not submitting anymore.
0 commit comments