Created
June 11, 2020 22:32
-
-
Save singh100ful/ad16fe57b9cd84fe4597aec5346b2dcf to your computer and use it in GitHub Desktop.
FieldArray with ArrayHelpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Form style={{ padding: 20 }}> | |
<FieldArray | |
name="diet" | |
render={(arrayHelpers) => ( | |
<div> | |
<Button | |
onClick={() => { | |
arrayHelpers.push({ | |
food_name: "", | |
quantity: "", | |
}); | |
}} | |
> | |
Add Food | |
</Button> | |
{formProps.values.diet.map((diet, index) => ( | |
<Row style={{ paddingBottom: 10 }} key={index}> | |
<Col> | |
<Label>Food</Label> | |
<Field | |
component={CustomInput} | |
type="text" | |
name={`diet.${index}.food_name`} | |
/> | |
</Col> | |
<Col> | |
<Label>Quantity</Label> | |
<Field | |
component={CustomInput} | |
type="text" | |
name={`diet.${index}.quantity`} | |
/> | |
</Col> | |
<Col style={{ paddingTop: 30 }}> | |
<Button onClick={() => arrayHelpers.remove(index)}>-</Button> | |
<Button onClick={() => arrayHelpers.insert(index, "")}>+</Button> | |
</Col> | |
<Col style={{ paddingTop: 30 }}> | |
<Button onClick={() => arrayHelpers.move(index, index - 1)}>Up</Button> | |
<Button onClick={() => arrayHelpers.move(index, index + 1)}>Down</Button> | |
</Col> | |
</Row> | |
))} | |
</div> | |
)} | |
/> | |
<Button color="success" type="submit"> | |
Submit | |
</Button> | |
</Form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment