简体   繁体   English

如何在Antd中使用嵌套的单选按钮组?

[英]How to use nested groups of radio buttons with Antd?

It looks like antd does not support nested radio button groups, so I will have to structure the input elements myself. 看来antd不支持嵌套的单选按钮组,因此我将不得不自己构造输入元素。 However, if I use the Radio.Button by itself it doesn't render correctly. 但是,如果我Radio.Button使用Radio.Button ,它将无法正确呈现。

Is there a way to use Radio.Button as a standalone component in a structure other than Radio.Group? 是否可以使用Radio.Button作为Radio.Group以外的结构中的独立组件?

--- edit --- -编辑-

So it is possible to use the Radio.Group directly and nest it. 因此可以直接使用Radio.Group并将其嵌套。 I have a start with the code below, so I'll post it here to for illustration. 我从下面的代码开始,因此将其发布到此处以进行说明。

import React from "react";
import { Radio } from "antd";

export class TestForm extends React.Component {
  public state = {
    level1: null,
    level2: null
  };

  private handleChange = (level: "level1" | "level2", value: any) => {
    console.log("handleChange", level, value);
    this.setState(state => ({ ...state, [level]: value }));
  };

  return (
      <Radio.Group
        onChange={evt => this.handleChange("level1", evt.target.value)}
        value={this.state.level1}
      >
        <Radio style={radioStyle} value={"a"}>
          Option A
        </Radio>
        <Radio style={radioStyle} value={"b"}>
          Option B
        </Radio>
        <Radio style={radioStyle} value={"c"}>
          Option C
        </Radio>
        {this.state.level1 === "c" ? (
          <Radio.Group
            onChange={evt => this.handleChange("level2", evt.target.value)}
            value={this.state.level2}
            defaultValue="c1"
          >
            <Radio style={radioStyle} value={"c1"}>
              Option C1
            </Radio>
            <Radio style={radioStyle} value={"c2"}>
              Option C2
            </Radio>

            <Radio style={radioStyle} value={"c3"}>
              Option C3
            </Radio>
          </Radio.Group>
        ) : null}

        <Radio style={radioStyle} value={"d"}>
          Option D
        </Radio>
      </Radio.Group>
    );
  }
}

Radio.Group and Radio.Button is just an implementation of Button.Group , Button Radio.GroupRadio.Button仅仅是一种实现Button.GroupButton

By saying "Radio.Button as a standalone component" I assume you want to use it as a single grouped radio button. 通过说“ Radio.Button作为独立组件”,我假设您想将其用作单个分组的单选按钮。

In this case, is just a Button component, refer to Button, Button.Group . 在这种情况下,它只是一个Button组件,请参考Button,Button.Group

export default function App() {
  return (
    <FlexBox>
      <Radio.Group>
        <Radio.Button value="a">Hangzhou</Radio.Button>
      </Radio.Group>
      <Button>Hangzhou</Button>
    </FlexBox>
  );
}

在此处输入图片说明

编辑Q-56885195-ButtonGroup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM