laravel-precognition-react-inertia
Advanced tools
+25
-12
| import { toSimpleValidationErrors } from 'laravel-precognition'; | ||
| import { useForm as usePrecognitiveForm } from 'laravel-precognition-react'; | ||
| import { useForm as useInertiaForm } from '@inertiajs/react'; | ||
| import { useRef } from 'react'; | ||
| export const useForm = (method, url, inputs, config = {}) => { | ||
| // @ts-expect-error | ||
| method = method.toLowerCase(); | ||
| const booted = useRef(false); | ||
| /** | ||
@@ -14,11 +18,2 @@ * The Inertia form. | ||
| /** | ||
| * Setup event listeners. | ||
| */ | ||
| precognitiveForm.validator().on('errorsChanged', () => { | ||
| inertiaClearErrors(); | ||
| inertiaSetError( | ||
| // @ts-expect-error | ||
| toSimpleValidationErrors(precognitiveForm.validator().errors())); | ||
| }); | ||
| /** | ||
| * The Inertia submit function. | ||
@@ -43,2 +38,14 @@ */ | ||
| const inertiaSetData = inertiaForm.setData.bind(inertiaForm); | ||
| if (!booted.current) { | ||
| /** | ||
| * Setup event listeners. | ||
| */ | ||
| precognitiveForm.validator().on('errorsChanged', () => { | ||
| inertiaClearErrors(); | ||
| inertiaSetError( | ||
| // @ts-expect-error | ||
| toSimpleValidationErrors(precognitiveForm.validator().errors())); | ||
| }); | ||
| booted.current = true; | ||
| } | ||
| /** | ||
@@ -58,5 +65,10 @@ * Patch the form. | ||
| }, | ||
| clearErrors() { | ||
| inertiaClearErrors(); | ||
| precognitiveForm.setErrors({}); | ||
| clearErrors(...names) { | ||
| inertiaClearErrors(...names); | ||
| if (names.length === 0) { | ||
| precognitiveForm.setErrors({}); | ||
| } | ||
| else { | ||
| names.forEach(precognitiveForm.forgetError); | ||
| } | ||
| return this; | ||
@@ -117,3 +129,4 @@ }, | ||
| }, | ||
| validator: precognitiveForm.validator, | ||
| }); | ||
| }; |
+3
-2
| { | ||
| "name": "laravel-precognition-react-inertia", | ||
| "version": "0.2.2", | ||
| "version": "0.3.0", | ||
| "description": "Laravel Precognition (React w/ Inertia).", | ||
@@ -31,3 +31,4 @@ "keywords": [ | ||
| "peerDependencies": { | ||
| "@inertiajs/react": "^1.0.0" | ||
| "@inertiajs/react": "^1.0.0", | ||
| "react": "^18.0.0" | ||
| }, | ||
@@ -34,0 +35,0 @@ "dependencies": { |
8305
5.94%130
11.11%4
33.33%