Skip to content

Commit 0c7eba8

Browse files
committed
Update getting started
1 parent 205f45c commit 0c7eba8

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

articles/Getting-started.md

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ npm install --save typed-react-form
1111
yarn add typed-react-form
1212
```
1313

14-
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!
1515

1616
## Step 2: Creating a form
1717

@@ -34,7 +34,8 @@ function MyForm() {
3434

3535
### Creating the submit handler
3636

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!
3839

3940
✔️ **`<form>` element with `onSubmit` event**
4041

@@ -43,29 +44,27 @@ import { useForm } from "typed-react-form";
4344

4445
function MyForm() {
4546
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.
4748
return (
4849
<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...
5553
console.log("submit", form.values);
56-
}}
54+
})}
5755
>
58-
<button>Submit!</button>
56+
{/* Make sure to add type="submit" */}
57+
<button type="submit">Submit!</button>
5958
</form>
6059
);
6160
}
6261
```
6362

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()`.
6564

6665
### Creating inputs
6766

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.
6968

7069
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.
7170

@@ -87,25 +86,19 @@ function MyForm() {
8786
const form = useForm({ email: "", password: "" });
8887
return (
8988
<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);
9692
// Fake fetch, by waiting for 500ms
97-
console.log("submit", form.values);
9893
await new Promise((res) => setTimeout(res, 500));
99-
// Notify every input that the form is not submitting anymore.
100-
form.setState({ isSubmitting: false });
101-
// Set new default values if needed
94+
// Optional: set new default values
10295
form.setDefaultValues(form.values);
103-
}}
96+
})}
10497
>
10598
{/* Make sure to pass the form prop! */}
10699
<FormInput form={form} name="email" type="text" />
107100
<FormInput form={form} name="password" type="password" />
108-
<button>Submit!</button>
101+
<button type="submit">Submit!</button>
109102
</form>
110103
);
111104
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"react-markdown": "^5.0.3",
1919
"react-syntax-highlighter": "^15.4.3",
2020
"styled-components": "^5.2.1",
21-
"typed-react-form": "^1.2.10"
21+
"typed-react-form": "^1.2.11"
2222
},
2323
"devDependencies": {
2424
"@types/node": "^14.14.34",

pages/docs/[doc].tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const ReactMarkdownContainer = styled.div`
3636
overflow: hidden;
3737
margin-bottom: 30em;
3838
39+
pre {
40+
border-radius: 0.3em;
41+
}
42+
3943
code {
4044
font-size: 1.3em;
4145
}
@@ -66,15 +70,21 @@ const ReactMarkdownContainer = styled.div`
6670
margin: 1.2em 0 0.5em 0;
6771
}
6872
69-
/* h1,
73+
h1,
7074
h2 {
75+
/* color: #004; */
7176
padding-bottom: 0.2em;
7277
border-bottom: 1px solid #0002;
73-
} */
78+
}
7479
7580
p {
7681
color: #333;
7782
}
83+
84+
li {
85+
margin-top: 0.2em;
86+
margin-bottom: 0.2em;
87+
}
7888
`;
7989

8090
const SidebarHolder = styled.div`
@@ -102,7 +112,10 @@ export default function DocPage(props: Props) {
102112
<main onClick={() => setShowMobileMenu(false)}>
103113
<Head>
104114
<link rel="preconnect" href="https://fonts.gstatic.com" />
105-
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&family=Roboto&display=swap" rel="stylesheet" />
115+
<link
116+
href="https://fonts.googleapis.com/css2?family=Open+Sans&family=Roboto&display=swap"
117+
rel="stylesheet"
118+
/>
106119
</Head>
107120
<NavBar onMenu={() => setShowMobileMenu(true)} />
108121
<CenterContainer style={{ margin: "0 0.5em" }}>
@@ -121,9 +134,16 @@ export default function DocPage(props: Props) {
121134
);
122135
},
123136
code: ({ language, value }) => {
124-
return <SyntaxHighlighter style={materialOceanic} language={language} children={value} />;
137+
return (
138+
<SyntaxHighlighter
139+
style={materialOceanic}
140+
language={language}
141+
children={value}
142+
/>
143+
);
125144
},
126-
}}>
145+
}}
146+
>
127147
{props.content}
128148
</ReactMarkdown>
129149
</ReactMarkdownContainer>

pages/docs/test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from "react";
2+
import { AnyListener, FormInput, useForm } from "typed-react-form";
3+
4+
function MyForm() {
5+
const form = useForm({ email: "" });
6+
// Use the standard html form element, which exposes the onSubmit event.
7+
return (
8+
<form
9+
onSubmit={form.handleSubmit(() => {
10+
// The form.handleSubmit validates the form before calling your callback
11+
console.log("submit", form.values);
12+
})}
13+
>
14+
{/* Make sure to add type="submit" */}
15+
<button type="submit">Submit!</button>
16+
</form>
17+
);
18+
}
19+
20+
function MyForm2() {
21+
const form = useForm({ email: "", password: "" });
22+
return (
23+
<form
24+
onSubmit={form.handleSubmit(async (ev) => {
25+
// Implement your submit logic here...
26+
console.log("submitting", form.values);
27+
// Fake fetch, by waiting for 500ms
28+
await new Promise((res) => setTimeout(res, 500));
29+
// Optional: set new default values
30+
form.setDefaultValues(form.values);
31+
})}
32+
>
33+
{/* Make sure to pass the form prop! */}
34+
<FormInput form={form} name="email" type="text" />
35+
<FormInput form={form} name="password" type="password" />
36+
<button type="submit">Submit!</button>
37+
</form>
38+
);
39+
}
40+
41+
export default function Testing() {
42+
return (
43+
<>
44+
<MyForm />
45+
<hr />
46+
<MyForm2 />
47+
</>
48+
);
49+
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,10 +2106,10 @@ type-fest@^0.7.1:
21062106
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.7.1.tgz#8dda65feaf03ed78f0a3f9678f1869147f7c5c48"
21072107
integrity sha512-Ne2YiiGN8bmrmJJEuTWTLJR32nh/JdL1+PSicowtNb0WFpn59GK8/lfD61bVtzguz7b3PBt74nxpv/Pw5po5Rg==
21082108

2109-
typed-react-form@^1.2.10:
2110-
version "1.2.10"
2111-
resolved "https://registry.yarnpkg.com/typed-react-form/-/typed-react-form-1.2.10.tgz#e43844284023b3a5b36253c8b9e6f4697e8a7436"
2112-
integrity sha512-4eW/kdSSlfovsaIyUvt6nsML1BREEKYruGfaA8m2/jr/+zm4CQLHbLXx+6YT/M/ZEs3XvomkJxIG0fFN4VkK3g==
2109+
typed-react-form@^1.2.11:
2110+
version "1.2.11"
2111+
resolved "https://registry.yarnpkg.com/typed-react-form/-/typed-react-form-1.2.11.tgz#bf0ad996df8d2344193dfb323d1087cd5a3e2bd7"
2112+
integrity sha512-yxDt4KsHC4tVTbeshtuwLFiFE1k/BnHL8m0pMpbTKnifA/G98Dl82ui1UxzgZSgOYNYhnFdGxJXE9SmN+dttfA==
21132113

21142114
typescript@^4.2.3:
21152115
version "4.2.3"

0 commit comments

Comments
 (0)