jaredpalmer / formik

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

Formik Plugins #3109

Open johnrom opened 3 years ago

johnrom commented 3 years ago

🔌 Formik Plugins

I've identified several use cases across Formik issues for adding a plugin system to Formik. The functionality of a plugin would be:

  1. Modify the Return Value of useFormik
  2. Intercept reducer and decorate state
  3. Expose their own hooks to access bits that aren't relevant to Formik's Core API.

It would allow us to use composition to expand Formik's functionality without adding responsibilities to the core library.

Example Uses

The Proposed API

I propose we create Formik initializers using createFormik which we then pass to useFormik or Formik to add extra functionality to the reducer. It would be something like:

// I have no idea if this is possible with current TypeScript version
// initializer is automatically decorated with types of plugins, like:
// formikInitializer: FormikInitializer<Values, [FormikStatusPlugin<YourType>, FormikYupValidatorPlugin()]>
const myFormikInitializer = createFormik()
  .addPlugin(new FormikStatusPlugin<YourType>())
  .addPlugin(new FormikYupValidatorPlugin()); // example of other uses for plugins

const MyForm = () => {
  const formik = useFormik({ ...formikProps }, myFormikInitializer);
  // or
  return <Formik initializer={myFormikInitializer} {...formikprops} />
};
github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days