Skip to content

Commit

Permalink
added value check
Browse files Browse the repository at this point in the history
should not trigger change event for the value equal to the previous one,
facebook/react#9893
  • Loading branch information
mightyaleksey committed Jun 8, 2017
1 parent 001f54c commit 195f894
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions component/Radio.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class Radio extends Component {

_onChange = (e, {value}) => {
const {name} = this.props;
const isSameValue = this.state.value === value;

if (!this._controlled) this.setState({value});
this.props.onChange(e, {name, value});
// workaround for https://github.com/facebook/react/issues/9893
if (!isSameValue && !this._controlled) this.setState({value});
if (!isSameValue) this.props.onChange(e, {name, value});
}

computeOptions(options) {
Expand Down
18 changes: 18 additions & 0 deletions test/component/Radio.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,24 @@ describe('onChange', () => {
value: 'bb',
});
});

it('should not call the handler for the same value twice', () => {
const onChange = jest.fn();
const component = shallow(
<Radio
name='control'
onChange={onChange}
options={[
{label: 'AA', value: 'aa'},
{label: 'BB', value: 'bb'},
{label: 'CC', value: 'cc'},
]}/>
);

component.instance()._onChange(null, {value: 'bb'});
component.instance()._onChange(null, {value: 'bb'});
expect(onChange).toHaveBeenCalledTimes(1);
});
});

describe('onFocus', () => {
Expand Down

0 comments on commit 195f894

Please sign in to comment.