2019-07-03 20:14:39 +08:00
|
|
|
---
|
|
|
|
order: 3
|
|
|
|
title:
|
|
|
|
zh-CN: 表单布局
|
|
|
|
en-US: Form Layout
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
表单有三种布局。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
There are three layout for form: `horizontal`, `vertical`, `inline`.
|
|
|
|
|
|
|
|
```tsx
|
2020-01-22 12:11:49 +08:00
|
|
|
import React, { useState } from 'react';
|
2019-07-03 20:14:39 +08:00
|
|
|
import { Form, Input, Button, Radio } from 'antd';
|
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
type LayoutType = Parameters<typeof Form>[0]['layout'];
|
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const FormLayoutDemo = () => {
|
2019-12-30 14:53:47 +08:00
|
|
|
const [form] = Form.useForm();
|
2020-12-17 15:09:18 +08:00
|
|
|
const [formLayout, setFormLayout] = useState<LayoutType>('horizontal');
|
2019-07-03 20:14:39 +08:00
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
const onFormLayoutChange = ({ layout }: { layout: LayoutType }) => {
|
2019-12-30 14:53:47 +08:00
|
|
|
setFormLayout(layout);
|
2019-07-03 20:14:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const formItemLayout =
|
|
|
|
formLayout === 'horizontal'
|
|
|
|
? {
|
|
|
|
labelCol: { span: 4 },
|
|
|
|
wrapperCol: { span: 14 },
|
|
|
|
}
|
|
|
|
: null;
|
2019-12-30 14:53:47 +08:00
|
|
|
|
2019-07-03 20:14:39 +08:00
|
|
|
const buttonItemLayout =
|
|
|
|
formLayout === 'horizontal'
|
|
|
|
? {
|
|
|
|
wrapperCol: { span: 14, offset: 4 },
|
|
|
|
}
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return (
|
2020-07-07 22:06:00 +08:00
|
|
|
<>
|
2019-12-30 14:53:47 +08:00
|
|
|
<Form
|
|
|
|
{...formItemLayout}
|
|
|
|
layout={formLayout}
|
|
|
|
form={form}
|
|
|
|
initialValues={{ layout: formLayout }}
|
|
|
|
onValuesChange={onFormLayoutChange}
|
|
|
|
>
|
|
|
|
<Form.Item label="Form Layout" name="layout">
|
|
|
|
<Radio.Group value={formLayout}>
|
2019-07-03 20:14:39 +08:00
|
|
|
<Radio.Button value="horizontal">Horizontal</Radio.Button>
|
|
|
|
<Radio.Button value="vertical">Vertical</Radio.Button>
|
|
|
|
<Radio.Button value="inline">Inline</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
2019-12-30 14:53:47 +08:00
|
|
|
<Form.Item label="Field A">
|
2019-07-03 20:14:39 +08:00
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
2019-12-30 14:53:47 +08:00
|
|
|
<Form.Item label="Field B">
|
2019-07-03 20:14:39 +08:00
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item {...buttonItemLayout}>
|
|
|
|
<Button type="primary">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
2020-07-07 22:06:00 +08:00
|
|
|
</>
|
2019-07-03 20:14:39 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReactDOM.render(<FormLayoutDemo />, mountNode);
|
|
|
|
```
|