|
1 | 1 | import React from "react";
|
2 |
| -import { useForm, Field, AnyListener, FieldError, FormField, Listener } from "typed-react-form"; |
| 2 | +import { useForm, Field } from "typed-react-form"; |
3 | 3 |
|
4 |
| -function Input(props: { value?: string; onChange?: (value: string) => void; style: React.CSSProperties }) { |
5 |
| - return <input style={{ padding: "0.3em", ...props.style }} value={props.value} onChange={(ev) => props.onChange?.(ev.target.value)} />; |
6 |
| -} |
7 |
| - |
8 |
| -function validate(_: any) { |
9 |
| - return { |
10 |
| - email: "yikes" |
11 |
| - }; |
12 |
| -} |
13 |
| - |
14 |
| -function Error(props: { children: React.ReactNode; field: FormField }) { |
15 |
| - return ( |
16 |
| - <p style={{ color: "red" }}> |
17 |
| - <strong> |
18 |
| - {props.children} ({props.field.dirty + ""}) |
19 |
| - </strong> |
20 |
| - </p> |
21 |
| - ); |
| 4 | +function CustomInput(props: { value: any; onChange: React.ChangeEventHandler; style?: React.CSSProperties; className?: string }) { |
| 5 | + return <input value={props.value} onChange={props.onChange} style={{ ...props.style, padding: "0.3em" }} className={props.className} />; |
22 | 6 | }
|
23 | 7 |
|
24 | 8 | export function FieldForm() {
|
25 |
| - const form = useForm({ email: "", firstName: "", gender: "male" as "male" | "female", enableEmail: true }, validate); |
| 9 | + const form = useForm({ nice: "" }, (values) => ({ nice: values.nice.length < 5 ? "Must be longer" : undefined })); |
26 | 10 |
|
27 | 11 | function submit() {
|
28 |
| - console.log("this is epic"); |
| 12 | + console.log(form.values); |
| 13 | + form.setDefaultValues(form.values); |
29 | 14 | }
|
30 | 15 |
|
31 | 16 | return (
|
32 | 17 | <form onSubmit={form.handleSubmit(submit)}>
|
33 |
| - <Field form={form} name="firstName" as="input" /> |
34 |
| - <Field form={form} name="enableEmail" type="checkbox" /> |
35 |
| - <Field form={form} name="email" style={{ margin: "2em" }} /> |
36 |
| - <Field form={form} name="email" as={Input} style={{ margin: "2em" }} /> |
37 |
| - <FieldError form={form} name="email" as={Error} /> |
38 |
| - <Field form={form} name="gender" as="select"> |
39 |
| - <option value="male">male</option> |
40 |
| - <option value="female">female</option> |
41 |
| - </Field> |
42 |
| - <p> |
43 |
| - value: <Listener form={form} name="firstName" /> |
44 |
| - </p> |
45 |
| - <pre> |
46 |
| - <AnyListener form={form} render={() => JSON.stringify(form.values, null, 2)} /> |
47 |
| - </pre> |
48 |
| - <button type="submit">Submit</button> |
49 |
| - </form> |
50 |
| - ); |
51 |
| -} |
52 |
| - |
53 |
| -function CustomInput(props: { value: any; onChange: React.ChangeEventHandler; style: React.CSSProperties }) { |
54 |
| - return <input value={props.value} onChange={props.onChange} style={{ ...props.style, padding: "0.3em" }} />; |
55 |
| -} |
56 |
| - |
57 |
| -function Testform() { |
58 |
| - const form = useForm({ nice: "" }); |
59 |
| - return ( |
60 |
| - <form> |
61 |
| - <Field form={form} name="nice" as={CustomInput} style={{ color: "gray" }} />; |
| 18 | + <Field |
| 19 | + form={form} |
| 20 | + name="nice" |
| 21 | + as={CustomInput} |
| 22 | + style={{ color: "gray", fontSize: "2em" }} |
| 23 | + className="blink" |
| 24 | + dirtyStyle={{ fontWeight: "bold" }} |
| 25 | + errorStyle={{ color: "red" }} |
| 26 | + /> |
| 27 | + <button type="submit">Go</button> |
62 | 28 | </form>
|
63 | 29 | );
|
64 | 30 | }
|
0 commit comments