简体   繁体   English

.map中的条件显示/隐藏“反应”单选按钮

[英]Conditional show/hide for react radio button in .map

I have a radio button that when true/checked it shows that option as select. 我有一个单选按钮,当该按钮为true /选中时,将其显示为select。 The problem I have is that all options are selected as true within my radio group mapping. 我的问题是在我的无线电组映射中所有选项都选择为true。 Here is an codesandbox example - https://codesandbox.io/s/73k0onx32x 下面是一个例子codesandbox - https://codesandbox.io/s/73k0onx32x

  <RadioGroup
    aria-label="matches"
    name="matches"
    value={String(this.state.value)}
    onChange={this.handlePersonToggle}
  >
    {data.map(person => (
      <FormControlLabel
        onClick={e => this.setState({ checked: !this.state.checked })}
        checked={this.state.checked}
        key={String(person.Id)}
        value={String(person.Id)}
        control={<Radio color="primary" />}
        label={
          <div>
            {this.state.checked === true && <div>if true show</div>}
          </div>
        }
      />
    ))}
  </RadioGroup>

Any help would be appreciated here. 任何帮助将不胜感激。

Your component state contains a single value, checked, which can be set to true or false. 您的组件状态包含一个已选中的值,可以将其设置为true或false。 You will need something like a value containing the id of the selected item. 您将需要类似包含所选项目ID的值之类的东西。

Try something like this 试试这个

<RadioGroup
  aria-label="matches"
  name="matches"
  value={String(this.state.value)}
  onChange={this.handlePersonToggle}
>
  {data.map(person => (
    <FormControlLabel
      onClick={e => this.setState({ selectedItem: person.Id })}
      checked={this.state.selectedItem === person.Id}
      key={String(person.Id)}
      value={String(person.Id)}
      control={<Radio color="primary" />}
      label={
        <div>
          {this.state.selectedItem === person.Id && <div>if true show</div>}
        </div>
      }
    />
  ))}
</RadioGroup>

This is the working demo: https://codesandbox.io/s/l261qp002m 这是工作示例: https : //codesandbox.io/s/l261qp002m

在此处输入图片说明


Final code 最终代码

class RadioButtonsGroup extends React.Component {
  state = {
    value: 1
  };

  handlePersonToggle = event => {
    // console.log(typeof event.target.value)   //string
    this.setState({ value: event.target.value });
  };

  render() {
    const { classes } = this.props;
    return (
      <RadioGroup
        aria-label="matches"
        name="matches"
        value={String(this.state.value)}
      >
        {data.map(person => {
          return (
            <FormControlLabel
              onClick={e => this.handlePersonToggle(e)}
              key={String(person.Id)}
              value={String(person.Id)}
              control={<Radio color="primary" />}
              label={
                <div>
                  {this.state.value == person.Id ? <div>if true show</div> : ""}
                </div>
              }
            />
          );
        })}
      </RadioGroup>
    );
  }
}

There are several things that we need to fix. 我们需要修复一些问题。

Firstly, we don't need the checked attribute on the FormControlLabel component and also don't need to manage the state checked, just the value attribute of RadioGroup and FormControlLabel components are enough, as long as they match, it will know which option is selected. 首先,我们不需要FormControlLabel组件上的checked属性,也不需要管理已检查的状态,只需RadioGroupFormControlLabel组件的value属性FormControlLabel足够了,只要它们匹配,它将知道哪个选项是已选择。

Secondly, since we're passing the String(this.state.value) , hence, our event.target.value become a string rather than an integer . 其次,由于我们要传递String(this.state.value) ,因此,我们的event.target.value成为string而不是integer Therefore, we can't use the strict comparison === , we use the == instead. 因此,我们不能使用严格的比较=== ,而是使用==

Finally, we write the event handler for the onClick event on the FormControlLabel component. 最后,我们在FormControlLabel组件上为onClick事件编写事件处理程序。

You'r onClick handling is wrong. 您的onClick处理错误。 I have fixed the problem as below also 我也解决了以下问题

  <RadioGroup
    aria-label="matches"
    name="matches"
    value={String(this.state.value)}
    onChange={this.handlePersonToggle}
  >
    {data.map(person => (
      <FormControlLabel
        onClick={e => this.setState({ checked: person.Id })}
        checked={this.state.checked}
        key={String(person.Id)}
        value={String(person.Id)}
        control={<Radio color="primary" />}
        label={
          <div>{this.state.checked === person.Id && <div>true</div>}</div>
        }
      />
    ))}
  </RadioGroup>

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

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