feat: establish shared field and form foundation
This commit is contained in:
@@ -39,6 +39,7 @@ const groups = [
|
||||
{ title: "介绍", url: "/introduction" },
|
||||
{ title: "安装", url: "/guide" },
|
||||
{ title: "主题", url: "/themes" },
|
||||
{ title: "字段与表单基础", url: "/fields" },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import {
|
||||
Button,
|
||||
Description,
|
||||
FieldError,
|
||||
Form,
|
||||
Input,
|
||||
Label,
|
||||
TextArea,
|
||||
TextField,
|
||||
Switch,
|
||||
SwitchContent,
|
||||
SwitchControl,
|
||||
SwitchThumb,
|
||||
type ValidationErrors,
|
||||
} from "@heroui-vue/components";
|
||||
const result = ref("");
|
||||
const validationErrors = ref<ValidationErrors>({});
|
||||
function submit(event: Event) {
|
||||
const data = new FormData(event.currentTarget as HTMLFormElement);
|
||||
result.value = JSON.stringify(Object.fromEntries(data), null, 2);
|
||||
}
|
||||
function reset() {
|
||||
result.value = "";
|
||||
validationErrors.value = {};
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<Form
|
||||
class="flex w-full max-w-md flex-col gap-4"
|
||||
:validation-errors="validationErrors"
|
||||
@submit.prevent="submit"
|
||||
@reset="reset"
|
||||
>
|
||||
<TextField
|
||||
name="email"
|
||||
type="email"
|
||||
is-required
|
||||
default-value=""
|
||||
full-width
|
||||
>
|
||||
<Label>邮箱</Label>
|
||||
<Input placeholder="name@example.com" />
|
||||
<Description>输入有效邮箱后可提交,必填错误会在提交时显示。</Description>
|
||||
<FieldError />
|
||||
</TextField>
|
||||
<TextField
|
||||
name="bio"
|
||||
default-value=""
|
||||
full-width
|
||||
:validate="
|
||||
(value) => (value.length > 80 ? '个人简介不能超过 80 个字符。' : null)
|
||||
"
|
||||
>
|
||||
<Label>个人简介</Label>
|
||||
<TextArea placeholder="介绍一下你自己…" :rows="3" />
|
||||
<FieldError />
|
||||
</TextField>
|
||||
<Switch name="notifications" default-checked>
|
||||
<SwitchContent>
|
||||
<SwitchControl><SwitchThumb /></SwitchControl>
|
||||
<Label>接收活动通知</Label>
|
||||
</SwitchContent>
|
||||
<Description>关闭时原生 FormData 不包含此项。</Description>
|
||||
</Switch>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<Button type="submit">提交</Button>
|
||||
<Button type="reset" variant="secondary">重置</Button>
|
||||
<Button
|
||||
variant="tertiary"
|
||||
@click="
|
||||
validationErrors = { email: '此邮箱已被使用,请修改后再提交。' }
|
||||
"
|
||||
>
|
||||
显示服务端错误
|
||||
</Button>
|
||||
</div>
|
||||
<pre
|
||||
v-if="result"
|
||||
aria-label="提交结果"
|
||||
role="status"
|
||||
class="whitespace-pre-wrap text-xs"
|
||||
>{{ result }}</pre>
|
||||
</Form>
|
||||
</template>
|
||||
@@ -0,0 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { Input, Label, TextField } from "@heroui-vue/components";
|
||||
</script>
|
||||
<template>
|
||||
<TextField class="w-full max-w-64" name="email" type="email">
|
||||
<Label>邮箱</Label>
|
||||
<Input placeholder="输入你的邮箱" />
|
||||
</TextField>
|
||||
</template>
|
||||
@@ -0,0 +1,42 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from "vue";
|
||||
import {
|
||||
Description,
|
||||
FieldError,
|
||||
Input,
|
||||
Label,
|
||||
TextArea,
|
||||
TextField,
|
||||
} from "@heroui-vue/components";
|
||||
const username = ref("");
|
||||
const bio = ref("");
|
||||
const isUsernameInvalid = computed(
|
||||
() => username.value.length > 0 && username.value.length < 3,
|
||||
);
|
||||
const isBioInvalid = computed(
|
||||
() => bio.value.length > 0 && bio.value.length < 20,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<div class="flex w-full max-w-64 flex-col gap-4">
|
||||
<TextField
|
||||
v-model="username"
|
||||
is-required
|
||||
:is-invalid="isUsernameInvalid"
|
||||
name="username"
|
||||
>
|
||||
<Label>用户名</Label>
|
||||
<Input placeholder="jane_doe" />
|
||||
<FieldError v-if="isUsernameInvalid">
|
||||
用户名至少需要 3 个字符。
|
||||
</FieldError>
|
||||
<Description v-else>为你的资料选择一个唯一的用户名。</Description>
|
||||
</TextField>
|
||||
<TextField v-model="bio" is-required :is-invalid="isBioInvalid" name="bio">
|
||||
<Label>个人简介</Label>
|
||||
<TextArea placeholder="介绍一下你自己…" />
|
||||
<FieldError v-if="isBioInvalid">个人简介至少需要 20 个字符。</FieldError>
|
||||
<Description v-else>至少 20 个字符 ({{ bio.length }}/20).</Description>
|
||||
</TextField>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { Description, Input, Label, TextField } from "@heroui-vue/components";
|
||||
</script>
|
||||
<template>
|
||||
<TextField class="w-full max-w-64" name="username">
|
||||
<Label>用户名</Label>
|
||||
<Input placeholder="输入用户名" />
|
||||
<Description>为你的账户选择一个唯一的用户名</Description>
|
||||
</TextField>
|
||||
</template>
|
||||
@@ -0,0 +1,64 @@
|
||||
---
|
||||
title: 字段与表单基础
|
||||
description: 组合标签、说明、校验和原生表单提交
|
||||
---
|
||||
|
||||
## 基本组合
|
||||
|
||||
`TextField` 管理一个文本控件的值、状态和关联。`Label` 自动关联到实际控件,`Description` 与 `FieldError` 负责说明和错误提示。`Input` 与 `TextArea` 也可以独立使用。
|
||||
|
||||
<ComponentPreview name="textfield-basic" />
|
||||
|
||||
## 描述与错误
|
||||
|
||||
说明可位于控件之后;动态替换描述或错误时,关联 ID 会同步更新。以下两个示例沿用固定上游的文案与布局。
|
||||
|
||||
<ComponentPreview name="textfield-with-description" />
|
||||
<ComponentPreview name="textfield-validation" />
|
||||
|
||||
## 提交与重置
|
||||
|
||||
填写邮箱后点击“提交”,结果来自原生 `FormData`;“重置”恢复初始值。“显示服务端错误”用于体验错误关联,修改对应字段后清除本次错误;这里没有真实服务端请求。
|
||||
|
||||
<ComponentPreview name="form-foundation" />
|
||||
|
||||
## 值与状态
|
||||
|
||||
优先在 `TextField` 使用 `v-model` 或 `default-value`,子控件负责渲染和输入;每个 TextField 只放一个 Input 或 TextArea。兼容已有的子控件 `v-model`,但不要同时在父字段与子控件绑定两个不同的受控值源。
|
||||
|
||||
| 能力 | 用法 | 说明 |
|
||||
| ---------- | ---------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
|
||||
| 受控值 | `v-model` | 父级拒绝更新时恢复实际 DOM 值 |
|
||||
| 初始值 | `default-value` | 非受控 reset 恢复初始值;受控值由父级决定 |
|
||||
| 原生提交 | `name`、`type` | TextField 下发到控件;禁用项省略、只读项保留 |
|
||||
| 状态 | `is-required`、`is-disabled`、`is-read-only`、`is-invalid` | 统一控件状态、标签样式和错误展示;isInvalid 是展示状态,自定义提交约束用 validate 或 validationErrors |
|
||||
| 变体 | `variant`、`full-width` | 继承到控件,子控件可显式覆盖视觉变体 |
|
||||
| 原生约束 | `min-length`、`max-length`、`pattern` | 下发到实际文本控件,额外原生属性可放在 Input/TextArea |
|
||||
| 标签 | `Label` | id 优先使用控件显式 id,其次 TextField id,最后自动生成 |
|
||||
| 描述 | `Description` | 合并调用方 aria-describedby;支持多个描述与动态 ID |
|
||||
| 错误 | `FieldError` | 自动取当前字段错误,也可用默认/作用域插槽自定义 |
|
||||
| 自定义样式 | `class` | 遵循 Vue 字符串、数组、对象绑定,class/style 留在对应根节点 |
|
||||
|
||||
## 校验
|
||||
|
||||
默认 `validation-behavior="native"`:原生约束与同步 `validate(value)` 会拦截无效提交,错误在提交后展示。`validate` 返回错误字符串、字符串数组或表示通过的 `true` / `null` / `undefined`。
|
||||
|
||||
`validation-behavior="aria"`:显示错误语义但允许提交,由调用方决定处理。可以在 Form 统一配置,也可在字段局部覆盖。自定义 Form 通过原生 Constraint Validation API 检查 native 控件,并跳过 aria 控件;使用普通原生 form 配合 aria 模式时,由调用方设置 novalidate 并负责提交决策。
|
||||
|
||||
Form 的 `validation-errors` 是以字段 name 为键、字符串或字符串数组为值的对象。它不替代实际后端验证。修改某字段会忽略当前这次服务端响应中该字段的错误;下一次响应请提供新的对象。异步请求、竞态控制和 schema 库由调用方负责,当前 validate 不接受 Promise。
|
||||
|
||||
| Form 能力 | 类型 / 用法 | 说明 |
|
||||
| -------------------- | ------------------------------------------------ | ----------------------------------------------- |
|
||||
| `validationBehavior` | `'native' \| 'aria'` | 默认 native,向字段提供默认策略 |
|
||||
| `validationErrors` | `Record<string, string \| string[]>` | 服务端错误或外部 schema 的错误结果 |
|
||||
| `@submit` | 原生 SubmitEvent | 验证通过后触发;使用 @submit.prevent 接管提交 |
|
||||
| `@reset` | 原生 Event | 可 preventDefault 取消;不强行覆盖受控值 |
|
||||
| 实例方法 | `checkValidity()`、`reportValidity()`、`reset()` | 按 native/aria 策略验证,报告错误时聚焦无效控件 |
|
||||
|
||||
## 当前边界
|
||||
|
||||
本页交付共享协议和验证场景,尚未宣称完整迁移 Form、TextField 的所有上游文档。已迁移的三个 TextField 基线示例在此展示,其余示例后续按清单补齐。
|
||||
|
||||
SSR 支持标准的直接子部件、原生元素/Fragment 包裹的 Description/FieldError,即使说明放在控件之后也能生成完整 ID 关联。自定义 Vue 包装组件中的描述在客户端注册;若首屏 SSR 必须关联,请显式提供控件 aria-describedby 和对应描述 id。Nuxt 完整消费验收尚未进行。
|
||||
|
||||
Fieldset 的禁用继承、Checkbox/Radio/集合字段、异步校验调度仍是后续任务,不从本页示例推断已经实现。
|
||||
Reference in New Issue
Block a user