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

types: Align FormikHelpers and FieldHelper types to useFormik ones #3843

Merged
merged 5 commits into from Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-flowers-rule.md
@@ -0,0 +1,5 @@
---
'formik': patch
---

Fix FormikHelper and FieldHelperProps types
10 changes: 8 additions & 2 deletions docs/api/formik.md
Expand Up @@ -177,11 +177,13 @@ Set `errors` imperatively.
Set the error message of a field imperatively. `field` should match the key of
`errors` you wish to update. Useful for creating custom input error handlers.

#### `setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => void`
#### `setFieldTouched: (field: string, isTouched?: boolean, shouldValidate?: boolean) => Promise<void | FormikErrors>`

Set the touched state of a field imperatively. `field` should match the key of
`touched` you wish to update. Useful for creating custom input blur handlers. Calling this method will trigger validation to run if `validateOnBlur` is set to `true` (which it is by default). `isTouched` defaults to `true` if not specified. You can also explicitly prevent/skip validation by passing a third argument as `false`.

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

#### `submitForm: () => Promise`

Trigger a form submission. The promise will be rejected if form is invalid.
Expand All @@ -208,14 +210,18 @@ use it to pass API responses back into your component in `handleSubmit`.

Set `isSubmitting` imperatively. You would call it with `setSubmitting(false)` in your `onSubmit` handler to finish the cycle. To learn more about the submission process, see [Form Submission](../guides/form-submission.md).

#### `setTouched: (fields: { [field: string]: boolean }, shouldValidate?: boolean) => void`
#### `setTouched: (fields: { [field: string]: boolean }, shouldValidate?: boolean) => Promise<void | FormikErrors>`

Set `touched` imperatively. Calling this will trigger validation to run if `validateOnBlur` is set to `true` (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as `false`.

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

#### `setValues: (fields: React.SetStateAction<{ [field: string]: any }>, shouldValidate?: boolean) => void`

Set `values` imperatively. 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 second argument as `false`.

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

#### `status?: any`

A top-level status object that you can use to represent form state that can't
Expand Down
6 changes: 5 additions & 1 deletion docs/api/useField.md
Expand Up @@ -159,6 +159,10 @@ An object that contains relevant computed metadata about a field. More specifica

An object that contains helper functions which you can use to imperatively change the value, error value or touched status for the field in question. This is useful for components which need to change a field's status directly, without triggering change or blur events.

- `setValue(value: any, shouldValidate?: boolean): void` - A function to change the field's value. 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 second argument as `false`.
- `setValue(value: any, shouldValidate?: boolean): Promise<void | FormikErrors>` - A function to change the field's value. 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 second argument as `false`.
If `validateOnChange` is set to `true` and there are errors, they will be resolved in the returned `Promise`.

- `setTouched(value: boolean, shouldValidate?: boolean): void` - A function to change the field's touched status. Calling this will trigger validation to run if `validateOnBlur` is set to `true` (which it is by default). You can also explicitly prevent/skip validation by passing a second argument as `false`.
If `validateOnBlur` is set to `true` and there are errors, they will be resolved in the returned `Promise`.

- `setError(value: any): void` - A function to change the field's error value
12 changes: 6 additions & 6 deletions packages/formik/src/types.tsx
Expand Up @@ -86,12 +86,12 @@ export interface FormikHelpers<Values> {
setTouched: (
touched: FormikTouched<Values>,
shouldValidate?: boolean
) => void;
) => Promise<void | FormikErrors<Values>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

/** Manually set values object */
setValues: (
values: React.SetStateAction<Values>,
shouldValidate?: boolean
) => void;
) => Promise<void | FormikErrors<Values>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

/** Set value of form field directly */
setFieldValue: (
field: string,
Expand All @@ -105,11 +105,11 @@ export interface FormikHelpers<Values> {
field: string,
isTouched?: boolean,
shouldValidate?: boolean
) => void;
) => Promise<void | FormikErrors<Values>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

/** Validate form values */
validateForm: (values?: any) => Promise<FormikErrors<Values>>;
/** Validate field value */
validateField: (field: string) => void;
validateField: (field: string) => Promise<void> | Promise<string | undefined>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

/** Reset form */
resetForm: (nextState?: Partial<FormikState<Values>>) => void;
/** Submit the form imperatively */
Expand Down Expand Up @@ -302,9 +302,9 @@ export interface FieldMetaProps<Value> {
/** Imperative handles to change a field's value, error and touched */
export interface FieldHelperProps<Value> {
/** Set the field's value */
setValue: (value: Value, shouldValidate?: boolean) => void;
setValue: (value: Value, shouldValidate?: boolean) => Promise<void | FormikErrors<Value>>;
/** Set the field's touched value */
setTouched: (value: boolean, shouldValidate?: boolean) => void;
setTouched: (value: boolean, shouldValidate?: boolean) => Promise<void | FormikErrors<Value>>;
/** Set the field's error value */
setError: (value: string | undefined) => void;
}
Expand Down