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
|
2020-09-17 17:11:45 +08:00
|
|
|
import { InfoCircleOutlined } from '@ant-design/icons';
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Button, Form, Input, Radio } from 'antd';
|
|
|
|
import React, { useState } from 'react';
|
2020-08-21 12:58:14 +08:00
|
|
|
|
2020-12-17 15:09:18 +08:00
|
|
|
type RequiredMark = boolean | 'optional';
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
2020-08-21 12:58:14 +08:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2020-08-21 12:58:14 +08:00
|
|
|
```
|