@@ -15,6 +15,10 @@ import {
1515import { hasTrueValues } from './utils'
1616
1717export type UseFormStatusOptions < V extends Values , E , R > = {
18+ /**
19+ * Update the form when it is modified or touched (happens only at the form level).
20+ */
21+ forceUpdateOnStatusChange ?: boolean ;
1822 /**
1923 * The form state hook.
2024 */
@@ -142,6 +146,7 @@ export type UseFormStatusHook<V extends Values> = {
142146
143147function useFormStatus < V extends Values , E , R > ( options : UseFormStatusOptions < V , E , R > ) : UseFormStatusHook < V > {
144148 const {
149+ forceUpdateOnStatusChange,
145150 formState,
146151 mode
147152 } = options
@@ -161,7 +166,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
161166 // MODIFIED
162167
163168 const clearModified = useCallback < UseFormStatusHook < V > [ 'clearModified' ] > ( ( paths , opts ) => {
164- const { forceUpdate = false } = opts ?? { }
169+ const { forceUpdate = forceUpdateOnStatusChange } = opts ?? { }
165170
166171 if ( paths ) {
167172 for ( let i = 0 ; i < paths . length ; i ++ ) {
@@ -178,7 +183,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
178183 modifiedFields : modifiedRef . current
179184 } ) )
180185 }
181- } , [ mode , modifiedRef , setState ] )
186+ } , [ forceUpdateOnStatusChange , mode , modifiedRef , setState ] )
182187
183188 const getModified = useCallback < UseFormStatusHook < V > [ 'getModified' ] > ( ( ) => {
184189 return modifiedRef . current
@@ -192,7 +197,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
192197 } , [ modifiedRef ] )
193198
194199 const resetModified = useCallback < UseFormStatusHook < V > [ 'resetModified' ] > ( ( paths , opts ) => {
195- const { forceUpdate = false } = opts ?? { }
200+ const { forceUpdate = forceUpdateOnStatusChange } = opts ?? { }
196201
197202 if ( paths ) {
198203 for ( let i = 0 ; i < paths . length ; i ++ ) {
@@ -209,12 +214,12 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
209214 modifiedFields : modifiedRef . current
210215 } ) )
211216 }
212- } , [ initialModified , mode , modifiedRef , setState ] )
217+ } , [ forceUpdateOnStatusChange , initialModified , mode , modifiedRef , setState ] )
213218
214219 const setModified = useCallback < UseFormStatusHook < V > [ 'setModified' ] > ( ( values , opts ) => {
215220 const {
216221 partial = false ,
217- forceUpdate = false
222+ forceUpdate = forceUpdateOnStatusChange
218223 } = opts ?? { }
219224
220225 // const previousModified = clone(modifiedRef.current)
@@ -232,7 +237,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
232237 modifiedFields : nextModified
233238 } ) )
234239 }
235- } , [ mode , modifiedRef , setState ] )
240+ } , [ forceUpdateOnStatusChange , mode , modifiedRef , setState ] )
236241
237242 const setModifiedField = useCallback < UseFormStatusHook < V > [ 'setModifiedField' ] > ( ( path , value ) => {
238243 setModified ( { [ path ] : value } , { partial : true } )
@@ -241,7 +246,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
241246 // TOUCHED
242247
243248 const clearTouched = useCallback < UseFormStatusHook < V > [ 'clearTouched' ] > ( ( paths , opts ) => {
244- const { forceUpdate = false } = opts ?? { }
249+ const { forceUpdate = forceUpdateOnStatusChange } = opts ?? { }
245250
246251 if ( paths ) {
247252 for ( let i = 0 ; i < paths . length ; i ++ ) {
@@ -258,7 +263,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
258263 touchedFields : touchedRef . current
259264 } ) )
260265 }
261- } , [ mode , setState , touchedRef ] )
266+ } , [ forceUpdateOnStatusChange , mode , setState , touchedRef ] )
262267
263268 const getTouched = useCallback < UseFormStatusHook < V > [ 'getTouched' ] > ( ( ) => {
264269 return touchedRef . current
@@ -272,7 +277,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
272277 } , [ touchedRef ] )
273278
274279 const resetTouched = useCallback < UseFormStatusHook < V > [ 'resetTouched' ] > ( ( paths , opts ) => {
275- const { forceUpdate = false } = opts ?? { }
280+ const { forceUpdate = forceUpdateOnStatusChange } = opts ?? { }
276281
277282 if ( paths ) {
278283 for ( let i = 0 ; i < paths . length ; i ++ ) {
@@ -289,12 +294,12 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
289294 touchedFields : touchedRef . current
290295 } ) )
291296 }
292- } , [ initialTouched , mode , setState , touchedRef ] )
297+ } , [ forceUpdateOnStatusChange , initialTouched , mode , setState , touchedRef ] )
293298
294299 const setTouched = useCallback < UseFormStatusHook < V > [ 'setTouched' ] > ( ( values , opts ) => {
295300 const {
296- partial = false ,
297- forceUpdate = false
301+ forceUpdate = forceUpdateOnStatusChange ,
302+ partial = false
298303 } = opts ?? { }
299304
300305 // const previousTouched = clone(touchedRef.current)
@@ -312,7 +317,7 @@ function useFormStatus<V extends Values, E, R> (options: UseFormStatusOptions<V,
312317 touchedFields : nextTouched
313318 } ) )
314319 }
315- } , [ mode , setState , touchedRef ] )
320+ } , [ forceUpdateOnStatusChange , mode , setState , touchedRef ] )
316321
317322 const setTouchedField = useCallback < UseFormStatusHook < V > [ 'setTouchedField' ] > ( ( path , value ) => {
318323 setTouched ( { [ path ] : value } , { partial : true } )
0 commit comments