Skip to content

Instantly share code, notes, and snippets.

@singh100ful
Created June 11, 2020 22:32
Show Gist options
  • Save singh100ful/ad16fe57b9cd84fe4597aec5346b2dcf to your computer and use it in GitHub Desktop.
Save singh100ful/ad16fe57b9cd84fe4597aec5346b2dcf to your computer and use it in GitHub Desktop.
FieldArray with ArrayHelpers
<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