Docs
Combobox
Combobox
Autocomplete input and command palette with a list of suggestions.
关于
<Command />
组件使用 pacocourse 的 cmdk
组件。
安装
npx shadcn-ui@latest add command
用法
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut,
} from "@/components/ui/command"
<Command>
<CommandInput placeholder="输入命令或搜索..." />
<CommandList>
<CommandEmpty>找不到结果。</CommandEmpty>
<CommandGroup heading="建议">
<CommandItem>日历</CommandItem>
<CommandItem>搜索表情</CommandItem>
<CommandItem>计算器</CommandItem>
</CommandGroup>
<CommandSeparator />
<CommandGroup heading="设置">
<CommandItem>个人资料</CommandItem>
<CommandItem>账单</CommandItem>
<CommandItem>设置</CommandItem>
</CommandGroup>
</CommandList>
</Command>
示例
对话框
Press ⌘J
要在对话框中显示命令菜单,请使用 <CommandDialog />
组件。
export function CommandMenu() {
const [open, setOpen] = React.useState(false)
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault()
setOpen((open) => !open)
}
}
document.addEventListener("keydown", down)
return () => document.removeEventListener("keydown", down)
}, [])
return (
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput placeholder="输入命令或搜索..." />
<CommandList>
<CommandEmpty>找不到结果。</CommandEmpty>
<CommandGroup heading="建议">
<CommandItem>日历</CommandItem>
<CommandItem>搜索表情</CommandItem>
<CommandItem>计算器</CommandItem>
</CommandGroup>
</CommandList>
</CommandDialog>
)
}
组合框
你可以将 <Command />
组件用作组合框。有关详细信息,请参见 组合框 页面。