2020-08-21 12:58:14 +08:00
|
|
|
---
|
|
|
|
order: 3.1
|
|
|
|
title:
|
|
|
|
zh-CN: 必选样式
|
|
|
|
en-US: Required style
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
通过 `requiredMark` 切换必选与可选样式。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Switch required or optional style with `requiredMark`.
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Form, Input, Button, Radio } from 'antd';
|
2020-09-17 17:11:45 +08:00
|
|
|
import { InfoCircleOutlined } from '@ant-design/icons';
|
2020-08-21 12:58:14 +08:00
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
type RequiredMark = boolean | 'optional';
|
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
const FormLayoutDemo = () => {
|
|
|
|
const [form] = Form.useForm();
|
2020-12-17 15:09:18 +08:00
|
|
|
const [requiredMark, setRequiredMarkType] = useState<RequiredMark>('optional');
|
2020-08-21 12:58:14 +08:00
|
|
|
|
2021-02-23 10:45:11 +08:00
|
|
|
const onRequiredTypeChange = ({ requiredMarkValue }: { requiredMarkValue: RequiredMark }) => {
|
|
|
|
setRequiredMarkType(requiredMarkValue);
|
2020-08-21 12:58:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
layout="vertical"
|
2021-03-28 16:21:57 +08:00
|
|
|
initialValues={{ requiredMarkValue: requiredMark }}
|
2020-08-21 12:58:14 +08:00
|
|
|
onValuesChange={onRequiredTypeChange}
|
|
|
|
requiredMark={requiredMark}
|
|
|
|
>
|
2021-03-28 16:21:57 +08:00
|
|
|
<Form.Item label="Required Mark" name="requiredMarkValue">
|
2020-08-21 12:58:14 +08:00
|
|
|
<Radio.Group>
|
|
|
|
<Radio.Button value="optional">Optional</Radio.Button>
|
2021-02-23 10:45:11 +08:00
|
|
|
<Radio.Button value>Required</Radio.Button>
|
2020-08-21 12:58:14 +08:00
|
|
|
<Radio.Button value={false}>Hidden</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
2020-09-17 17:11:45 +08:00
|
|
|
<Form.Item label="Field A" required tooltip="This is a required field">
|
2020-08-21 12:58:14 +08:00
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
2020-09-17 17:11:45 +08:00
|
|
|
<Form.Item
|
|
|
|
label="Field B"
|
|
|
|
tooltip={{ title: 'Tooltip with customize icon', icon: <InfoCircleOutlined /> }}
|
|
|
|
>
|
2020-08-21 12:58:14 +08:00
|
|
|
<Input placeholder="input placeholder" />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
<Button type="primary">Submit</Button>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReactDOM.render(<FormLayoutDemo />, mountNode);
|
|
|
|
```
|