53e76f676d
HD-02 scope, all against heroui ce90ea3 cn sources:
- card: add the text-primary class to the coin icons (source parity);
card-with-form drops the added required validation and inline business
note, restores the plain href="#" forgot-password link and the baseline
alert("表单提交成功!") submit feedback (autocomplete kept: no visible
impact, allowed by the task card)
- input-group-with-textarea: restore the four Tooltips and the Enter Kbd
hint; drop the stale "Tooltip 组件尚未迁移" note from input-group.md
- alert-dialog: restore the h-px top light line in custom-styles and the
truncated/missing description text in sizes/custom-portal/
custom-animations (+ controlled); align the fluid-slide icon with the
modal mapping (IconArrowBarUp for gravity-ui:arrow-up-from-line)
- toast: restore the upstream demo layout shells (variants/simple/
custom-indicator/custom-queue) and the action button default variant:
ToastActionButton now falls back to the Button defaults (primary/md)
like upstream ToastActionButton, so variants' actions render primary,
danger stays danger and default stays tertiary; toast.md defaults fixed
- submit demos: restore baseline feedback and structure — alert() for
card/fieldset/form/checkbox/checkbox-group/switch/select/combo-box/
autocomplete and console.log for search/number/time field form examples;
modal-with-form returns to the close-only footer (no submit/result row);
drop the added inline status/result rows and the max-w-full/
max-sm:min-w-0 width guards from the restored demos
- radio-group-validation: add the missing text-muted to the result line;
label.md invalid example gives the real control its invalid state and
matches the baseline input shape
Tests re-pinned to the baseline contracts (alert/console/close behavior,
four tooltips + Kbd, positional modal footer); full suite 110 files /
1617 tests, typecheck, build:docs and the m35-m36 tarball contract pass.
273 lines
13 KiB
Markdown
273 lines
13 KiB
Markdown
---
|
||
title: SearchField 搜索框
|
||
description: 搜索输入字段,包含清除按钮与搜索图标。
|
||
component: search-field
|
||
group: 组件
|
||
---
|
||
|
||
## 用法
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
SearchField,
|
||
SearchFieldGroup,
|
||
SearchFieldInput,
|
||
SearchFieldSearchIcon,
|
||
SearchFieldClearButton,
|
||
Label,
|
||
Description,
|
||
FieldError,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
```
|
||
|
||
<ComponentPreview name="search-field-basic" />
|
||
|
||
## 组件结构
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
SearchField,
|
||
SearchFieldGroup,
|
||
SearchFieldInput,
|
||
SearchFieldSearchIcon,
|
||
SearchFieldClearButton,
|
||
Label,
|
||
Description,
|
||
FieldError,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
|
||
<template>
|
||
<SearchField>
|
||
<Label />
|
||
<SearchFieldGroup>
|
||
<SearchFieldSearchIcon />
|
||
<SearchFieldInput />
|
||
<SearchFieldClearButton />
|
||
</SearchFieldGroup>
|
||
<Description />
|
||
<FieldError />
|
||
</SearchField>
|
||
</template>
|
||
```
|
||
|
||
> **SearchField** 允许用户输入并清空搜索关键词。它包含搜索图标,并提供可选的清除按钮以便快速重置。
|
||
|
||
## 示例
|
||
|
||
### 变体
|
||
|
||
SearchField 组件支持两种视觉变体:
|
||
|
||
- **`primary`**(默认)— 带阴影的标准样式,适用于大多数场景
|
||
- **`secondary`** — 低强调、无阴影,适合在 Surface 等表面背景上使用
|
||
|
||
<ComponentPreview name="search-field-variants" />
|
||
|
||
### 表面样式
|
||
|
||
在 [Surface](/components/surface) 内使用时,请使用 `variant="secondary"`,以应用适合表面背景的低强调变体。
|
||
|
||
<ComponentPreview name="search-field-on-surface" />
|
||
|
||
### 带描述
|
||
|
||
<ComponentPreview name="search-field-with-description" />
|
||
|
||
### 必填字段
|
||
|
||
<ComponentPreview name="search-field-required" />
|
||
|
||
### 禁用状态
|
||
|
||
<ComponentPreview name="search-field-disabled" />
|
||
|
||
### 宽度充满
|
||
|
||
<ComponentPreview name="search-field-full-width" />
|
||
|
||
### 表单校验
|
||
|
||
将 `is-invalid` 与 `FieldError` 配合使用,以展示校验消息。
|
||
|
||
<ComponentPreview name="search-field-validation" />
|
||
|
||
### 受控组件
|
||
|
||
使用 v-model 控制值 以与其他组件同步或执行自定义格式化。
|
||
|
||
<ComponentPreview name="search-field-controlled" />
|
||
|
||
### 表单示例
|
||
|
||
完整的表单集成示例,包含校验与模拟提交(提交值输出到控制台);没有后台搜索请求。
|
||
|
||
<ComponentPreview name="search-field-form-example" />
|
||
|
||
### 带校验
|
||
|
||
通过受控 value 实现自定义校验逻辑。
|
||
|
||
<ComponentPreview name="search-field-with-validation" />
|
||
|
||
### 自定义图标
|
||
|
||
自定义搜索图标与清除按钮图标。
|
||
|
||
<ComponentPreview name="search-field-custom-icons" />
|
||
|
||
### 键盘快捷键
|
||
|
||
添加快捷键以快速聚焦搜索框。
|
||
|
||
<ComponentPreview name="search-field-with-keyboard-shortcut" />
|
||
|
||
### 渲染函数
|
||
|
||
基线示例的根为 div + data-custom;Vue 通过根属性透传保留效果,暂不提供 React render 根类型替换。
|
||
|
||
<ComponentPreview
|
||
name="search-field-render-function"
|
||
/>
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="search-field-custom-styles" />
|
||
|
||
### 全局 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)
|