简体   繁体   English

Material-UI 单选按钮都被选中

[英]Both Material-UI radio button are checked

I have a file which renders RadioGroup, FormControlLabel and FormControl.我有一个呈现 RadioGroup、FormControlLabel 和 FormControl 的文件。 Below is the code for this下面是这个的代码

import React from 'react';
import PropTypes from 'prop-types';
import Radio from '@material-ui/core/Radio';
import RadioGroup from '@material-ui/core/RadioGroup';
import FormControl from '@material-ui/core/FormControl';
import FormLabel from '@material-ui/core/FormLabel';
import FormControlLabel from '@material-ui/core/FormControlLabel';

export const RenderRadioGroup = ({ label, children }) => {
  return (
    <FormControl>
      <FormLabel
        style={{
          margin: '20px 0 20px 0',
          color: '#000000',
          fontSize: '20px',
          fontWeight: 'bold',
        }}
      >
        {label}
      </FormLabel>
      <RadioGroup row>{children}</RadioGroup>
    </FormControl>
  );
};

export const RenderRadioButtonWrapper = props => {
  return <FormControlLabel control={<CustomRadioButton />} {...props} />;
};

export const CustomRadioButton = props => {
  return <Radio {...props} />;
};

RenderRadioGroup.propTypes = {
  children: PropTypes.node.isRequired,
  label: PropTypes.string,
};

RenderRadioButtonWrapper.propTypes = {
  label: PropTypes.string,
};

CustomRadioButton.propTypes = {
  label: PropTypes.string,
};

In the main file, I am calling these components as follows在主文件中,我按如下方式调用这些组件

    <form>
       <Field
           name="typeofintimation"
           label="Type of intimation"
           component={RenderRadioGroup}
        >
                <Field
                  label="Via Call"
                  name="viacall"
                  value="viacall"
                  component={RenderRadioButtonWrapper}
                />
                <Field
                  label="Via Email"
                  name="viaemail"
                  value="viaemail"
                  component={RenderRadioButtonWrapper}
                />
              </Field>
              <Field component={renderTextFieldGroup} label="Caller Details">
                <Field component={renderTextField} label="Caller Name" />
                <Field component={renderTextField} label="Caller Email" />
                <Field component={renderTextField} label="Caller Contact" />
              </Field>
            </form>

I am getting the radio fields but both are checked.我正在接收无线电场,但都已检查。 I cannot select or deselect the radio buttons.我不能 select 或取消选择单选按钮。 What am I missing here?我在这里错过了什么? Is there some props which I need to pass in either the component or the main file?我需要在组件或主文件中传递一些道具吗?

I am also using redux-form here so I need some assitance in that.我在这里也使用 redux-form 所以我需要一些帮助。

You need to explicitly supply a name in the props so that the system knows that the two radio buttons belong to the same group.您需要在道具中明确提供一个名称,以便系统知道这两个单选按钮属于同一组。 The documentation for the API in its list of props says:道具列表中 API 的文档说:

name string The name used to reference the value of the control. name string 用于引用控件值的名称。 If you don't provide this prop, it falls back to a randomly generated name.如果您不提供此道具,它将退回到随机生成的名称。

See https://material-ui.com/api/radio-group/ for the full list.有关完整列表,请参阅https://material-ui.com/api/radio-group/

Radio buttons are grouped by name - so if you click one of the buttons in a group all the others are cleared.单选按钮按名称分组 - 因此如果您单击组中的一个按钮,所有其他按钮都会被清除。 You have the two radio buttons but they have different names so the system thinks they are in different groups and so both can be checked.您有两个单选按钮,但它们的名称不同,因此系统认为它们在不同的组中,因此都可以选中。

If you try replacing:如果您尝试更换:

        <Field
          label="Via Call"
          name="viacall"
          value="viacall"
          component={RenderRadioButtonWrapper}
        />
        <Field
          label="Via Email"
          name="viaemail"
          value="viaemail"
          component={RenderRadioButtonWrapper}
        />

with

            <Field
              label="Via Call"
              name="viamethod"
              value="viacall"
              component={RenderRadioButtonWrapper}
            />
            <Field
              label="Via Email"
              name="viamethod"
              value="viaemail"
              component={RenderRadioButtonWrapper}
            />

you can only select one in 'naked' HTML. However, where you are creating the radio buttons using the API you need to explicitly set name: and make it the same name for both eg name: 'viamethod' otherwise you will get random names and the two radio buttons won't be in the same group.您只能在“裸”HTML 中使用 select 中的一个。但是,在使用 API 创建单选按钮的地方,您需要明确设置名称:并使两者的名称相同,例如名称:“viamethod”,否则您将获得随机名称并且两个单选按钮不会在同一组中。

start with radioValue's state as "".从 radioValue 的 state 作为“”开始。

 <RadioGroup row className={classes.FormGroup}> <FormControlLabel classes={{ label: classes.label }} control={ <Radio name="Yes" checked={this.state.radioValue[key]} onClick={(e) => {this.handleRowClick(e, row, index)}} value="Y" /> } label="Yes" labelPlacement="end" /> <FormControlLabel classes={{ label: classes.label }} control={ <Radio name="No" checked={this.state.radioValue[key]} onClick={(e) => {this.handleRowClick(e, row)}} color="secondary" value="N" /> } label="No" labelPlacement="end" /> </RadioGroup>

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

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