|
| 1 | +import React from "react"; |
| 2 | +import { AnyListener, ArrayForm, FormInput, FormState, useChildForm, useForm, useListener } from "typed-react-form"; |
| 3 | + |
| 4 | +interface Apple { |
| 5 | + type: "apple"; |
| 6 | + color: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface Bread { |
| 10 | + type: "bread"; |
| 11 | + size: number; |
| 12 | +} |
| 13 | + |
| 14 | +type FormDataObject = Apple | Bread; |
| 15 | + |
| 16 | +interface FormData { |
| 17 | + objects: FormDataObject[]; |
| 18 | +} |
| 19 | + |
| 20 | +export default function OneOfObjectArrayForm() { |
| 21 | + const form = useForm<FormData>({ |
| 22 | + objects: [ |
| 23 | + { type: "apple", color: "#ff0000" }, |
| 24 | + { type: "bread", size: 200 } |
| 25 | + ] |
| 26 | + }); |
| 27 | + return ( |
| 28 | + <form |
| 29 | + style={{ margin: "0.5em" }} |
| 30 | + onReset={() => form.resetAll()} |
| 31 | + onSubmit={async (ev) => { |
| 32 | + ev.preventDefault(); |
| 33 | + form.setState({ isSubmitting: true }); |
| 34 | + await new Promise((res) => setTimeout(res, 500)); |
| 35 | + form.setState({ isSubmitting: false }); |
| 36 | + console.log(form.values); |
| 37 | + form.setDefaultValues(form.values); |
| 38 | + }} |
| 39 | + > |
| 40 | + <ArrayForm |
| 41 | + form={form} |
| 42 | + name="objects" |
| 43 | + render={({ form, values, append, remove }) => ( |
| 44 | + <> |
| 45 | + <ul> |
| 46 | + {values.map((_, i) => ( |
| 47 | + <li key={i}> |
| 48 | + {/* Make sure to use the form from ArrayForm! */} |
| 49 | + <BreadOrAppleForm parent={form} index={i} remove={() => remove(i)} /> |
| 50 | + </li> |
| 51 | + ))} |
| 52 | + </ul> |
| 53 | + <hr /> |
| 54 | + <button type="button" onClick={() => append({ type: "apple", color: "#ff0000" })}> |
| 55 | + Add Apple |
| 56 | + </button> |
| 57 | + <button type="button" onClick={() => append({ type: "bread", size: 200 })}> |
| 58 | + Add Bread |
| 59 | + </button> |
| 60 | + </> |
| 61 | + )} |
| 62 | + /> |
| 63 | + <AnyListener |
| 64 | + form={form} |
| 65 | + render={({ values, dirty }) => ( |
| 66 | + <pre> |
| 67 | + {dirty ? "MODIFIED" : "UNMODIFIED"} |
| 68 | + <br /> |
| 69 | + {JSON.stringify(values, null, 2)} |
| 70 | + </pre> |
| 71 | + )} |
| 72 | + /> |
| 73 | + <button>Submit!</button> |
| 74 | + <button type="reset">Reset</button> |
| 75 | + </form> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +function BreadOrAppleForm(props: { parent: FormState<FormDataObject[]>; index: number; remove: () => void }) { |
| 80 | + // Create a new child form with the array as the parent and index as the key |
| 81 | + const form = useChildForm(props.parent, props.index); |
| 82 | + // Listen for changes on the 'type' field, which contains 'apple' or 'bread'. This component will rerender when it changes |
| 83 | + const { value: type } = useListener(form, "type"); |
| 84 | + return ( |
| 85 | + <div style={{ background: "#0001", padding: "1em", margin: "1em" }}> |
| 86 | + {/* A select input that sets new form values when a new object type (apple or bread) was chosen */} |
| 87 | + <label>Object type: </label> |
| 88 | + <select |
| 89 | + value={type} |
| 90 | + onChange={(ev) => { |
| 91 | + if (ev.target.value === "apple") form.setValues({ type: "apple", color: "red" }); |
| 92 | + else if (ev.target.value === "bread") form.setValues({ type: "bread", size: 200 }); |
| 93 | + }} |
| 94 | + > |
| 95 | + <option value="apple">Apple</option> |
| 96 | + <option value="bread">Bread</option> |
| 97 | + </select> |
| 98 | + |
| 99 | + {/* Render AppleForm when the type is 'apple'. When type is 'bread', render BreadForm */} |
| 100 | + {type === "apple" ? <AppleForm form={form as FormState<Apple>} /> : <BreadForm form={form as FormState<Bread>} />} |
| 101 | + |
| 102 | + {/* Remove this item from the list */} |
| 103 | + <button type="button" onClick={() => props.remove()}> |
| 104 | + Remove |
| 105 | + </button> |
| 106 | + </div> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +function AppleForm({ form }: { form: FormState<Apple> }) { |
| 111 | + return ( |
| 112 | + <div> |
| 113 | + <h4>Apple editor</h4> |
| 114 | + <p>Select the color of your apple</p> |
| 115 | + <FormInput form={form} type="color" name="color" /> |
| 116 | + </div> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +function BreadForm({ form }: { form: FormState<Bread> }) { |
| 121 | + return ( |
| 122 | + <div> |
| 123 | + <h4>Bread editor</h4> |
| 124 | + <p>Select the size of your bread</p> |
| 125 | + <FormInput form={form} type="number" name="size" /> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +} |
0 commit comments