feat: more props for form field

This commit is contained in:
zernonia 2024-04-18 15:10:01 +08:00
parent 49ad6c85e4
commit 318d4c8643
11 changed files with 29 additions and 29 deletions

View File

@ -91,9 +91,9 @@ const schema = z.object({
subSubObject: z subSubObject: z
.object({ .object({
subSubField: z.string().default('Sub Sub Field'), subSubField: z.string().default('Sub Sub Field'),
})
.describe('Sub Sub Object Description'),
}), }),
}).describe('123 Description'),
optionalSubObject: z optionalSubObject: z
.object({ .object({
optionalSubField: z.string(), optionalSubField: z.string(),

View File

@ -1,8 +1,8 @@
<script setup lang="ts" generic="U extends ZodRawShape, T extends ZodObject<U>"> <script setup lang="ts" generic="U extends ZodRawShape, T extends ZodObject<U>">
import { computed } from 'vue' import { computed } from 'vue'
import type { ZodAny, ZodObject, ZodRawShape, z } from 'zod' import type { ZodAny, ZodObject, ZodRawShape, z } from 'zod'
import { getBaseType, getDefaultValueInZodStack } from './utils' import { getBaseSchema, getBaseType, getDefaultValueInZodStack } from './utils'
import type { Config, ConfigItem } from './interface' import type { Config, ConfigItem, Shape } from './interface'
import AutoFormField from './AutoFormField.vue' import AutoFormField from './AutoFormField.vue'
const props = defineProps<{ const props = defineProps<{
@ -12,15 +12,7 @@ const props = defineProps<{
const shapes = computed(() => { const shapes = computed(() => {
// @ts-expect-error ignore {} not assignable to object // @ts-expect-error ignore {} not assignable to object
const val: { const val: { [key in keyof T]: Shape } = {}
[key in keyof T]: {
type: string
default: any
required?: boolean
options?: string[]
schema?: ZodAny
}
} = {}
const shape = props.schema.shape const shape = props.schema.shape
Object.keys(shape).forEach((name) => { Object.keys(shape).forEach((name) => {
const item = shape[name] as ZodAny const item = shape[name] as ZodAny

View File

@ -1,16 +1,11 @@
<script setup lang="ts" generic="U extends ZodAny"> <script setup lang="ts" generic="U extends ZodAny">
import type { ZodAny } from 'zod' import type { ZodAny } from 'zod'
import type { Config, ConfigItem } from './interface' import type { Config, ConfigItem, Shape } from './interface'
import { DEFAULT_ZOD_HANDLERS, INPUT_COMPONENTS } from './constant' import { DEFAULT_ZOD_HANDLERS, INPUT_COMPONENTS } from './constant'
defineProps<{ defineProps<{
name: string name: string
shape: { shape: Shape
type: string
required?: boolean
options?: any[]
schema?: ZodAny
}
config?: ConfigItem | Config<U> config?: ConfigItem | Config<U>
}>() }>()

View File

@ -21,7 +21,7 @@ defineProps<{
<Switch v-if="config?.component === 'switch'" v-bind="{ ...componentField }" /> <Switch v-if="config?.component === 'switch'" v-bind="{ ...componentField }" />
<Checkbox v-else v-bind="{ ...componentField }" /> <Checkbox v-else v-bind="{ ...componentField }" />
</FormControl> </FormControl>
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
</div> </div>

View File

@ -25,7 +25,7 @@ const df = new DateFormatter('en-US', {
<template> <template>
<FormField v-slot="{ componentField }" :name="name"> <FormField v-slot="{ componentField }" :name="name">
<FormItem> <FormItem>
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
<FormControl> <FormControl>

View File

@ -21,7 +21,7 @@ const computedOptions = computed(() => props.config?.enumProps?.options || props
<template> <template>
<FormField v-slot="{ componentField }" :name="name"> <FormField v-slot="{ componentField }" :name="name">
<FormItem> <FormItem>
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
<FormControl> <FormControl>

View File

@ -30,7 +30,7 @@ async function parseFileAsString(file: File | undefined): Promise<string> {
<template> <template>
<FormField v-slot="{ componentField }" :name="name"> <FormField v-slot="{ componentField }" :name="name">
<FormItem v-bind="$attrs"> <FormItem v-bind="$attrs">
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
<FormControl> <FormControl>

View File

@ -16,7 +16,7 @@ defineProps<{
<template> <template>
<FormField v-slot="{ componentField }" :name="name"> <FormField v-slot="{ componentField }" :name="name">
<FormItem v-bind="$attrs"> <FormItem v-bind="$attrs">
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
<FormControl> <FormControl>

View File

@ -19,7 +19,7 @@ defineProps<{
<template> <template>
<FormField v-slot="{ componentField }" :name="name"> <FormField v-slot="{ componentField }" :name="name">
<FormItem> <FormItem>
<AutoFormLabel :required="required"> <AutoFormLabel v-if="!config?.hideLabel" :required="required">
{{ config?.label || beautifyObjectName(name) }} {{ config?.label || beautifyObjectName(name) }}
</AutoFormLabel> </AutoFormLabel>
<FormControl> <FormControl>

View File

@ -52,7 +52,7 @@ const shapes = computed(() => {
<Accordion type="multiple" class="w-full" collapsible> <Accordion type="multiple" class="w-full" collapsible>
<AccordionItem :value="name" class="border-none"> <AccordionItem :value="name" class="border-none">
<AccordionTrigger class="text-base"> <AccordionTrigger class="text-base">
{{ beautifyObjectName(name) }} {{ schema?.description || beautifyObjectName(name) }}
</AccordionTrigger> </AccordionTrigger>
<AccordionContent class="p-2 space-y-5"> <AccordionContent class="p-2 space-y-5">
<template v-for="(shape, key) in shapes" :key="key"> <template v-for="(shape, key) in shapes" :key="key">

View File

@ -1,11 +1,24 @@
import type { InputHTMLAttributes, SelectHTMLAttributes } from 'vue' import type { InputHTMLAttributes, SelectHTMLAttributes } from 'vue'
import type { z } from 'zod' import type { ZodAny, z } from 'zod'
import type { INPUT_COMPONENTS } from './constant' import type { INPUT_COMPONENTS } from './constant'
export interface Shape {
type: string
default: any
required?: boolean
options?: string[]
schema?: ZodAny
}
export interface ConfigItem { export interface ConfigItem {
/** Value for the `FormLabel` */
label?: string label?: string
/** Value for the `FormDescription` */
description?: string description?: string
/** Pick which component to be rendered. */
component?: keyof typeof INPUT_COMPONENTS component?: keyof typeof INPUT_COMPONENTS
/** Hide `FormLabel`. */
hideLabel?: boolean
inputProps?: InputHTMLAttributes inputProps?: InputHTMLAttributes
enumProps?: SelectHTMLAttributes & { options?: any[] } enumProps?: SelectHTMLAttributes & { options?: any[] }
} }