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

feat: add automatic formik form submission #129

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 83 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"@fortawesome/react-fontawesome": "^0.2.0",
"autoprefixer": "^10.0.0",
"dayjs": "^1.0.0",
"debounce": "^2.1.0",
"formik": "^2.2.9",
"postcss-import": "^15.1.0",
"react": "^18.2.0",
Expand Down
35 changes: 35 additions & 0 deletions src/forms/FormikAutoSubmit.stories.tsx
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' }}
sjschlapbach marked this conversation as resolved.
Show resolved Hide resolved
placeholder="Placeholder"
/>
</Form>
</div>
</Formik>
</div>
)
36 changes: 36 additions & 0 deletions src/forms/FormikAutoSubmit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// https://github.com/jaredpalmer/formik/issues/2769#issuecomment-1872382784
sjschlapbach marked this conversation as resolved.
Show resolved Hide resolved

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])
LiineKasak marked this conversation as resolved.
Show resolved Hide resolved

return null
}

export default FormikAutoSubmit
Loading