Skip to content

Commit 53ab140

Browse files
committed
Replace FormError occurences with FieldError
1 parent 7d27306 commit 53ab140

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

docs/reference/FormError.md

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ layout: default
33
parent: Reference
44
---
55

6-
# `<FormError />`
6+
# `<FieldError />`
77

8-
A very simple field error component.
8+
This component renders an error for a specific form field.
99

1010
```tsx
1111
const form = useForm(
@@ -17,28 +17,18 @@ const form = useForm(
1717

1818
<Field form={form} name="name" />
1919

20-
// Will render a `p` element on error.
21-
<FormError form={form} name="name" />
22-
```
23-
24-
## Custom error component
25-
26-
Because this component doesn't have a lot of functionality (only props are `form` and `name`), it is recommended to create a FormError component yourself.
27-
28-
Below is an example of a custom form error component.
20+
// Will render a `React.Fragment` (plain text) element on error.
21+
<FieldError form={form} name="name" />
2922

30-
```tsx
31-
// Use generics to create type-safe code
32-
function CustomFormError<T>(props: { form: FormState<T>; name: keyof T }) {
33-
// Listen for changes on a form field, behaves like useState
34-
const { error } = useListener(props.form, props.name);
35-
36-
// Render nothing when no error
37-
if (!error) return null;
23+
// Will render a `p` element on error.
24+
<FieldError as="p" form={form} name="name" />
3825

39-
// Render a styled span on error.
40-
return <span style={{ color: "red", fontWeight: "bold" }}>{error}</span>;
26+
function CustomError(props: {children?: React.ReactNode}) {
27+
return <p style={{color: "red", margin: "0.3em"}}>
28+
{props.children}
29+
</p>
4130
}
42-
```
4331

44-
You can also create custom input components, look [here](/typed-react-form/examples/Custom-input).
32+
// Will render `CustomError`, with the error passed in the children prop, on error.
33+
<FieldError as={CustomError} form={form} name="name" />
34+
```

docs/validation/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ You can ofcourse use every validation library there is, but the errors must be c
2323
## Example
2424

2525
```tsx
26-
import { useForm, Field, FormError, AnyListener, ErrorMap } from "typed-react-form";
26+
import { useForm, Field, FieldError, AnyListener, ErrorMap } from "typed-react-form";
2727

2828
interface LoginRequest {
2929
email: string;

0 commit comments

Comments
 (0)