Skip to content

Commit

Permalink
fix(radio): the checked state also triggers the change event (#3000)
Browse files Browse the repository at this point in the history
* fix(radio): the checked state also triggers the change event

* test(radio): the checked state also triggers the change event
  • Loading branch information
betavs authored and uyarn committed Jan 2, 2024
1 parent 7ec0cb8 commit 14fa90b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/radio/__tests__/vitest-radio.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('Radio Component', () => {
expect(onChangeFn.mock.calls[0][0]).toBe(true);
expect(onChangeFn.mock.calls[0][1].e.type).toBe('click');
});
it('events.change: checked value is true, without allowUncheck, click radio and trigger change', async () => {
it('events.change: checked value is true, without allowUncheck, click radio and can not trigger change', async () => {
const onChangeFn = vi.fn();
const wrapper = mount({
render() {
Expand All @@ -154,9 +154,7 @@ describe('Radio Component', () => {
});
wrapper.find('.t-radio__label').trigger('click');
await wrapper.vm.$nextTick();
expect(onChangeFn).toHaveBeenCalled();
expect(onChangeFn.mock.calls[0][0]).toBe(true);
expect(onChangeFn.mock.calls[0][1].e.type).toBe('click');
expect(onChangeFn).not.toHaveBeenCalled();
});

it('events.click works fine', async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/radio/radio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export default mixins(Vue as VueConstructor<RadioParentInjectInstance>, classPre
checkRadio(e: MouseEvent) {
const tChecked = this.getChecked();
const allowUncheck = this.allowUncheck || this.radioGroup?.allowUncheck;

if (tChecked && !allowUncheck) return;

if (this.radioGroup) {
const value = tChecked && allowUncheck ? undefined : this.value;
this.radioGroup.handleRadioChange(value, { e });
Expand Down

0 comments on commit 14fa90b

Please sign in to comment.