You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/Array-fields.md
+32-25Lines changed: 32 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,9 @@
1
+
# Array fields
2
+
1
3
To create dynamic array fields, you should use the [`ArrayForm`](https://github.com/CodeStix/typed-react-form/wiki/ArrayForm) component or [`useArrayForm`](https://github.com/CodeStix/typed-react-form/wiki/useArrayForm) hook. These are wrappers around [`useChildForm`](https://github.com/CodeStix/typed-react-form/wiki/useChildForm) which provide useful functions and optimizations for arrays.
If you have an array field with a constant size, you should probably just use [`ChildForm`](https://github.com/CodeStix/typed-react-form/wiki/ChildForm). (See bottom for examples)
7
9
@@ -10,24 +12,27 @@ If you have an array field with a constant size, you should probably just use [`
10
12
## Dynamic array examples
11
13
12
14
✔️ **Dynamic string array field using `ArrayForm`**
15
+
13
16
```jsx
14
17
functionNotesList() {
15
18
constform=useForm({
16
-
notes: ["Do the dishes", "Go outside", "Drink some water"]
19
+
notes: ["Do the dishes", "Go outside", "Drink some water"],
17
20
});
18
21
return (
19
22
<form
20
23
onSubmit={(ev) => {
21
24
ev.preventDefault();
22
25
console.log("submit", form.values);
23
-
}}
24
-
>
26
+
}}>
25
27
<ArrayForm
26
28
parent={form}
27
29
name="notes"
28
30
render={({ form, values, append, remove }) => (
29
31
<>
30
-
{values.map((_, i) => ( // You SHOULD use index as key. See top for info.
32
+
{values.map((
33
+
_,
34
+
i // You SHOULD use index as key. See top for info.
35
+
) => (
31
36
<div key={i}>
32
37
<FormInput form={form} name={i} />
33
38
<button type="button" onClick={() =>remove(i)}>
@@ -55,16 +60,15 @@ Remember: this is all type checked!
55
60
functionShoppingListForm() {
56
61
constform=useForm({
57
62
title:"My shopping list",
58
-
items: [{ name:"Coffee", amount:1 }]
63
+
items: [{ name:"Coffee", amount:1 }],
59
64
});
60
65
61
66
return (
62
67
<form
63
68
onSubmit={(ev) => {
64
69
ev.preventDefault();
65
70
console.log("submit", form.values);
66
-
}}
67
-
>
71
+
}}>
68
72
<h2>Title</h2>
69
73
<FormInput form={form} type="text" name="title"/>
70
74
@@ -108,6 +112,7 @@ function ShoppingListForm() {
108
112
```
109
113
110
114
✔️ **Dynamic object array field with seperate component for each child form and using `useArrayForm`**
@@ -173,19 +180,19 @@ function ShoppingListItemForm(props: { parent: FormState<ShoppingListItem[]>; in
173
180
A fixed array always has the same size, [`ChildForm`](https://github.com/CodeStix/typed-react-form/wiki/ChildForm) is used, and the index into the array is given using the name prop.
174
181
175
182
✔️ **Fixed array field containing strings**
183
+
176
184
```jsx
177
185
functionAnswerForm() {
178
186
constform=useForm({
179
187
// Always 3 items in array
180
-
answers: ["", "", ""]
188
+
answers: ["", "", ""],
181
189
});
182
190
return (
183
191
<form
184
192
onSubmit={(ev) => {
185
193
ev.preventDefault();
186
194
console.log("submit", form.values);
187
-
}}
188
-
>
195
+
}}>
189
196
<ChildForm
190
197
parent={form}
191
198
name="answers"
@@ -208,27 +215,27 @@ function AnswerForm() {
208
215
```
209
216
210
217
✔️ **Fixed array field containing objects**
218
+
211
219
```jsx
212
220
functionSettingsForm() {
213
221
constform=useForm({
214
222
settings: [
215
223
{ name:"Enable email", value:true },
216
-
{ name:"Enable notifications", value:false }
217
-
]
224
+
{ name:"Enable notifications", value:false },
225
+
],
218
226
});
219
227
return (
220
228
<form
221
229
onSubmit={(ev) => {
222
230
ev.preventDefault();
223
231
console.log("submit", form.values);
224
-
}}
225
-
>
232
+
}}>
226
233
<ChildForm // First child form is array
227
234
parent={form}
228
235
name="settings"
229
236
render={(form) =>
230
237
form.values.map((_, i) => (
231
-
<ChildForm // Second child form is object in array
238
+
<ChildForm // Second child form is object in array
0 commit comments