String 字符串
Input
基础输入框,通过 controlProps 配置图标、颜色等属性:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
text: afz.string({
controlProps: {
icon: 'i-lucide-text'
}
})
.min(3, '至少 3 个字符')
.max(20, '最多 20 个字符')
.meta({
label: '基础输入框',
placeholder: '请输入文本',
hint: '支持图标、颜色等配置'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Textarea
多行文本输入,设置 type: 'textarea',支持自动调整高度:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
description: afz.string({
type: 'textarea',
controlProps: {
maxrows: 2,
autoresize: true
}
})
.min(10, '至少 10 个字符')
.meta({
label: '文本域',
placeholder: '请输入多行文本...',
hint: '这是一个长文本,将自动调整高度,最多 2 行。'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
PinInput
验证码或 PIN 码输入,设置 type: 'pinInput':
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
code: afz.string({
type: 'pinInput',
controlProps: {
length: 6,
mask: false
}
})
.length(6, '请输入 6 位验证码')
.meta({
label: '验证码输入',
hint: '输入 6 位验证码'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
InputMenu
支持输入和下拉选择,设置 type: 'inputMenu':
<script lang="ts" setup>
import type { FormSubmitEvent, InputMenuItem } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const items = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Orange', value: 'orange' },
{ label: 'Grape', value: 'grape' }
] satisfies InputMenuItem[]
const schema = afz.object({
fruit: afz.string({
type: 'inputMenu',
controlProps: {
items,
valueKey: 'value'
}
})
.meta({
label: '输入菜单',
placeholder: '输入或选择水果',
hint: '支持输入和下拉选择'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
动态类型
通过交互式示例切换输入框与文本域,并调整尺寸、颜色、行数、最大长度等配置。
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type z from 'zod'
const props = defineProps<{
type: 'string' | 'textarea'
size: 'sm' | 'xs' | 'md' | 'lg' | 'xl'
color: 'error' | 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'neutral'
rows?: number
maxLength?: number
}>()
const { afz } = useAutoForm()
const toast = useToast()
const resolvedControlProps = computed(() => ({
leadingIcon: 'i-lucide-type',
color: props.color,
rows: props.rows,
maxlength: props.maxLength
}))
const schema = computed(() => (afz.object({
stringWithType: afz.string({
type: props.type,
controlProps: resolvedControlProps.value
}).meta({
size: props.size
}).optional()
})))
type Schema = z.output<typeof schema.value>
const form = ref<Partial<Schema>>({})
async function onSubmit(event: FormSubmitEvent<Schema>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<UCard class="w-lg">
<MAutoForm
v-model="form"
:schema="schema"
:submit-button-props="{
color
}"
@submit="onSubmit"
/>
</UCard>
</template>
Number 数字
InputNumber
基础数字输入框,支持整数、小数、范围限制等配置:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
count: afz.number()
.int('必须是整数')
.min(0, '不能小于 0')
.max(100, '不能大于 100')
.meta({
label: '数字输入',
placeholder: '请输入 0-100 的整数',
hint: '支持步长、范围限制等配置'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Slider
数值滑块,设置 type: 'slider',适合有明确范围的数值选择:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
volume: afz.number({
type: 'slider',
controlProps: {
min: 0,
max: 100,
step: 5
}
})
.default(50)
.meta({
label: '音量滑块',
hint: '拖动滑块调整数值,步长为 5'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
InputRating
评分输入,通过 component 直接传入 UInputRating 组件实例,支持半星粒度与自定义星数:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
import { UInputRating } from '#components'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
rating: afz.number({
component: UInputRating,
controlProps: {
step: 0.5,
length: 10
}
})
.min(0, '评分不能小于 0')
.max(10, '评分不能大于 10')
.meta({
label: '评分',
hint: '支持半星评分'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Boolean 布尔
Checkbox
默认的复选框控件,适合单个选项的勾选:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
agree: afz.boolean({
controlProps: {
label: '我已阅读并同意服务条款'
}
})
.refine(val => val === true, '必须同意条款才能继续')
.meta({
label: '复选框'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Switch
开关控件,设置 type: 'switch',适合功能的启用/禁用:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
enabled: afz.boolean({
type: 'switch'
})
.default(true)
.meta({
label: '开关',
hint: '切换功能启用/禁用状态'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Toggle
用 afz.boolean({ type: 'toggle' }) 渲染可按下的布尔值按钮,适合置顶等操作。通过 controlProps.label 设置按钮文字。
<script setup lang="ts">
import type { z } from 'zod'
const { afz } = useAutoForm()
const schema = afz.object({
pinned: afz.boolean({
type: 'toggle',
controlProps: { label: '置顶', activeVariant: 'solid' }
}).default(false).meta({ label: '文章设置' })
})
const state = reactive<z.input<typeof schema>>({})
</script>
<template>
<MAutoForm v-model="state" :schema="schema" :submit="false" />
</template>
Date 日期时间
使用 afz.calendarDate() 创建日期字段,基于 @internationalized/date 提供强大的日期处理能力:
日期选择器
单日期选择,支持日期格式化和验证:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
import { CalendarDate } from '@internationalized/date'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
appointmentDate: afz.calendarDate({ controlProps: { labelFormat: 'iso' } })
.refine(
date => date > new CalendarDate(2026, 1, 1),
{ message: '日期必须在 2026 年之后' }
)
.transform(date => formatter.toTimestamp(date))
.meta({ label: '预约日期', description: '请选择一个在 2026 年之后的日期' })
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
日期范围
日期范围选择,设置 controlProps.range: true:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
vacation: afz.calendarDate({
controlProps: {
labelFormat: 'iso',
range: true,
valueFormat: 'iso',
numberOfMonths: 2
}
})
.meta({
label: '日期范围',
hint: '选择日期范围,显示两个月'
})
})
type Schema = z.output<typeof schema>
async function onSubmit(event: FormSubmitEvent<Schema>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
日期输入框
使用 afz.inputDate() 创建日期输入框,基于 UInputDate 组件提供原生的日期输入体验。
单日期输入
简洁的日期输入框,适合表单填写场景:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
birthDate: afz.inputDate()
.transform(date => formatter.convertToISO(date))
.meta({
label: '出生日期',
description: '使用日期输入框选择您的出生日期'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
日期范围输入
支持范围选择的日期输入框:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
eventPeriod: afz.inputDate({
controlProps: {
range: true
}
})
.transform(date => formatter.convertToISO(date))
.meta({
label: '活动周期',
hint: '选择活动的起止日期'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
时间输入框
使用 afz.inputTime() 创建时间输入框,返回 Time 类型(来自 @internationalized/date):
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
meetingTime: afz.inputTime()
.refine(
time => time.hour >= 9 && time.hour < 18,
{ message: '会议时间必须在工作时间内 (9:00-18:00)' }
)
.transform(time => `${String(time.hour).padStart(2, '0')}:${String(time.minute).padStart(2, '0')}`)
.meta({
label: '会议时间',
description: '请选择会议时间(工作时间内)'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
ISO 格式字符串
对于需要 ISO 标准格式字符串的场景,提供三种工厂方法:
afz.isoDate()- 验证YYYY-MM-DD格式afz.isoTime()- 验证HH:MM[:SS[.s+]]格式afz.isoDatetime()- 验证 ISO 8601 完整格式
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
scheduledDate: afz.isoDate()
.meta({
label: '计划日期',
description: '输入 ISO 格式日期 (YYYY-MM-DD)',
hint: '例如: 2025-12-31'
}),
scheduledTime: afz.isoTime()
.meta({
label: '计划时间',
description: '输入 ISO 格式时间 (HH:MM:SS)',
hint: '例如: 14:30:00'
}),
createdAt: afz.isoDatetime()
.optional()
.meta({
label: '创建时间',
description: '输入 ISO 8601 完整时间戳',
hint: '例如: 2025-12-31T14:30:00Z'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Enum 枚举
不指定控件类型,使用默认的 select 控件:
afz.enum(['a', 'b'], {
label: '选择选项',
hint: '请选择一个'
})
通过 type 覆盖默认控件,同时保持输出类型不变:
afz.enum(['a', 'b', 'c'], {
type: 'radioGroup', // 覆盖默认的 select
controlProps: {
items: [
{ label: '选项A', value: 'a' },
{ label: '选项B', value: 'b' }
]
}
})
// 输出类型仍为:'a' | 'b' | 'c'
// 但使用 radioGroup 渲染
直接传入自定义组件实例:
import MyEnumControl from './MyEnumControl.vue'
afz.enum(['a', 'b'], {
component: MyEnumControl,
controlProps: { /* ... */ }
})
controlProps.items 以最高优先级决定渲染内容,enum() 的第一个参数决定输出类型,两者可以兼用。- 方式 1:自动提取枚举值,系统会自动将枚举值转换为
items:const Fruits = ['apple', 'banana', 'orange'] as const const schema = afz.object({ fruit: afz.enum(Fruits, { type: 'selectMenu', controlProps: { placeholder: '选择水果' // 系统自动转换:items: ['apple', 'banana', 'orange'] } }) }) - 方式 2:手动提供 items,使用空数组
[]作为第一个参数,完全由items决定类型和渲染:const schema = afz.object({ fruit: afz.enum([], { type: 'selectMenu', controlProps: { valueKey: 'value', items: [ { label: '苹果', value: 'apple' }, { label: '香蕉', value: 'banana' } ] } }) }) - 方式 3:类型 + 自定义显示(推荐),同时使用枚举参数和
items,既保证类型安全,又自定义显示:const schema = afz.object({ gender: afz.enum(['male', 'female', 'other'], { type: 'radioGroup', controlProps: { items: [ { label: '男', value: 'male' }, { label: '女', value: 'female' }, { label: '其他', value: 'other' } ] } }) }) // 输出类型:'male' | 'female' | 'other' // 显示内容:男 / 女 / 其他
Select
原生选择框,适合简单的枚举值:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
status: afz.enum(['active', 'inactive', 'pending'])
.default('active')
.meta({
label: '选择框',
placeholder: '请选择状态',
hint: '基础枚举选择'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
SelectMenu
下拉菜单,设置 type: 'selectMenu',支持搜索、图标、分组:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
fruit: afz.enum([], {
type: 'selectMenu',
controlProps: {
placeholder: '选择水果',
valueKey: 'value',
items: [
{ label: '苹果', value: 'apple' },
{ label: '香蕉', value: 'banana' },
{ label: '橙子', value: 'orange' }
]
}
})
.meta({
label: '下拉菜单'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
RadioGroup
单选按钮组,设置 type: 'radioGroup',适合 2-4 个选项:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
theme: afz.enum([], {
type: 'radioGroup',
controlProps: {
valueKey: 'id',
items: [
{ label: 'System', description: '跟随系统设置', id: 'system' },
{ label: 'Light', description: '浅色主题', id: 'light' },
{ label: 'Dark', description: '深色主题', id: 'dark' }
]
}
})
.default('system')
.meta({
label: '单选组'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
ToggleGroup
用 afz.enum(values, { type: 'toggleGroup' }) 渲染单选按钮组。AutoForm 根据枚举值生成选项,也可以通过 controlProps.items 提供自定义标签。再次点击已选项会清空选值。允许清空时,给 schema 加上 .optional()。
多选使用 afz.array(),设置 type: 'toggleGroup',并在 controlProps 中传入 type: 'multiple' 和选项对象数组。表单值是所选选项的 value 数组。
<script setup lang="ts">
import type { z } from 'zod'
const { afz } = useAutoForm()
const schema = afz.object({
period: afz.enum(['日', '周', '月'], {
type: 'toggleGroup',
controlProps: { attached: true }
}).optional().meta({ label: '统计周期' }),
formats: afz.array(afz.enum(['bold', 'italic', 'underline']), {
type: 'toggleGroup',
controlProps: {
type: 'multiple',
items: [
{ label: '加粗', value: 'bold' },
{ label: '斜体', value: 'italic' },
{ label: '下划线', value: 'underline' }
]
}
}).default([]).meta({ label: '文字格式' })
})
const state = reactive<z.input<typeof schema>>({})
</script>
<template>
<MAutoForm v-model="state" :schema="schema" :submit="false" />
</template>
File 文件
afz.file() 创建文件上传字段:单文件上传
基础的单文件上传:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
avatar: afz.file({
controlProps: {
accept: 'image/*'
}
})
.meta({
label: '单文件上传',
hint: '仅支持图片格式'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: `文件名: ${event.data.avatar.name}`
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
多文件上传
多文件上传,使用 array 配合 type: 'file':
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
attachments: afz.array(afz.file(), {
type: 'file',
controlProps: {
multiple: true,
layout: 'list'
}
})
.min(1, '至少上传一个文件')
.max(5, '最多上传 5 个文件')
.meta({
label: '多文件上传',
hint: '最多上传 5 个文件'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: `已上传 ${event.data.attachments.length} 个文件`
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>