最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Radio Button Group in Material-UI - Stack Overflow

programmeradmin3浏览0评论

I have this project built with react and bootstrap. I wanted to change to material-ui and it worked well until changing the radio button.

Here is the old one i used (worked pretty well).

<label>
    <input type= {that.props.questionType} name={that.props.questionID} value={choice}/>
    {choice}
</label>

Here is the material-ui version.

<RadioButtonGroup >
    <RadioButton
        value={that.props.questionID}
        label={choice}
    />
</RadioButtonGroup>

Here is the map function I use to generate the radio buttons:

var iterator = (
  <RadioButtonGroup selectedValue={that.props.questionID}>
    {that.props.questionChoices.map(choice => <RadioButton value={choice} label={choice} /> )}
  </RadioButtonGroup>
);

I have this project built with react and bootstrap. I wanted to change to material-ui and it worked well until changing the radio button.

Here is the old one i used (worked pretty well).

<label>
    <input type= {that.props.questionType} name={that.props.questionID} value={choice}/>
    {choice}
</label>

Here is the material-ui version.

<RadioButtonGroup >
    <RadioButton
        value={that.props.questionID}
        label={choice}
    />
</RadioButtonGroup>

Here is the map function I use to generate the radio buttons:

var iterator = (
  <RadioButtonGroup selectedValue={that.props.questionID}>
    {that.props.questionChoices.map(choice => <RadioButton value={choice} label={choice} /> )}
  </RadioButtonGroup>
);
Share Improve this question edited Jan 21, 2018 at 19:12 Jules Dupont 7,5877 gold badges42 silver badges41 bronze badges asked Jan 20, 2018 at 20:46 Nizar JailaneNizar Jailane 411 gold badge3 silver badges8 bronze badges 6
  • the old one generates 2 or 3 , because i used JSON files and used map function to display them , the one does too , but it selectes all of them at ounce – Nizar Jailane Commented Jan 20, 2018 at 20:48
  • Can you show us the map function you're using? – Jules Dupont Commented Jan 20, 2018 at 22:39
  • 1 Does it generates list of radio buttons? or is that only one? – Hemadri Dasari Commented Jan 21, 2018 at 3:01
  • it generates a list of radio buttons,, sense i use map function – Nizar Jailane Commented Jan 21, 2018 at 15:38
  • 1 var iterator = ( <RadioButtonGroup selectedValue={that.props.questionID} > {that.props.questionChoices.map(choice => <RadioButton value={choice} label={choice} /> )} </RadioButtonGroup> ); – Nizar Jailane Commented Jan 21, 2018 at 15:39
 |  Show 1 more ment

1 Answer 1

Reset to default 4

When creating a RadioButtonGroup, you MUST assign name property (please refer the material-ui documentation).

Also when using a map function you should add a key property to each of the returned elements.

Following snippet has those issues addressed:

var iterator = ( 
  <RadioButtonGroup 
    name="questionChoices"
    valueSelected={that.props.questionID}
    onChange={that.props.handleChange}
  > 
    {that.props.questionChoices.map(choice => (
       <RadioButton value={choice} label={choice} key={choice}/> 
    )} 
  </RadioButtonGroup> 
)
发布评论

评论列表(0)

  1. 暂无评论