From 4d875c03a27ce0e21bb6e3a12b0441172f6a3a71 Mon Sep 17 00:00:00 2001 From: Romain Hamel Date: Tue, 15 Apr 2025 12:48:10 +0200 Subject: [PATCH] chore: up --- .../examples/form/FormExampleElements.vue | 7 ++++--- src/runtime/components/Form.vue | 9 +++++---- src/runtime/types/form.ts | 16 ++++++++-------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/docs/app/components/content/examples/form/FormExampleElements.vue b/docs/app/components/content/examples/form/FormExampleElements.vue index a841fce1..37253add 100644 --- a/docs/app/components/content/examples/form/FormExampleElements.vue +++ b/docs/app/components/content/examples/form/FormExampleElements.vue @@ -34,9 +34,10 @@ const schema = z.object({ pin: z.string().regex(/^\d$/).array().length(5) }) -type Schema = z.input +type Input = z.input +type Output = z.output -const state = reactive>({}) +const state = reactive>({}) const form = useTemplateRef('form') @@ -47,7 +48,7 @@ const items = [ ] const toast = useToast() -async function onSubmit(event: FormSubmitEvent) { +async function onSubmit(event: FormSubmitEvent) { toast.add({ title: 'Success', description: 'The form has been submitted.', color: 'success' }) console.log(event.data) } diff --git a/src/runtime/components/Form.vue b/src/runtime/components/Form.vue index 1ba25abd..f30f6ab0 100644 --- a/src/runtime/components/Form.vue +++ b/src/runtime/components/Form.vue @@ -11,8 +11,9 @@ export interface FormProps { id?: string | number /** Schema to validate the form state. Supports Standard Schema objects, Yup, Joi, and Superstructs. */ schema?: FormSchema + /** An object representing the current state of the form. */ - state: Partial | O + state: Partial /** * Custom validation function to validate the form state. * @param state - The current state of the form. @@ -172,7 +173,7 @@ async function getErrors(): Promise { return resolveErrorIds(errs) } -async function _validate(opts: { name?: keyof I | (keyof I)[], silent?: boolean, nested?: boolean, transform?: boolean } = { silent: false, nested: true, transform: false }): Promise { +async function _validate(opts: { name?: keyof I | (keyof I)[], silent?: boolean, nested?: boolean, transform?: boolean } = { silent: false, nested: true, transform: false }): Promise { const names = opts.name && !Array.isArray(opts.name) ? [opts.name] : opts.name as (keyof I)[] const nestedValidatePromises = !names && opts.nested @@ -213,7 +214,7 @@ async function _validate(opts: { name?: keyof I | (keyof I)[], silent?: boolean, Object.assign(props.state, transformedState.value) } - return props.state as I + return props.state as O } const loading = ref(false) @@ -251,7 +252,7 @@ provide(formOptionsInjectionKey, computed(() => ({ validateOnInputDelay: props.validateOnInputDelay }))) -defineExpose>({ +defineExpose>({ validate: _validate, errors, diff --git a/src/runtime/types/form.ts b/src/runtime/types/form.ts index 56cda23f..c1270d0d 100644 --- a/src/runtime/types/form.ts +++ b/src/runtime/types/form.ts @@ -5,26 +5,26 @@ import type { ObjectSchema as YupObjectSchema } from 'yup' import type { GetObjectField } from './utils' import type { Struct as SuperstructSchema } from 'superstruct' -export interface Form { - validate (opts?: { name?: keyof T | (keyof T)[], silent?: boolean, nested?: boolean, transform?: boolean }): Promise +export interface Form { + validate (opts?: { name?: keyof I | (keyof I)[], silent?: boolean, nested?: boolean, transform?: boolean }): Promise clear (path?: string): void errors: Ref - setErrors (errs: FormError[], name?: keyof T): void - getErrors (name?: keyof T): FormError[] + setErrors (errs: FormError[], name?: keyof I): void + getErrors (name?: keyof I): FormError[] submit (): Promise disabled: ComputedRef dirty: ComputedRef loading: Ref - dirtyFields: DeepReadonly> - touchedFields: DeepReadonly> - blurredFields: DeepReadonly> + dirtyFields: DeepReadonly> + touchedFields: DeepReadonly> + blurredFields: DeepReadonly> } export type FormSchema = | YupObjectSchema | JoiSchema - | SuperstructSchema + | SuperstructSchema | StandardSchemaV1 export type FormInputEvents = 'input' | 'blur' | 'change' | 'focus'