11import { Config , NamedInputEvent , RequestMethod , SimpleValidationErrors , toSimpleValidationErrors , ValidationConfig , ValidationErrors } from 'laravel-precognition'
2- import { useForm as useVueForm } from 'laravel-precognition-vue '
3- import { useForm as useInertiaForm } from '@inertiajs/vue3 '
2+ import { useForm as useReactForm } from 'laravel-precognition-react '
3+ import { useForm as useInertiaForm } from '@inertiajs/react '
44
55export const useForm = < Data extends Record < string , unknown > > ( method : RequestMethod , url : string , inputs : Data , config : ValidationConfig = { } ) : any => {
66 /**
@@ -9,19 +9,19 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
99 const inertiaForm = useInertiaForm ( inputs )
1010
1111 /**
12- * The Vue form.
12+ * The React form.
1313 */
14- const vueForm = useVueForm ( method , url , inputs , config )
14+ const precognitiveForm = useReactForm ( method , url , inputs , config )
1515
1616 /**
1717 * Setup event listeners.
1818 */
19- vueForm . validator ( ) . on ( 'errorsChanged' , ( ) => {
19+ precognitiveForm . validator ( ) . on ( 'errorsChanged' , ( ) => {
2020 inertiaClearErrors ( )
2121
2222 inertiaSetError (
2323 // @ts -expect-error
24- toSimpleValidationErrors ( vueForm . validator ( ) . errors ( ) )
24+ toSimpleValidationErrors ( precognitiveForm . validator ( ) . errors ( ) )
2525 )
2626 } )
2727
@@ -45,30 +45,42 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
4545 */
4646 const inertiaSetError = inertiaForm . setError . bind ( inertiaForm )
4747
48+ /**
49+ * The Inertia set data function.
50+ */
51+ const inertiaSetData = inertiaForm . setData . bind ( inertiaForm )
52+
4853 /**
4954 * Patch the form.
5055 */
5156 return Object . assign ( inertiaForm , {
52- processing : vueForm . processing ,
53- validating : vueForm . validating ,
54- touched : vueForm . touched ,
55- valid : vueForm . valid ,
56- invalid : vueForm . invalid ,
57+ processing : precognitiveForm . processing ,
58+ validating : precognitiveForm . validating ,
59+ touched : precognitiveForm . touched ,
60+ valid : precognitiveForm . valid ,
61+ invalid : precognitiveForm . invalid ,
62+ setData ( key : any , value ?: any ) {
63+ inertiaSetData ( key , value )
64+
65+ precognitiveForm . setData ( key , value )
66+
67+ return this
68+ } ,
5769 clearErrors ( ) {
5870 inertiaClearErrors ( )
5971
60- vueForm . setErrors ( { } )
72+ precognitiveForm . setErrors ( { } )
6173
6274 return this
6375 } ,
6476 reset ( ...names : string [ ] ) {
6577 inertiaReset ( ...names )
6678
67- vueForm . reset ( ...names )
79+ precognitiveForm . reset ( ...names )
6880 } ,
6981 setErrors ( errors : SimpleValidationErrors | ValidationErrors ) {
7082 // @ts -expect-error
71- vueForm . setErrors ( errors )
83+ precognitiveForm . setErrors ( errors )
7284
7385 return this
7486 } ,
@@ -83,14 +95,14 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
8395 return this
8496 } ,
8597 validate ( name : string | NamedInputEvent ) {
86- vueForm . setData ( inertiaForm . data ( ) )
98+ precognitiveForm . setData ( inertiaForm . data )
8799
88- vueForm . validate ( name )
100+ precognitiveForm . validate ( name )
89101
90102 return this
91103 } ,
92104 setValidationTimeout ( duration : number ) {
93- vueForm . setValidationTimeout ( duration )
105+ precognitiveForm . setValidationTimeout ( duration )
94106
95107 return this
96108 } ,
@@ -104,7 +116,7 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet
104116 const options = {
105117 ...userOptions ,
106118 onError : ( errors : SimpleValidationErrors ) : any => {
107- vueForm . validator ( ) . setErrors ( errors )
119+ precognitiveForm . validator ( ) . setErrors ( errors )
108120
109121 if ( userOptions . onError ) {
110122 return userOptions . onError ( errors )
0 commit comments