Radio单选框

单选框。

何时使用

  • 用于在多个备选项中选中单个状态。

  • 和 Select 的区别是,Radio 所有选项默认可见,方便用户在比较中选择,因此选项不宜过多。

代码演示

Radio单选框 - 图1

基本

最简单的用法。

  1. import { Radio } from 'antd';
  2. ReactDOM.render(<Radio>Radio</Radio>, mountNode);

Radio单选框 - 图2

单选组合

一组互斥的 Radio 配合使用。

  1. import { Radio } from 'antd';
  2. const App = () => {
  3. const [value, setValue] = React.useState(1);
  4. const onChange = e => {
  5. console.log('radio checked', e.target.value);
  6. setValue(e.target.value);
  7. };
  8. return (
  9. <Radio.Group onChange={onChange} value={value}>
  10. <Radio value={1}>A</Radio>
  11. <Radio value={2}>B</Radio>
  12. <Radio value={3}>C</Radio>
  13. <Radio value={4}>D</Radio>
  14. </Radio.Group>
  15. );
  16. };
  17. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图3

Radio.Group 组合 - 配置方式

通过配置 options 参数来渲染单选框。也可通过 optionType 参数来设置 Radio 类型。

  1. import { Radio } from 'antd';
  2. const plainOptions = ['Apple', 'Pear', 'Orange'];
  3. const options = [
  4. { label: 'Apple', value: 'Apple' },
  5. { label: 'Pear', value: 'Pear' },
  6. { label: 'Orange', value: 'Orange' },
  7. ];
  8. const optionsWithDisabled = [
  9. { label: 'Apple', value: 'Apple' },
  10. { label: 'Pear', value: 'Pear' },
  11. { label: 'Orange', value: 'Orange', disabled: true },
  12. ];
  13. class App extends React.Component {
  14. state = {
  15. value1: 'Apple',
  16. value2: 'Apple',
  17. value3: 'Apple',
  18. value4: 'Apple',
  19. };
  20. onChange1 = e => {
  21. console.log('radio1 checked', e.target.value);
  22. this.setState({
  23. value1: e.target.value,
  24. });
  25. };
  26. onChange2 = e => {
  27. console.log('radio2 checked', e.target.value);
  28. this.setState({
  29. value2: e.target.value,
  30. });
  31. };
  32. onChange3 = e => {
  33. console.log('radio3 checked', e.target.value);
  34. this.setState({
  35. value3: e.target.value,
  36. });
  37. };
  38. onChange4 = e => {
  39. console.log('radio4 checked', e.target.value);
  40. this.setState({
  41. value4: e.target.value,
  42. });
  43. };
  44. render() {
  45. const { value1, value2, value3, value4 } = this.state;
  46. return (
  47. <>
  48. <Radio.Group options={plainOptions} onChange={this.onChange1} value={value1} />
  49. <br />
  50. <Radio.Group options={optionsWithDisabled} onChange={this.onChange2} value={value2} />
  51. <br />
  52. <br />
  53. <Radio.Group
  54. options={options}
  55. onChange={this.onChange3}
  56. value={value3}
  57. optionType="button"
  58. />
  59. <br />
  60. <br />
  61. <Radio.Group
  62. options={optionsWithDisabled}
  63. onChange={this.onChange4}
  64. value={value4}
  65. optionType="button"
  66. buttonStyle="solid"
  67. />
  68. </>
  69. );
  70. }
  71. }
  72. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图4

单选组合 - 配合 name 使用

可以为 Radio.Group 配置 name 参数,为组合内的 input 元素赋予相同的 name 属性,使浏览器把 Radio.Group 下的 Radio 真正看作是一组(例如可以通过方向键始终在同一组内更改选项)。

  1. import { Radio } from 'antd';
  2. const App = () => (
  3. <Radio.Group name="radiogroup" defaultValue={1}>
  4. <Radio value={1}>A</Radio>
  5. <Radio value={2}>B</Radio>
  6. <Radio value={3}>C</Radio>
  7. <Radio value={4}>D</Radio>
  8. </Radio.Group>
  9. );
  10. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图5

填底的按钮样式

实色填底的单选按钮样式。

  1. import { Radio } from 'antd';
  2. ReactDOM.render(
  3. <>
  4. <Radio.Group defaultValue="a" buttonStyle="solid">
  5. <Radio.Button value="a">Hangzhou</Radio.Button>
  6. <Radio.Button value="b">Shanghai</Radio.Button>
  7. <Radio.Button value="c">Beijing</Radio.Button>
  8. <Radio.Button value="d">Chengdu</Radio.Button>
  9. </Radio.Group>
  10. <Radio.Group defaultValue="c" buttonStyle="solid" style={{ marginTop: 16 }}>
  11. <Radio.Button value="a">Hangzhou</Radio.Button>
  12. <Radio.Button value="b" disabled>
  13. Shanghai
  14. </Radio.Button>
  15. <Radio.Button value="c">Beijing</Radio.Button>
  16. <Radio.Button value="d">Chengdu</Radio.Button>
  17. </Radio.Group>
  18. </>,
  19. mountNode,
  20. );

Radio单选框 - 图6

不可用

Radio 不可用。

  1. import { Radio, Button } from 'antd';
  2. class App extends React.Component {
  3. state = {
  4. disabled: true,
  5. };
  6. toggleDisabled = () => {
  7. this.setState({
  8. disabled: !this.state.disabled,
  9. });
  10. };
  11. render() {
  12. return (
  13. <>
  14. <Radio defaultChecked={false} disabled={this.state.disabled}>
  15. Disabled
  16. </Radio>
  17. <Radio defaultChecked disabled={this.state.disabled}>
  18. Disabled
  19. </Radio>
  20. <br />
  21. <Button type="primary" onClick={this.toggleDisabled} style={{ marginTop: 16 }}>
  22. Toggle disabled
  23. </Button>
  24. </>
  25. );
  26. }
  27. }
  28. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图7

Radio.Group 垂直

垂直的 Radio.Group,配合更多输入框选项。

  1. import { Radio, Input, Space } from 'antd';
  2. class App extends React.Component {
  3. state = {
  4. value: 1,
  5. };
  6. onChange = e => {
  7. console.log('radio checked', e.target.value);
  8. this.setState({
  9. value: e.target.value,
  10. });
  11. };
  12. render() {
  13. const { value } = this.state;
  14. return (
  15. <Radio.Group onChange={this.onChange} value={value}>
  16. <Space direction="vertical">
  17. <Radio value={1}>Option A</Radio>
  18. <Radio value={2}>Option B</Radio>
  19. <Radio value={3}>Option C</Radio>
  20. <Radio value={4}>
  21. More...
  22. {value === 4 ? <Input style={{ width: 100, marginLeft: 10 }} /> : null}
  23. </Radio>
  24. </Space>
  25. </Radio.Group>
  26. );
  27. }
  28. }
  29. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图8

按钮样式

按钮样式的单选组合。

  1. import { Radio } from 'antd';
  2. function onChange(e) {
  3. console.log(`radio checked:${e.target.value}`);
  4. }
  5. ReactDOM.render(
  6. <>
  7. <Radio.Group onChange={onChange} defaultValue="a">
  8. <Radio.Button value="a">Hangzhou</Radio.Button>
  9. <Radio.Button value="b">Shanghai</Radio.Button>
  10. <Radio.Button value="c">Beijing</Radio.Button>
  11. <Radio.Button value="d">Chengdu</Radio.Button>
  12. </Radio.Group>
  13. <Radio.Group onChange={onChange} defaultValue="a" style={{ marginTop: 16 }}>
  14. <Radio.Button value="a">Hangzhou</Radio.Button>
  15. <Radio.Button value="b" disabled>
  16. Shanghai
  17. </Radio.Button>
  18. <Radio.Button value="c">Beijing</Radio.Button>
  19. <Radio.Button value="d">Chengdu</Radio.Button>
  20. </Radio.Group>
  21. <Radio.Group disabled onChange={onChange} defaultValue="a" style={{ marginTop: 16 }}>
  22. <Radio.Button value="a">Hangzhou</Radio.Button>
  23. <Radio.Button value="b">Shanghai</Radio.Button>
  24. <Radio.Button value="c">Beijing</Radio.Button>
  25. <Radio.Button value="d">Chengdu</Radio.Button>
  26. </Radio.Group>
  27. </>,
  28. mountNode,
  29. );

Radio单选框 - 图9

大小

大中小三种组合,可以和表单输入框进行对应配合。

  1. import { Radio } from 'antd';
  2. ReactDOM.render(
  3. <>
  4. <Radio.Group defaultValue="a" size="large">
  5. <Radio.Button value="a">Hangzhou</Radio.Button>
  6. <Radio.Button value="b">Shanghai</Radio.Button>
  7. <Radio.Button value="c">Beijing</Radio.Button>
  8. <Radio.Button value="d">Chengdu</Radio.Button>
  9. </Radio.Group>
  10. <Radio.Group defaultValue="a" style={{ marginTop: 16 }}>
  11. <Radio.Button value="a">Hangzhou</Radio.Button>
  12. <Radio.Button value="b">Shanghai</Radio.Button>
  13. <Radio.Button value="c">Beijing</Radio.Button>
  14. <Radio.Button value="d">Chengdu</Radio.Button>
  15. </Radio.Group>
  16. <Radio.Group defaultValue="a" size="small" style={{ marginTop: 16 }}>
  17. <Radio.Button value="a">Hangzhou</Radio.Button>
  18. <Radio.Button value="b">Shanghai</Radio.Button>
  19. <Radio.Button value="c">Beijing</Radio.Button>
  20. <Radio.Button value="d">Chengdu</Radio.Button>
  21. </Radio.Group>
  22. </>,
  23. mountNode,
  24. );

API

Radio/Radio.Button

参数说明类型默认值
autoFocus自动获取焦点booleanfalse
checked指定当前是否选中booleanfalse
defaultChecked初始是否选中booleanfalse
disabled禁用 Radiobooleanfalse
value根据 value 进行比较,判断是否选中any-

RadioGroup

单选框组合,用于包裹一组 Radio

参数说明类型默认值版本
buttonStyleRadioButton 的风格样式,目前有描边和填色两种风格outline | solidoutline
defaultValue默认选中的值any-
disabled禁选所有子单选器booleanfalse
nameRadioGroup 下所有 input[type=”radio”]name 属性string-
options以配置形式设置子元素string[] | Array<{ label: string value: string disabled?: boolean }>-
optionType用于设置 Radio options 类型default | buttondefault4.4.0
size大小,只对按钮样式生效large | middle | small-
value用于设置当前选中的值any-
onChange选项变化时的回调函数function(e:Event)-

方法

Radio

名称描述
blur()移除焦点
focus()获取焦点