翻譯進行中
本頁尚未翻譯為繁體中文,以下顯示簡體中文原文。範例的預設元件文字仍使用繁體中文。
FormItem 表单项
连接表单模型与输入控件,展示标签、提示和校验结果。
引入
uni-app 配置 easycom 后使用 <hui-form-item>;公开类型从 @nscode/handy-uni 导入。普通 Vue 3 项目安装 @nscode/handy-uni/web 预览版插件后使用同名组件。
示例
操作控件后,页面直接显示当前值和事件结果。
vue
<template>
<view>
<hui-form :model="model"
><hui-form-item
ref="control"
prop="name"
label="姓名 / Name"
:rules="rules"
tip="至少两字 / At least two characters"
@click="event0"
><hui-input v-model="model.name" clearable /></hui-form-item></hui-form
><hui-button @click="validate">校验字段 / Validate field</hui-button
><hui-button @click="control?.clearValidate()">清除结果 / Clear validation</hui-button>
<hui-button @click="() => control?.validate()">validate()</hui-button>
<hui-button @click="() => control?.resetField()">resetField()</hui-button>
<hui-button @click="() => control?.clearValidate()">clearValidate()</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 })
const rules: FormRule[] = [
{ required: true, message: '请输入姓名 / Enter a name' },
{ min: 2, message: '至少两字 / At least two characters' }
]
const validate = async () => {
lastEvent.value = String((await control.value?.validate()) ?? '有效 / Valid')
}
const event0 = (arg0: unknown) => {
lastEvent.value = 'click' + ': ' + [describe(arg0)].join(', ')
}
</script>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
prop | string | — | model 里的路径,支持 a.b[0].c |
label | string | — | 可见标签。 |
required | boolean | undefined | 只控制星号;不传时按规则里有没有 required 推断 |
rules | FormRule<FormAbortSignal>[] | — | 和 Form 的同名规则合并 |
labelWidth | string | number | — | 覆盖 Form |
labelPosition | "left" | "right" | "top" | — | 覆盖 Form |
size | ComponentSize | — | 覆盖 Form |
showMessage | boolean | undefined | 覆盖 Form |
readonly | boolean | undefined | 覆盖 Form |
error | string | — | 外部错误(例如接口返回),不触发 validate 事件 |
tip | string | — | 控件下方的说明 |
isLink | boolean | false | 右侧箭头(选择类字段) |
根节点属性见通用属性。布尔默认值为 undefined 时支持继承;显式 false 会覆盖继承值。其他布尔值按表中默认值处理。
Events
| 事件 | 参数 | 说明 |
|---|---|---|
click | (event: unknown) | 点击可交互区域。 |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
label | — | 自定义 label 区域,覆盖默认内容。 |
default | — | 主体内容。 |
extra | — | 自定义 extra 区域,覆盖默认内容。 |
error | { error } | 自定义 error 区域,覆盖默认内容。 |
tip | — | 自定义 tip 区域,覆盖默认内容。 |
方法
| 方法 | 签名 | 说明 |
|---|---|---|
validate | (trigger?: FormValidateTrigger | undefined): Promise<string | undefined> | 校验并返回结果。 |
resetField | (): void | 恢复本字段初值。 |
clearValidate | (): void | 清理校验展示。 |
类型
FormItemProps、FormItemExpose、FormItemEmits 可从 @nscode/handy-uni 导入。
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 基本交互 | 已实现,待设备复核 | 已实现,待设备复核 | 已实现,待设备复核 | 由 Web 平台适配层实现 |
设备操作见冒烟清单。真实设备读数单独记录,单元测试通过不代表真机验收。