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 (
|
2021-11-26 12:18:21 +08:00
|
|
|
<Form
|
|
|
|
{...formItemLayout}
|
|
|
|
layout={formLayout}
|
|
|
|
form={form}
|
|
|
|
initialValues={{ layout: formLayout }}
|
|
|
|
onValuesChange={onFormLayoutChange}
|
|
|
|
>
|
|
|
|
<Form.Item label="Form Layout" name="layout">
|
|
|
|
<Radio.Group value={formLayout}>
|
|
|
|
<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>
|
|
|
|
<Form.Item label="Field A">
|
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Field B">
|
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item {...buttonItemLayout}>
|
|
|
|
<Button type="primary">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
2019-07-03 20:14:39 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-03 23:27:45 +08:00
|
|
|
export default () => <FormLayoutDemo />;
|
2019-07-03 20:14:39 +08:00
|
|
|
```
|