|
1 | | -import { Config, NamedInputEvent, RequestMethod, SimpleValidationErrors, toSimpleValidationErrors, ValidationConfig, ValidationErrors, resolveUrl, resolveMethod } from 'laravel-precognition' |
| 1 | +import { NamedInputEvent, RequestMethod, SimpleValidationErrors, toSimpleValidationErrors, ValidationConfig, ValidationErrors, resolveUrl, resolveMethod } from 'laravel-precognition' |
2 | 2 | import { useForm as usePrecognitiveForm, client } from 'laravel-precognition-vue' |
3 | 3 | import { useForm as useInertiaForm } from '@inertiajs/vue3' |
| 4 | +import { VisitOptions } from '@inertiajs/core' |
4 | 5 | import { watchEffect } from 'vue' |
5 | 6 | import { Form } from './types' |
6 | 7 |
|
@@ -107,12 +108,22 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet |
107 | 108 |
|
108 | 109 | return form |
109 | 110 | }, |
110 | | - setError(key: any, value?: any) { |
| 111 | + setError(key: (keyof Data)|Record<keyof Data, string>, value?: string) { |
| 112 | + let errors: SimpleValidationErrors |
| 113 | + |
| 114 | + if (typeof key !== 'object') { |
| 115 | + if (typeof value === 'undefined') { |
| 116 | + throw new Error('The `value` is required.') |
| 117 | + } |
| 118 | + |
| 119 | + errors = { key: value } |
| 120 | + } else { |
| 121 | + errors = key |
| 122 | + } |
| 123 | + |
111 | 124 | form.setErrors({ |
112 | 125 | ...inertiaForm.errors, |
113 | | - ...typeof value === 'undefined' |
114 | | - ? key |
115 | | - : { [key]: value }, |
| 126 | + ...errors, |
116 | 127 | }) |
117 | 128 |
|
118 | 129 | return form |
@@ -155,28 +166,20 @@ export const useForm = <Data extends Record<string, unknown>>(method: RequestMet |
155 | 166 |
|
156 | 167 | return form |
157 | 168 | }, |
158 | | - submit(submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void { |
159 | | - const isPatchedCall = typeof submitMethod !== 'string' |
160 | | - |
161 | | - submitOptions = isPatchedCall |
162 | | - ? submitMethod |
163 | | - : submitOptions |
164 | | - |
165 | | - submitUrl = isPatchedCall |
166 | | - ? resolveUrl(url) |
167 | | - : submitUrl! |
168 | | - |
169 | | - submitMethod = isPatchedCall |
170 | | - ? resolveMethod(method) |
171 | | - : submitMethod as RequestMethod |
| 169 | + submit(submitMethod: RequestMethod|Partial<VisitOptions> = {}, submitUrl?: string, submitOptions?: Partial<VisitOptions>): void { |
| 170 | + if (typeof submitMethod !== 'string') { |
| 171 | + submitOptions = submitMethod |
| 172 | + submitUrl = resolveUrl(url) |
| 173 | + submitMethod = resolveMethod(method) |
| 174 | + } |
172 | 175 |
|
173 | | - inertiaSubmit(submitMethod, submitUrl, { |
| 176 | + inertiaSubmit(submitMethod, submitUrl!, { |
174 | 177 | ...submitOptions, |
175 | | - onError: (errors: SimpleValidationErrors): any => { |
| 178 | + onError: (errors: SimpleValidationErrors): void => { |
176 | 179 | precognitiveForm.validator().setErrors(errors) |
177 | 180 |
|
178 | | - if (submitOptions.onError) { |
179 | | - return submitOptions.onError(errors) |
| 181 | + if (submitOptions!.onError) { |
| 182 | + return submitOptions!.onError(errors) |
180 | 183 | } |
181 | 184 | }, |
182 | 185 | }) |
|
0 commit comments