|
1 | 1 | import React from "react"; |
2 | | -import { AnyListener, FormInput, useForm } from "typed-react-form"; |
| 2 | +import { |
| 3 | + AnyListener, |
| 4 | + FormInput, |
| 5 | + useForm, |
| 6 | + ArrayForm, |
| 7 | + ChildForm, |
| 8 | + FormState, |
| 9 | + useAnyListener, |
| 10 | + ErrorMap, |
| 11 | + FormError, |
| 12 | +} from "typed-react-form"; |
3 | 13 |
|
4 | 14 | function MyForm() { |
5 | 15 | const form = useForm({ email: "" }); |
@@ -38,12 +48,177 @@ function MyForm2() { |
38 | 48 | ); |
39 | 49 | } |
40 | 50 |
|
| 51 | +function ShoppingListForm() { |
| 52 | + const form = useForm({ |
| 53 | + title: "My shopping list", |
| 54 | + items: [{ name: "Coffee", amount: 1 }], |
| 55 | + }); |
| 56 | + |
| 57 | + return ( |
| 58 | + <form |
| 59 | + onSubmit={(ev) => { |
| 60 | + ev.preventDefault(); |
| 61 | + console.log("submit", form.values); |
| 62 | + }} |
| 63 | + > |
| 64 | + <h2>Title</h2> |
| 65 | + <FormInput form={form} type="text" name="title" /> |
| 66 | + |
| 67 | + {/* Create an array child form for the 'items' field */} |
| 68 | + <h2>Items</h2> |
| 69 | + <ArrayForm |
| 70 | + form={form} |
| 71 | + name="items" |
| 72 | + render={({ form, values, append, remove }) => ( |
| 73 | + <> |
| 74 | + {/* This only rerenders when the whole array changes. */} |
| 75 | + |
| 76 | + {values.map((_, i) => ( |
| 77 | + // Create a child form for each item in the array, because each array item is an object. |
| 78 | + <ChildForm |
| 79 | + key={i} // You should index as key |
| 80 | + form={form} // Pass the parent form |
| 81 | + name={i} // Pass the current index as the name |
| 82 | + render={(form) => ( |
| 83 | + <div> |
| 84 | + {/* Everything here is type-checked! */} |
| 85 | + <FormInput form={form} type="text" name="name" /> |
| 86 | + <FormInput form={form} type="number" name="amount" /> |
| 87 | + <button type="button" onClick={() => remove(i)}> |
| 88 | + Remove |
| 89 | + </button> |
| 90 | + </div> |
| 91 | + )} |
| 92 | + /> |
| 93 | + ))} |
| 94 | + |
| 95 | + {/* Use the append helper function to add an item to the array */} |
| 96 | + <button type="button" onClick={() => append({ name: "", amount: 1 })}> |
| 97 | + Add item |
| 98 | + </button> |
| 99 | + </> |
| 100 | + )} |
| 101 | + /> |
| 102 | + <button>Submit</button> |
| 103 | + </form> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function ToggleForm() { |
| 108 | + const form = useForm({ |
| 109 | + name: "codestix", |
| 110 | + location: { long: 123, lat: 456 }, // Object field |
| 111 | + }); |
| 112 | + return ( |
| 113 | + <form |
| 114 | + onSubmit={(ev) => { |
| 115 | + ev.preventDefault(); |
| 116 | + console.log("submit", form.values); |
| 117 | + }} |
| 118 | + > |
| 119 | + <FormInput form={form} name="name" type="text" /> |
| 120 | + |
| 121 | + {/* Use the setNullOnUncheck prop. The value prop contains the value that is set when the box gets checked again, you can omit it to use the default value */} |
| 122 | + <FormInput form={form} name="location" type="checkbox" setNullOnUncheck /> |
| 123 | + {/* ChildForm hides its contents when null/undefined by default */} |
| 124 | + <ChildForm |
| 125 | + form={form} |
| 126 | + name="location" |
| 127 | + render={(form) => ( |
| 128 | + <> |
| 129 | + <p>Location lat/long</p> |
| 130 | + <FormInput form={form} name="lat" type="number" /> |
| 131 | + <FormInput form={form} name="long" type="number" /> |
| 132 | + </> |
| 133 | + )} |
| 134 | + /> |
| 135 | + <button>Submit</button> |
| 136 | + </form> |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +// Take a form containing any values |
| 141 | +function FormJson(props: { form: FormState<any> }) { |
| 142 | + // Listen for all changes on the form |
| 143 | + const { values } = useAnyListener(props.form); |
| 144 | + // Show the json representation of the values in the form |
| 145 | + return <pre>{JSON.stringify(values, null, 2)}</pre>; |
| 146 | +} |
| 147 | + |
| 148 | +// Usage |
| 149 | +function ExampleForm() { |
| 150 | + const form = useForm({ name: "John", age: 19 }); |
| 151 | + return ( |
| 152 | + <form |
| 153 | + style={{ margin: "1em" }} |
| 154 | + onSubmit={async (ev) => { |
| 155 | + ev.preventDefault(); |
| 156 | + console.log("submit", form.values); |
| 157 | + }} |
| 158 | + > |
| 159 | + <FormInput form={form} name="name" /> |
| 160 | + <FormInput type="number" form={form} name="age" /> |
| 161 | + {/* Use your component, pass the form */} |
| 162 | + <FormJson form={form} /> |
| 163 | + {/* Using AnyListener, provides the same functionality */} |
| 164 | + <AnyListener form={form} render={({ values }) => <pre>{JSON.stringify(values, null, 2)}</pre>} /> |
| 165 | + <button>Submit</button> |
| 166 | + </form> |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +interface LoginRequest { |
| 171 | + email: string; |
| 172 | + password: string; |
| 173 | +} |
| 174 | + |
| 175 | +// May be async if needed |
| 176 | +function loginValidator(values: LoginRequest): ErrorMap<LoginRequest, string> { |
| 177 | + // Example validator logic, the returned error object should follow the same structure as the values object. |
| 178 | + return { |
| 179 | + email: values.email.length < 10 ? "Email must be longer" : undefined, |
| 180 | + password: values.password.length < 5 ? "Password must be longer" : undefined, |
| 181 | + }; |
| 182 | +} |
| 183 | + |
| 184 | +function FormExample() { |
| 185 | + const form = useForm<LoginRequest>( |
| 186 | + { email: "", password: "" }, |
| 187 | + loginValidator, // Pass loginValidator to useForm |
| 188 | + true, // Validate on change (false by default) |
| 189 | + false // Validate on mount (false by default) |
| 190 | + ); |
| 191 | + return ( |
| 192 | + <form |
| 193 | + onSubmit={(ev) => { |
| 194 | + ev.preventDefault(); |
| 195 | + console.log("submit", form.values); |
| 196 | + }} |
| 197 | + > |
| 198 | + <FormInput form={form} name="email" type="email" /> |
| 199 | + <FormError form={form} name="email" /> |
| 200 | + <FormInput form={form} name="password" type="password" /> |
| 201 | + <FormError form={form} name="password" /> |
| 202 | + {/* Listen for any change on the form, and disable the submit button when there is an error */} |
| 203 | + <AnyListener form={form} render={(form) => <button disabled={form.error}>Submit</button>} /> |
| 204 | + </form> |
| 205 | + ); |
| 206 | +} |
| 207 | + |
41 | 208 | export default function Testing() { |
42 | 209 | return ( |
43 | 210 | <> |
44 | 211 | <MyForm /> |
45 | 212 | <hr /> |
46 | 213 | <MyForm2 /> |
| 214 | + <hr /> |
| 215 | + <ShoppingListForm /> |
| 216 | + <hr /> |
| 217 | + <ToggleForm /> |
| 218 | + <hr /> |
| 219 | + <ExampleForm /> |
| 220 | + <hr /> |
| 221 | + <FormExample /> |
47 | 222 | </> |
48 | 223 | ); |
49 | 224 | } |
0 commit comments