Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  37] [ 6]  / answers: 1 / hits: 5169  / 4 Years ago, sat, may 16, 2020, 12:00:00

This is probably an easy one for someone. In AntD's forms documentation and demo here, their demo starts with no fields. You have to click the + Add Field button for the dynamic form to add and show the first field as shown here..



enter



If I want it to start out by showing the first field without having to first click the + Add Field button, how is that done? Seems like I'd need to somehow call the add function upon render? Anyways I'm looking for it to show the first passenger when it initially loads the form such as in this screenshot after i've pressed the Add Field button..



enter



Put another way, I want there always to be a minimum of 1 passenger.



Their demo's code can be viewed in the link above that takes you right to their documentation but here it is as well:



import { Form, Input, Button } from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';

const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 4 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 20 },
},
};
const formItemLayoutWithOutLabel = {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 20, offset: 4 },
},
};

const DynamicFieldSet = () => {
const onFinish = values => {
console.log('Received values of form:', values);
};

return (
<Form name=dynamic_form_item {...formItemLayoutWithOutLabel} onFinish={onFinish}>
<Form.List name=names>
{(fields, { add, remove }) => {
return (
<div>
{fields.map((field, index) => (
<Form.Item
{...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
label={index === 0 ? 'Passengers' : ''}
required={false}
key={field.key}
>
<Form.Item
{...field}
validateTrigger={['onChange', 'onBlur']}
rules={[
{
required: true,
whitespace: true,
message: Please input passenger's name or delete this field.,
},
]}
noStyle
>
<Input placeholder=passenger name style={{ width: '60%' }} />
</Form.Item>
{fields.length > 1 ? (
<MinusCircleOutlined
className=dynamic-delete-button
style={{ margin: '0 8px' }}
onClick={() => {
remove(field.name);
}}
/>
) : null}
</Form.Item>
))}
<Form.Item>
<Button
type=dashed
onClick={() => {
add();
}}
style={{ width: '60%' }}
>
<PlusOutlined /> Add field
</Button>
</Form.Item>
</div>
);
}}
</Form.List>

<Form.Item>
<Button type=primary htmlType=submit>
Submit
</Button>
</Form.Item>
</Form>
);
};

ReactDOM.render(<DynamicFieldSet />, mountNode);
.dynamic-delete-button {
cursor: pointer;
position: relative;
top: 4px;
font-size: 24px;
color: #999;
transition: all 0.3s;
}
.dynamic-delete-button:hover {
color: #777;
}
.dynamic-delete-button[disabled] {
cursor: not-allowed;
opacity: 0.5;
}

More From » reactjs

 Answers
2

you can use initialValues prop. take look at this example:



https://codesandbox.io/s/dynamic-form-item-ant-design-demo-qm1d8


[#3801] Wednesday, May 13, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jonrened

Total Points: 627
Total Questions: 114
Total Answers: 99

Location: Zimbabwe
Member since Thu, Jul 21, 2022
2 Years ago
jonrened questions
Mon, Nov 2, 20, 00:00, 4 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Tue, Jan 21, 20, 00:00, 4 Years ago
Thu, Nov 7, 19, 00:00, 5 Years ago
Mon, Aug 26, 19, 00:00, 5 Years ago
;