@@ -52,6 +52,7 @@ const Form = forwardRef<FormComponentRef, ComponentProps>(
52
52
onSuccess = noop ,
53
53
onError = noop ,
54
54
onCancelToken = noop ,
55
+ onSubmitComplete = noop ,
55
56
disableWhileProcessing = false ,
56
57
children,
57
58
...props
@@ -88,6 +89,15 @@ const Form = forwardRef<FormComponentRef, ComponentProps>(
88
89
return ( ) => formEvents . forEach ( ( e ) => formElement . current ?. removeEventListener ( e , updateDirtyState ) )
89
90
} , [ ] )
90
91
92
+ const reset = ( ...fields : string [ ] ) => {
93
+ resetFormFields ( formElement . current , defaults . current , fields )
94
+ }
95
+
96
+ const resetAndClearErrors = ( ...fields : string [ ] ) => {
97
+ form . clearErrors ( ...fields )
98
+ reset ( ...fields )
99
+ }
100
+
91
101
const submit = ( ) => {
92
102
const [ url , _data ] = mergeDataIntoQueryString (
93
103
resolvedMethod ,
@@ -104,7 +114,14 @@ const Form = forwardRef<FormComponentRef, ComponentProps>(
104
114
onBefore,
105
115
onStart,
106
116
onProgress,
107
- onFinish,
117
+ onFinish : ( ...args ) => {
118
+ onFinish ( ...args )
119
+ onSubmitComplete ( {
120
+ reset,
121
+ resetAndClearErrors,
122
+ clearErrors : form . clearErrors ,
123
+ } )
124
+ } ,
108
125
onCancel,
109
126
onSuccess,
110
127
onError,
@@ -115,27 +132,22 @@ const Form = forwardRef<FormComponentRef, ComponentProps>(
115
132
form . submit ( resolvedMethod , url , submitOptions )
116
133
}
117
134
118
- useImperativeHandle (
119
- ref ,
120
- ( ) => ( {
121
- errors : form . errors ,
122
- hasErrors : form . hasErrors ,
123
- processing : form . processing ,
124
- progress : form . progress ,
125
- wasSuccessful : form . wasSuccessful ,
126
- recentlySuccessful : form . recentlySuccessful ,
127
- clearErrors : form . clearErrors ,
128
- resetAndClearErrors : ( ...fields : string [ ] ) => {
129
- form . clearErrors ( ...fields )
130
- resetFormFields ( formElement . current , defaults . current , fields )
131
- } ,
132
- setError : form . setError ,
133
- isDirty,
134
- reset : ( ...fields ) => resetFormFields ( formElement . current , defaults . current , fields ) ,
135
- submit,
136
- } ) ,
137
- [ form , isDirty , submit ] ,
138
- )
135
+ const exposed = ( ) => ( {
136
+ errors : form . errors ,
137
+ hasErrors : form . hasErrors ,
138
+ processing : form . processing ,
139
+ progress : form . progress ,
140
+ wasSuccessful : form . wasSuccessful ,
141
+ recentlySuccessful : form . recentlySuccessful ,
142
+ isDirty,
143
+ clearErrors : form . clearErrors ,
144
+ resetAndClearErrors,
145
+ setError : form . setError ,
146
+ reset,
147
+ submit,
148
+ } )
149
+
150
+ useImperativeHandle ( ref , exposed , [ form , isDirty , submit ] )
139
151
140
152
return createElement (
141
153
'form' ,
@@ -150,25 +162,7 @@ const Form = forwardRef<FormComponentRef, ComponentProps>(
150
162
} ,
151
163
inert : disableWhileProcessing && form . processing ,
152
164
} ,
153
- typeof children === 'function'
154
- ? children ( {
155
- errors : form . errors ,
156
- hasErrors : form . hasErrors ,
157
- processing : form . processing ,
158
- progress : form . progress ,
159
- wasSuccessful : form . wasSuccessful ,
160
- recentlySuccessful : form . recentlySuccessful ,
161
- setError : form . setError ,
162
- clearErrors : form . clearErrors ,
163
- resetAndClearErrors : ( ...fields : string [ ] ) => {
164
- form . clearErrors ( ...fields )
165
- resetFormFields ( formElement . current , defaults . current , fields )
166
- } ,
167
- isDirty,
168
- reset : ( ...fields ) => resetFormFields ( formElement . current , defaults . current , fields ) ,
169
- submit,
170
- } )
171
- : children ,
165
+ typeof children === 'function' ? children ( exposed ( ) ) : children ,
172
166
)
173
167
} ,
174
168
)
0 commit comments