Skip to content

Commit b5cd32a

Browse files
committed
fix: Radio.Group triggers multiple change callback issues #1280
1 parent a0c8874 commit b5cd32a

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

components/radio/Group.jsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
},
3131
data() {
3232
const { value, defaultValue } = this;
33+
this.updatingValue = false;
3334
return {
3435
stateValue: value === undefined ? defaultValue : value,
3536
};
@@ -61,6 +62,7 @@ export default {
6162
},
6263
watch: {
6364
value(val) {
65+
this.updatingValue = false;
6466
this.stateValue = val;
6567
},
6668
},
@@ -72,11 +74,13 @@ export default {
7274
this.stateValue = value;
7375
}
7476
// nextTick for https://github.com/vueComponent/ant-design-vue/issues/1280
77+
if (!this.updatingValue && value !== lastValue) {
78+
this.updatingValue = true;
79+
this.$emit('input', value);
80+
this.$emit('change', ev);
81+
}
7582
this.$nextTick(() => {
76-
if (value !== lastValue) {
77-
this.$emit('input', value);
78-
this.$emit('change', ev);
79-
}
83+
this.updatingValue = false;
8084
});
8185
},
8286
},

components/radio/__tests__/group.test.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ describe('Radio', () => {
8080
wrapper.vm.$refs.radioGroup.stateValue = 'B';
8181
// wrapper.setData({ value: 'B' })
8282
radios.at(0).trigger('change');
83-
});
84-
await asyncExpect(() => {
8583
expect(onChange.mock.calls.length).toBe(1);
8684
});
8785
await asyncExpect(() => {
@@ -91,8 +89,6 @@ describe('Radio', () => {
9189
// controlled component
9290
wrapper.setProps({ value: 'A' });
9391
radios.at(1).trigger('change');
94-
});
95-
await asyncExpect(() => {
9692
expect(onChange.mock.calls.length).toBe(2);
9793
});
9894
await asyncExpect(() => {
@@ -135,9 +131,7 @@ describe('Radio', () => {
135131
wrapper.vm.$refs.radioGroup.stateValue = 'B';
136132
radios.at(0).trigger('change');
137133
expect(onChange.mock.calls.length).toBe(1);
138-
await asyncExpect(() => {
139-
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
140-
});
134+
expect(onChangeRadioGroup.mock.calls.length).toBe(1);
141135

142136
// controlled component
143137
wrapper.setProps({ value: 'A' });
@@ -159,13 +153,12 @@ describe('Radio', () => {
159153
// uncontrolled component
160154
wrapper.vm.$refs.radioGroup.stateValue = 'B';
161155
radios.at(0).trigger('change');
162-
await asyncExpect(() => {
163-
expect(onChange.mock.calls.length).toBe(1);
164-
});
165-
// controlled component
166-
wrapper.setProps({ value: 'A' });
167-
radios.at(1).trigger('change');
168-
await asyncExpect(() => {
156+
expect(onChange.mock.calls.length).toBe(1);
157+
158+
asyncExpect(() => {
159+
// controlled component
160+
wrapper.setProps({ value: 'A' });
161+
radios.at(1).trigger('change');
169162
expect(onChange.mock.calls.length).toBe(2);
170163
});
171164
});

0 commit comments

Comments
 (0)