Skip to content

Commit

Permalink
fix: make sure removePathState removes proper pathState (#4649)
Browse files Browse the repository at this point in the history
closes #4643

Co-authored-by: Bartosz Gościński <[email protected]>
  • Loading branch information
2 people authored and logaretm committed Jan 29, 2024
1 parent 998ca37 commit d779980
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-seas-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

fix: make sure removePathState removes the correct path state
4 changes: 3 additions & 1 deletion packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ export function useForm<
handleSubmit.withControlled = makeSubmissionFactory(true);

function removePathState<TPath extends Path<TValues>>(path: TPath, id: number) {
const idx = pathStates.value.findIndex(s => s.path === path);
const idx = pathStates.value.findIndex(s => {
return s.path === path && (Array.isArray(s.id) ? s.id.includes(id) : s.id === id);
});
const pathState = pathStates.value[idx];
if (idx === -1 || !pathState) {
return;
Expand Down
41 changes: 39 additions & 2 deletions packages/vee-validate/tests/Form.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineRule, useField, Form, Field, useIsValidating } from '@/vee-validate';
import { defineRule, useField, Form, Field, useIsValidating, useForm } from '@/vee-validate';
import { mountWithHoc, setValue, setChecked, dispatchEvent, flushPromises } from './helpers';
import * as yup from 'yup';
import { computed, defineComponent, onErrorCaptured, ref, Ref } from 'vue';
import { InvalidSubmissionContext } from '../src/types';
import { InvalidSubmissionContext, PrivateFormContext } from '../src/types';

describe('<Form />', () => {
const REQUIRED_MESSAGE = `This field is required`;
Expand Down Expand Up @@ -3141,3 +3141,40 @@ test('radio fields with single field component binding', async () => {
expect(model.value).toBe('Tea');
expect(submit).toHaveBeenLastCalledWith({ drink: 'Tea' }, expect.anything());
});

// #4643
test('removes proper pathState when field is unmounting', async () => {
const renderTemplateField = ref(false);
let form!: PrivateFormContext;

mountWithHoc({
template: `
<form>
<Field v-if="renderTemplateField" name="foo" rules="required" />
</form>
`,
setup() {
form = useForm() as unknown as PrivateFormContext;
useField('foo');
return { renderTemplateField };
},
});

expect(form.meta.value.valid).toBe(true);
expect(form.getAllPathStates()).toMatchObject([{ id: 0, path: 'foo' }]);

renderTemplateField.value = true;
await flushPromises();

expect(form.meta.value.valid).toBe(false);
expect(form.getAllPathStates()).toMatchObject([
{ id: 0, path: 'foo' },
{ id: 1, path: 'foo' },
]);

renderTemplateField.value = false;
await flushPromises();

expect(form.meta.value.valid).toBe(true);
expect(form.getAllPathStates()).toMatchObject([{ id: 0, path: 'foo' }]);
});

0 comments on commit d779980

Please sign in to comment.