For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/v/0.6.0/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/v/0.6.0/components/command-palette.md.
  • 简体中文
  • CommandPalette 命令面板

    快速搜索并执行操作。

    基础用法

    basic.tsx
    import { Button, CommandPalette, useMessage } from '@ifhover/leaf-ui';
    
    import { FilePlus2, Search, Settings } from 'lucide-react';
    
    import { useState } from 'react';
    
    export function CommandPaletteBasic() {
      const [open, setOpen] = useState(false);
      const { message } = useMessage();
      return (
        <>
          <Button startIcon={<Search size={16} />} variant="outline" onClick={() => setOpen(true)}>
            {'搜索命令 · Ctrl/Cmd K'}
          </Button>
          <CommandPalette
            open={open}
            onOpenChange={setOpen}
            items={[
              {
                key: 'create',
                label: '新建项目',
                icon: <FilePlus2 size={17} />,
                group: '项目',
                keywords: 'new add',
                onSelect: () => {
                  message.success('项目已创建');
                },
              },
              {
                key: 'settings',
                label: '设置',
                icon: <Settings size={17} />,
                group: '工作区',
                shortcut: 'G S',
                onSelect: () => {
                  message.info('打开设置');
                },
              },
            ]}
          />
        </>
      );
    }
    

    异步操作

    basic.tsx
    import { Button, CommandPalette } from '@ifhover/leaf-ui';
    
    import { useState } from 'react';
    
    export function CommandPaletteFailure() {
      const [open, setOpen] = useState(false);
      return (
        <>
          <Button variant="outline" onClick={() => setOpen(true)}>
            {'异步操作反馈'}
          </Button>
          <CommandPalette
            shortcut={false}
            open={open}
            onOpenChange={setOpen}
            items={[
              {
                key: 'sync',
                label: '同步项目',
                onSelect: async () => {
                  throw new Error(
                    '连接暂不可用,请重试。',
                  );
                },
              },
            ]}
          />
        </>
      );
    }
    

    CommandItem

    属性类型默认值说明
    keystring—必需。稳定且唯一的标识。
    labelReactNode—必需。展示文字或内容。
    keywordsstring—搜索匹配的额外关键词。
    groupReactNode—条目的分组标题。
    iconReactNode—图标内容。
    shortcutstring—展示在条目旁的快捷键提示。
    disabledboolean—禁用交互。
    onSelect() => void | Promise<void>—条目被选择时调用。

    CommandPalette API

    属性类型默认值说明
    itemsreadonly CommandItem[]—必需。展示的条目。
    openboolean—控制面板是否展开。
    defaultOpenbooleanfalse面板初始展开状态。
    onOpenChange(open: boolean) => void—面板展开或收起时调用。
    onSelect(item: CommandItem) => void—条目被选择时调用。
    shortcutstring | false'k'使用 Ctrl/Cmd 与该按键打开面板;false 禁用快捷键。
    placeholderstring—设置 placeholder。
    loadingboolean—展示加载状态。
    emptyContentReactNode—没有内容时的展示。
    onSearch(query: string) => void—搜索文字改变时调用。
    onError(error: unknown, item: CommandItem) => void—操作失败时调用;命令面板会保留并展示错误。

    类型与交互

    组装可复用配置时,可导入对应的属性和数据类型。

    import type { CommandItem, CommandPaletteProps } from '@ifhover/leaf-ui';

    为仅展示图标的操作提供可访问名称。交互控件支持键盘操作,并跟随 ConfigProvider 的主题和减少动画设置。