@@ -43,10 +43,12 @@ const submitForm = ({rules, values, extras, initialValues}, dispatch, getActiveF
4343 true
4444 ) ;
4545
46- if ( passed ) {
47- const setErrors = errors => Object . entries ( errors ) . forEach ( ( [ name , payload ] ) => setError ( name , payload ) ) ;
48- onSubmit ( values , { initialValues, extras, setErrors} ) ;
46+ if ( ! passed ) {
47+ return null ;
4948 }
49+
50+ const setErrors = errors => Object . entries ( errors ) . forEach ( ( [ name , payload ] ) => setError ( name , payload ) ) ;
51+ return onSubmit ( values , { initialValues, extras, setErrors} ) ;
5052 } ) ;
5153} ;
5254
@@ -61,12 +63,15 @@ const Form = ({
6163 ...props
6264} ) => {
6365 const [ state , dispatch ] = useReducer ( reducer , { initialValues, initialExtras, rules} , getInitialState ) ;
64- const handleSubmit = useCallback (
65- event => ( event . preventDefault ( ) , submitForm ( state , dispatch , getActiveFields , extraValidation , onSubmit ) ) ,
66- [ state ]
67- ) ;
66+ const submit = useCallback ( ( ) => submitForm ( state , dispatch , getActiveFields , extraValidation , onSubmit ) , [
67+ state ,
68+ getActiveFields ,
69+ extraValidation ,
70+ onSubmit ,
71+ ] ) ;
72+ const handleSubmit = useCallback ( event => ( event . preventDefault ( ) , submit ( ) ) , [ submit ] ) ;
6873 return (
69- < FormContext . Provider value = { { state, dispatch} } >
74+ < FormContext . Provider value = { { state, dispatch, submit } } >
7075 < form { ...props } onSubmit = { handleSubmit } >
7176 { children }
7277 </ form >
0 commit comments