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

Conversation

bonimba
Copy link
Contributor

@bonimba bonimba commented Jul 4, 2023

This is yet another PR around TypeScript.

The inferred types for the helpers in useFormik are the correct ones and should be used everywhere.
See

export declare function useFormik<Values extends FormikValues = FormikValues>({ validateOnChange, validateOnBlur, validateOnMount, isInitialValid, enableReinitialize, onSubmit, ...rest }: FormikConfig<Values>): {
   
   [...redacted for readability...]
   
    setFieldTouched: (field: string, touched?: boolean, shouldValidate?: boolean) => Promise<FormikErrors<Values>> | Promise<void>;
    setFieldValue: (field: string, value: any, shouldValidate?: boolean) => Promise<FormikErrors<Values>> | Promise<void>;
    setFieldError: (field: string, value: string | undefined) => void;
    setStatus: (status: any) => void;
    setSubmitting: (isSubmitting: boolean) => void;
    setTouched: (touched: FormikTouched<Values>, shouldValidate?: boolean) => Promise<FormikErrors<Values>> | Promise<void>;
    setValues: (values: React.SetStateAction<Values>, shouldValidate?: boolean) => Promise<FormikErrors<Values>> | Promise<void>;
    submitForm: () => Promise<any>;
    validateForm: (values?: Values) => Promise<FormikErrors<Values>>;
    validateField: (name: string) => Promise<void> | Promise<string | undefined>;
    
    [...redacted for readability...]
};

Having the correct types, and the awaiting these helpers helps with otherwise possible race conditions when used in an imperative way, like so

const handleChange = ({ target }) => {
  await setFieldTouched('fieldName', true, false) 
  await setFieldValue('fieldName', target.value, false)
  await validateField('fieldName')
}

@changeset-bot
Copy link

changeset-bot bot commented Jul 4, 2023

🦋 Changeset detected

Latest commit: 4adda22

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
formik Patch
formik-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Jul 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
formik-docs ⬜️ Ignored (Inspect) Visit Preview Jul 12, 2023 8:13am

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 4, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 4adda22:

Sandbox Source
Formik TypeScript Playground (forked) Configuration

/** 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

@@ -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

@@ -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

@bonimba bonimba changed the title Align FormikHelpers and FieldHelper types to useFormik ones types: Align FormikHelpers and FieldHelper types to useFormik ones Jul 4, 2023
@bonimba bonimba marked this pull request as ready for review July 4, 2023 17:37
@bonimba
Copy link
Contributor Author

bonimba commented Jul 18, 2023

@probablyup is there something more I need to do to make this PR reviewable?

@quantizor
Copy link
Collaborator

Just haven't had time to look at it yet

Copy link
Collaborator

@quantizor quantizor left a comment

Choose a reason for hiding this comment

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

Looks fantastic, thank you!

@kodiakhq kodiakhq bot merged commit 9e0a661 into jaredpalmer:main Aug 2, 2023
@github-actions github-actions bot mentioned this pull request Aug 2, 2023
kodiakhq bot pushed a commit that referenced this pull request Aug 2, 2023
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

# Releases
## formik@2.4.3

### Patch Changes

-   [`9e0a661`](9e0a661) [#3843](#3843) Thanks [@bonimba](https://github.com/bonimba)! - Fix FormikHelper and FieldHelperProps types

 ## formik-native@2.1.26

### Patch Changes

-   Updated dependencies \[[`9e0a661`](9e0a661)]:
    -   formik@2.4.3
@bonimba bonimba deleted the cohesive-types branch August 2, 2023 10:58
kstar0707 added a commit to kstar0707/formik that referenced this pull request Nov 29, 2023
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated.

# Releases
## formik@2.4.3

### Patch Changes

-   [`9e0a661`](jaredpalmer/formik@9e0a661) [#3843](jaredpalmer/formik#3843) Thanks [@bonimba](https://github.com/bonimba)! - Fix FormikHelper and FieldHelperProps types

 ## formik-native@2.1.26

### Patch Changes

-   Updated dependencies \[[`9e0a661`](jaredpalmer/formik@9e0a661)]:
    -   formik@2.4.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants