From a4fb3c550ab3a741c35a9b2520e6dd568c98d447 Mon Sep 17 00:00:00 2001 From: maoyingjie Date: Thu, 22 Oct 2026 08:46:02 +0800 Subject: [PATCH] feat: add Fieldset and SearchField infrastructure --- AGENTS.md | 7 + apps/docs/.vitepress/theme/Layout.vue | 2 + apps/docs/components.md | 3 + apps/docs/components/fieldset.md | 130 +++++ apps/docs/components/search-field.md | 272 +++++++++ apps/docs/demos/fieldset-basic.vue | 67 +++ apps/docs/demos/fieldset-custom-styles.vue | 77 +++ apps/docs/demos/fieldset-disabled.vue | 76 +++ apps/docs/demos/fieldset-on-surface.vue | 74 +++ apps/docs/demos/search-field-basic.vue | 21 + apps/docs/demos/search-field-controlled.vue | 32 ++ apps/docs/demos/search-field-custom-icons.vue | 30 + .../docs/demos/search-field-custom-styles.vue | 24 + apps/docs/demos/search-field-disabled.vue | 34 ++ apps/docs/demos/search-field-form-example.vue | 70 +++ apps/docs/demos/search-field-full-width.vue | 23 + apps/docs/demos/search-field-on-surface.vue | 35 ++ .../demos/search-field-render-function.vue | 21 + apps/docs/demos/search-field-required.vue | 33 ++ apps/docs/demos/search-field-validation.vue | 38 ++ apps/docs/demos/search-field-variants.vue | 31 ++ .../demos/search-field-with-description.vue | 34 ++ .../search-field-with-keyboard-shortcut.vue | 58 ++ .../demos/search-field-with-validation.vue | 37 ++ docs/compatibility.md | 8 +- docs/decisions/field-form-foundation.md | 20 +- docs/decisions/selection-field-foundation.md | 35 ++ .../FORM-02-fieldset-search-field.md | 39 ++ .../M15-M17-display-navigation-prompt.md | 42 ++ docs/upstream.md | 4 + docs/verification.md | 10 + package.json | 2 +- packages/styles/components/fieldset.css | 20 + packages/styles/components/search-field.css | 188 +++++++ packages/styles/index.css | 17 + packages/styles/src/index.ts | 45 ++ packages/vue/src/components/button/Button.vue | 7 +- .../components/close-button/CloseButton.vue | 9 +- packages/vue/src/components/field/disabled.ts | 9 + .../src/components/field/useTextControl.ts | 11 +- .../vue/src/components/field/useTextField.ts | 346 ++++++++++++ .../src/components/fieldset/FieldGroup.vue | 14 + .../vue/src/components/fieldset/Fieldset.vue | 80 +++ .../components/fieldset/FieldsetActions.vue | 14 + .../components/fieldset/FieldsetLegend.vue | 14 + packages/vue/src/components/label/Label.vue | 6 +- packages/vue/src/components/link/Link.vue | 32 +- .../components/search-field/SearchField.vue | 121 ++++ .../search-field/SearchFieldClearButton.vue | 45 ++ .../search-field/SearchFieldGroup.vue | 50 ++ .../search-field/SearchFieldInput.vue | 51 ++ .../search-field/SearchFieldSearchIcon.vue | 48 ++ .../src/components/search-field/context.ts | 15 + packages/vue/src/components/switch/Switch.vue | 3 +- .../src/components/textfield/TextField.vue | 303 +--------- packages/vue/src/index.ts | 34 ++ packages/vue/src/types.ts | 37 ++ tests/fieldset-search-field.test.ts | 520 ++++++++++++++++++ tests/ssr.test.ts | 34 ++ 59 files changed, 3131 insertions(+), 331 deletions(-) create mode 100644 apps/docs/components/fieldset.md create mode 100644 apps/docs/components/search-field.md create mode 100644 apps/docs/demos/fieldset-basic.vue create mode 100644 apps/docs/demos/fieldset-custom-styles.vue create mode 100644 apps/docs/demos/fieldset-disabled.vue create mode 100644 apps/docs/demos/fieldset-on-surface.vue create mode 100644 apps/docs/demos/search-field-basic.vue create mode 100644 apps/docs/demos/search-field-controlled.vue create mode 100644 apps/docs/demos/search-field-custom-icons.vue create mode 100644 apps/docs/demos/search-field-custom-styles.vue create mode 100644 apps/docs/demos/search-field-disabled.vue create mode 100644 apps/docs/demos/search-field-form-example.vue create mode 100644 apps/docs/demos/search-field-full-width.vue create mode 100644 apps/docs/demos/search-field-on-surface.vue create mode 100644 apps/docs/demos/search-field-render-function.vue create mode 100644 apps/docs/demos/search-field-required.vue create mode 100644 apps/docs/demos/search-field-validation.vue create mode 100644 apps/docs/demos/search-field-variants.vue create mode 100644 apps/docs/demos/search-field-with-description.vue create mode 100644 apps/docs/demos/search-field-with-keyboard-shortcut.vue create mode 100644 apps/docs/demos/search-field-with-validation.vue create mode 100644 docs/decisions/selection-field-foundation.md create mode 100644 docs/migrations/FORM-02-fieldset-search-field.md create mode 100644 docs/migrations/M15-M17-display-navigation-prompt.md create mode 100644 packages/styles/components/fieldset.css create mode 100644 packages/styles/components/search-field.css create mode 100644 packages/vue/src/components/field/disabled.ts create mode 100644 packages/vue/src/components/field/useTextField.ts create mode 100644 packages/vue/src/components/fieldset/FieldGroup.vue create mode 100644 packages/vue/src/components/fieldset/Fieldset.vue create mode 100644 packages/vue/src/components/fieldset/FieldsetActions.vue create mode 100644 packages/vue/src/components/fieldset/FieldsetLegend.vue create mode 100644 packages/vue/src/components/search-field/SearchField.vue create mode 100644 packages/vue/src/components/search-field/SearchFieldClearButton.vue create mode 100644 packages/vue/src/components/search-field/SearchFieldGroup.vue create mode 100644 packages/vue/src/components/search-field/SearchFieldInput.vue create mode 100644 packages/vue/src/components/search-field/SearchFieldSearchIcon.vue create mode 100644 packages/vue/src/components/search-field/context.ts create mode 100644 tests/fieldset-search-field.test.ts diff --git a/AGENTS.md b/AGENTS.md index bfac9c1..b4a4c03 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -303,3 +303,10 @@ Vaul Vue 官方已说明停止维护并推荐 Reka Drawer;新项目不采用 V - 本库 InputGroup 是 TextField SSR 预规划支持的透明包装层,InputGroupInput/TextArea 显式 id 必须在服务端首屏就与 Label 匹配,并回归实际水合、动态 id 与插槽;不能只用直接 Input 的旧测试代替组合组件测试。任意用户 Vue 包装组件仍有已记录限制。 - invalid 与父字段错误合并;叶子 disabled/readonly/required 不能通过 false 解除父字段状态。文档不得把这些描述成可自由覆盖。 - 示例中的上游复制图标按钮若没有处理器,明确仅演示布局,不声称复制已经生效。未迁移 Tooltip 时,不能将其内容直接移到常驻操作栏改变布局;定时交互清理卸载时的定时器。 + +## FORM-02 之后的共享文件边界(2026-10-22) + +- TextField 与 SearchField 共用 `field/useTextField.ts`,真实输入共用 useTextControl;Fieldset 禁用统一用 field/disabled.ts。不复制第二份值/reset/校验状态,不给一个字段生成重复表单值。 +- 集成人维护上述内核、Fieldset/Legend、SearchField、Form 与 Switch。普通迁移任务遇到缺口先交失败场景,不自行改共享协议。 +- 首个直接 Legend 与嵌套禁用必须按原生语义回归,不能简单给所有后代设置 disabled。SearchField 输入 Enter 的专属 submit 与原生表单提交不得重复,调用者取消、IME、Escape 空值与受控拒绝都有既有测试。 +- 下一阶段 Checkbox/Radio 协议见 docs/decisions/selection-field-foundation.md;当前 Reka 的 CheckboxGroup 隐藏值序列化/禁用/reset 与目标有实际差异,不直接传 name 就宣布表单兼容。 diff --git a/apps/docs/.vitepress/theme/Layout.vue b/apps/docs/.vitepress/theme/Layout.vue index bc6250d..488eef1 100644 --- a/apps/docs/.vitepress/theme/Layout.vue +++ b/apps/docs/.vitepress/theme/Layout.vue @@ -55,11 +55,13 @@ const groups = [ { title: "CloseButton 关闭按钮", url: "/components/close-button" }, { title: "Description 描述", url: "/components/description" }, { title: "FieldError 字段错误", url: "/components/field-error" }, + { title: "Fieldset 字段组", url: "/components/fieldset" }, { title: "Input 输入框", url: "/components/input" }, { title: "InputGroup 输入框组", url: "/components/input-group" }, { title: "Kbd 键盘按键", url: "/components/kbd" }, { title: "Label 标签", url: "/components/label" }, { title: "Link 链接", url: "/components/link" }, + { title: "SearchField 搜索框", url: "/components/search-field" }, { title: "Separator 分隔符", url: "/components/separator" }, { title: "Skeleton 骨架屏", url: "/components/skeleton" }, { title: "Spinner 加载指示器", url: "/components/spinner" }, diff --git a/apps/docs/components.md b/apps/docs/components.md index e82ea3f..cb4350f 100644 --- a/apps/docs/components.md +++ b/apps/docs/components.md @@ -25,3 +25,6 @@ description: 已迁移组件及文档 - [Switch 开关](./components/switch) - [TextArea 多行文本框](./components/textarea) - [TextField 文本输入框](./components/text-field) + +- [Fieldset 字段组](./components/fieldset) +- [SearchField 搜索框](./components/search-field) diff --git a/apps/docs/components/fieldset.md b/apps/docs/components/fieldset.md new file mode 100644 index 0000000..51918af --- /dev/null +++ b/apps/docs/components/fieldset.md @@ -0,0 +1,130 @@ +--- +title: Fieldset 字段组 +description: 将相关表单控件与图例、描述和操作组合在一起 +component: fieldset +group: 组件 +--- + +## 用法 + +```vue + +``` + + + +## 组件结构 + +```vue + + + +``` + +## 示例 + +### 表面样式 + +在 [Surface](/components/surface) 内使用时,请在表单控件(Input、TextArea 等)上使用 `variant="secondary"`,以应用适合 Surface 背景的低强调变体。 + + + +## 自定义样式 + +### Tailwind CSS + + + +### 全局 CSS + +使用 `@layer components` 指令定位 Fieldset 的 [BEM](https://getbem.com/) 风格类。 + +```css +@layer components { + .hv-fieldset { + @apply gap-5 rounded-xl border border-border/60 bg-surface p-6 shadow-field; + } + + .hv-fieldset__legend { + @apply text-lg font-semibold; + } + + .hv-fieldset__field-group { + @apply gap-3 md:grid md:grid-cols-2; + } + + .hv-fieldset__actions { + @apply flex justify-end gap-2 pt-2; + } +} +``` + +## 样式参考 + +HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。 + +### CSS 类 + +Fieldset 复合组件暴露以下 CSS 选择器: + +#### 基础类 [!toc] + +- `.hv-fieldset` – 根容器 +- `.hv-fieldset__legend` – 图例元素 +- `.hv-fieldset__field-group` – 分组字段包裹层 +- `.hv-fieldset__actions` – 字段下方的操作栏 + +## 禁用继承 + +原生 `disabled` 同步到本库文本输入、SearchField、Switch、Button、CloseButton 与 Link 的状态、事件和表单值。内层字段组不能解除祖先禁用;首个**直接** Legend 内的控件保留原生例外,仍受外层字段组与自身禁用约束。原生 legend 也支持,第三方自定义包装内部的 legend 不作静态识别,请直接使用 FieldsetLegend。 + +下面是本库新增的协议验证例,不属于上游三项基线示例。 + + + +## API 参考 + +### Fieldset + +| 属性 | 类型 | 默认值 | 描述 | +| --------------- | --------------------------- | ------- | ------------------------------ | +| `disabled` | `boolean` | `false` | 原生禁用并下发组件禁用上下文 | +| `name` / `form` | `string` | - | 原生 fieldset 属性,透传根节点 | +| `class` | `string \| Array \| Object` | - | 合并基础类 | +| 默认插槽 | 内容 | - | Legend、字段和操作区 | + +### FieldsetLegend / FieldGroup / FieldsetActions + +| 部件 | 根元素 | 属性与插槽 | +| --------------- | -------- | ----------------------------------------- | +| FieldsetLegend | `legend` | 原生属性、class/style、DOM 事件、默认插槽 | +| FieldGroup | `div` | 原生属性、class/style、DOM 事件、默认插槽 | +| FieldsetActions | `div` | 原生属性、class/style、DOM 事件、默认插槽 | + +三个部件使用对应 BEM 基础类,class 接受字符串、数组和对象。命名导出替代 React 对象式子部件;原生属性直接传递,不使用 nativeProps 对象。根元素固定为语义化原生元素,暂不提供 React render 根元素替换。 + +Fieldset 不维护字段值或校验队列。请使用 Form + TextField / SearchField,提交与 reset 沿用原生表单;示例展示实际 FormData 与成功反馈,不执行后台保存。 + +## 相关组件 + +[TextField](./text-field) · [SearchField](./search-field) · [Input](./input) diff --git a/apps/docs/components/search-field.md b/apps/docs/components/search-field.md new file mode 100644 index 0000000..06e79b3 --- /dev/null +++ b/apps/docs/components/search-field.md @@ -0,0 +1,272 @@ +--- +title: SearchField 搜索框 +description: 搜索输入字段,包含清除按钮与搜索图标。 +component: search-field +group: 组件 +--- + +## 用法 + +```vue + +``` + + + +## 组件结构 + +```vue + + + +``` + +> **SearchField** 允许用户输入并清空搜索关键词。它包含搜索图标,并提供可选的清除按钮以便快速重置。 + +## 示例 + +### 变体 + +SearchField 组件支持两种视觉变体: + +- **`primary`**(默认)— 带阴影的标准样式,适用于大多数场景 +- **`secondary`** — 低强调、无阴影,适合在 Surface 等表面背景上使用 + + + +### 表面样式 + +在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。 + + + +### 带描述 + + + +### 必填字段 + + + +### 禁用状态 + + + +### 宽度充满 + + + +### 表单校验 + +将 `is-invalid` 与 `FieldError` 配合使用,以展示校验消息。 + + + +### 受控组件 + +使用 v-model 控制值 以与其他组件同步或执行自定义格式化。 + + + +### 表单示例 + +完整的表单集成示例,包含校验、实际 FormData 和模拟提交反馈;没有后台搜索请求。 + + + +### 带校验 + +通过受控 value 实现自定义校验逻辑。 + + + +### 自定义图标 + +自定义搜索图标与清除按钮图标。 + + + +### 键盘快捷键 + +添加快捷键以快速聚焦搜索框。 + + + +### 渲染函数 + +基线示例的根为 div + data-custom;Vue 通过根属性透传保留效果,暂不提供 React render 根类型替换。 + + + +## 自定义样式 + +### Tailwind CSS + + + +### 全局 CSS + +若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。 + +```css +@layer components { + .hv-search-field { + @apply flex flex-col gap-1; + } + + /* When invalid, the description is hidden automatically */ + .hv-search-field[data-invalid], + .hv-search-field[aria-invalid] { + [data-slot="description"] { + @apply hidden; + } + } + + .hv-search-field__group { + @apply bg-field text-field-foreground shadow-field rounded-field inline-flex h-9 items-center overflow-hidden border; + } + + .hv-search-field__input { + @apply flex-1 rounded-none border-0 bg-transparent px-3 py-2 shadow-none outline-none; + } + + .hv-search-field__search-icon { + @apply text-field-placeholder pointer-events-none shrink-0 ml-3 mr-0 size-4; + } + + .hv-search-field__clear-button { + @apply mr-1 shrink-0; + } +} +``` + +## 样式参考 + +HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。 + +### CSS 类 + +#### 基础类 [!toc] + +- `.hv-search-field` – 根容器,样式非常克制(`flex flex-col gap-1`) +- `.hv-search-field__group` – 搜索图标、输入框与清除按钮的容器,包含边框与背景样式 +- `.hv-search-field__input` – 搜索输入字段 +- `.hv-search-field__search-icon` – 左侧显示的搜索图标 +- `.hv-search-field__clear-button` – 用于清空搜索字段的按钮 + +#### 变体类 [!toc] + +- `.hv-search-field--primary` – 带阴影的主变体(默认) +- `.hv-search-field--secondary` – 无阴影的次变体,适合用在 surface 上 + +> **说明:** 子组件([Label](/components/label)、[Description](/components/description)、[FieldError](/components/field-error))拥有各自的 CSS 类与样式。自定义方式请参见对应文档。 + +### 交互状态 + +SearchField 会根据状态自动管理以下 data 属性: + +- **Invalid**:`[data-invalid="true"]` 或 `[aria-invalid="true"]` – 无效时会自动隐藏 description 插槽 +- **Disabled**:`[data-disabled="true"]` – 当 `isDisabled` 为 true 时应用 +- **Focus Within**:`[data-focus-within="true"]` – 当输入框聚焦时应用 +- **Focus Visible**:`[data-focus-visible="true"]` – 当焦点可见(键盘导航)时应用 +- **Hovered**:`[data-hovered="true"]` – 当悬停在整个组合上时应用 +- **Empty**:`[data-empty="true"]` – 当字段为空时应用(会隐藏清除按钮) + +更多状态由 Vue 作用域插槽提供,见下方 API。 + +## API 参考 + +### SearchField + +SearchField 与 TextField 共用文本字段内核、表单策略和 ID 协议。一个根对应一个 SearchFieldInput;推荐在根使用 v-model。与父 TextField 嵌套时,搜索字段拥有独立上下文,不共享一个值。 + +| 属性 | 类型 | 默认值 | 描述 | +| ------------------------------------- | -------------------------------------------------------------------- | ---------------------- | ------------------------------------------------- | +| `modelValue` / `defaultValue` | `string` | - | 受控值 / 非受控初始值 | +| `variant` | `'primary' \| 'secondary'` | `'primary'` | 根与 Group 的视觉变体 | +| `fullWidth` | `boolean` | `false` | 根与 Group 全宽 | +| `isDisabled` / `isReadOnly` | `boolean` | `false` | 禁用 / 只读;均禁止清空,只读仍允许提交现有关键词 | +| `isRequired` | `boolean` | `false` | 原生或 aria 必填校验 | +| `isInvalid` | `boolean` | `false` | 错误展示状态,不单独阻止提交 | +| `validate` | `(value: string) => string \| string[] \| true \| null \| undefined` | - | 同步自定义校验 | +| `validationBehavior` | `'native' \| 'aria'` | 继承 Form,否则 native | native 拦截无效提交;aria 仅提供语义 | +| `name` / `id` / `type` | `string` | - / 自动 / `'search'` | 落在真实输入;叶子显式 id 优先 | +| `minLength` / `maxLength` / `pattern` | `number` / `number` / `string` | - | 下发原生约束 | +| `autoComplete` / `inputMode` / `form` | `string` | - | 下发真实输入 | +| `autoFocus` | `boolean` | `false` | 挂载后聚焦可用输入 | +| `class` / `style` | Vue class / style 绑定 | - | 根节点属性;ARIA 下发到输入 | +| 默认插槽 | 作用域插槽 | - | 字段部件及状态 | + +服务端错误通过 Form 的 validationErrors 按 name 映射;不提供 SearchField 独立 validationErrors 数组。validate 暂不接受 Promise;FieldError 无完整 React Aria validationDetails。 + +### 事件与暴露 + +| 接口 | 参数 / 类型 | 行为 | +| ------------------- | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | +| `update:modelValue` | `string` | 值变更请求;父级拒绝后恢复 DOM 值 | +| `submit` | `string` | 输入 Enter 时触发一次;忽略组合输入、重复按键和被取消的 keydown。注册 @submit 时接管该次 Enter,native 模式先校验;不再触发表单默认提交 | +| `clear` | 无参数 | 按钮或非空时 Escape 发出清空请求;恢复输入焦点,受控值仍由父级决定 | +| `focus()` | 无参数 | 模板 ref 方法,聚焦真实输入 | +| `element` | `HTMLInputElement \| undefined` | 模板 ref 公开实例中的实际控件 | + +未监听 @submit 时,Enter 保留原生表单的隐式提交。@keydown.prevent 可取消搜索键盘行为,清除按钮的 @click.prevent 可取消清空。空值时按钮透明、不可交互且移出 Tab 顺序;只读/禁用时不可清空。Escape 空值时不拦截,可由外层弹层处理。 + +### 子部件 + +| 部件 | API / 根元素 | 说明 | +| ---------------------- | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- | +| SearchFieldGroup | `div`,默认插槽,原生属性/事件 | 管理组合焦点、悬停、无效、禁用状态;继承根的变体/全宽 | +| SearchFieldInput | `input`;modelValue/defaultValue/type/invalid;原生属性/事件 | 共用 useTextControl;类型默认 search;name/约束/ARIA 落点为该 input;暴露 focus()/element | +| SearchFieldSearchIcon | 默认 SVG,或唯一默认插槽根 | 基线 IconSearch;自定义图标通过插槽合并 class,默认装饰语义 | +| SearchFieldClearButton | CloseButton;variant/isDisabled/disabled、DOM 事件及默认插槽 | type 固定 button;复用 CloseButton 外观与基线关闭图标;aria-label 默认 Clear,可按 locale 覆盖 | + +各部件 class 均支持字符串、数组、对象,合并对应基础类;不使用 className/children 或 React render prop。 + +### 作用域插槽状态 + +| 参数 | 类型 | 说明 | +| ------------------------------------------------------------ | ---------- | ---------------------- | +| `isDisabled` / `isReadOnly` / `isRequired` / `isInvalid` | `boolean` | 当前字段状态 | +| `validationErrors` | `string[]` | 当前错误列表 | +| `isEmpty` / `isHovered` / `isFocusWithin` / `isFocusVisible` | `boolean` | 值、组合悬停与焦点状态 | + +默认 SSR 关联包括直接或原生/Fragment/SearchFieldGroup 包裹的字段部件;任意 Vue 自定义包装仍需显式 id / aria-describedby 保证首屏引用,详情见字段协议。浅深主题、RTL 逻辑边距与 reduced-motion CSS 沿用基线,不据此宣称完成全部读屏/触屏/RTL 验收。 + +## 相关组件 + +[TextField](./text-field) · [InputGroup](./input-group) · [FieldError](./field-error) diff --git a/apps/docs/demos/fieldset-basic.vue b/apps/docs/demos/fieldset-basic.vue new file mode 100644 index 0000000..1469e29 --- /dev/null +++ b/apps/docs/demos/fieldset-basic.vue @@ -0,0 +1,67 @@ + +