Skip to content

Commit 67ff970

Browse files
committed
Try using forwardRef (bypassed commit hooks, sorry!)
1 parent 6618dbc commit 67ff970

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

packages/formik/src/Formik.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
FieldHelperProps,
1414
FieldInputProps,
1515
FormikHelpers,
16+
FormikContextType,
1617
} from './types';
1718
import {
1819
isFunction,
@@ -28,6 +29,7 @@ import {
2829
import { FormikProvider } from './FormikContext';
2930
import invariant from 'tiny-warning';
3031
import { LowPriority, unstable_runWithPriority } from 'scheduler';
32+
import { FormikBag } from './withFormik';
3133

3234
type FormikMessage<Values> =
3335
| { type: 'SUBMIT_ATTEMPT' }
@@ -994,15 +996,15 @@ export function useFormik<Values extends FormikValues = FormikValues>({
994996
return ctx;
995997
}
996998

997-
export function Formik<
999+
export const Formik = React.forwardRef(<
9981000
Values extends FormikValues = FormikValues,
9991001
ExtraProps = {}
1000-
>(props: FormikConfig<Values> & ExtraProps) {
1002+
>(props: FormikConfig<Values> & ExtraProps, ref: React.Ref<FormikContextType<Values>>) => {
10011003
const formikbag = useFormik<Values>(props);
1002-
const { component, children, render, innerRef } = props;
1004+
const { component, children, render } = props;
10031005

10041006
// This allows folks to pass a ref to <Formik />
1005-
React.useImperativeHandle(innerRef, () => formikbag);
1007+
React.useImperativeHandle(ref, () => formikbag);
10061008

10071009
React.useEffect(() => {
10081010
if (__DEV__) {
@@ -1030,7 +1032,10 @@ export function Formik<
10301032
: null}
10311033
</FormikProvider>
10321034
);
1033-
}
1035+
}) as <
1036+
Values extends FormikValues = FormikValues,
1037+
ExtraProps = {}
1038+
>(props: FormikConfig<Values> & ExtraProps & React.RefAttributes<FormikContextType<Values>>) => JSX.Element;
10341039

10351040
function warnAboutMissingIdentifier({
10361041
htmlContent,

packages/formik/test/Formik.test.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { render, fireEvent, wait } from 'react-testing-library';
3+
import { FormikContextType } from '../src/types';
34
import * as Yup from 'yup';
45

56
import {
@@ -50,7 +51,7 @@ function Form({
5051

5152
const InitialValues = { name: 'jared' };
5253

53-
function renderFormik<V = Values>(props?: Partial<FormikConfig<V>>) {
54+
function renderFormik<V = Values>(props?: Partial<FormikConfig<V>> & React.RefAttributes<FormikContextType<Values>>) {
5455
let injected: any;
5556
const { rerender, ...rest } = render(
5657
<Formik
@@ -1296,4 +1297,14 @@ describe('<Formik>', () => {
12961297

12971298
expect(innerRef.current).toEqual(getProps());
12981299
});
1300+
1301+
it('exposes formikbag with forwardRef', () => {
1302+
const ref = React.createRef<FormikContextType<Values>>();
1303+
1304+
const { getProps } = renderFormik({ ref });
1305+
1306+
ref.current.values.name;
1307+
1308+
expect(ref.current).toEqual(getProps());
1309+
});
12991310
});

0 commit comments

Comments
 (0)