diff --git a/packages/vue-inertia/src/index.ts b/packages/vue-inertia/src/index.ts index ef05440..1b25bf2 100644 --- a/packages/vue-inertia/src/index.ts +++ b/packages/vue-inertia/src/index.ts @@ -1,11 +1,42 @@ -import { Config, NamedInputEvent, RequestMethod, SimpleValidationErrors, toSimpleValidationErrors, ValidationConfig, ValidationErrors, resolveUrl, resolveMethod } from 'laravel-precognition' +import { + Config, + NamedInputEvent, + RequestMethod, + SimpleValidationErrors, + toSimpleValidationErrors, + ValidationConfig, + ValidationErrors, + resolveUrl, + resolveMethod, + Validator, +} from 'laravel-precognition' import { useForm as usePrecognitiveForm, client } from 'laravel-precognition-vue' -import { useForm as useInertiaForm } from '@inertiajs/vue3' +import { InertiaForm, useForm as useInertiaForm } from '@inertiajs/vue3' import { watchEffect } from 'vue' export { client } -export const useForm = >(method: RequestMethod|(() => RequestMethod), url: string|(() => string), inputs: Data, config: ValidationConfig = {}): any => { +type Precognitive = { + validating: boolean + touched: (name: keyof Data) => boolean + valid: (name: keyof Data) => boolean + invalid: (name: keyof Data) => boolean + clearErrors(...names: string[]): Form + reset(...names: string[]): void + setErrors(errors: SimpleValidationErrors | ValidationErrors): Form + forgetError(name: string | NamedInputEvent): Form + setError(key: any, value?: any): Form + transform(callback: (data: Data) => Record): Form + validate(name?: string | NamedInputEvent): Form + setValidationTimeout(duration: number): Form + validateFiles(): Form + submit(submitMethod: RequestMethod | Config, submitUrl?: string, submitOptions?: any): void + validator(): Validator +}; + +export type Form = InertiaForm & Precognitive + +export const useForm = >(method: RequestMethod|(() => RequestMethod), url: string|(() => string), inputs: Data, config: ValidationConfig = {}): Form => { /** * The Inertia form. */ @@ -61,7 +92,7 @@ export const useForm = >(method: RequestMet /** * Patch the form. */ - const form = Object.assign(inertiaForm, { + const form: Form = Object.assign(inertiaForm, { validating: precognitiveForm.validating, touched: precognitiveForm.touched, touch(name: Array|string|NamedInputEvent) { @@ -173,7 +204,7 @@ export const useForm = >(method: RequestMet }) }, validator: precognitiveForm.validator, - }) + } as Precognitive) // Due to the nature of `reactive` elements, reactivity is not inherited by // the patched Inertia form as we have to destructure the Precog form. We