-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5962 from alanpoulain/reset-submit-errors
Reset submission error when the field is changed
- Loading branch information
Showing
6 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
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
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,27 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useForm } from 'react-final-form'; | ||
|
||
/** | ||
* Reset the submission error when the corresponding field changes. | ||
* final-form doesn't do this by default. | ||
*/ | ||
const useResetSubmitErrors = () => { | ||
const form = useForm(); | ||
const prevValues = useRef(form.getState().values); | ||
useEffect(() => { | ||
const unsubscribe = form.subscribe( | ||
({ values }) => { | ||
form.mutators.resetSubmitErrors({ | ||
current: values, | ||
prev: prevValues.current, | ||
}); | ||
|
||
prevValues.current = values; | ||
}, | ||
{ values: true } | ||
); | ||
return unsubscribe; | ||
}, [form]); | ||
}; | ||
|
||
export default useResetSubmitErrors; |
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