Skip to content

Commit

Permalink
feat: added ResetFormOpts arg to useResetForm closes #4707
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed May 29, 2024
1 parent dadc58a commit fd008c1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-schools-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

feat: added ResetFormOpts arg to useResetForm closes #4707
6 changes: 3 additions & 3 deletions packages/vee-validate/src/useResetForm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormContextKey } from './symbols';
import { FormState } from './types';
import { FormState, ResetFormOpts } from './types';
import { injectWithSelf, warn } from './utils';

export function useResetForm<TValues extends Record<string, unknown> = Record<string, unknown>>() {
Expand All @@ -10,11 +10,11 @@ export function useResetForm<TValues extends Record<string, unknown> = Record<st
}
}

return function resetForm(state?: Partial<FormState<TValues>>) {
return function resetForm(state?: Partial<FormState<TValues>>, opts?: ResetFormOpts) {
if (!form) {
return;
}

return form.resetForm(state);
return form.resetForm(state, opts);
};
}
28 changes: 27 additions & 1 deletion packages/vee-validate/tests/useResetForm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useField, useForm, useResetForm } from '@/vee-validate';
import { FormContext, useField, useForm, useResetForm } from '@/vee-validate';
import { mountWithHoc, setValue, flushPromises } from './helpers';

describe('useResetForm()', () => {
Expand Down Expand Up @@ -73,4 +73,30 @@ describe('useResetForm()', () => {
expect(spy).toHaveBeenCalled();
spy.mockRestore();
});

test('resets a form with passed options', async () => {
let resetForm: any;
let form!: FormContext<{ fname: string; lname: string }>;

mountWithHoc({
setup() {
form = useForm({
initialValues: { fname: '123', lname: '456' },
});

resetForm = useResetForm();

return {};
},
template: `
<div></div>
`,
});

await flushPromises();

resetForm({ values: { fname: 'test' } }, { force: true });
expect(form.values.lname).toBeUndefined();
expect(form.values.fname).toBe('test');
});
});

0 comments on commit fd008c1

Please sign in to comment.