mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-02 20:19:44 +08:00
FIx RadioButton cause RadioGroup onChange event trigger twice (#14523)
fix #14485
This commit is contained in:
parent
de8ec0152e
commit
18332d8e76
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user