Skip to content

Command 命令面板

新粗野主义风格的命令面板组件,用于搜索和导航。基于 reka-ui 的 Listbox 原语构建,内置搜索过滤功能。

预览

Preview

基础用法

未找到结果。

建议
日历
搜索表情
计算器
设置
个人资料 ⌘P
账单 ⌘B
设置 ⌘S

命令对话框

程序化控制(filterSearch)

通过 ref 写入 filterSearch 可在不操作输入框的情况下触发过滤。

未找到结果。

建议
日历
搜索表情
计算器
设置
个人资料
设置

安装

pnpm dlx brutx-vue@latest add command

用法

vue
<script setup>
import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem, CommandSeparator, CommandShortcut } from 'brutx-ui-vue'
</script>

<template>
    <Command>
        <CommandInput placeholder="Type a command or search..." />
        <CommandList>
            <CommandEmpty>No results found.</CommandEmpty>
            <CommandGroup title="Suggestions">
                <CommandItem value="calendar">Calendar</CommandItem>
                <CommandItem value="search">Search Emoji</CommandItem>
                <CommandItem value="calculator">Calculator</CommandItem>
            </CommandGroup>
            <CommandSeparator />
            <CommandGroup title="Settings">
                <CommandItem value="profile">
                    Profile
                    <CommandShortcut>⌘P</CommandShortcut>
                </CommandItem>
                <CommandItem value="billing">
                    Billing
                    <CommandShortcut>⌘B</CommandShortcut>
                </CommandItem>
                <CommandItem value="settings">
                    Settings
                    <CommandShortcut>⌘S</CommandShortcut>
                </CommandItem>
            </CommandGroup>
        </CommandList>
    </Command>
</template>

搜索过滤

CommandInput 输入的文本会自动过滤 CommandItem,匹配逻辑基于项目文本内容。当所有项目都被过滤掉时,CommandEmpty 会自动显示;当某个分组内所有项目都被过滤掉时,该分组会自动隐藏。

vue
<Command>
    <CommandInput />
    <CommandList>
        <CommandEmpty />
        <CommandGroup title="Suggestions">
            <CommandItem value="calendar">Calendar</CommandItem>
            <CommandItem value="search">Search Emoji</CommandItem>
        </CommandGroup>
        <CommandGroup title="Settings">
            <CommandItem value="profile">Profile</CommandItem>
        </CommandGroup>
    </CommandList>
</Command>

禁用内部过滤

当外部组件自行处理过滤逻辑时(如 Combobox),可使用 disable-filter 禁用 Command 的内部搜索过滤,避免双重过滤冲突:

vue
<Command disable-filter>
    <CommandInput v-model="searchQuery" />
    <CommandList>
        <CommandEmpty />
        <CommandGroup>
            <CommandItem
                v-for="item in filteredItems"
                :key="item.value"
                :value="item.value"
            >
                {{ item.label }}
            </CommandItem>
        </CommandGroup>
    </CommandList>
</Command>

搜索组件 recipe

可直接用 Command disable-filter 组合搜索框、分组结果、最近搜索和加载状态。

vue
<script setup>
import { computed, ref } from 'vue'
import {
    Command,
    CommandInput,
    CommandList,
    CommandEmpty,
    CommandGroup,
    CommandItem,
} from 'brutx-ui-vue'

const query = ref('')
const suggestions = [
    { label: 'Button 按钮', value: 'button', group: '组件' },
    { label: '主题配置', value: 'theming', group: '文档' },
]

const filteredItems = computed(() =>
    suggestions.filter(item => item.label.toLowerCase().includes(query.value.toLowerCase()))
)
</script>

<template>
    <Command disable-filter class="w-full max-w-lg border-3 border-brutal shadow-brutal">
        <CommandInput v-model="query" placeholder="搜索组件或文档..." />
        <CommandList>
            <CommandEmpty />
            <CommandGroup title="搜索结果">
                <CommandItem v-for="item in filteredItems" :key="item.value" :value="item.value">
                    {{ item.label }}
                </CommandItem>
            </CommandGroup>
        </CommandList>
    </Command>
</template>

命令对话框

使用 CommandDialog 实现模态命令面板:

vue
<script setup>
import { ref } from 'vue'
import { CommandDialog, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from 'brutx-ui-vue'

const open = ref(false)
</script>

<template>
    <button @click="open = true">Open Command Palette</button>
    <CommandDialog v-model:open="open">
        <CommandInput placeholder="Type a command..." />
        <CommandList>
            <CommandEmpty />
            <CommandGroup title="Actions">
                <CommandItem value="new">New File</CommandItem>
                <CommandItem value="open">Open File</CommandItem>
            </CommandGroup>
        </CommandList>
    </CommandDialog>
</template>

子组件

组件说明
Command根容器,管理过滤状态,基于 ListboxRoot 构建
CommandDialog模态对话框包装器,基于 DialogRoot 构建
CommandInput搜索输入框,输入时自动过滤项目
CommandList可滚动列表容器,基于 ListboxContent 构建
CommandEmpty无匹配结果时显示
CommandGroup带标题的分组区域,过滤为空时自动隐藏
CommandItem可选项,基于 ListboxItem 构建,支持 @select 事件
CommandSeparator分组之间的视觉分隔线
CommandShortcut键盘快捷键提示

程序化控制

Command 通过 defineExpose 暴露 filterSearch 响应式引用,允许父组件程序化读取或设置搜索关键词,从而在不依赖 CommandInput 的情况下触发项目过滤。

注意:仅当内部过滤启用时(即未设置 disable-filter)写入 filterSearch 才会触发过滤逻辑;disable-filtertrue 时内部过滤被禁用,写入不会影响项目显示。

vue
<script setup>
import { ref } from 'vue'
import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from 'brutx-ui-vue'

const commandRef = ref()
</script>

<template>
    <Command ref="commandRef">
        <CommandInput />
        <CommandList>
            <CommandEmpty />
            <CommandGroup title="建议">
                <CommandItem value="calendar">日历</CommandItem>
                <CommandItem value="search">搜索表情</CommandItem>
                <CommandItem value="calculator">计算器</CommandItem>
            </CommandGroup>
        </CommandList>
    </Command>

    <button @click="commandRef?.filterSearch = 'cal'">外部触发搜索 "cal"</button>
    <button @click="commandRef?.filterSearch = ''">清除搜索</button>
</template>

暴露的 API

方法/属性类型说明
filterSearchRef<string>当前搜索关键词,可读写;写入后会触发内部过滤逻辑(需 disableFilterfalse

Props

Command

属性类型默认值说明
disableFilterbooleanfalse禁用内部搜索过滤,适用于外部自行过滤的场景
classstring自定义 CSS 类名

CommandInput

属性类型默认值说明
modelValuestring输入框的值,支持 v-model
placeholderstringt('command.placeholder')占位符文本
classstring自定义 CSS 类名

CommandItem

属性类型默认值说明
valuestring项目的唯一标识值
disabledboolean是否禁用该项目
classstring自定义 CSS 类名

CommandGroup

属性类型默认值说明
titlestring分组标题文本
classstring自定义 CSS 类名

CommandList

属性类型默认值说明
classstring自定义 CSS 类名

CommandEmpty

属性类型默认值说明
classstring自定义 CSS 类名

CommandSeparator

属性类型默认值说明
classstring自定义 CSS 类名

CommandShortcut

属性类型默认值说明
classstring自定义 CSS 类名

CommandDialog

属性类型默认值说明
openbooleanfalse对话框是否打开,支持 v-model:open
titlestringt('command.dialogTitle')对话框标题(无障碍访问用)
descriptionstringt('command.dialogDescription')对话框描述(无障碍访问用)
classstring自定义 CSS 类名

事件

CommandInput

事件参数说明
update:modelValuestring输入值变化时触发

CommandItem

事件参数说明
selectstring选中项目时触发

CommandDialog

事件参数说明
update:openboolean对话框开关状态变化时触发

插槽

组件插槽说明
Commanddefault用于放置 CommandInputCommandList 等子组件
CommandDialogdefault用于放置 CommandInputCommandList 等子组件
CommandListdefault用于放置 CommandEmptyCommandGroup 等子组件
CommandGroupdefault用于放置 CommandItem 子组件
CommandItemdefault用于放置项目内容和 CommandShortcut
CommandEmptydefault自定义无匹配结果时的显示内容,默认值为 t('command.emptyText')
CommandShortcutdefault用于放置快捷键文本

可访问性

  • 键盘操作:支持 / 上下移动焦点,Enter 选中当前项,Escape 关闭对话框(CommandDialog 内)
  • ARIA 属性:CommandDialog 支持 titledescription 属性用于无障碍访问
  • 焦点管理:打开对话框时焦点锁定在组件内,关闭时恢复焦点

蛮力铸就。