Skip to content

Commit

Permalink
[RadioGroup] Support defaultValue in uncontrolled mode (#14092)
Browse files Browse the repository at this point in the history
* Respect defaultValue for uncontrolled RadioGroup

* Remove props from RadioGroup super() and split uncontrolled/defaultValue tests

* Change RadioGroup isControlled check to compare with null
  • Loading branch information
Slessi authored and oliviertassinari committed Jan 6, 2019
1 parent 2a97344 commit f2e1869
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/material-ui/src/RadioGroup/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +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;

if (!this.isControlled) {
this.state = {
value: props.defaultValue,
};
}
}

focus = () => {
Expand Down
29 changes: 29 additions & 0 deletions packages/material-ui/src/RadioGroup/RadioGroup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ describe('<RadioGroup />', () => {
);
});

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

assert.strictEqual(
wrapper
.children()
.first()
.props().checked,
true,
);

const radio = wrapper.children().last();
const event = { target: { value: 'one' } };
radio.simulate('change', event, true);

assert.strictEqual(
wrapper
.children()
.last()
.props().checked,
true,
);
});

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

Expand Down

0 comments on commit f2e1869

Please sign in to comment.