Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RadioGroup] Fix onChange chaining #11793

Merged
merged 1 commit into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/material-ui/src/RadioGroup/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import FormGroup from '../FormGroup';
import { find } from '../utils/helpers';
import { createChainedFunction, find } from '../utils/helpers';

class RadioGroup extends React.Component {
radios = [];
Expand Down Expand Up @@ -56,7 +56,7 @@ class RadioGroup extends React.Component {
}
},
checked: value === child.props.value,
onChange: this.handleRadioChange,
onChange: createChainedFunction(child.props.onChange, this.handleRadioChange),
});
})}
</FormGroup>
Expand Down
48 changes: 32 additions & 16 deletions packages/material-ui/src/RadioGroup/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,51 @@ describe('<RadioGroup />', () => {
);
});

describe('children radios fire change event', () => {
let wrapper;

beforeEach(() => {
wrapper = shallow(
<RadioGroup value="">
describe('prop: onChange', () => {
it('should fire onChange', () => {
const handleChange = spy();
const wrapper = shallow(
<RadioGroup value="" onChange={handleChange}>
<Radio />
<Radio />
</RadioGroup>,
);
});

it('should fire onChange', () => {
const internalRadio = wrapper.children().first();
const event = { target: { value: 'woofRadioGroup' } };
const onChangeSpy = spy();
wrapper.setProps({ onChange: onChangeSpy });

internalRadio.simulate('change', event, true);
assert.strictEqual(onChangeSpy.callCount, 1);
assert.strictEqual(onChangeSpy.calledWith(event), true);
assert.strictEqual(handleChange.callCount, 1);
assert.strictEqual(handleChange.calledWith(event), true);
});

it('should not fire onChange if not checked', () => {
const handleChange = spy();
const wrapper = shallow(
<RadioGroup value="" onChange={handleChange}>
<Radio />
<Radio />
</RadioGroup>,
);

const internalRadio = wrapper.children().first();
const onChangeSpy = spy();
wrapper.setProps({ onChange: onChangeSpy });
internalRadio.simulate('change', { target: { value: 'woofRadioGroup' } }, false);
assert.strictEqual(onChangeSpy.callCount, 0);
assert.strictEqual(handleChange.callCount, 0);
});

it('should chain the onChange property', () => {
const handleChange1 = spy();
const handleChange2 = spy();
const wrapper = shallow(
<RadioGroup value="" onChange={handleChange1}>
<Radio onChange={handleChange2} />
<Radio />
</RadioGroup>,
);

const internalRadio = wrapper.children().first();
internalRadio.simulate('change', { target: { value: 'woofRadioGroup' } }, true);
assert.strictEqual(handleChange1.callCount, 1);
assert.strictEqual(handleChange2.callCount, 1);
});
});

Expand Down
6 changes: 5 additions & 1 deletion packages/material-ui/src/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ export function find(arr: Array<any>, pred: any) {
* @returns {function|null}
*/
export function createChainedFunction(...funcs: Array<any>) {
return funcs.filter(func => func != null).reduce(
return funcs.reduce(
(acc, func) => {
if (func == null) {
return acc;
}

warning(
typeof func === 'function',
'Material-UI: invalid Argument Type, must only provide functions, undefined, or null.',
Expand Down