Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure removePathState removes proper pathState #4649

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' }]);
});