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, rồi dùng phím mũi tên và Enter.
Ô tìm kiếm
Ô nhập chính là ô tìm. Gõ một phần tên kho; danh sách chỉ giữ những kho khớp.
ComboboxBasic.vue
<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'
interface Warehouse {
id: string
name: string
}
const warehouses: Warehouse[] = [
{ id: 'hcm-d7', name: 'TP. Hồ Chí Minh, Quận 7' },
{ id: 'hcm-td', name: 'TP. Hồ Chí Minh, Thủ Đức' },
{ id: 'hn-lb', name: 'Hà Nội, Long Biên' },
{ id: 'dn-hc', name: 'Đà Nẵng, Hải Châu' },
{ id: 'ct-nk', name: 'Cần Thơ, Ninh Kiều' },
]
const warehouse = ref<Warehouse>()
</script>
<template>
<div class="grid w-full max-w-72 gap-(--bh-space-100)">
<label for="ship-from" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Giao từ kho</label>
<Combobox v-model="warehouse" by="id" open-on-click>
<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>
</div>
</template>Nút có ô tìm trong danh sách
Một nút Default mở danh sách, trong đó có ô tìm riêng. Dùng khi một ô nhập sẽ chiếm quá nhiều chỗ, như phía trên bảng. Tên của nút ghép nhãn và giá trị bằng aria-labelledby.
Cửa hàng
ComboboxButton.vue
<script setup lang="ts">
import { Check, ChevronsUpDown } from '@lucide/vue'
import { ref } from 'vue'
import { Button } from '@/components/ui/button'
import {
Combobox,
ComboboxAnchor,
ComboboxEmpty,
ComboboxGroup,
ComboboxInput,
ComboboxItem,
ComboboxItemIndicator,
ComboboxList,
ComboboxSeparator,
ComboboxTrigger,
ComboboxViewport,
} from '@/components/ui/combobox'
interface Store {
id: string
name: string
}
const groups: { heading: string, stores: Store[] }[] = [
{ heading: 'Cửa hàng online', stores: [{ id: 'web', name: 'Cửa hàng online chính' }, { id: 'b2b', name: 'Cửa hàng bán sỉ' }] },
{ heading: 'Sàn thương mại điện tử', stores: [{ id: 'mp-1', name: 'Sàn A' }, { id: 'mp-2', name: 'Sàn B' }] },
]
const store = ref<Store>()
</script>
<template>
<div class="grid w-full max-w-64 gap-(--bh-space-100)">
<span id="store-label" class="text-(--bh-color-text) [font:var(--bh-font-label-default)]">Cửa hàng</span>
<Combobox v-model="store" by="id">
<ComboboxAnchor as-child>
<ComboboxTrigger as-child>
<Button id="store-picker" variant="outline" class="w-full justify-between" aria-labelledby="store-label store-picker">
{{ store?.name ?? 'Chọn cửa hàng' }}
<ChevronsUpDown />
</Button>
</ComboboxTrigger>
</ComboboxAnchor>
<ComboboxList>
<ComboboxInput placeholder="Tìm cửa hàng…" aria-label="Tìm cửa hàng" />
<ComboboxEmpty>Không tìm thấy cửa hàng.</ComboboxEmpty>
<ComboboxViewport>
<template v-for="(group, index) in groups" :key="group.heading">
<ComboboxSeparator v-if="index > 0" />
<ComboboxGroup :heading="group.heading">
<ComboboxItem v-for="item in group.stores" :key="item.id" :value="item">
{{ item.name }}
<ComboboxItemIndicator>
<Check />
</ComboboxItemIndicator>
</ComboboxItem>
</ComboboxGroup>
</template>
</ComboboxViewport>
</ComboboxList>
</Combobox>
</div>
</template>