Trên trang này

Command

Ví dụ chạy được của Command, kèm đúng file Vue tạo ra từng ví dụ.

Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào app đã cài registry, hoặc mở trong StackBlitz. Gõ để lọc; dùng phím mũi tên và Enter.

Trong trang

Hai nhóm có icon và phím tắt. Nhóm không có kết quả thì ẩn; thông báo trống hiện khi không có gì khớp.

Đi tới
Đơn hàng
Sản phẩm
Khách hàng
Thao tác
Đồng bộ ngayCtrl+R
Kết nối cửa hàng online
Cài đặtCtrl+,
CommandInline.vue
<script setup lang="ts">
import { Boxes, PackageSearch, RefreshCw, Settings, Store, Users } from '@lucide/vue'
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator,
  CommandShortcut,
} from '@/components/ui/command'
</script>

<template>
  <Command class="max-w-sm border-(length:--bh-border-width-default) border-(--bh-color-border)">
    <CommandInput placeholder="Tìm trang và thao tác" />
    <CommandList>
      <CommandEmpty>Không có kết quả phù hợp.</CommandEmpty>
      <CommandGroup heading="Đi tới">
        <CommandItem value="orders"><PackageSearch />Đơn hàng</CommandItem>
        <CommandItem value="products"><Boxes />Sản phẩm</CommandItem>
        <CommandItem value="customers"><Users />Khách hàng</CommandItem>
      </CommandGroup>
      <CommandSeparator />
      <CommandGroup heading="Thao tác">
        <CommandItem value="sync"><RefreshCw />Đồng bộ ngay<CommandShortcut>Ctrl+R</CommandShortcut></CommandItem>
        <CommandItem value="store"><Store />Kết nối cửa hàng online</CommandItem>
        <CommandItem value="settings"><Settings />Cài đặt<CommandShortcut>Ctrl+,</CommandShortcut></CommandItem>
      </CommandGroup>
    </CommandList>
  </Command>
</template>

Bảng lệnh

CommandDialog mở bằng một nút hoặc Ctrl + K. Tiêu đề và mô tả của nó dành cho trình đọc màn hình.

CommandPalette.vue
<script setup lang="ts">
import { FileText, PackageSearch, RefreshCw, Store } from '@lucide/vue'
import { useMagicKeys, whenever } from '@vueuse/core'
import { ref } from 'vue'
import { Button } from '@/components/ui/button'
import {
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/command'

const open = ref(false)
const { Ctrl_K, Meta_K } = useMagicKeys({
  passive: false,
  onEventFired(e) {
    if (e.key === 'k' && (e.ctrlKey || e.metaKey) && e.type === 'keydown') e.preventDefault()
  },
})
whenever(() => Ctrl_K.value || Meta_K.value, () => { open.value = true })

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

<template>
  <Button variant="outline" aria-keyshortcuts="Control+K" @click="open = true">Tìm kiếm (Ctrl+K)</Button>
  <CommandDialog v-model:open="open" title="Tìm kiếm" description="Tìm một trang hoặc một thao tác để chạy.">
    <CommandInput placeholder="Gõ tên trang, mã đơn hoặc thao tác" />
    <CommandList>
      <CommandEmpty>Không có kết quả phù hợp.</CommandEmpty>
      <CommandGroup heading="Gần đây">
        <CommandItem value="order-10482" @select="run"><PackageSearch />Đơn #10482</CommandItem>
        <CommandItem value="invoice-2211" @select="run"><FileText />Hóa đơn INV-2211</CommandItem>
      </CommandGroup>
      <CommandGroup heading="Thao tác">
        <CommandItem value="sync-orders" @select="run"><RefreshCw />Đồng bộ đơn hàng sang ERP</CommandItem>
        <CommandItem value="connect-store" @select="run"><Store />Kết nối cửa hàng online</CommandItem>
      </CommandGroup>
    </CommandList>
  </CommandDialog>
</template>