jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.73k stars 2.77k forks source link

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

Closed bonimba closed 11 months ago

bonimba commented 1 year ago

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[bot] commented 1 year ago

🦋 Changeset detected

Latest commit: 4adda22123bcf0b44fdd77231ed35555c40277ed

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[bot] commented 1 year ago

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](https://vercel.com/formik/formik-docs/7P8gWBwJ19czufCMnZuHYVifpv2M)) | [Visit Preview](https://formik-docs-git-fork-bonimba-cohesive-types-formik.vercel.app) | | Jul 12, 2023 8:13am |
codesandbox-ci[bot] commented 1 year ago

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 4adda22123bcf0b44fdd77231ed35555c40277ed:

Sandbox Source
Formik TypeScript Playground (forked) Configuration
bonimba commented 11 months ago

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

quantizor commented 11 months ago

Just haven't had time to look at it yet