Skip to content

Commit f1dc135

Browse files
committedJun 6, 2023
fix: use event value if no checked value for checkbox/radio closes #4308
1 parent d4fafc9 commit f1dc135

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed
 

Diff for: ‎.changeset/lazy-years-mate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: use event value if no checked value for checkbox/radio closes #4308

Diff for: ‎packages/vee-validate/src/useField.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,11 @@ function useFieldWithChecked<TValue = unknown>(
518518
const path = toValue(name);
519519
const pathState = form?.getPathState(path);
520520
const value = normalizeEventValue(e);
521-
let newValue!: TValue;
521+
let newValue = unref(checkedValue) ?? value;
522522
if (form && pathState?.multiple && pathState.type === 'checkbox') {
523-
newValue = resolveNextCheckboxValue(getFromPath(form.values, path) || [], value, undefined) as TValue;
524-
} else {
525-
// Single checkbox field without a form to toggle it's value
526-
newValue = resolveNextCheckboxValue(unref(field.value), unref(checkedValue), unref(uncheckedValue)) as TValue;
523+
newValue = resolveNextCheckboxValue(getFromPath(form.values, path) || [], newValue, undefined) as TValue;
524+
} else if (opts?.type === 'checkbox') {
525+
newValue = resolveNextCheckboxValue(unref(field.value), newValue, unref(uncheckedValue)) as TValue;
527526
}
528527

529528
handleChange(newValue, shouldValidate);

Diff for: ‎packages/vee-validate/tests/Form.spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -3150,3 +3150,46 @@ test('unmounted fields should not be validated when keep-values is on', async ()
31503150
};
31513151
expect(spy).toHaveBeenLastCalledWith(expected);
31523152
});
3153+
3154+
// #4308
3155+
test('radio fields with single field component binding', async () => {
3156+
const submit = vi.fn();
3157+
const model = ref('');
3158+
const wrapper = mountWithHoc({
3159+
setup() {
3160+
return {
3161+
onSubmit: submit,
3162+
model,
3163+
};
3164+
},
3165+
template: `
3166+
<VForm @submit="onSubmit">
3167+
<Field name="drink" type="radio" v-model="model" v-slot="{ field }">
3168+
<input type="radio" value="Coffee" v-bind="field" />
3169+
<input type="radio" value="Tea" v-bind="field" />
3170+
</Field>
3171+
3172+
<button>Submit</button>
3173+
</VForm>
3174+
`,
3175+
});
3176+
3177+
const inputs = wrapper.$el.querySelectorAll('input');
3178+
const button = wrapper.$el.querySelector('button');
3179+
3180+
wrapper.$el.querySelector('button').click();
3181+
await flushPromises();
3182+
3183+
setChecked(inputs[0]);
3184+
await flushPromises();
3185+
button.click();
3186+
await flushPromises();
3187+
expect(model.value).toBe('Coffee');
3188+
expect(submit).toHaveBeenLastCalledWith({ drink: 'Coffee' }, expect.anything());
3189+
setChecked(inputs[1]);
3190+
await flushPromises();
3191+
button.click();
3192+
await flushPromises();
3193+
expect(model.value).toBe('Tea');
3194+
expect(submit).toHaveBeenLastCalledWith({ drink: 'Tea' }, expect.anything());
3195+
});

0 commit comments

Comments
 (0)
Please sign in to comment.