|
| 1 | +# Array fields |
| 2 | + |
| 3 | +To create dynamic array fields, you should use the [`ArrayForm`](/docs/ArrayForm) component or [`useArrayForm`](/docs/useArrayForm) hook. These are wrappers around [`useChildForm`](/docs/useChildForm) which provide useful functions and optimizations for arrays. |
| 4 | + |
| 5 | +- [ArrayForm](/docs/ArrayForm) |
| 6 | +- [useArrayForm](/docs/useArrayForm) |
| 7 | + |
| 8 | +If you have an array field with a constant size, you should probably just use [`ChildForm`](/docs/ChildForm). (See bottom for examples) |
| 9 | + |
| 10 | +**Note on keys**: you **should** use the index as key, this seems against nature at first, but remember that this library does not rerender each time something in the array changes. When 2 array items get swapped, it does not rerender either, only when the array size changes, it rerenders. For this reason, it is not a problem (and it's recommended) to use index as the key. (This can change in the future) |
| 11 | + |
| 12 | +## Dynamic array examples |
| 13 | + |
| 14 | +✔️ **Dynamic string array field using `ArrayForm`** |
| 15 | + |
| 16 | +```tsx |
| 17 | +function NotesList() { |
| 18 | + const form = useForm({ |
| 19 | + notes: ["Do the dishes", "Go outside", "Drink some water"], |
| 20 | + }); |
| 21 | + return ( |
| 22 | + <form |
| 23 | + onSubmit={(ev) => { |
| 24 | + ev.preventDefault(); |
| 25 | + console.log("submit", form.values); |
| 26 | + }} |
| 27 | + > |
| 28 | + <ArrayForm |
| 29 | + form={form} |
| 30 | + name="notes" |
| 31 | + render={({ form, values, append, remove }) => ( |
| 32 | + <> |
| 33 | + {/* You SHOULD use index as key. See top for info. */} |
| 34 | + {values.map((_, i) => ( |
| 35 | + <div key={i}> |
| 36 | + <FormInput form={form} name={i} /> |
| 37 | + <button type="button" onClick={() => remove(i)}> |
| 38 | + Remove |
| 39 | + </button> |
| 40 | + </div> |
| 41 | + ))} |
| 42 | + <button type="button" onClick={() => append("New note")}> |
| 43 | + Add note |
| 44 | + </button> |
| 45 | + </> |
| 46 | + )} |
| 47 | + /> |
| 48 | + <button>Submit</button> |
| 49 | + </form> |
| 50 | + ); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +✔️ **Dynamic object array field using `ArrayForm`** |
| 55 | + |
| 56 | +Remember: this is all type checked! |
| 57 | + |
| 58 | +```tsx |
| 59 | +function ShoppingListForm() { |
| 60 | + const form = useForm({ |
| 61 | + title: "My shopping list", |
| 62 | + items: [{ name: "Coffee", amount: 1 }], |
| 63 | + }); |
| 64 | + |
| 65 | + return ( |
| 66 | + <form |
| 67 | + onSubmit={(ev) => { |
| 68 | + ev.preventDefault(); |
| 69 | + console.log("submit", form.values); |
| 70 | + }} |
| 71 | + > |
| 72 | + <h2>Title</h2> |
| 73 | + <FormInput form={form} type="text" name="title" /> |
| 74 | + |
| 75 | + {/* Create an array child form for the 'items' field */} |
| 76 | + <h2>Items</h2> |
| 77 | + <ArrayForm |
| 78 | + form={form} |
| 79 | + name="items" |
| 80 | + render={({ form, values, append, remove }) => ( |
| 81 | + <> |
| 82 | + {/* This only rerenders when the whole array changes. */} |
| 83 | + |
| 84 | + {values.map((_, i) => ( |
| 85 | + // Create a child form for each item in the array, because each array item is an object. |
| 86 | + <ChildForm |
| 87 | + key={i} // You should index as key |
| 88 | + form={form} // Pass the parent form |
| 89 | + name={i} // Pass the current index as the name |
| 90 | + render={(form) => ( |
| 91 | + <div> |
| 92 | + {/* Everything here is type-checked! */} |
| 93 | + <FormInput form={form} type="text" name="name" /> |
| 94 | + <FormInput form={form} type="number" name="amount" /> |
| 95 | + <button type="button" onClick={() => remove(i)}> |
| 96 | + Remove |
| 97 | + </button> |
| 98 | + </div> |
| 99 | + )} |
| 100 | + /> |
| 101 | + ))} |
| 102 | + |
| 103 | + {/* Use the append helper function to add an item to the array */} |
| 104 | + <button type="button" onClick={() => append({ name: "", amount: 1 })}> |
| 105 | + Add item |
| 106 | + </button> |
| 107 | + </> |
| 108 | + )} |
| 109 | + /> |
| 110 | + <button>Submit</button> |
| 111 | + </form> |
| 112 | + ); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +✔️ **Dynamic object array field with seperate component for each child form and using `useArrayForm`** |
| 117 | + |
| 118 | +```tsx |
| 119 | +interface ShoppingListItem { |
| 120 | + name: string; |
| 121 | + amount: number; |
| 122 | +} |
| 123 | +interface ShoppingList { |
| 124 | + title: string; |
| 125 | + items: ShoppingListItem[]; |
| 126 | +} |
| 127 | + |
| 128 | +function ShoppingListForm() { |
| 129 | + const form = |
| 130 | + useForm < |
| 131 | + ShoppingList > |
| 132 | + { |
| 133 | + title: "My shopping list", |
| 134 | + items: [{ name: "Coffee", amount: 1 }], |
| 135 | + }; |
| 136 | + return ( |
| 137 | + <form |
| 138 | + onSubmit={(ev) => { |
| 139 | + ev.preventDefault(); |
| 140 | + console.log("submit", form.values); |
| 141 | + }} |
| 142 | + > |
| 143 | + <h2>Title</h2> |
| 144 | + <FormInput form={form} type="text" name="title" /> |
| 145 | + <h2>Items</h2> |
| 146 | + <ShoppingListItemsForm parent={form} /> |
| 147 | + <button>Submit</button> |
| 148 | + </form> |
| 149 | + ); |
| 150 | +} |
| 151 | + |
| 152 | +function ShoppingListItemsForm(props: { parent: FormState<ShoppingList> }) { |
| 153 | + const { form, values, append, remove } = useArrayForm(props.parent, "items"); |
| 154 | + // This component only rerenders when the whole array changes. |
| 155 | + return ( |
| 156 | + <> |
| 157 | + {values.map((_, i) => ( |
| 158 | + <ShoppingListItemForm parent={form} index={i} key={i} remove={remove} /> |
| 159 | + ))} |
| 160 | + <button type="button" onClick={() => append({ name: "", amount: 1 })}> |
| 161 | + Add item |
| 162 | + </button> |
| 163 | + </> |
| 164 | + ); |
| 165 | +} |
| 166 | + |
| 167 | +function ShoppingListItemForm(props: { |
| 168 | + parent: FormState<ShoppingListItem[]>; |
| 169 | + index: number; |
| 170 | + remove: (i: number) => void; |
| 171 | +}) { |
| 172 | + const form = useChildForm(props.parent, props.index); |
| 173 | + return ( |
| 174 | + <div> |
| 175 | + <FormInput form={form} type="text" name="name" /> |
| 176 | + <FormInput form={form} type="number" name="amount" /> |
| 177 | + <button type="button" onClick={() => props.remove(props.index)}> |
| 178 | + Remove |
| 179 | + </button> |
| 180 | + </div> |
| 181 | + ); |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +## Fixed array example |
| 186 | + |
| 187 | +A fixed array always has the same size, [`ChildForm`](/docs/ChildForm) is used, and the index into the array is given using the name prop. |
| 188 | + |
| 189 | +✔️ **Fixed array field containing strings** |
| 190 | + |
| 191 | +```tsx |
| 192 | +function AnswerForm() { |
| 193 | + const form = useForm({ |
| 194 | + // Always 3 items in array |
| 195 | + answers: ["", "", ""], |
| 196 | + }); |
| 197 | + return ( |
| 198 | + <form |
| 199 | + onSubmit={(ev) => { |
| 200 | + ev.preventDefault(); |
| 201 | + console.log("submit", form.values); |
| 202 | + }} |
| 203 | + > |
| 204 | + <ChildForm |
| 205 | + parent={form} |
| 206 | + name="answers" |
| 207 | + render={(form) => ( |
| 208 | + <div> |
| 209 | + {/* Use array index as field name */} |
| 210 | + <p>Answer 1</p> |
| 211 | + <FormInput form={form} name={0} type="text" /> |
| 212 | + <p>Answer 2</p> |
| 213 | + <FormInput form={form} name={1} type="text" /> |
| 214 | + <p>Answer 3</p> |
| 215 | + <FormInput form={form} name={2} type="text" /> |
| 216 | + </div> |
| 217 | + )} |
| 218 | + /> |
| 219 | + <button>Submit</button> |
| 220 | + </form> |
| 221 | + ); |
| 222 | +} |
| 223 | +``` |
0 commit comments