Skip to content

Commit 8ba7541

Browse files
authored
fix: checkbox can't trigger change (#3285)
* fix: checkbox can't trigger change * test: add unit test * chore: update vc-checkbox
1 parent 1581ff3 commit 8ba7541

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

components/radio/__tests__/group.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,41 @@ describe('Radio', () => {
230230
});
231231
expect(wrapper.html()).toMatchSnapshot();
232232
});
233+
234+
it('when onChange do not change the value, change event can be also triggered.', async () => {
235+
const onChange = jest.fn();
236+
const onChangeRadioGroup = () => {
237+
onChange();
238+
wrapper.setProps({ value: 'A' });
239+
};
240+
241+
const wrapper = mount(
242+
{
243+
props: ['value'],
244+
render() {
245+
const value = this.value || 'A';
246+
return (
247+
<RadioGroup ref="radioGroup" value={value} onChange={onChangeRadioGroup}>
248+
<Radio value="A">A</Radio>
249+
<Radio value="B">B</Radio>
250+
<Radio value="C">C</Radio>
251+
</RadioGroup>
252+
);
253+
},
254+
},
255+
{ sync: false },
256+
);
257+
258+
const radios = wrapper.findAll('input');
259+
260+
await asyncExpect(() => {
261+
radios.at(1).trigger('click');
262+
expect(onChange.mock.calls.length).toBe(1);
263+
});
264+
265+
await asyncExpect(() => {
266+
radios.at(1).trigger('click');
267+
expect(onChange.mock.calls.length).toBe(2);
268+
});
269+
});
233270
});

components/vc-checkbox/src/Checkbox.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export default {
9292
nativeEvent: e,
9393
});
9494
this.eventShiftKey = false;
95+
// fix https://github.com/vueComponent/ant-design-vue/issues/3047
96+
if ('checked' in props) {
97+
this.$refs.input.checked = props.checked;
98+
}
9599
},
96100
onClick(e) {
97101
this.__emit('click', e);

0 commit comments

Comments
 (0)