mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-23 23:18:36 +08:00
8d7dd80120
* docs: update select opt * docs: update form deps demo * docs: upload onChange * docs: update form nest Form.List demo
87 lines
2.8 KiB
TypeScript
87 lines
2.8 KiB
TypeScript
import React from 'react';
|
|
import { CloseOutlined } from '@ant-design/icons';
|
|
import { Button, Card, Form, Input, Space, Typography } from 'antd';
|
|
|
|
const App: React.FC = () => {
|
|
const [form] = Form.useForm();
|
|
|
|
return (
|
|
<Form
|
|
labelCol={{ span: 6 }}
|
|
wrapperCol={{ span: 18 }}
|
|
form={form}
|
|
name="dynamic_form_complex"
|
|
style={{ maxWidth: 600 }}
|
|
autoComplete="off"
|
|
initialValues={{ items: [{}] }}
|
|
>
|
|
<Form.List name="items">
|
|
{(fields, { add, remove }) => (
|
|
<div style={{ display: 'flex', rowGap: 16, flexDirection: 'column' }}>
|
|
{fields.map((field) => (
|
|
<Card
|
|
size="small"
|
|
title={`Item ${field.name + 1}`}
|
|
key={field.key}
|
|
extra={
|
|
<CloseOutlined
|
|
onClick={() => {
|
|
remove(field.name);
|
|
}}
|
|
/>
|
|
}
|
|
>
|
|
<Form.Item label="Name" name={[field.name, 'name']}>
|
|
<Input />
|
|
</Form.Item>
|
|
|
|
{/* Nest Form.List */}
|
|
<Form.Item label="List">
|
|
<Form.List name={[field.name, 'list']}>
|
|
{(subFields, subOpt) => (
|
|
<div style={{ display: 'flex', flexDirection: 'column', rowGap: 16 }}>
|
|
{subFields.map((subField) => (
|
|
<Space key={subField.key}>
|
|
<Form.Item noStyle name={[subField.name, 'first']}>
|
|
<Input placeholder="first" />
|
|
</Form.Item>
|
|
<Form.Item noStyle name={[subField.name, 'second']}>
|
|
<Input placeholder="second" />
|
|
</Form.Item>
|
|
<CloseOutlined
|
|
onClick={() => {
|
|
subOpt.remove(subField.name);
|
|
}}
|
|
/>
|
|
</Space>
|
|
))}
|
|
<Button type="dashed" onClick={() => subOpt.add()} block>
|
|
+ Add Sub Item
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</Form.List>
|
|
</Form.Item>
|
|
</Card>
|
|
))}
|
|
|
|
<Button type="dashed" onClick={() => add()} block>
|
|
+ Add Item
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</Form.List>
|
|
|
|
<Form.Item noStyle shouldUpdate>
|
|
{() => (
|
|
<Typography>
|
|
<pre>{JSON.stringify(form.getFieldsValue(), null, 2)}</pre>
|
|
</Typography>
|
|
)}
|
|
</Form.Item>
|
|
</Form>
|
|
);
|
|
};
|
|
|
|
export default App;
|