Skip to content

Commit 4832cfb

Browse files
author
August Toman-Yih
committed
fix(radio-group): fixes bug where onChange callback would be called twice
once with the change event, once with the radio button value [fixes #100254792]
1 parent 258fc99 commit 4832cfb

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

spec/pivotal-ui-react/radio-group/radio-group_spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ var Radio = require('../../../src/pivotal-ui-react/radio/radio').Radio;
44

55
describe('RadioGroup', function() {
66
describe('basic RadioGroup', function() {
7-
var changeSpy;
7+
var changeSpy, callValue;
88
beforeEach(function() {
99

10-
changeSpy = jasmine.createSpy('change');
10+
changeSpy = jasmine.createSpy('change').and.callFake(function(e) {
11+
callValue = e.target.value;
12+
});
1113
React.render(
1214
<RadioGroup name="bananas" onChange={changeSpy}>
1315
<Radio value="1">One!!!</Radio>
@@ -35,11 +37,12 @@ describe('RadioGroup', function() {
3537

3638
describe('when the radio button is changed', function() {
3739
beforeEach(function() {
38-
$('.radio-group :radio').simulate('change').simulate('click');
40+
$('.radio-group :radio:eq(0)').simulate('click');
3941
});
4042

4143
it('calls the change callback', function() {
42-
expect(changeSpy).toHaveBeenCalledWith('1');
44+
expect(callValue).toEqual('1');
45+
expect(changeSpy.calls.count()).toEqual(1);
4346
});
4447
});
4548
});

src/pivotal-ui-react/radio-group/radio-group.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@ var RadioGroup = React.createClass({
3636
onChange: React.PropTypes.func
3737
},
3838

39-
getDefaultProps: function() {
40-
return {onChange: () => null};
41-
},
39+
render: function() {
40+
var {name, children, onChange, ...others} = this.props;
4241

43-
onChange: function(e) {
44-
this.props.onChange(e.target.value);
45-
},
42+
children = React.Children.map(children,
43+
(child) => cloneWithProps(child, {name, onChange: onChange})
44+
);
4645

47-
render: function() {
48-
var {name, children, ...others} = this.props;
49-
children = React.Children.map(children, (child) => cloneWithProps(child, {name, onChange: this.onChange}));
5046
var props = mergeProps(others, {className: 'radio-group'});
5147

5248

@@ -110,8 +106,8 @@ Similar to the `name` property, the `onChange` handlers is passed down to all ch
110106
var MyComponent = React.createClass({
111107
getInitialState: function() { return {selection: null}; },
112108
113-
change: function(value) {
114-
this.setState({selection: value});
109+
change: function(e) {
110+
this.setState({selection: e.target.value});
115111
},
116112
117113
render: function() {
@@ -129,15 +125,19 @@ var MyComponent = React.createClass({
129125
</RadioGroup>
130126
</Col>
131127
</div>
132-
{this.state.selection === 'special' && (<div className="form-group">
133-
<Col md={3}>
134-
<label>Stuff that appears</label>
135-
</Col>
136-
<Col md={21}>
137-
<label htmlFor="exampleInputEmail1">Email address</label>
138-
<input type="email" className="form-control" id="exampleInputEmail1" placeholder="Enter email" />
139-
</Col>
140-
</div>)}
128+
{this.state.selection === 'special' && (
129+
130+
<div className="form-group">
131+
<Col md={3}>
132+
<label>Stuff that appears</label>
133+
</Col>
134+
<Col md={21}>
135+
<label htmlFor="exampleInputEmail1">Email address</label>
136+
<input type="email" className="form-control" id="exampleInputEmail1" placeholder="Enter email" />
137+
</Col>
138+
</div>
139+
140+
)}
141141
</form>
142142
);
143143
}

0 commit comments

Comments
 (0)