Skip to content

Commit b665679

Browse files
committed
fix: radio emit two change event #1280
1 parent 4218f19 commit b665679

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

components/radio/Group.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ export default {
7171
if (!hasProp(this, 'value')) {
7272
this.stateValue = value;
7373
}
74-
if (value !== lastValue) {
75-
this.$emit('input', value);
76-
this.$emit('change', ev);
77-
}
74+
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
75+
this.$nextTick(() => {
76+
if (value !== lastValue) {
77+
this.$emit('input', value);
78+
this.$emit('change', ev);
79+
}
80+
});
7881
},
7982
},
8083
render() {

components/radio/__tests__/group.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ describe('Radio', () => {
8080
wrapper.vm.$refs.radioGroup.stateValue = 'B';
8181
// wrapper.setData({ value: 'B' })
8282
radios.at(0).trigger('change');
83+
});
84+
await asyncExpect(() => {
8385
expect(onChange.mock.calls.length).toBe(1);
8486
});
8587
await asyncExpect(() => {
@@ -89,6 +91,8 @@ describe('Radio', () => {
8991
// controlled component
9092
wrapper.setProps({ value: 'A' });
9193
radios.at(1).trigger('change');
94+
});
95+
await asyncExpect(() => {
9296
expect(onChange.mock.calls.length).toBe(2);
9397
});
9498
await asyncExpect(() => {
@@ -131,15 +135,17 @@ describe('Radio', () => {
131135
wrapper.vm.$refs.radioGroup.stateValue = 'B';
132136
radios.at(0).trigger('change');
133137
expect(onChange.mock.calls.length).toBe(1);
134-
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
138+
await asyncExpect(() => {
139+
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
140+
});
135141

136142
// controlled component
137143
wrapper.setProps({ value: 'A' });
138144
radios.at(1).trigger('change');
139145
expect(onChange.mock.calls.length).toBe(2);
140146
});
141147

142-
it('Trigger onChange when both of radioButton and radioGroup exists', () => {
148+
it('Trigger onChange when both of radioButton and radioGroup exists', async () => {
143149
const onChange = jest.fn();
144150
const props = {};
145151
const wrapper = mount(
@@ -153,12 +159,15 @@ describe('Radio', () => {
153159
// uncontrolled component
154160
wrapper.vm.$refs.radioGroup.stateValue = 'B';
155161
radios.at(0).trigger('change');
156-
expect(onChange.mock.calls.length).toBe(1);
157-
162+
await asyncExpect(() => {
163+
expect(onChange.mock.calls.length).toBe(1);
164+
});
158165
// controlled component
159166
wrapper.setProps({ value: 'A' });
160167
radios.at(1).trigger('change');
161-
expect(onChange.mock.calls.length).toBe(2);
168+
await asyncExpect(() => {
169+
expect(onChange.mock.calls.length).toBe(2);
170+
});
162171
});
163172

164173
// it('should only trigger once when in group with options', () => {

0 commit comments

Comments
 (0)