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. 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. return (
  14. <Radio.Group onChange={this.onChange} value={this.state.value}>
  15. <Radio value={1}>A</Radio>
  16. <Radio value={2}>B</Radio>
  17. <Radio value={3}>C</Radio>
  18. <Radio value={4}>D</Radio>
  19. </Radio.Group>
  20. );
  21. }
  22. }
  23. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图3

Radio.Group 组合 - 配置方式

通过配置 options 参数来渲染单选框。

  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: false },
  12. ];
  13. class App extends React.Component {
  14. state = {
  15. value1: 'Apple',
  16. value2: 'Apple',
  17. value3: 'Apple',
  18. };
  19. onChange1 = e => {
  20. console.log('radio1 checked', e.target.value);
  21. this.setState({
  22. value1: e.target.value,
  23. });
  24. };
  25. onChange2 = e => {
  26. console.log('radio2 checked', e.target.value);
  27. this.setState({
  28. value2: e.target.value,
  29. });
  30. };
  31. onChange3 = e => {
  32. console.log('radio3 checked', e.target.value);
  33. this.setState({
  34. value3: e.target.value,
  35. });
  36. };
  37. render() {
  38. return (
  39. <div>
  40. <Radio.Group options={plainOptions} onChange={this.onChange1} value={this.state.value1} />
  41. <Radio.Group options={options} onChange={this.onChange2} value={this.state.value2} />
  42. <Radio.Group
  43. options={optionsWithDisabled}
  44. onChange={this.onChange3}
  45. value={this.state.value3}
  46. />
  47. </div>
  48. );
  49. }
  50. }
  51. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图4

单选组合 - 配合 name 使用

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

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

Radio单选框 - 图5

填底的按钮样式

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

  1. import { Radio } from 'antd';
  2. ReactDOM.render(
  3. <div>
  4. <div>
  5. <Radio.Group defaultValue="a" buttonStyle="solid">
  6. <Radio.Button value="a">Hangzhou</Radio.Button>
  7. <Radio.Button value="b">Shanghai</Radio.Button>
  8. <Radio.Button value="c">Beijing</Radio.Button>
  9. <Radio.Button value="d">Chengdu</Radio.Button>
  10. </Radio.Group>
  11. </div>
  12. <div style={{ marginTop: 16 }}>
  13. <Radio.Group defaultValue="c" buttonStyle="solid">
  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. </div>
  22. </div>,
  23. mountNode,
  24. );

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. <div>
  14. <Radio defaultChecked={false} disabled={this.state.disabled}>
  15. Disabled
  16. </Radio>
  17. <br />
  18. <Radio defaultChecked disabled={this.state.disabled}>
  19. Disabled
  20. </Radio>
  21. <div style={{ marginTop: 20 }}>
  22. <Button type="primary" onClick={this.toggleDisabled}>
  23. Toggle disabled
  24. </Button>
  25. </div>
  26. </div>
  27. );
  28. }
  29. }
  30. ReactDOM.render(<App />, mountNode);

Radio单选框 - 图7

Radio.Group 垂直

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

  1. import { Radio, Input } 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 radioStyle = {
  14. display: 'block',
  15. height: '30px',
  16. lineHeight: '30px',
  17. };
  18. return (
  19. <Radio.Group onChange={this.onChange} value={this.state.value}>
  20. <Radio style={radioStyle} value={1}>
  21. Option A
  22. </Radio>
  23. <Radio style={radioStyle} value={2}>
  24. Option B
  25. </Radio>
  26. <Radio style={radioStyle} value={3}>
  27. Option C
  28. </Radio>
  29. <Radio style={radioStyle} value={4}>
  30. More...
  31. {this.state.value === 4 ? <Input style={{ width: 100, marginLeft: 10 }} /> : null}
  32. </Radio>
  33. </Radio.Group>
  34. );
  35. }
  36. }
  37. 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. <div>
  7. <div>
  8. <Radio.Group onChange={onChange} defaultValue="a">
  9. <Radio.Button value="a">Hangzhou</Radio.Button>
  10. <Radio.Button value="b">Shanghai</Radio.Button>
  11. <Radio.Button value="c">Beijing</Radio.Button>
  12. <Radio.Button value="d">Chengdu</Radio.Button>
  13. </Radio.Group>
  14. </div>
  15. <div style={{ marginTop: 16 }}>
  16. <Radio.Group onChange={onChange} defaultValue="a">
  17. <Radio.Button value="a">Hangzhou</Radio.Button>
  18. <Radio.Button value="b" disabled>
  19. Shanghai
  20. </Radio.Button>
  21. <Radio.Button value="c">Beijing</Radio.Button>
  22. <Radio.Button value="d">Chengdu</Radio.Button>
  23. </Radio.Group>
  24. </div>
  25. <div style={{ marginTop: 16 }}>
  26. <Radio.Group disabled onChange={onChange} defaultValue="a">
  27. <Radio.Button value="a">Hangzhou</Radio.Button>
  28. <Radio.Button value="b">Shanghai</Radio.Button>
  29. <Radio.Button value="c">Beijing</Radio.Button>
  30. <Radio.Button value="d">Chengdu</Radio.Button>
  31. </Radio.Group>
  32. </div>
  33. </div>,
  34. mountNode,
  35. );

Radio单选框 - 图9

大小

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

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

API

Radio

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

RadioGroup

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

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

方法

Radio

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