ant-design/components/radio/demo/radiogroup-options.md

82 lines
2.1 KiB
Markdown
Raw Normal View History

2017-03-28 11:56:38 +08:00
---
order: 2
title:
zh-CN: Radio.Group 组合 - 配置方式
en-US: Radio.Group group - optional
2017-03-28 11:56:38 +08:00
---
## zh-CN
通过配置 `options` 参数来渲染单选框。也可通过 `optionType` 参数来设置 Radio 类型。
2017-04-05 11:20:37 +08:00
2017-03-28 11:56:38 +08:00
## en-US
Render radios by configuring `options`. Radio type can also be set through the `optionType` parameter.
2017-04-05 11:20:37 +08:00
```tsx
import type { RadioChangeEvent } from 'antd';
2017-03-28 11:56:38 +08:00
import { Radio } from 'antd';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-06-27 15:55:04 +08:00
2017-03-28 11:56:38 +08:00
const plainOptions = ['Apple', 'Pear', 'Orange'];
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear' },
{ label: 'Orange', value: 'Orange' },
];
const optionsWithDisabled = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Pear', value: 'Pear' },
{ label: 'Orange', value: 'Orange', disabled: true },
2017-03-28 11:56:38 +08:00
];
const App: React.FC = () => {
const [value1, setValue1] = useState('Apple');
const [value2, setValue2] = useState('Apple');
const [value3, setValue3] = useState('Apple');
const [value4, setValue4] = useState('Apple');
2018-06-27 15:55:04 +08:00
const onChange1 = ({ target: { value } }: RadioChangeEvent) => {
console.log('radio1 checked', value);
setValue1(value);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const onChange2 = ({ target: { value } }: RadioChangeEvent) => {
console.log('radio2 checked', value);
setValue2(value);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const onChange3 = ({ target: { value } }: RadioChangeEvent) => {
console.log('radio3 checked', value);
setValue3(value);
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
const onChange4 = ({ target: { value } }: RadioChangeEvent) => {
console.log('radio4 checked', value);
setValue4(value);
};
return (
<>
<Radio.Group options={plainOptions} onChange={onChange1} value={value1} />
<br />
<Radio.Group options={optionsWithDisabled} onChange={onChange2} value={value2} />
<br />
<br />
<Radio.Group options={options} onChange={onChange3} value={value3} optionType="button" />
<br />
<br />
<Radio.Group
options={optionsWithDisabled}
onChange={onChange4}
value={value4}
optionType="button"
buttonStyle="solid"
/>
</>
);
};
2017-03-28 11:56:38 +08:00
export default App;
2017-04-05 11:20:37 +08:00
```