Skip to content

Commit

Permalink
feat: expose useFormContext closes #4490
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Oct 18, 2024
1 parent 135916c commit f7a4929
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-dolls-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

feat: expose useFormContext closes #4490
2 changes: 1 addition & 1 deletion packages/vee-validate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { Form } from './Form';
export { FieldArray } from './FieldArray';
export { ErrorMessage } from './ErrorMessage';
export { useField, FieldOptions, RuleExpression } from './useField';
export { useForm, FormOptions } from './useForm';
export { useForm, useFormContext, FormOptions } from './useForm';
export { useFieldArray } from './useFieldArray';
export * from './types';
export { useResetForm } from './useResetForm';
Expand Down
4 changes: 3 additions & 1 deletion packages/vee-validate/src/symbols.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { InjectionKey } from 'vue';
import { PrivateFormContext, PrivateFieldContext } from './types';
import { PrivateFormContext, PrivateFieldContext, FormContext } from './types';

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

export const PublicFormContextKey: InjectionKey<FormContext> = Symbol('vee-validate-form-context');

export const FieldContextKey: InjectionKey<PrivateFieldContext<unknown>> = Symbol('vee-validate-field-instance');

export const IS_ABSENT = Symbol('Default empty value');
16 changes: 14 additions & 2 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
toValue,
MaybeRef,
MaybeRefOrGetter,
inject,
} from 'vue';
import { PartialDeep } from 'type-fest';
import { klona as deepCopy } from 'klona/full';
Expand Down Expand Up @@ -68,7 +69,7 @@ import {
debounceNextTick,
normalizeEventValue,
} from './utils';
import { FormContextKey } from './symbols';
import { FormContextKey, PublicFormContextKey } from './symbols';
import { validateTypedSchema, validateObjectSchema } from './validate';
import { refreshInspector, registerFormWithDevTools } from './devtools';
import { isCallable, merge, normalizeFormPath } from '../../shared';
Expand Down Expand Up @@ -1197,12 +1198,16 @@ export function useForm<
});
}

return {
const ctx: FormContext<TValues, TOutput> = {
...formCtx,
values: readonly(formValues) as TValues,
handleReset: () => resetForm(),
submitForm,
};

provide(PublicFormContextKey, ctx);

return ctx;
}

/**
Expand Down Expand Up @@ -1328,3 +1333,10 @@ function mergeValidationResults<TValue extends GenericObject>(
errors: [...a.errors, ...b.errors],
};
}

export function useFormContext<
TValues extends GenericObject = GenericObject,
TOutput extends GenericObject = TValues,
>(): FormContext<TValues, TOutput> {
return inject(PublicFormContextKey) as FormContext<TValues, TOutput>;
}

0 comments on commit f7a4929

Please sign in to comment.