Skip to content

Commit 135d3d3

Browse files
committed
Pass form field to FieldError
1 parent 78bb40c commit 135d3d3

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/FieldError.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { DefaultError, DefaultState, FormState } from "./form";
2+
import { DefaultError, DefaultState, ErrorType, FormState } from "./form";
33
import { useListener } from "./hooks";
44
import { ElementProps } from "./Field";
55

@@ -29,11 +29,11 @@ export function FieldError<
2929
* `<FieldError as={CustomError} {...} />`
3030
*/
3131
as?: C;
32-
transformer?: (error: Error) => React.ReactNode;
33-
} & Omit<ElementProps<C>, "transformer" | "as" | "name" | "form" | "children">
32+
transformer?: (error: ErrorType<T[K], Error>) => React.ReactNode;
33+
} & Omit<ElementProps<C>, "transformer" | "as" | "name" | "form" | "children" | "field">
3434
) {
3535
const { form, as = React.Fragment, transformer, ...rest } = props;
36-
const { error } = useListener(form, props.name);
37-
if (!error || typeof error === "object") return null;
38-
return React.createElement(as, { ...rest, children: String(transformer ? transformer(error as Error) : error) });
36+
const field = useListener(form, props.name);
37+
if (!field.error || typeof field.error === "object") return null;
38+
return React.createElement(as, { ...rest, field, children: transformer ? transformer(field.error) : String(field.error) });
3939
}

testing/src/Fieldform.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { useForm, Field, AnyListener, FieldError } from "typed-react-form";
2+
import { useForm, Field, AnyListener, FieldError, FormField } from "typed-react-form";
33

44
function Input(props: { value?: string; onChange?: (value: string) => void; style: React.CSSProperties }) {
55
return <input style={{ padding: "0.3em", ...props.style }} value={props.value} onChange={(ev) => props.onChange?.(ev.target.value)} />;
@@ -11,11 +11,12 @@ function validate(_: any) {
1111
};
1212
}
1313

14-
function Error(props: { children: React.ReactNode }) {
14+
function Error(props: { children: React.ReactNode; field: FormField }) {
1515
return (
16-
<p>
17-
not epic:
18-
<strong>{props.children}</strong>
16+
<p style={{ color: "red" }}>
17+
<strong>
18+
{props.children} ({props.field.dirty + ""})
19+
</strong>
1920
</p>
2021
);
2122
}

0 commit comments

Comments
 (0)