Skip to content

Commit

Permalink
fix(types): setFieldValue returns a promise resolving to nothing or a…
Browse files Browse the repository at this point in the history
…n error object (#3792)

Closes #2384 
Closes #3689
  • Loading branch information
quantizor authored May 26, 2023
1 parent 9289545 commit 6d8f018
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-apples-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Update the type for `setFieldValue` to reflect the returned `Promise` and potential returned error(s).
4 changes: 3 additions & 1 deletion docs/api/formik.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,13 @@ Trigger a form submission. The promise will be rejected if form is invalid.
Number of times user tried to submit the form. Increases when [`handleSubmit`](#handlesubmit-e-reactformevent-htmlformelement-void) is called, resets after calling
[`handleReset`](#handlereset-void). `submitCount` is readonly computed property and should not be mutated directly.

#### `setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void`
#### `setFieldValue: (field: string, value: any, shouldValidate?: boolean) => Promise<void | FormikErrors>`

Set the value of a field imperatively. `field` should match the key of
`values` you wish to update. Useful for creating custom input change handlers. Calling this will trigger validation to run if `validateOnChange` is set to `true` (which it is by default). You can also explicitly prevent/skip validation by passing a third argument as `false`.

If `validateOnChange` is set to `true` and there are errors, they will be resolved in the returned `Promise`.

#### `setStatus: (status?: any) => void`

Set a top-level `status` to anything you want imperatively. Useful for
Expand Down
6 changes: 5 additions & 1 deletion packages/formik/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ export interface FormikHelpers<Values> {
shouldValidate?: boolean
) => void;
/** Set value of form field directly */
setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void;
setFieldValue(
field: string,
value: any,
shouldValidate?: boolean
): Promise<void | FormikErrors<Values>>;
/** Set error message of a form field directly */
setFieldError: (field: string, message: string | undefined) => void;
/** Set whether field has been touched directly */
Expand Down

0 comments on commit 6d8f018

Please sign in to comment.