-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add automatic formik form submission, fixes old values in formi…
…k onChange function
- Loading branch information
1 parent
2c9c441
commit d495c30
Showing
4 changed files
with
155 additions
and
69 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Form, Formik } from 'formik' | ||
import React from 'react' | ||
import FormikAutoSubmit from './FormikAutoSubmit' | ||
import FormikTextField from './NewFormikTextField' | ||
|
||
export const Default = () => ( | ||
<div> | ||
<div> | ||
Formik with an AutoSubmit component calls onSubmit with a given debounce | ||
(here 1000) | ||
</div> | ||
<Formik | ||
initialValues={{ | ||
name: '', | ||
}} | ||
isInitialValid={false} | ||
onSubmit={async (values) => { | ||
alert(`Form submitted with value: ${values.name}`) | ||
}} | ||
> | ||
<div> | ||
<Form> | ||
<FormikAutoSubmit debounceWait={1000} /> | ||
<FormikTextField | ||
name="name" | ||
label="Label" | ||
tooltip="Tooltip for this input" | ||
className={{ root: 'mb-1 w-80' }} | ||
placeholder="Placeholder" | ||
/> | ||
</Form> | ||
</div> | ||
</Formik> | ||
</div> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// https://github.com/jaredpalmer/formik/issues/2769#issuecomment-1872382784 | ||
|
||
import debounce from 'debounce' | ||
import { useFormikContext } from 'formik' | ||
import { useEffect, useMemo } from 'react' | ||
|
||
export interface AutoSubmitProps { | ||
debounceWait: number | ||
} | ||
|
||
export function FormikAutoSubmit({ debounceWait }: AutoSubmitProps) { | ||
/* | ||
This component is used to automatically submit the form when the form is valid | ||
and has been changed(dirty). | ||
*/ | ||
const { isValid, values, dirty, submitForm } = useFormikContext() | ||
|
||
// avoid repetitive calls to debounce | ||
const debouncedSubmit = useMemo( | ||
() => | ||
debounce(() => { | ||
if (isValid && dirty) { | ||
void submitForm() | ||
} | ||
}, debounceWait), | ||
[isValid, dirty, submitForm, debounceWait] | ||
) | ||
|
||
useEffect(() => { | ||
debouncedSubmit() | ||
}, [isValid, values, dirty, submitForm, debouncedSubmit]) | ||
|
||
return null | ||
} | ||
|
||
export default FormikAutoSubmit |