Appearance
xtf-form
组件说明
xtf-form 是表单,用于集中管理字段校验、提交、重置和错误汇总。当前实现读取 model 数据、通过 validate 等事件反馈交互、提供实例方法。
- 实际 Demo 路由:
#/pages/components/xtf-form/index - Demo 源码
基础用法
1. 最小可用
提供能够直接看到或操作组件的最少业务内容。
vue
<template>
<view class="doc-minimum">
<XtfForm :model="formModel" :rules="formRules">
<XtfFormItem label="账单名称" prop="name" required><XtfInput :value="formModel.name" placeholder="请输入账单名称" @input="updateName" /></XtfFormItem>
</XtfForm>
</view>
</template>
<script setup lang="uts">
import { reactive } from "vue"
import XtfFormItem from "@/uni_modules/xtf-linkuiunix/components/xtf-form-item/xtf-form-item.uvue"
import XtfInput from "@/uni_modules/xtf-linkuiunix/components/xtf-input/xtf-input.uvue"
import XtfForm from "@/uni_modules/xtf-linkuiunix/components/xtf-form/xtf-form.uvue"
const formModel = reactive({ name: "本月账单" })
const formRules = { name: [{ required: true, message: "请输入账单名称" }] }
function updateName(value: any): void { formModel.name = value as string }
</script>
<style>
.doc-minimum { display: flex; flex-direction: column; padding: 24rpx; }
</style>2. 核心数据与主要事件
使用组件真实数据入口,并记录当前源码声明的主要事件。
vue
<template>
<view class="doc-core">
<XtfForm :model="formModel" :rules="formRules" @validate="onPrimaryEvent">
<XtfFormItem label="账单名称" prop="name" required><XtfInput :value="formModel.name" placeholder="请输入账单名称" @input="updateName" /></XtfFormItem>
</XtfForm>
</view>
</template>
<script setup lang="uts">
import { reactive } from "vue"
import XtfFormItem from "@/uni_modules/xtf-linkuiunix/components/xtf-form-item/xtf-form-item.uvue"
import XtfInput from "@/uni_modules/xtf-linkuiunix/components/xtf-input/xtf-input.uvue"
import XtfForm from "@/uni_modules/xtf-linkuiunix/components/xtf-form/xtf-form.uvue"
const formModel = reactive({ name: "本月账单" })
const formRules = { name: [{ required: true, message: "请输入账单名称" }] }
function updateName(value: any): void { formModel.name = value as string }
function onPrimaryEvent(payload: any): void { console.log("xtf-form:validate", payload) }
</script>
<style>
.doc-core { display: flex; flex-direction: column; padding: 24rpx; }
</style>3. 动态 disabled 状态
运行时切换 disabled,观察组件对应状态变化。
vue
<template>
<view class="doc-dynamic">
<XtfForm :model="formModel" :rules="formRules" :disabled="state">
<XtfFormItem label="账单名称" prop="name" required><XtfInput :value="formModel.name" placeholder="请输入账单名称" @input="updateName" /></XtfFormItem>
</XtfForm>
<button class="doc-action-button" @click="toggleState">切换 disabled</button>
<text class="doc-state-text">disabled: {{ state }}</text>
</view>
</template>
<script setup lang="uts">
import { reactive } from "vue"
import XtfFormItem from "@/uni_modules/xtf-linkuiunix/components/xtf-form-item/xtf-form-item.uvue"
import XtfInput from "@/uni_modules/xtf-linkuiunix/components/xtf-input/xtf-input.uvue"
import XtfForm from "@/uni_modules/xtf-linkuiunix/components/xtf-form/xtf-form.uvue"
import { ref } from "vue"
const formModel = reactive({ name: "本月账单" })
const formRules = { name: [{ required: true, message: "请输入账单名称" }] }
function updateName(value: any): void { formModel.name = value as string }
const state = ref(false)
function toggleState(): void { state.value = !state.value; console.log("disabled", state.value) }
</script>
<style>
.doc-dynamic { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
.doc-state-text { margin-top: 12rpx; color: #475569; font-size: 24rpx; }
</style>4. 定制 default 插槽
使用源码真实声明的 default slot 替换对应内容。
vue
<template>
<view class="doc-slot">
<XtfForm :model="formModel" :rules="formRules">
<template #default="scope"><text class="doc-content-text">自定义 default 内容:{{ scope.disabled }}</text></template>
</XtfForm>
</view>
</template>
<script setup lang="uts">
import { reactive } from "vue"
import XtfFormItem from "@/uni_modules/xtf-linkuiunix/components/xtf-form-item/xtf-form-item.uvue"
import XtfInput from "@/uni_modules/xtf-linkuiunix/components/xtf-input/xtf-input.uvue"
import XtfForm from "@/uni_modules/xtf-linkuiunix/components/xtf-form/xtf-form.uvue"
const formModel = reactive({ name: "本月账单" })
const formRules = { name: [{ required: true, message: "请输入账单名称" }] }
function updateName(value: any): void { formModel.name = value as string }
</script>
<style>
.doc-slot { display: flex; flex-direction: column; padding: 24rpx; }
.doc-content-text { color: #334155; font-size: 26rpx; }
</style>5. 调用 validate()
校验全部字段;签名为 validate(callback?: (valid: boolean, errors: any) => void): Promise<boolean>。
vue
<template>
<view class="doc-method">
<XtfForm :model="formModel" :rules="formRules" ref="componentRef">
<XtfFormItem label="账单名称" prop="name" required><XtfInput :value="formModel.name" placeholder="请输入账单名称" @input="updateName" /></XtfFormItem>
</XtfForm>
<button class="doc-action-button" @click="callMethod">校验全部字段</button>
</view>
</template>
<script setup lang="uts">
import { reactive } from "vue"
import XtfFormItem from "@/uni_modules/xtf-linkuiunix/components/xtf-form-item/xtf-form-item.uvue"
import XtfInput from "@/uni_modules/xtf-linkuiunix/components/xtf-input/xtf-input.uvue"
import XtfForm from "@/uni_modules/xtf-linkuiunix/components/xtf-form/xtf-form.uvue"
import { ref } from "vue"
const formModel = reactive({ name: "本月账单" })
const formRules = { name: [{ required: true, message: "请输入账单名称" }] }
function updateName(value: any): void { formModel.name = value as string }
const componentRef = ref<any>(null)
function callMethod(): void { componentRef.value?.validate(); console.log("调用 validate") }
</script>
<style>
.doc-method { display: flex; flex-direction: column; padding: 24rpx; }
.doc-action-button { margin-top: 16rpx; }
</style>全部属性
| 属性 | 源码类型 | withDefaults 默认值 | 实际行为 / 范围 | 适用范围 |
|---|---|---|---|---|
model | any | () => ({}) | 参与组件运行逻辑;源码在 12 处读取该值;消费位置:script 行 32、script 行 42、script 行 51、script 行 74 | 需要覆盖组件默认业务值时设置。 |
rules | any | () => ({}) | 参与组件运行逻辑;源码在 4 处读取该值;消费位置:script 行 27 | 需要覆盖组件默认业务值时设置。 |
labelWidth | string | number | 168 | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
labelPosition | string | "vertical" | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
size | string | "" | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 12 | 需要覆盖组件默认业务值时设置。 |
disabled | boolean | false | 参与组件运行逻辑;源码在 1 处读取该值;消费位置:script 行 11 | 需要控制显示、可用性或交互状态时设置。 |
readonly | boolean | false | 参与组件运行逻辑;源码在 4 处读取该值;消费位置:script 行 105、script 行 110、script 行 114、template 行 7 | 需要控制显示、可用性或交互状态时设置。 |
loading | boolean | false | 参与组件运行逻辑;源码在 4 处读取该值;消费位置:script 行 14、script 行 105、script 行 110、script 行 114 | 需要控制显示、可用性或交互状态时设置。 |
validateTrigger | string | Array<any> | "change" | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
requiredMark | string | "start" | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
feedback | boolean | false | 兼容接收,当前实现未消费/不产生行为 | 仅用于兼容现有调用;当前版本不应依赖其效果。 |
scrollToError | boolean | UTSJSONObject | true | 参与组件运行逻辑;源码在 3 处读取该值;消费位置:script 行 96、script 行 124 | 需要覆盖组件默认业务值时设置。 |
linkage | Array<any> | () => [] as Array<any> | 参与组件运行逻辑;源码在 1 处读取该值;消费位置:script 行 30 | 需要覆盖组件默认业务值时设置。 |
showErrorSummary | boolean | false | 开关对应显示或交互行为;消费位置:template 行 3 | 需要控制显示、可用性或交互状态时设置。 |
errorSummaryTitle | string | "" | 参与组件运行逻辑;源码在 2 处读取该值;消费位置:script 行 20 | 需要提供业务文案或图标内容时设置。 |
customClass | string | "" | 参与组件运行逻辑;源码在 1 处读取该值;消费位置:script 行 19 | 需要调整布局或视觉表现时设置。 |
customStyle | string | UTSJSONObject | "" | 参与组件运行逻辑;源码在 1 处读取该值;消费位置:template 行 2 | 需要调整布局或视觉表现时设置。 |
事件
| 事件名称 | 源码触发边界 | 回调参数 | 参数说明 |
|---|---|---|---|
validate | validateField 内触发(script 行 95);参数表达式:{ valid: nextErrors.length == 0, errors: nextErrors };前置 guard:!alive || generation !== epoch、revision !== wanted[i].getValidationVersion()、!alive || generation !== epoch || snapshot !== JSON.stringify(props.model) | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
reset | reset 内触发(script 行 102);参数表达式:wanted.map((field: any) => field.prop) | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
submit | setup 内触发(script 行 115);参数表达式:failure == null ? { valid: true, model: clone(props.model) } : { valid: false, model: clone(props.model), errors: failure };前置 guard:!alive || generation !== epoch || resolvedDisabled.value || props.readonly || props.loading、!alive || generation !== epoch || resolvedDisabled.value || props.readonly || props.loading || (!valid && failure == null) | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
validate-status-change | notifyValidateStatusChange 内触发(script 行 72);参数表达式:payload | (...args: any[]) | 表中参数表达式来自当前源码直接调用点;禁用、守卫失败或未进入该函数分支时不会触发。 |
插槽
| 插槽 | Scope 参数 | 实际渲染位置 |
|---|---|---|
default | { disabled, readonly, loading } | 仅在模板对应 <slot> 渲染位置生效。 |
方法
实例方法
| 方法 | 签名 / 返回值 | 调用边界 |
|---|---|---|
validate | validate(callback?: (valid: boolean, errors: any) => void): Promise<boolean> | 校验全部字段;通过组件 ref 调用,参数和返回值按签名处理。 |
validateField | validateField(target: any = null, trigger: string = ""): Promise<boolean> | 校验指定字段;通过组件 ref 调用,参数和返回值按签名处理。 |
reset | reset(target: any = null): void | 恢复初始状态;通过组件 ref 调用,参数和返回值按签名处理。 |
clearValidate | clearValidate(target: any = null): void | 清除校验结果;通过组件 ref 调用,参数和返回值按签名处理。 |
submit | submit(callback?: (valid: boolean, errors: any) => void): Promise<boolean> | 提交当前数据;通过组件 ref 调用,参数和返回值按签名处理。 |
getErrors | getErrors(): Array<any> | 调用该公开实例能力;通过组件 ref 调用,参数和返回值按签名处理。 |
addField | addField(path: string, value: any = {}, index: number = -1): Array<any> | 调用该公开实例能力;通过组件 ref 调用,参数和返回值按签名处理。 |
removeField | removeField(path: string, index: number): any | 调用该公开实例能力;通过组件 ref 调用,参数和返回值按签名处理。 |
公开状态
| 状态 | 类型 / 包装 | 读取边界 |
|---|---|---|
| - | - | 当前组件没有公开状态。 |
主题说明
- 当前组件 CSS 未读取
var(--xtf-*)主题变量;颜色或尺寸可能来自 props、固定样式或父级上下文。
平台与实现边界
- 基线为 HBuilderX 5.26+ 与 uni-app x;当前报告已验证 Web Chromium 的对应 Demo,不据此承诺 Safari、小程序或 App 原生端等价。
- Android、iOS、HarmonyOS、Vapor 原生路径、触摸手势、字体、Canvas、安全区和系统权限能力,只有源码或完整性报告明确覆盖的部分才可视为已验证。
- 本文按当前源码生成,不补入旧版
xtf-linkui存在但迁移版未实现的 props、事件、slot 或方法。 - 当前仅兼容接收、未发现运行消费的 props:
labelWidth、labelPosition、validateTrigger、requiredMark、feedback。 - 详细公共边界见 平台与实现边界,逐项运行证据见项目根目录 COMPONENT_COMPLETENESS.md。