Skip to content

Command 命令面板

可搜索的命令面板,用于快速导航和操作,支持键盘操作和弹窗模式。

名称分类说明类型默认值
maxHeight? 风格列表最大高度string'300px'
disabled? 状态是否禁用booleanfalse
modelValue / v-model? 状态是否打开弹窗booleanfalse
filter? 行为自定义过滤函数(value: string, search: string, keywords?: string[]) => boolean
shortcut? 行为启用 Ctrl+K 快捷键booleantrue
ArrowDown? 行为高亮下一项
ArrowUp? 行为高亮上一项
Enter? 行为选择高亮项
Escape? 行为关闭弹窗
Ctrl+K / Cmd+K? 行为切换命令面板弹窗
modelValue / v-model? 内容搜索文本string''
label? 内容aria-label 标签string
placeholder? 内容输入框占位文本string''
icon? 内容搜索图标名称string'search'
value? 内容项目唯一值(必填)string
keywords? 内容额外的搜索关键词string[][]
icon? 内容项目图标string
shortcut? 内容快捷键标签string[][]
heading? 内容分组标题string
update:modelValue? 事件搜索文本变更时触发(value: string) => void
select? 事件选择项目时触发(value: string) => void
update:modelValue? 事件弹窗状态变更时触发(value: boolean) => void
open? 事件弹窗打开时触发() => void
close? 事件弹窗关闭时触发() => void

基础用法

一个简单的命令菜单,支持搜索过滤。

<template>
  <px-command label="Command menu">
    <px-command-input placeholder="Type a command or search..." />
    <px-command-list>
      <px-command-empty>No results found.</px-command-empty>
      <px-command-item value="calendar" icon="calendar">Calendar</px-command-item>
      <px-command-item value="search" icon="search">Search</px-command-item>
      <px-command-item value="settings" icon="settings">Settings</px-command-item>
      <px-command-item value="profile" icon="user">Profile</px-command-item>
    </px-command-list>
  </px-command>
</template>

<style scoped>
.px-command {
  max-width: 480px;
}
</style>

分组项目

使用 CommandGroup 按标题组织项目,使用 CommandSeparator 添加视觉分隔线。

<template>
  <px-command label="Grouped command menu">
    <px-command-input placeholder="Search commands..." />
    <px-command-list>
      <px-command-empty>No results found.</px-command-empty>
      <px-command-group heading="Navigation">
        <px-command-item value="home" icon="home">Home</px-command-item>
        <px-command-item value="dashboard" icon="analytics">Dashboard</px-command-item>
        <px-command-item value="settings" icon="settings">Settings</px-command-item>
      </px-command-group>
      <px-command-separator />
      <px-command-group heading="Actions">
        <px-command-item value="new-file" icon="add-box">New File</px-command-item>
        <px-command-item value="upload" icon="upload">Upload</px-command-item>
        <px-command-item value="download" icon="download">Download</px-command-item>
      </px-command-group>
    </px-command-list>
  </px-command>
</template>

<style scoped>
.px-command {
  max-width: 480px;
}
</style>

弹窗模式

使用 CommandDialog 在模态弹窗中显示命令面板。按 Ctrl+K(Mac 上为 Cmd+K)切换。

<template>
  <div>
    <px-button type="primary" @click="open = true">
      Open Command Palette
      <px-badge size="small" type="info">Ctrl+K</px-badge>
    </px-button>

    <px-command-dialog v-model="open">
      <px-command @select="onSelect" label="Command palette">
        <px-command-input placeholder="What do you need?" />
        <px-command-list>
          <px-command-empty>No results found.</px-command-empty>
          <px-command-group heading="Pages">
            <px-command-item value="home" icon="home" :shortcut="['Ctrl', 'H']">Home</px-command-item>
            <px-command-item value="docs" icon="bookmark" :shortcut="['Ctrl', 'D']">Documentation</px-command-item>
          </px-command-group>
          <px-command-separator />
          <px-command-group heading="Theme">
            <px-command-item value="light" icon="sun">Light Mode</px-command-item>
            <px-command-item value="dark" icon="moon">Dark Mode</px-command-item>
          </px-command-group>
        </px-command-list>
      </px-command>
    </px-command-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';

const open = ref(false);

function onSelect() {
  open.value = false;
}
</script>

自定义过滤

提供自定义 filter 函数进行高级匹配(例如模糊搜索)。

<template>
  <px-command :filter="fuzzyFilter" label="Custom filter">
    <px-command-input placeholder="Try fuzzy search..." />
    <px-command-list>
      <px-command-empty>No matches.</px-command-empty>
      <px-command-item value="calendar">Calendar</px-command-item>
      <px-command-item value="calculator">Calculator</px-command-item>
      <px-command-item value="contacts">Contacts</px-command-item>
      <px-command-item value="configuration">Configuration</px-command-item>
    </px-command-list>
  </px-command>
</template>

<script setup lang="ts">
function fuzzyFilter(value: string, search: string): boolean {
  if (!search) return true;
  const chars = search.toLowerCase().split('');
  let idx = 0;
  for (const char of value.toLowerCase()) {
    if (char === chars[idx]) idx++;
    if (idx === chars.length) return true;
  }
  return false;
}
</script>

<style scoped>
.px-command {
  max-width: 480px;
}
</style>

禁用项目

设置 disabled 来阻止选择。禁用项目在键盘导航时会被跳过。

<template>
  <px-command label="Command with disabled items">
    <px-command-input placeholder="Search..." />
    <px-command-list>
      <px-command-item value="save" icon="check">Save</px-command-item>
      <px-command-item value="delete" icon="close" disabled>Delete (Disabled)</px-command-item>
      <px-command-item value="export" icon="download">Export</px-command-item>
      <px-command-item value="archive" icon="archive" disabled>Archive (Disabled)</px-command-item>
    </px-command-list>
  </px-command>
</template>

<style scoped>
.px-command {
  max-width: 480px;
}
</style>