Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Improve onReset to get the new values provided by resetForm #4024

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/afraid-garlics-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': major
---

Feat: Improve onReset to get the new values provided by resetForm
4 changes: 2 additions & 2 deletions docs/api/formik.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,10 @@ an input from uncontrolled to controlled.
Note: `initialValues` not available to the higher-order component, use
`mapPropsToValues` instead.

### `onReset?: (values: Values, formikBag: FormikBag) => void`
### `onReset?: (values: Values, formikBag: FormikBag, initialValues: Values) => void`

Your optional form reset handler. It is passed your forms `values` and the
"FormikBag".
"FormikBag" and the `initialValues`.

### `onSubmit: (values: Values, formikBag: FormikBag) => void | Promise<any>`

Expand Down
3 changes: 2 additions & 1 deletion packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ export function useFormik<Values extends FormikValues = FormikValues>({
if (props.onReset) {
const maybePromisedOnReset = (props.onReset as any)(
state.values,
imperativeMethods
imperativeMethods,
values
);

if (isPromise(maybePromisedOnReset)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/formik/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export interface FormikConfig<Values> extends FormikSharedConfig {
/**
* Reset handler
*/
onReset?: (values: Values, formikHelpers: FormikHelpers<Values>) => void;
onReset?: (values: Values, formikHelpers: FormikHelpers<Values>, initialValues?: Values) => void;

/**
* Submission handler
Expand Down
34 changes: 33 additions & 1 deletion packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,39 @@ describe('<Formik>', () => {
setSubmitting: expect.any(Function),
setTouched: expect.any(Function),
setValues: expect.any(Function),
})
}),
InitialValues
);
});

it('should call onReset with values and actions and initialValues when form is reset with values', () => {
const onReset = jest.fn();
const { getProps } = renderFormik({
initialValues: InitialValues,
onSubmit: noop,
onReset,
});

const NewInitialValues = { name: 'jared', age: 31,}

act(() => {
getProps().resetForm({values: NewInitialValues});
});

expect(onReset).toHaveBeenCalledWith(
InitialValues,
expect.objectContaining({
resetForm: expect.any(Function),
setErrors: expect.any(Function),
setFieldError: expect.any(Function),
setFieldTouched: expect.any(Function),
setFieldValue: expect.any(Function),
setStatus: expect.any(Function),
setSubmitting: expect.any(Function),
setTouched: expect.any(Function),
setValues: expect.any(Function),
}),
NewInitialValues
);
});

Expand Down