Form 表单
统一字段状态、校验、重置和错误定位。
引入
uni-app 配置 easycom 后使用 <hui-form>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<hui-form
ref="control"
:model="model"
:rules="rules"
@validate="event0"
@scroll-to-field="event1"
><hui-form-item prop="name" label="姓名 / Name"
><hui-input v-model="model.name" clearable /></hui-form-item
><hui-form-item prop="note" label="备注 / Note"
><hui-textarea v-model="model.note" /></hui-form-item></hui-form
><hui-button @click="validate">校验 / Validate</hui-button
><hui-button @click="control?.resetFields()">重置 / Reset</hui-button>
<hui-button @click="() => control?.validate()">validate()</hui-button>
<hui-button @click="() => control?.validateField('name')">validateField()</hui-button>
<hui-button @click="() => control?.resetFields()">resetFields()</hui-button>
<hui-button @click="() => control?.clearValidate()">clearValidate()</hui-button>
<hui-button @click="() => control?.scrollToField('name')">scrollToField()</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { type FormRule } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const control = ref()
const model = ref({ name: '' as string | number, note: '' as string | number })
const rules: Record<string, FormRule[]> = {
name: [{ required: true, message: '请输入姓名 / Enter a name', trigger: 'blur' }]
}
const validate = async () => {
lastEvent.value = JSON.stringify(await control.value?.validate())
}
const event0 = (arg0: unknown, arg1: unknown, arg2: unknown) => {
lastEvent.value =
'validate' + ': ' + [describe(arg0), describe(arg1), describe(arg2)].join(', ')
}
const event1 = (position: { prop: string; top: number }) => {
lastEvent.value = 'scroll-to-field: ' + describe(position)
}
</script>
校验方法返回结果,不以校验失败抛出异常。将可提交前的异步任务登记为 blocker,上传等任务未完成时表单应给出阻塞信息。
校验失败时测量所有错误项,定位视口中 top 最小的一项,后插入的字段也按实际位置排序。显式调用 scrollToField 和失败后的自动定位都会发出 scroll-to-field,top 是字段相对视口的位置(px)。
表单位于 scroll-view 时,设置 :scroll-to-page="false",监听位置事件自行修改容器 scroll-top:nextScrollTop = Math.max(0, currentScrollTop + top - containerTop)。其中 containerTop 由容器的 boundingClientRect 取得,也相对视口;同步记录容器 scroll 事件的当前 scrollTop。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model | Record<string, unknown> | — | 表单数据 |
rules | Record<string, FormRule<FormAbortSignal>[]> | — | 和 Form 的同名规则合并 |
labelPosition | "left" | "right" | "top" | 'left' | 覆盖 Form |
labelWidth | string | number | '6em' | 覆盖 Form |
labelSuffix | string | '' | 标签尾部追加内容。 |
hideRequiredAsterisk | boolean | false | 隐藏必填星号,不取消规则校验。 |
showMessage | boolean | true | 覆盖 Form |
size | ComponentSize | — | 覆盖 Form |
disabled | boolean | false | 整表禁用:下层控件加原生 disabled |
readonly | boolean | false | 覆盖 Form |
scrollToError | boolean | true | validate() 失败时滚到第一个错误项 |
scrollToPage | boolean | true | 定位字段后是否滚动页面;设为 false 时只发位置事件,由滚动容器处理。 |
border | boolean | true | 表单项之间的分隔线(单元格风格) |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
validate | (prop: string, valid: boolean, error?: string) | 字段校验完成。 |
scroll-to-field | (position: { prop: string; top: number }) | 定位到字段;top 为视口坐标(px)。 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
default | — | 主体内容。 |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
validate | (): Promise<FormValidateResult> | 校验并返回结果。 |
validateField | (props: string | string[]): Promise<FormValidateResult> | 仅校验指定字段。 |
resetFields | (props?: string | string[] | undefined): void | 恢复登记时的字段初值并清理校验。 |
clearValidate | (props?: string | string[] | undefined): void | 清理校验展示。 |
scrollToField | (prop: string): Promise<void> | 滚动到指定字段。 |
类型
FormProps、FormRule、FormValidateResult、FormValidateTrigger、FormExpose、FormEmits 可从 @nscode/handy-uni 导入。
ts
export type FormRule = SharedFormRule
export type FormValidateResult = SharedFormValidateResult
export type FormValidateTrigger = SharedFormValidateTrigger
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。