Codesandbox link: https://codesandbox.io/s/compassionate-sanderson-1m8nv?file=/src/App.js
I have not been able to find much information on this topic. The following is what I want to achieve:
I want the user to be able to edit some details of a purchase order that already exists in the database, then resubmit the purchase order with a form. The details of the original purchase order should be displayed in the form input fields, and the user can change them directly via those field and then submit.
I am not savvy with web development so please bear with me.
I want the final form object to look like this:
{
po_number:"123abc",
carrier:"Fastway",
items: [{
item_code:"dnh75n",
quantity:"10",
special_requirements:"Add picture of happy dog"
},
{
item_code:"456def",
quantity:"4",
special_requirements:"Do not include lids"
}
]
}
The number of form input fields generated will be based off how many items are in the purchase order. I have created a simple React component below to demonstrate what I am trying to do. Or just checkout out the code sandbox link above. Any help would be appreciated. I can't even find information on how to group ANTd form items to create the array of items in the purchase order. I've seen plenty of dynamic form examples on their website but I want to create the form based on the preexisting items in the purchase order, not add fields with user input.
import { Form, Input, Button } from 'antd';
//This is the order that already exists
const order = {
po_number:"123abc",
carrier:"Fastway",
items: [{
item_code:"dnh75n",
quantity:"10",
special_requirements:"Add picture of happy dog"
},
{
item_code:"456def",
quantity:"4",
special_requirements:"Do not include lids"
}
]
};
const GroupForm = () => {
const onFinish = values => {
console.log(values);
}
//Create form fields based off how many items are in the order
const itemInputs = order.items.map(item => {
return (
<div>
<b>Item{" " + item.item_code}</b>
<Form.Item name={item.item_code + "_quantity"} label="quantity">
<Input defaultValue={item.quantity} style={{width: "500px"}} />
</Form.Item>
<Form.Item name={item.item_code + "_requirements"} label="speacial requirements">
<Input defaultValue={item.special_requirements} style={{width: "500px"}} />
</Form.Item>
</div>
);
});
return(
<div>
<Form onFinish={onFinish}>
<b>{"Order " + order.po_number}</b>
<Form.Item name="carrier" label="carrier">
<Input defaultValue={order.carrier} style={{width: "500px"}} />
</Form.Item>
<b>Order Items</b>
{itemInputs}
<Form.Item>
<Button type="primary" htmlType="submit"> Change Details </Button>
</Form.Item>
</Form>
</div>
);
}
export default GroupForm;