|
| 1 | +import { 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' |
| 4 | + |
| 5 | +export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod, url: string, inputs: Data, config: ValidationConfig = {}): any => { |
| 6 | + /** |
| 7 | + * The Inertia form. |
| 8 | + */ |
| 9 | + const inertiaForm = useInertiaForm(inputs) |
| 10 | + |
| 11 | + /** |
| 12 | + * The Vue form. |
| 13 | + */ |
| 14 | + const vueForm = useVueForm(method, url, inputs, config) |
| 15 | + |
| 16 | + /** |
| 17 | + * Setup event listeners. |
| 18 | + */ |
| 19 | + vueForm.validator().on('errorsChanged', () => { |
| 20 | + inertiaClearErrors() |
| 21 | + |
| 22 | + inertiaSetError( |
| 23 | + // @ts-expect-error |
| 24 | + toSimpleValidationErrors(vueForm.validator().errors()) |
| 25 | + ) |
| 26 | + }) |
| 27 | + |
| 28 | + /** |
| 29 | + * The Inertia submit function. |
| 30 | + */ |
| 31 | + const inertiaSubmit = inertiaForm.submit.bind(inertiaForm) |
| 32 | + |
| 33 | + /** |
| 34 | + * The Inertia reset function. |
| 35 | + */ |
| 36 | + const inertiaReset = inertiaForm.reset.bind(inertiaForm) |
| 37 | + |
| 38 | + /** |
| 39 | + * The Inertia clear errors function. |
| 40 | + */ |
| 41 | + const inertiaClearErrors = inertiaForm.clearErrors.bind(inertiaForm) |
| 42 | + |
| 43 | + /** |
| 44 | + * The Inertia set error function. |
| 45 | + */ |
| 46 | + const inertiaSetError = inertiaForm.setError.bind(inertiaForm) |
| 47 | + |
| 48 | + /** |
| 49 | + * Patch the form. |
| 50 | + */ |
| 51 | + 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 | + clearErrors() { |
| 58 | + inertiaClearErrors() |
| 59 | + |
| 60 | + vueForm.setErrors({}) |
| 61 | + |
| 62 | + return this |
| 63 | + }, |
| 64 | + reset(...names: string[]) { |
| 65 | + inertiaReset(...names) |
| 66 | + |
| 67 | + vueForm.reset(...names) |
| 68 | + }, |
| 69 | + setErrors(errors: SimpleValidationErrors|ValidationErrors) { |
| 70 | + // @ts-expect-error |
| 71 | + vueForm.setErrors(errors) |
| 72 | + |
| 73 | + return this |
| 74 | + }, |
| 75 | + setError(key: any, value?: any) { |
| 76 | + this.setErrors({ |
| 77 | + ...inertiaForm.errors, |
| 78 | + ...typeof value === 'undefined' |
| 79 | + ? key |
| 80 | + : { [key]: value } |
| 81 | + }) |
| 82 | + |
| 83 | + return this |
| 84 | + }, |
| 85 | + validate(name: string|NamedInputEvent) { |
| 86 | + vueForm.setData(inertiaForm.data()) |
| 87 | + |
| 88 | + vueForm.validate(name) |
| 89 | + |
| 90 | + return this |
| 91 | + }, |
| 92 | + setValidationTimeout(duration: number) { |
| 93 | + vueForm.setValidationTimeout(duration) |
| 94 | + |
| 95 | + return this |
| 96 | + }, |
| 97 | + submit(submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void { |
| 98 | + const isPatchedCall = typeof submitMethod !== 'string' |
| 99 | + |
| 100 | + const userOptions = isPatchedCall |
| 101 | + ? submitMethod |
| 102 | + : submitOptions |
| 103 | + |
| 104 | + const options = { |
| 105 | + ...userOptions, |
| 106 | + onError: (errors: SimpleValidationErrors): any => { |
| 107 | + vueForm.validator().setErrors(errors) |
| 108 | + |
| 109 | + if (userOptions.onError) { |
| 110 | + return userOptions.onError(errors) |
| 111 | + } |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + inertiaSubmit( |
| 116 | + isPatchedCall ? method : submitMethod, |
| 117 | + // @ts-expect-error |
| 118 | + (isPatchedCall ? url : submitUrl), |
| 119 | + options |
| 120 | + ) |
| 121 | + }, |
| 122 | + }) |
| 123 | +} |
0 commit comments