diff --git a/src/runtime/components/Form.vue b/src/runtime/components/Form.vue index f12a33fa..c5f4d5dc 100644 --- a/src/runtime/components/Form.vue +++ b/src/runtime/components/Form.vue @@ -2,23 +2,23 @@ import type { DeepReadonly } from 'vue' import type { AppConfig } from '@nuxt/schema' import theme from '#build/ui/form' -import type { FormSchema, FormError, FormInputEvents, FormErrorEvent, FormSubmitEvent, FormEvent, Form, FormErrorWithId } from '../types/form' +import type { FormSchema, FormError, FormInputEvents, FormErrorEvent, FormSubmitEvent, FormEvent, Form, FormErrorWithId, InferInput, InferOutput } from '../types/form' import type { ComponentConfig } from '../types/utils' type FormConfig = ComponentConfig -export interface FormProps { +export interface FormProps { id?: string | number /** Schema to validate the form state. Supports Standard Schema objects, Yup, Joi, and Superstructs. */ - schema?: FormSchema + schema?: S /** An object representing the current state of the form. */ - state: Partial + state: Partial> /** * Custom validation function to validate the form state. * @param state - The current state of the form. * @returns A promise that resolves to an array of FormError objects, or an array of FormError objects directly. */ - validate?: (state: Partial) => Promise | FormError[] + validate?: (state: Partial>) => Promise | FormError[] /** * The list of input events that trigger the form validation. * @defaultValue `['blur', 'change', 'input']` @@ -50,11 +50,11 @@ export interface FormProps { */ loadingAuto?: boolean class?: any - onSubmit?: ((event: FormSubmitEvent) => void | Promise) | (() => void | Promise) + onSubmit?: ((event: FormSubmitEvent>) => void | Promise) | (() => void | Promise) } -export interface FormEmits { - (e: 'submit', payload: FormSubmitEvent): void +export interface FormEmits { + (e: 'submit', payload: FormSubmitEvent>): void (e: 'error', payload: FormErrorEvent): void } @@ -63,7 +63,7 @@ export interface FormSlots { } - diff --git a/src/runtime/types/form.ts b/src/runtime/types/form.ts index 351b6df6..4ca43173 100644 --- a/src/runtime/types/form.ts +++ b/src/runtime/types/form.ts @@ -21,11 +21,26 @@ export interface Form { blurredFields: DeepReadonly> } -export type FormSchema = - | YupObjectSchema - | JoiSchema +export type FormSchema = + | YupObjectSchema + | JoiSchema | SuperstructSchema - | StandardSchemaV1 + | StandardSchemaV1 + +// Define a utility type to infer the input type based on the schema type +export type InferInput = Schema extends StandardSchemaV1 ? StandardSchemaV1.InferInput + : Schema extends YupObjectSchema ? I + : Schema extends JoiSchema ? I + : Schema extends SuperstructSchema ? I + : Schema extends StandardSchemaV1 ? StandardSchemaV1.InferInput + : never + +// Define a utility type to infer the output type based on the schema type +export type InferOutput = Schema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput + : Schema extends YupObjectSchema ? O + : Schema extends JoiSchema ? O + : Schema extends SuperstructSchema ? O + : never export type FormInputEvents = 'input' | 'blur' | 'change' | 'focus'