|
1 | 1 | import React from "react"; |
2 | | -import { useForm, Field, FormField } from "typed-react-form"; |
| 2 | +import { useForm, Field } from "typed-react-form"; |
3 | 3 |
|
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)} />; |
13 | 12 | } |
14 | 13 |
|
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: "" }); |
17 | 16 |
|
18 | 17 | function submit() { |
19 | 18 | console.log(form.values); |
20 | | - form.setDefaultValues(form.values); |
21 | 19 | } |
22 | 20 |
|
23 | 21 | return ( |
24 | 22 | <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} /> |
34 | 25 | <button type="submit">Go</button> |
35 | 26 | </form> |
36 | 27 | ); |
|
0 commit comments