Skip to content

Commit f7a4929

Browse files
committed
feat: expose useFormContext closes #4490
1 parent 135916c commit f7a4929

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

.changeset/angry-dolls-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vee-validate": patch
3+
---
4+
5+
feat: expose useFormContext closes #4490

packages/vee-validate/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export { Form } from './Form';
77
export { FieldArray } from './FieldArray';
88
export { ErrorMessage } from './ErrorMessage';
99
export { useField, FieldOptions, RuleExpression } from './useField';
10-
export { useForm, FormOptions } from './useForm';
10+
export { useForm, useFormContext, FormOptions } from './useForm';
1111
export { useFieldArray } from './useFieldArray';
1212
export * from './types';
1313
export { useResetForm } from './useResetForm';
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { InjectionKey } from 'vue';
2-
import { PrivateFormContext, PrivateFieldContext } from './types';
2+
import { PrivateFormContext, PrivateFieldContext, FormContext } from './types';
33

44
export const FormContextKey: InjectionKey<PrivateFormContext> = Symbol('vee-validate-form');
55

6+
export const PublicFormContextKey: InjectionKey<FormContext> = Symbol('vee-validate-form-context');
7+
68
export const FieldContextKey: InjectionKey<PrivateFieldContext<unknown>> = Symbol('vee-validate-field-instance');
79

810
export const IS_ABSENT = Symbol('Default empty value');

packages/vee-validate/src/useForm.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
toValue,
1717
MaybeRef,
1818
MaybeRefOrGetter,
19+
inject,
1920
} from 'vue';
2021
import { PartialDeep } from 'type-fest';
2122
import { klona as deepCopy } from 'klona/full';
@@ -68,7 +69,7 @@ import {
6869
debounceNextTick,
6970
normalizeEventValue,
7071
} from './utils';
71-
import { FormContextKey } from './symbols';
72+
import { FormContextKey, PublicFormContextKey } from './symbols';
7273
import { validateTypedSchema, validateObjectSchema } from './validate';
7374
import { refreshInspector, registerFormWithDevTools } from './devtools';
7475
import { isCallable, merge, normalizeFormPath } from '../../shared';
@@ -1197,12 +1198,16 @@ export function useForm<
11971198
});
11981199
}
11991200

1200-
return {
1201+
const ctx: FormContext<TValues, TOutput> = {
12011202
...formCtx,
12021203
values: readonly(formValues) as TValues,
12031204
handleReset: () => resetForm(),
12041205
submitForm,
12051206
};
1207+
1208+
provide(PublicFormContextKey, ctx);
1209+
1210+
return ctx;
12061211
}
12071212

12081213
/**
@@ -1328,3 +1333,10 @@ function mergeValidationResults<TValue extends GenericObject>(
13281333
errors: [...a.errors, ...b.errors],
13291334
};
13301335
}
1336+
1337+
export function useFormContext<
1338+
TValues extends GenericObject = GenericObject,
1339+
TOutput extends GenericObject = TValues,
1340+
>(): FormContext<TValues, TOutput> {
1341+
return inject(PublicFormContextKey) as FormContext<TValues, TOutput>;
1342+
}

0 commit comments

Comments
 (0)