Trên trang này

Combobox

Cài đặt, prop và các phần tạo nên Combobox.

Cài đặt

Chạy lệnh này trong app, sau shadcn-vue init và theme Beehexa (Button › Code):

npx shadcn-vue@latest add https://beehexa.design/r/combobox.json

Lệnh này cũng cài Input group, phần vẽ ô tìm.

Cách dùng

<script setup lang="ts">
import { Check } from '@lucide/vue'
import { ref } from 'vue'
import {
  Combobox, ComboboxAnchor, ComboboxEmpty, ComboboxInput,
  ComboboxItem, ComboboxItemIndicator, ComboboxList, ComboboxViewport,
} from '@/components/ui/combobox'

const warehouses = [{ id: 'hn', name: 'Hà Nội, Long Biên' }, { id: 'dn', name: 'Đà Nẵng, Hải Châu' }]
const warehouse = ref<(typeof warehouses)[number]>()
</script>

<template>
  <label for="ship-from">Giao từ kho</label>
  <Combobox v-model="warehouse" by="id">
    <ComboboxAnchor>
      <ComboboxInput id="ship-from" :display-value="(item) => item?.name ?? ''" placeholder="Tìm kho…" />
    </ComboboxAnchor>
    <ComboboxList>
      <ComboboxEmpty>Không tìm thấy kho.</ComboboxEmpty>
      <ComboboxViewport>
        <ComboboxItem v-for="item in warehouses" :key="item.id" :value="item">
          {{ item.name }}
          <ComboboxItemIndicator><Check /></ComboboxItemIndicator>
        </ComboboxItem>
      </ComboboxViewport>
    </ComboboxList>
  </Combobox>
</template>

Prop

Phần Prop Giá trị Ghi chú
Combobox v-model, defaultValue object hoặc chuỗi; mảng khi có multiple Không bao giờ là chuỗi rỗng
Combobox by tên khóa hoặc hàm So sánh giá trị object, như by="id"
Combobox multiple, disabled, openOnClick, openOnFocus boolean
Combobox ignoreFilter boolean Tắt bộ lọc sẵn có khi danh sách lấy từ tìm kiếm trên server
ComboboxInput displayValue (value) => string Chữ hiện trong ô cho giá trị đang chọn
ComboboxList side, align, sideOffset Mặc định: bên dưới, căn giữa, 4 (space.overlay.offset)
ComboboxGroup heading chuỗi Tiêu đề nhóm, font.label.small
ComboboxItem value, disabled
ComboboxTrigger as-child boolean Dùng với Button để mở danh sách từ một nút

Các phần khác: ComboboxAnchor, ComboboxViewport, ComboboxEmpty, ComboboxItemIndicator, ComboboxSeparator, ComboboxCancel.

Dựng từ

Bọc Combobox của reka-ui theo API Combobox của shadcn-vue. reka-ui lo vai trò combobox và listbox, bộ lọc, xử lý bàn phím và tự đổi bên khi bị che. Beehexa thêm style và focus kiểu ô nhập cho ô tìm.

Giới hạn

  • Danh sách hiện và ẩn bằng class animation của shadcn-vue, chưa dùng token motion (motion.overlay.enter, .exit).
  • reka-ui chèn một thẻ <style> vào danh sách, và mức style chặt của Content Security Policy chặn thẻ này. Danh sách vẫn ẩn thanh cuộn bằng CSS của kit; để trình duyệt không báo lỗi ở mức đó, thêm plugin Vite ở Tools › Gói shadcn-vue.