Skip to content

Commit 1b377e1

Browse files
committed
Fixed in operator error bug
1 parent e068757 commit 1b377e1

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

docs/_config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# theme: "just-the-docs"
2-
# baseurl: "/typed-react-form"
3-
remote_theme: pmarsceill/just-the-docs
1+
theme: "just-the-docs"
2+
baseurl: "/typed-react-form"
3+
# remote_theme: pmarsceill/just-the-docs
44

55
aux_links:
66
"GitHub":

docs/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ function MyForm() {
3636
}
3737
```
3838

39+
<iframe src="https://codesandbox.io/embed/basic-typed-react-form-example-zz7uw?fontsize=14&hidenavigation=1&theme=dark"
40+
style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;"
41+
title="basic typed-react-form example"
42+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
43+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
44+
></iframe>
45+
3946
### Creating the submit handler
4047

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

src/Field.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export function Field<T extends object, K extends keyof T, C extends React.Funct
103103
style: { ...style, ...(field.dirty ? dirtyStyle : {}), ...(field.error ? errorStyle : {}) },
104104
field,
105105
onChange: (ev: any) => {
106-
let v = "target" in ev ? (["checkbox", "radio"].includes(props.type!) ? ev.target.checked : ev.target.value) : ev;
106+
let v =
107+
typeof ev === "object" && "target" in ev ? (["checkbox", "radio"].includes(props.type!) ? ev.target.checked : ev.target.value) : ev;
107108
if (typeof v === "string" || typeof v === "boolean")
108109
field.setValue((deserializer ?? defaultDeserializer)(v, field.value, serializeProps));
109110
else field.setValue(v);

testing/src/Fieldform.tsx

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

4-
function CustomInput(props: { value: any; onChange: React.ChangeEventHandler; style?: React.CSSProperties; className?: string; field: FormField }) {
5-
return (
6-
<input
7-
value={props.value}
8-
onChange={props.onChange}
9-
style={{ ...props.style, padding: "0.3em", color: props.field.error ? "yellow" : "unset" }}
10-
className={props.className}
11-
/>
12-
);
4+
const inputStyle: React.CSSProperties = {
5+
color: "gray",
6+
padding: "0.3em"
7+
};
8+
9+
// value and onChange are handled by the Field component
10+
function CustomInput(props: { value: string; onChange: (val: string) => void; myCustomProp?: boolean }) {
11+
return <input style={inputStyle} value={props.value} onChange={(ev) => props.onChange(ev.target.value)} />;
1312
}
1413

15-
export function FieldForm() {
16-
const form = useForm({ nice: "" }, (values) => ({ nice: values.nice.length < 5 ? "Must be longer" : undefined }));
14+
export default function ExampleForm() {
15+
const form = useForm({ firstName: "", lastName: "" });
1716

1817
function submit() {
1918
console.log(form.values);
20-
form.setDefaultValues(form.values);
2119
}
2220

2321
return (
2422
<form onSubmit={form.handleSubmit(submit)}>
25-
<Field
26-
form={form}
27-
name="nice"
28-
as={CustomInput}
29-
style={{ color: "gray", fontSize: "2em" }}
30-
className="blink"
31-
dirtyStyle={{ fontWeight: "bold" }}
32-
errorStyle={{ color: "red" }}
33-
/>
23+
<Field form={form} name="firstName" as={CustomInput} myCustomProp={true} />
24+
<Field form={form} name="lastName" as={CustomInput} />
3425
<button type="submit">Go</button>
3526
</form>
3627
);

0 commit comments

Comments
 (0)