CommandSearch (顶栏搜索)
常驻的搜索框,聚焦时在下方展开分组结果:最近搜索、搜索建议、回车前往搜索页。和 CommandPalette 共用条目与渲染,但没有对话框,焦点始终留在输入框。
常驻在顶栏里的搜索框:聚焦时在框下方展开结果面板,输入时显示分组结果,回车前往完整的搜索页 —— Slack、Teams、GitHub、Jira、Steam 的顶栏搜索都是这种形态。它和 KunCommandPalette 共用同一套条目(KunCommandItem / 分组)、渲染与高亮,区别只在外壳:没有对话框、遮罩、滚动锁和焦点陷阱,焦点始终留在输入框里。 和面板一样,它不含搜索逻辑:用 v-model:query 拿到查询,自己算出 items。
基础用法
空输入框聚焦时显示你给的条目(这里是「最近搜索」);既没有条目、也没有 #empty 插槽时不弹出空面板。 输入后显示「搜索「…」」动作行和分组结果。和命令面板不同,默认不高亮任何一行: 回车触发 @submit,带上去掉首尾空格的查询;用 ↑↓ 移到某一行后回车才是 @select。 ↑ 从第一行再往上,回到「没有高亮」的状态。
第一次按 Esc 收起面板并保留文字,再按一次清空文字。Tab、点击外部、选中或提交都会收起面板。 带 removable 的条目(最近搜索)行尾有移除按钮,键盘上按 Shift+Delete 移除当前高亮行;两者都触发 @remove,由你把它从 items 里删掉。 「清空搜索记录」就是一条普通条目。
防抖和请求都由你来做;请求开始时就把 loading 设为 true,输入框左侧的放大镜会变成加载圈。 已经显示的结果在新结果到达前保留,列表不会每敲一个字就闪一次。
最近一次:—
窄屏:换成命令面板
手机顶栏放不下常驻搜索框。组件本身不按屏宽切换(用 JS 判断屏宽会让 SSR 渲染出错的布局), 而是由你用断点类组合:md 以上显示 KunCommandSearch,以下显示 KunCommandPalette 的图标按钮,两者共用同一份 query 和 items。
快捷键默认是 ⌘K / Ctrl K,作用是聚焦搜索框。搜索框不在屏幕上(被断点隐藏)或在打开的模态框后面时, 它不响应,同一页上的 KunCommandPalette 照常接手打开面板;搜索框在屏幕上时,面板则让出这个快捷键。 这里的示例关掉了快捷键,以免和本站顶部的搜索冲突。
最近一次:—
属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| ariaLabel | string | "" | Accessible name for the field. |
| className | string | — | Classes for the root wrapper: give it the field's width here. |
| highlight | boolean | true | Highlight the query terms in the default item render. |
| items | readonly KunCommandItem[] | readonly KunCommandGroup<KunCommandItem>[] | [] | Results to show under the field: a flat item list (one unlabelled group) OR grouped, the same shape KunCommandPalette takes. With an empty `query` these are what focus shows (recent searches, say). You compute them from `v-model:query`; the field does no matching itself. |
| loading | boolean | false | Async search in flight: the field's search icon becomes a spinner. With no rows to show, the panel shows a loading state instead of the no-result text; rows already showing stay until new ones arrive, so the list does not blink on every keystroke. |
| maxHeight | number | 420 | Upper bound on the panel's height in px. The panel is also never taller than the room below the field. |
| noResultText | string | locale commandPalette.noResult | Shown when `query` is non-empty but there are no results. |
| placeholder | string | locale commandPalette.placeholder | Placeholder in the field; also its accessible name when `ariaLabel` is unset. |
| query | string | "" | |
| rounded | KunUIRounded | the KunUI config `rounded` | Corner radius of the field; the panel follows it. |
| shortcut | string | boolean | true | Shortcut that focuses the field from anywhere. `true` (default) = ⌘K / Ctrl-K; a single-char string sets a custom key (still with meta/ctrl); `false` disables it. A hidden field ignores it, so a KunCommandPalette shown in its place on narrow screens takes the same shortcut. |
| size | KunUISize | "md" | Field size. |
事件
| 事件 | 回调参数 | 说明 |
|---|---|---|
| remove | item: KunCommandItem | The remove button or Shift+Delete on a `removable` row. Drop the item from `items`; the highlight stays on the row that takes its place. |
| select | item: KunCommandItem | The row the user clicked, or highlighted with the arrow keys and confirmed with Enter. A disabled row never emits. |
| submit | query: string | Enter with no row highlighted, carrying the trimmed query: the way to the full search page. Nothing is highlighted until an arrow key moves onto a row, so Enter after typing always lands here. |
| update:query | value: string |
插槽
| 插槽 | 作用域 |
|---|---|
| #empty | — |
| #footer | — |
| #item | { item: KunCommandItem; active: boolean; index: number; highlight: (text: string) => string; } |
| #loading | — |
| #no-result | { query: string; } |