FIx RadioButton cause RadioGroup onChange event trigger twice (#14523)

fix #14485
This commit is contained in:
ztplz 2019-01-25 10:32:30 +08:00 committed by zombieJ
parent de8ec0152e
commit 18332d8e76
2 changed files with 24 additions and 1 deletions

View File

@ -2,6 +2,7 @@ import React from 'react';
import { mount, render } from 'enzyme';
import Radio from '../radio';
import RadioGroup from '../group';
import RadioButton from '../radioButton';
describe('Radio', () => {
function createRadioGroup(props) {
@ -99,6 +100,29 @@ describe('Radio', () => {
expect(onChange.mock.calls.length).toBe(2);
});
it('Trigger onChange when both of radioButton and radioGroup exists', () => {
const onChange = jest.fn();
const wrapper = mount(
<RadioGroup onChange={onChange}>
<RadioButton value="A">A</RadioButton>
<RadioButton value="B">B</RadioButton>
<RadioButton value="C">C</RadioButton>
</RadioGroup>,
);
const radios = wrapper.find('input');
// uncontrolled component
wrapper.setState({ value: 'B' });
radios.at(0).simulate('change');
expect(onChange.mock.calls.length).toBe(1);
// controlled component
wrapper.setProps({ value: 'A' });
radios.at(1).simulate('change');
expect(onChange.mock.calls.length).toBe(2);
});
it("won't fire change events when value not changes", () => {
const onChange = jest.fn();

View File

@ -18,7 +18,6 @@ export default class RadioButton extends React.Component<RadioButtonProps, any>
const { prefixCls: customizePrefixCls, ...radioProps }: RadioButtonProps = this.props;
const prefixCls = getPrefixCls('radio-button', customizePrefixCls);
if (this.context.radioGroup) {
radioProps.onChange = this.context.radioGroup.onChange;
radioProps.checked = this.props.value === this.context.radioGroup.value;
radioProps.disabled = this.props.disabled || this.context.radioGroup.disabled;
}