material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[RadioButtonGroup] onChange v.target.value != getSelectedValue()

Open 0x80 opened this issue 11 years ago • 7 comments

I noticed something strange with RadioButtonGroup. If I fetch the value directly from the event emitted by onChange it's not the same as when I get the value from a ref.getSelectedValue()

handleRadio(v){
  debug('radio change:', v.target.value, this.refs.radio.getSelectedValue());
}

Clicking a few time back and forth between two values: none all all none none all all none

and then clicking the same button a second time: none none

So v.target.value is always one click behind in updating its value

Loving the new radio buttons by the way! :)

0x80 avatar Feb 01 '15 22:02 0x80

Just hit myself on the same thing again, this time with a 0.13 approach. If this isn't a bug it's at least confusing ;)

The component below will print different values for _onChange. The 'value' is one selection behind on 'selected'.

export default class Question extends React.Component {

  _onChange(e, selected){
    let value = this._radio.getSelectedValue();
    console.log('selected', value, selected);
  }

  render() {

    const answers = this.props.answers.map((answer, index) => {
      return (<mui.RadioButton
        key={index}
        value={answer}
        label={answer} />);
    });

    return (
      <div className='Question'>
        <h2>{this.props.question}</h2>
        <mui.RadioButtonGroup
          ref={(c) => this._radio = c}
          name="answers"
          className="Question-answers"
          onChange={this._onChange.bind(this)}>
          {answers}
        </mui.RadioButtonGroup>
      </div>
    );
  }
};

0x80 avatar Apr 02 '15 21:04 0x80

@0x80 Sorry for getting back to you so late. This is definitely a bug! I believe it has to with React's setState being an asynchrounous function. One solution would be to wrap a timing event function over the onChange callback prop. However this hasn't been tested yet. Hopefully we can fix this quickly!

mmrtnz avatar Apr 03 '15 14:04 mmrtnz

No problem, I'll keep it in mind. Thanks!

0x80 avatar Apr 04 '15 11:04 0x80

Also seems to affect Dropdown - sometime you get the change, sometimes not.

mbrookes avatar Oct 10 '15 20:10 mbrookes

@mbrookes : Is it still an issue? Since you investigated something similar I need your opinion on this before closing.

tintin1343 avatar Apr 20 '16 02:04 tintin1343

No idea, sorry, but as there have been no other comments in 6 months I think it's safe to close.

mbrookes avatar Apr 21 '16 00:04 mbrookes

This bug still exists in 0.16.7, a simple delay can be used as a temporary workaround :

<RadioButtonGroup name='type' onChange={this.handleRadioChange}> // ...

and :

handleRadioChange (e) {
	e.persist();
	setTimeout(() => {
		this.setState({[e.target.name]: e.target.value}) 
	}, 100)
}

soualid avatar Jan 20 '17 21:01 soualid