mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 15:01:46 +01:00
fix(FormField): added a utility type to fix some type errors (#81)
Co-authored-by: Mauro Erta <mauro.erta@sap.com> Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import { inject, ref, computed } from 'vue'
|
||||
import { type UseEventBusReturn, useDebounceFn } from '@vueuse/core'
|
||||
import type { FormEvent, FormInputEvents, FormFieldInjectedOptions, FormInjectedOptions } from '#ui/types/form'
|
||||
import type { GetObjectField } from '#ui/types/utils'
|
||||
|
||||
type Props<T> = {
|
||||
id?: string
|
||||
name?: string
|
||||
// @ts-expect-error FIXME: TS doesn't like this
|
||||
size?: T['size']
|
||||
// @ts-expect-error FIXME: TS doesn't like this
|
||||
color?: T['color']
|
||||
size?: GetObjectField<T, 'size'>
|
||||
color?: GetObjectField<T, 'color'>
|
||||
eagerValidation?: boolean
|
||||
legend?: string
|
||||
disabled?: boolean
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ZodSchema } from 'zod'
|
||||
import type { Schema as JoiSchema } from 'joi'
|
||||
import type { ObjectSchema as YupObjectSchema } from 'yup'
|
||||
import type { ObjectSchemaAsync as ValibotObjectSchema } from 'valibot'
|
||||
import type { GetObjectField } from './utils'
|
||||
|
||||
export interface Form<T> {
|
||||
validate (opts?: { name: string | string[], silent?: false, nested?: boolean }): Promise<T | false>
|
||||
@@ -71,8 +72,7 @@ export interface FormInjectedOptions {
|
||||
export interface FormFieldInjectedOptions<T> {
|
||||
id: Ref<string | undefined>
|
||||
name: ComputedRef<string | undefined>
|
||||
// @ts-expect-error FIXME: TS doesn't like this
|
||||
size: ComputedRef<T['size']>
|
||||
size: ComputedRef<GetObjectField<T, 'size'>>
|
||||
error: ComputedRef<string | boolean | undefined>
|
||||
eagerValidation: ComputedRef<boolean | undefined>
|
||||
validateOnInputDelay: ComputedRef<number | undefined>
|
||||
|
||||
4
src/runtime/types/utils.d.ts
vendored
4
src/runtime/types/utils.d.ts
vendored
@@ -4,3 +4,7 @@ export type DeepPartial<T> = Partial<{
|
||||
|
||||
export type DynamicSlots<T extends { slot?: string }, SlotProps, Slot = T['slot']> =
|
||||
Record<string, SlotProps> & (Slot extends string ? Record<Slot, SlotProps> : Record<string, never>)
|
||||
|
||||
export type GetObjectField<MaybeObject, Key extends string> = MaybeObject extends Record<string, any>
|
||||
? MaybeObject[Key]
|
||||
: never
|
||||
|
||||
Reference in New Issue
Block a user