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

refactor: Adjust Radio.Group logic that value is undefined should be uncontrolled mode. #22245

Merged
merged 2 commits into from Mar 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion components/radio/__tests__/group.test.js
Expand Up @@ -170,10 +170,35 @@ describe('Radio Group', () => {
});

it('passes prefixCls down to radio', () => {
const options = [{ label: 'Apple', value: 'Apple' }, { label: 'Orange', value: 'Orange', style: { fontSize: 12 } }];
const options = [
{ label: 'Apple', value: 'Apple' },
{ label: 'Orange', value: 'Orange', style: { fontSize: 12 } },
];

const wrapper = render(<RadioGroup prefixCls="my-radio" options={options} />);

expect(wrapper).toMatchSnapshot();
});

describe('value is null or undefined', () => {
it('use `defaultValue` when `value` is undefined', () => {
const options = [{ label: 'Bamboo', value: 'bamboo' }];
const wrapper = mount(
<RadioGroup defaultValue="bamboo" value={undefined} options={options} />,
);

expect(wrapper.state().value).toEqual('bamboo');
});

[undefined, null].forEach(newValue => {
it(`should set value back when value change back to ${newValue}`, () => {
const options = [{ label: 'Bamboo', value: 'bamboo' }];
const wrapper = mount(<RadioGroup value="bamboo" options={options} />);
expect(wrapper.state().value).toEqual('bamboo');

wrapper.setProps({ value: newValue });
expect(wrapper.state().value).toEqual(newValue);
});
});
});
});
30 changes: 16 additions & 14 deletions components/radio/group.tsx
Expand Up @@ -28,35 +28,37 @@ class RadioGroup extends React.PureComponent<RadioGroupProps, RadioGroupState> {
buttonStyle: 'outline' as RadioGroupButtonStyle,
};

static getDerivedStateFromProps(nextProps: RadioGroupProps) {
if ('value' in nextProps) {
return {
value: nextProps.value,
};
}
const checkedValue = getCheckedValue(nextProps.children);
if (checkedValue) {
return {
value: checkedValue.value,
};
static getDerivedStateFromProps(nextProps: RadioGroupProps, prevState: RadioGroupState) {
const newState: Partial<RadioGroupState> = {
prevPropValue: nextProps.value,
};

if (nextProps.value !== undefined || prevState.prevPropValue !== nextProps.value) {
newState.value = nextProps.value;
} else {
const checkedValue = getCheckedValue(nextProps.children);
if (checkedValue) {
newState.value = checkedValue.value;
}
}

return null;
return newState;
}

constructor(props: RadioGroupProps) {
super(props);
let value;
if ('value' in props) {
if (props.value !== undefined) {
value = props.value;
} else if ('defaultValue' in props) {
} else if (props.defaultValue !== undefined) {
value = props.defaultValue;
} else {
const checkedValue = getCheckedValue(props.children);
value = checkedValue && checkedValue.value;
}
this.state = {
value,
prevPropValue: props.value,
};
}

Expand Down
1 change: 1 addition & 0 deletions components/radio/interface.tsx
Expand Up @@ -20,6 +20,7 @@ export interface RadioGroupProps extends AbstractCheckboxGroupProps {

export interface RadioGroupState {
value: any;
prevPropValue: any;
}

export interface RadioGroupContextProps {
Expand Down