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
Copy file name to clipboardExpand all lines: articles/Getting-started.md
+22-35Lines changed: 22 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,7 @@ This library works with both **Javascript** and **Typescript**. Typescript is ce
17
17
18
18
### Using the `useForm` hook
19
19
20
-
A form always starts with the `useForm` hook call, this function returns a form state manager ([FormState](/docs/FormState)), which you must then pass to all your inputs (this is required for type-checking).
21
-
22
-
All of the form hook ([`useForm`](/docs/useForm), [`useChildForm`](/docs/useChildForm) ...) must be called, unconditionally, at the start of your component, just like the normal React hooks.
20
+
A form always starts with the `useForm` hook call, this function returns a form state manager ([FormState](/docs/FormState)), which you must then pass to all your inputs.
23
21
24
22
✔️ **Importing and using `useForm`**
25
23
@@ -34,8 +32,7 @@ function MyForm() {
34
32
35
33
### Creating the submit handler
36
34
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!
35
+
Use `form.handleSubmit` to validate before calling your function. It does not get called if there is a validation error, and prevents the page from reloading.
39
36
40
37
✔️ **`<form>` element with `onSubmit` event**
41
38
@@ -44,37 +41,31 @@ import { useForm } from "typed-react-form";
44
41
45
42
function MyForm() {
46
43
const form =useForm({ email: "" });
44
+
45
+
function submit() {
46
+
// The form.handleSubmit validates the form before calling this function
47
+
console.log("submit", form.values);
48
+
}
49
+
47
50
// Use the standard html form element, which exposes the onSubmit event.
48
51
return (
49
-
<form
50
-
onSubmit={form.handleSubmit(() => {
51
-
// The form.handleSubmit validates the form before calling your callback
52
-
// Do your submit logic here...
53
-
console.log("submit", form.values);
54
-
})}
55
-
>
52
+
<formonSubmit={form.handleSubmit(submit)}>
56
53
{/* Make sure to add type="submit" */}
57
54
<buttontype="submit">Submit!</button>
58
55
</form>
59
56
);
60
57
}
61
58
```
62
59
63
-
`form.handleSubmit()` just returns a helper function that wraps `ev.preventDefault()`, `form.validate()` and `form.setState()`.
64
-
65
60
### Creating inputs
66
61
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.
68
-
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.
70
-
71
-
You are now ready to create inputs, this library provides the following built-in components to create type-checked inputs:
62
+
This library provides the following built-in components to create type-checked inputs:
72
63
73
64
-[FormInput](/docs/FormInput)
74
65
-[FormSelect](/docs/FormSelect)
75
66
-[FormTextArea](/docs/FormTextArea)
76
67
77
-
**When these inputs do not satisfy your needs**, you can always [create your own](/docs/Custom-inputs#example-custom-input). These built-in components are just abstractions around hook calls.
68
+
When these inputs do not satisfy your needs, you can always [implement your own](/docs/Custom-inputs#example-custom-input). These built-in components are just abstractions around hook calls.
78
69
79
70
✔️ **Example type-checked form consisting of 2 fields**
**When you have an object or array field**, you need to _unwrap_ this field by using a child/array form. When _unwrapped_ you can use the inputs above.
Copy file name to clipboardExpand all lines: articles/useArrayForm.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
A hook that provides array manipulation functions and optimizations for an array field. This is the array-optimized version of [`useChildform`](/docs/useChildForm). This hook only causes a rerender when the array size changes (this is why `values.map((e) => ...)` works when adding/removing a value).
4
4
5
+
This hook must be called, unconditionally, at the start of your component, just like the normal React hooks.
6
+
5
7
**If your array field can/will be null or undefined**, you should use the [`ArrayForm`](/docs/ArrayForm) component instead, which does not render when the array is null/undefined.
Copy file name to clipboardExpand all lines: articles/useChildForm.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
Creates a form based on another form's field. Use this with nested objects. This hook does not cause a rerender.
4
4
5
+
This hook must be called, unconditionally, at the start of your component, just like the normal React hooks.
6
+
5
7
**If your field can/will be null or undefined**, you should use the [`ChildForm`](/docs/ChildForm) component instead, which does not render when the field is null/undefined.
Copy file name to clipboardExpand all lines: articles/useForm.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
Creates a new form state manager. This hook does not cause a rerender.
4
4
5
+
This hook must be called, unconditionally, at the start of your component, just like the normal React hooks.
6
+
5
7
**Note for using `setState` along with `useForm`**: This library is built upon the fact that only the things that change should rerender. When using `setState` with the form, a state change causes the whole form to rerender. You can reduce this problem by creating components from the things that use the state, or you can use [custom form state](/docs/useForm#defaultstate-optional-issubmitting-false).
Copy file name to clipboardExpand all lines: articles/useListener.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
1
# `useListener`
2
2
3
-
Because this library does not rerender the whole form component when a field changes, there must be a way to get notified about state changes. This is where listeners come in.
3
+
This library is build upon the fact that only the things that change should rerender, for example: when the _name_ field changes, only the inputs that use the _name_ field will rerender.
4
+
5
+
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).
4
6
5
7
The useListener hook listens for changes on a specific form field. It behaves like useState. Because this hooks causes a rerender, you **shouldn't** use
6
8
this hook in the same component as the form it is using (causes the whole form to rerender). You **should** always create a new component which contains the hook and use that. Or use the [`Listener`](/docs/Listener) component, which wraps the `useListener` hook for you.
7
9
10
+
This hook must be called, unconditionally, at the start of your component, just like the normal React hooks.
11
+
8
12
**To listen for all form fields at once**, use the [`useAnyListener`](/docs/useAnyListener) hook instead.
0 commit comments