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] Support uncontrolled mode #13929

Merged
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
24 changes: 20 additions & 4 deletions packages/material-ui/src/RadioGroup/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { createChainedFunction, find } from '../utils/helpers';
class RadioGroup extends React.Component {
radios = [];

state = {
value: null,
};

constructor(props) {
super();
this.isControlled = props.value != null;
}

focus = () => {
if (!this.radios || !this.radios.length) {
return;
Expand All @@ -30,15 +39,22 @@ class RadioGroup extends React.Component {
focusRadios[0].focus();
};

handleRadioChange = (event, checked) => {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
if (checked && this.props.onChange) {
handleChange = event => {
if (!this.isControlled) {
this.setState({
value: event.target.value,
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
});
}

if (this.props.onChange) {
this.props.onChange(event, event.target.value);
}
};

render() {
const { children, name, value, onChange, ...other } = this.props;
const { children, name, value: valueProp, onChange, ...other } = this.props;

const value = this.isControlled ? valueProp : this.state.value;
this.radios = [];

return (
Expand All @@ -64,7 +80,7 @@ class RadioGroup extends React.Component {
}
},
checked: value === child.props.value,
onChange: createChainedFunction(child.props.onChange, this.handleRadioChange),
onChange: createChainedFunction(child.props.onChange, this.handleChange),
});
})}
</FormGroup>
Expand Down
33 changes: 19 additions & 14 deletions packages/material-ui/src/RadioGroup/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ describe('<RadioGroup />', () => {
assert.strictEqual(handleKeyDown.args[0][0], event);
});

it('should support uncontrolled mode', () => {
const wrapper = shallow(
<RadioGroup name="group">
<Radio value="one" />
</RadioGroup>,
);

const radio = wrapper.children().first();
const event = { target: { value: 'one' } };
radio.simulate('change', event, true);
assert.strictEqual(
wrapper
.children()
.first()
.props().checked,
true,
);
});

describe('imperative focus()', () => {
let wrapper;

Expand Down Expand Up @@ -134,20 +153,6 @@ describe('<RadioGroup />', () => {
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();
internalRadio.simulate('change', { target: { value: 'woofRadioGroup' } }, false);
assert.strictEqual(handleChange.callCount, 0);
});

it('should chain the onChange property', () => {
const handleChange1 = spy();
const handleChange2 = spy();
Expand Down