Skip to content

Commit

Permalink
added value check (#56)
Browse files Browse the repository at this point in the history
* added value check

should not trigger change event for the value equal to the previous one,
facebook/react#9893

* changelog notes
  • Loading branch information
mightyaleksey authored Jun 8, 2017
1 parent 001f54c commit 5a43916
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# change log


## 0.9.2

- Added value check for `input[type=radio]`. For now it will filter repetitive **change** events for the same value.


## 0.9.1

- Returned scroll back to **Select**'s popup.
Expand Down
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 5a43916

Please sign in to comment.