-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
57 additions
and
5 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
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,31 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
import { useFormikContext } from 'formik' | ||
import { usePrevious } from 'react-use' | ||
|
||
/** | ||
* This is a Formik workaround hook. Formik's isValidating only returns true DURING the validation. | ||
* However this doesn't work when we want to wait for the results of the validation before doing something. (i.e. positive validation) | ||
* To handle this we wait for Formik isValidating to change to true, then wait for it to turn back to false | ||
* before saying we're "not waiting" for validation anymore | ||
* @returns {isWaitingOnValidation, handleChange} | ||
*/ | ||
export const useIsWaitingForValidation = () => { | ||
const { isValidating } = useFormikContext() | ||
const [isWaitingForValidation, setIsWaitingForValidation] = useState(false) | ||
const wasValidating = usePrevious(isValidating) | ||
|
||
// We know formik has stopped validating when it differs from our previous value | ||
useEffect(() => { | ||
if (wasValidating && !isValidating) { | ||
setIsWaitingForValidation(false) | ||
} | ||
}, [isValidating, wasValidating]) | ||
|
||
return { | ||
isWaitingForValidation, | ||
handleChange: () => { | ||
setIsWaitingForValidation(true) | ||
} | ||
} | ||
} |
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