Command

用于搜索命令或目标,并通过键盘快速选择。

预览

可搜索的城市命令

筛选四个城市并选择本地结果,查看空结果提示与快捷键文字。

正在加载预览。

安装

首次使用此 Registry?请先完成 基础配置与共享样式安装 。

npx shadcn@latest add @neumorphism-ui/command

先安装一次共享 base,然后使用 CLI 添加组件及其依赖。

Registry JSON

用法

组合 CommandInput 与 CommandList,将 CommandItem 分组并提供 CommandEmpty。把 onSelect 接入实际操作;快捷键文字本身不会注册快捷键。

"use client";

import { useState } from "react";
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command";

export default function Example() {
  const [selection, setSelection] = useState("No command selected.");
  return (
    <div>
      <Command>
        <CommandInput placeholder="Search commands" />
        <CommandList>
          <CommandEmpty>No commands found.</CommandEmpty>
          <CommandGroup heading="Navigation">
            <CommandItem value="projects" onSelect={() => setSelection("Projects selected.")}>Projects</CommandItem>
            <CommandItem value="settings" onSelect={() => setSelection("Settings selected.")}>Settings</CommandItem>
          </CommandGroup>
        </CommandList>
      </Command>
      <p role="status">{selection}</p>
    </div>
  );
}

API 参考

保留 cmdk 的筛选、活动项和键盘导航,并将输入区呈现为内嵌表面、活动项呈现为按下状态。CommandDialog 可与现有 Dialog 组合。

组件属性类型默认值说明
CommandshouldFilterbooleantrue决定是否使用 cmdk 内置搜索筛选。
Commandvalue / onValueChangestring / (value: string) => void—控制当前活动项。
CommandInputvalue / onValueChangestring / (value: string) => void—控制搜索词,或作为非受控输入使用。
CommandItemvalue / onSelectstring / (value: string) => void—指定项目的搜索值和选择回调。
CommandItemdisabledbooleanfalse让项目保留在结果模型中但不可选择。

无障碍

  • 01

    为 CommandInput 提供能说明搜索用途的 placeholder 或 aria-label。

  • 02

    使用方向键移动活动项并按 Enter 选择,同时保留可见焦点。

  • 03

    筛选无结果时使用 CommandEmpty 提供明确的空状态。