From d267a7cecfa5415790dc227aa456a4e98e96fea9 Mon Sep 17 00:00:00 2001 From: Luciano Di Cocco Date: Fri, 6 Dec 2019 16:21:08 +0100 Subject: [PATCH] add validationSchema to FormikContext --- src/Formik.tsx | 15 +++++++++++---- test/FormikContext.test.tsx | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 test/FormikContext.test.tsx diff --git a/src/Formik.tsx b/src/Formik.tsx index 61b085478..43120f148 100755 --- a/src/Formik.tsx +++ b/src/Formik.tsx @@ -564,8 +564,10 @@ export function useFormik({ if ((eventOrTextValue as React.ChangeEvent).persist) { (eventOrTextValue as React.ChangeEvent).persist(); } - const target = eventOrTextValue.target ? (eventOrTextValue as React.ChangeEvent).target : (eventOrTextValue as React.ChangeEvent).currentTarget; - + const target = eventOrTextValue.target + ? (eventOrTextValue as React.ChangeEvent).target + : (eventOrTextValue as React.ChangeEvent).currentTarget; + const { type, name, @@ -914,7 +916,7 @@ export function Formik< ExtraProps = {} >(props: FormikConfig & ExtraProps) { const formikbag = useFormik(props); - const { component, children, render } = props; + const { component, children, render, validationSchema } = props; React.useEffect(() => { if (__DEV__) { invariant( @@ -925,7 +927,12 @@ export function Formik< // eslint-disable-next-line }, []); return ( - + {component ? React.createElement(component as any, formikbag) : render diff --git a/test/FormikContext.test.tsx b/test/FormikContext.test.tsx new file mode 100644 index 000000000..e00bf40a0 --- /dev/null +++ b/test/FormikContext.test.tsx @@ -0,0 +1,28 @@ +import * as React from 'react'; +import { render } from 'react-testing-library'; +import { Formik } from '../src/Formik'; +import { useFormikContext } from '../src/FormikContext'; + +describe('FormikContext', () => { + describe('useFormikContext', () => { + it('should return validationContext if set', () => { + const validationSchema = 'validationSchema'; + + const AComponenent: React.FC = () => { + const formikContext = useFormikContext(); + expect(formikContext.validationSchema).toBe(validationSchema); + return null; + }; + + render( + {}} + > + + + ); + }); + }); +});