11import {
22 CancelToken ,
3+ Errors ,
34 ErrorValue ,
45 FormDataErrors ,
56 FormDataKeys ,
67 FormDataType ,
78 FormDataValues ,
89 Method ,
910 Progress ,
11+ RequestPayload ,
1012 router ,
1113 UrlMethodPair ,
1214 VisitOptions ,
@@ -27,6 +29,8 @@ export type SetDataAction<TForm extends Record<any, any>> = SetDataByObject<TFor
2729 SetDataByKeyValuePair < TForm >
2830
2931type FormOptions = Omit < VisitOptions , 'data' >
32+ type SubmitArgs = [ Method , string , FormOptions ?] | [ UrlMethodPair , FormOptions ?]
33+ type TransformCallback < TForm > = ( data : TForm ) => object
3034
3135export interface InertiaFormProps < TForm extends object > {
3236 data : TForm
@@ -38,7 +42,7 @@ export interface InertiaFormProps<TForm extends object> {
3842 wasSuccessful : boolean
3943 recentlySuccessful : boolean
4044 setData : SetDataAction < TForm >
41- transform : ( callback : ( data : TForm ) => object ) => void
45+ transform : ( callback : TransformCallback < TForm > ) => void
4246 setDefaults ( ) : void
4347 setDefaults < T extends FormDataKeys < TForm > > ( field : T , value : FormDataValues < TForm , T > ) : void
4448 setDefaults ( fields : Partial < TForm > ) : void
@@ -47,7 +51,7 @@ export interface InertiaFormProps<TForm extends object> {
4751 resetAndClearErrors < K extends FormDataKeys < TForm > > ( ...fields : K [ ] ) : void
4852 setError < K extends FormDataKeys < TForm > > ( field : K , value : ErrorValue ) : void
4953 setError ( errors : FormDataErrors < TForm > ) : void
50- submit : ( ...args : [ Method , string , FormOptions ? ] | [ UrlMethodPair , FormOptions ? ] ) => void
54+ submit : ( ...args : SubmitArgs ) => void
5155 get : ( url : string , options ?: FormOptions ) => void
5256 patch : ( url : string , options ?: FormOptions ) => void
5357 post : ( url : string , options ?: FormOptions ) => void
@@ -66,23 +70,23 @@ export default function useForm<TForm extends FormDataType<TForm>>(
6670 rememberKeyOrInitialValues ?: string | TForm | ( ( ) => TForm ) ,
6771 maybeInitialValues ?: TForm | ( ( ) => TForm ) ,
6872) : InertiaFormProps < TForm > {
69- const isMounted = useRef ( null )
73+ const isMounted = useRef ( false )
7074 const rememberKey = typeof rememberKeyOrInitialValues === 'string' ? rememberKeyOrInitialValues : null
7175 const [ defaults , setDefaults ] = useState (
7276 ( typeof rememberKeyOrInitialValues === 'string' ? maybeInitialValues : rememberKeyOrInitialValues ) || ( { } as TForm ) ,
7377 )
7478 const cancelToken = useRef < CancelToken | null > ( null )
75- const recentlySuccessfulTimeoutId = useRef ( null )
79+ const recentlySuccessfulTimeoutId = useRef < number > ( undefined )
7680 const [ data , setData ] = rememberKey ? useRemember ( defaults , `${ rememberKey } :data` ) : useState ( defaults )
7781 const [ errors , setErrors ] = rememberKey
7882 ? useRemember ( { } as FormDataErrors < TForm > , `${ rememberKey } :errors` )
7983 : useState ( { } as FormDataErrors < TForm > )
8084 const [ hasErrors , setHasErrors ] = useState ( false )
8185 const [ processing , setProcessing ] = useState ( false )
82- const [ progress , setProgress ] = useState ( null )
86+ const [ progress , setProgress ] = useState < Progress | null > ( null )
8387 const [ wasSuccessful , setWasSuccessful ] = useState ( false )
8488 const [ recentlySuccessful , setRecentlySuccessful ] = useState ( false )
85- const transform = useRef ( ( data ) => data )
89+ const transform = useRef < TransformCallback < TForm > > ( ( data ) => data )
8690 const isDirty = useMemo ( ( ) => ! isEqual ( data , defaults ) , [ data , defaults ] )
8791
8892 useEffect ( ( ) => {
@@ -97,16 +101,16 @@ export default function useForm<TForm extends FormDataType<TForm>>(
97101 const setDefaultsCalledInOnSuccess = useRef ( false )
98102
99103 const submit = useCallback (
100- ( ...args ) => {
104+ ( ...args : SubmitArgs ) => {
101105 const objectPassed = args [ 0 ] !== null && typeof args [ 0 ] === 'object'
102106
103- const method = objectPassed ? args [ 0 ] . method : args [ 0 ]
104- const url = objectPassed ? args [ 0 ] . url : args [ 1 ]
107+ const method = objectPassed ? args [ 0 ] . method : ( args [ 0 ] as Method )
108+ const url = objectPassed ? args [ 0 ] . url : ( args [ 1 ] as string )
105109 const options = ( objectPassed ? args [ 1 ] : args [ 2 ] ) ?? { }
106110
107111 setDefaultsCalledInOnSuccess . current = false
108112
109- const _options = {
113+ const _options : VisitOptions = {
110114 ...options ,
111115 onCancelToken : ( token ) => {
112116 cancelToken . current = token
@@ -132,7 +136,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
132136 }
133137 } ,
134138 onProgress : ( event ) => {
135- setProgress ( event )
139+ setProgress ( event || null )
136140
137141 if ( options . onProgress ) {
138142 return options . onProgress ( event )
@@ -168,7 +172,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
168172 if ( isMounted . current ) {
169173 setProcessing ( false )
170174 setProgress ( null )
171- setErrors ( errors )
175+ setErrors ( errors as FormDataErrors < TForm > )
172176 setHasErrors ( true )
173177 }
174178
@@ -200,10 +204,12 @@ export default function useForm<TForm extends FormDataType<TForm>>(
200204 } ,
201205 }
202206
207+ const transformedData = transform . current ( data ) as RequestPayload
208+
203209 if ( method === 'delete' ) {
204- router . delete ( url , { ..._options , data : transform . current ( data ) } )
210+ router . delete ( url , { ..._options , data : transformedData } )
205211 } else {
206- router [ method ] ( url , transform . current ( data ) , _options )
212+ router [ method ] ( url , transformedData , _options )
207213 }
208214 } ,
209215 [ data , setErrors , transform ] ,
@@ -266,7 +272,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
266272 } , [ dataAsDefaults ] )
267273
268274 const reset = useCallback (
269- ( ...fields ) => {
275+ ( ...fields : string [ ] ) => {
270276 if ( fields . length === 0 ) {
271277 setData ( defaults )
272278 } else {
@@ -300,12 +306,12 @@ export default function useForm<TForm extends FormDataType<TForm>>(
300306 )
301307
302308 const clearErrors = useCallback (
303- ( ...fields ) => {
309+ ( ...fields : string [ ] ) => {
304310 setErrors ( ( errors ) => {
305311 const newErrors = Object . keys ( errors ) . reduce (
306312 ( carry , field ) => ( {
307313 ...carry ,
308- ...( fields . length > 0 && ! fields . includes ( field ) ? { [ field ] : errors [ field ] } : { } ) ,
314+ ...( fields . length > 0 && ! fields . includes ( field ) ? { [ field ] : ( errors as Errors ) [ field ] } : { } ) ,
309315 } ) ,
310316 { } ,
311317 )
@@ -317,16 +323,18 @@ export default function useForm<TForm extends FormDataType<TForm>>(
317323 )
318324
319325 const resetAndClearErrors = useCallback (
320- ( ...fields ) => {
326+ ( ...fields : string [ ] ) => {
321327 reset ( ...fields )
322328 clearErrors ( ...fields )
323329 } ,
324330 [ reset , clearErrors ] ,
325331 )
326332
327- const createSubmitMethod = ( method ) => ( url , options ) => {
328- submit ( method , url , options )
329- }
333+ const createSubmitMethod =
334+ ( method : Method ) =>
335+ ( url : string , options : VisitOptions = { } ) => {
336+ submit ( method , url , options )
337+ }
330338 const getMethod = useCallback ( createSubmitMethod ( 'get' ) , [ submit ] )
331339 const post = useCallback ( createSubmitMethod ( 'post' ) , [ submit ] )
332340 const put = useCallback ( createSubmitMethod ( 'put' ) , [ submit ] )
@@ -339,7 +347,7 @@ export default function useForm<TForm extends FormDataType<TForm>>(
339347 }
340348 } , [ ] )
341349
342- const transformFunction = useCallback ( ( callback ) => {
350+ const transformFunction = useCallback ( ( callback : TransformCallback < TForm > ) => {
343351 transform . current = callback
344352 } , [ ] )
345353
0 commit comments