Combobox
A field with a list that narrows as you type.
- Peter Wolfe
- Lena Ortiz
- Tim Sayed
- Vera Lane
- Glen Nash
- Evan Porter
- Mary Cooper
- Paul Hawkins
- Olga Graham
- Sam Ellis
- Nina Hart
- Alina Yusuf
<template> <NuiCombobox v-model="customer" :options="buyers" label="Customer" placeholder="Search by name" clearable /></template><script setup lang="ts">import { ref } from 'vue';import { NuiCombobox } from '@numior/kit';import { customers } from './customers';const customer = ref<number | null>(null);const buyers = customers.map((person) => ({ value: person.id, label: person.name }));</script>export interface Customer { readonly id: number; readonly name: string; readonly city: string; readonly email: string; readonly phone: string; readonly orders: number; readonly spent: number;}export const customers: readonly Customer[] = [ { id: 1, name: 'Peter Wolfe', city: 'Boston', email: 'peter.wolfe@example.com', phone: '(617) 555-0101', orders: 14, spent: 2480, }, { id: 2, name: 'Lena Ortiz', city: 'Chicago', email: 'lena.ortiz@example.com', phone: '(312) 555-0102', orders: 3, spent: 295, }, { id: 3, name: 'Tim Sayed', city: 'Denver', email: 'tim.sayed@example.com', phone: '(303) 555-0103', orders: 8, spent: 1920, }, { id: 4, name: 'Vera Lane', city: 'Boston', email: 'vera.lane@example.com', phone: '(617) 555-0104', orders: 1, spent: 180, }, { id: 5, name: 'Glen Nash', city: 'Austin', email: 'glen.nash@example.com', phone: '(512) 555-0105', orders: 5, spent: 645, }, { id: 6, name: 'Evan Porter', city: 'Seattle', email: 'evan.porter@example.com', phone: '(206) 555-0106', orders: 2, spent: 440, }, { id: 7, name: 'Mary Cooper', city: 'Portland', email: 'mary.cooper@example.com', phone: '(503) 555-0107', orders: 11, spent: 1300, }, { id: 8, name: 'Paul Hawkins', city: 'Boston', email: 'paul.hawkins@example.com', phone: '(617) 555-0108', orders: 6, spent: 965, }, { id: 9, name: 'Olga Graham', city: 'Chicago', email: 'olga.graham@example.com', phone: '(312) 555-0109', orders: 4, spent: 530, }, { id: 10, name: 'Sam Ellis', city: 'Denver', email: 'sam.ellis@example.com', phone: '(303) 555-0110', orders: 9, spent: 2190, }, { id: 11, name: 'Nina Hart', city: 'Austin', email: 'nina.hart@example.com', phone: '(512) 555-0111', orders: 2, spent: 755, }, { id: 12, name: 'Alina Yusuf', city: 'Portland', email: 'alina.yusuf@example.com', phone: '(503) 555-0112', orders: 7, spent: 820, },];Pass the options to options, as for a Select. The list keeps the options whose label contains the typed text, in any letter case.
Installation
import { NuiCombobox } from '@numior/kit';Usage
Change the size and the states of the field, then find a customer by name.
- Peter Wolfe
- Lena Ortiz
- Tim Sayed
- Vera Lane
- Glen Nash
- Evan Porter
- Mary Cooper
- Paul Hawkins
- Olga Graham
- Sam Ellis
- Nina Hart
- Alina Yusuf
<template> <NuiCombobox placeholder="Start typing a name" label="Customer" v-model="customer" :options="buyers" /></template><script setup lang="ts">import { ref } from 'vue';import { NuiCombobox } from '@numior/kit';import { customers } from './customers';const customer = ref<number | null>(null);const buyers = customers.map((person) => ({ value: person.id, label: person.name }));</script>export interface Customer { readonly id: number; readonly name: string; readonly city: string; readonly email: string; readonly phone: string; readonly orders: number; readonly spent: number;}export const customers: readonly Customer[] = [ { id: 1, name: 'Peter Wolfe', city: 'Boston', email: 'peter.wolfe@example.com', phone: '(617) 555-0101', orders: 14, spent: 2480, }, { id: 2, name: 'Lena Ortiz', city: 'Chicago', email: 'lena.ortiz@example.com', phone: '(312) 555-0102', orders: 3, spent: 295, }, { id: 3, name: 'Tim Sayed', city: 'Denver', email: 'tim.sayed@example.com', phone: '(303) 555-0103', orders: 8, spent: 1920, }, { id: 4, name: 'Vera Lane', city: 'Boston', email: 'vera.lane@example.com', phone: '(617) 555-0104', orders: 1, spent: 180, }, { id: 5, name: 'Glen Nash', city: 'Austin', email: 'glen.nash@example.com', phone: '(512) 555-0105', orders: 5, spent: 645, }, { id: 6, name: 'Evan Porter', city: 'Seattle', email: 'evan.porter@example.com', phone: '(206) 555-0106', orders: 2, spent: 440, }, { id: 7, name: 'Mary Cooper', city: 'Portland', email: 'mary.cooper@example.com', phone: '(503) 555-0107', orders: 11, spent: 1300, }, { id: 8, name: 'Paul Hawkins', city: 'Boston', email: 'paul.hawkins@example.com', phone: '(617) 555-0108', orders: 6, spent: 965, }, { id: 9, name: 'Olga Graham', city: 'Chicago', email: 'olga.graham@example.com', phone: '(312) 555-0109', orders: 4, spent: 530, }, { id: 10, name: 'Sam Ellis', city: 'Denver', email: 'sam.ellis@example.com', phone: '(303) 555-0110', orders: 9, spent: 2190, }, { id: 11, name: 'Nina Hart', city: 'Austin', email: 'nina.hart@example.com', phone: '(512) 555-0111', orders: 2, spent: 755, }, { id: 12, name: 'Alina Yusuf', city: 'Portland', email: 'alina.yusuf@example.com', phone: '(503) 555-0112', orders: 7, spent: 820, },];The search ignores case, Latin accents and the difference between the Russian «е» and «ё». Your own function in filter gets the text in lower case only and compares on its own.
Examples
Pick several values
Add multiple.
- New
- On sale
- Made to order
- Bestseller
Type a tag and press Enter to add it
<template> <NuiCombobox v-model:values="tags" :options="options" :custom-value="(text) => text" label="Product tags" description="Type a tag and press Enter to add it" multiple /></template><script setup lang="ts">import { ref } from 'vue';import { NuiCombobox, type NuiOptionSource } from '@numior/kit';const tags = ref(['new', 'sale']);const options: NuiOptionSource<string> = [ { value: 'new', label: 'New' }, { value: 'sale', label: 'On sale' }, { value: 'preorder', label: 'Made to order' }, { value: 'bestseller', label: 'Bestseller' },];</script>The picked values show as chips in the field. Pass customValue to turn typed text into a new value when the user presses Enter and no option is highlighted.
Search on the server
Turn off the filter and handle the search event.
- Oslo armchairS-101
- Bergen chairS-104
- Loft stoolS-110
- Malmo deskT-201
- Tromso coffee tableT-207
- Vik shelving unitH-301
- Fjord dresserH-305
- Nord table lampL-401
- Arc floor lampL-408
- Sphere pendantL-412
- Wool throwX-501
- Linen pillowX-507
<template> <NuiCombobox v-model="sku" :options="found" :filter="false" :loading="loading" label="Product" placeholder="Name or SKU" @search="search" /></template><script setup lang="ts">import { ref } from 'vue';import { promiseTimeout } from '@vueuse/core';import { NuiCombobox, type NuiOption } from '@numior/kit';import { products } from './products';const catalog: NuiOption<string>[] = products.map((product) => ({ value: product.sku, label: product.name, description: product.sku,}));const sku = ref<string | null>(null);const found = ref(catalog);const loading = ref(false);const latest = ref('');const matches = (option: NuiOption<string>, query: string): boolean => `${option.label} ${option.value}`.toLowerCase().includes(query.toLowerCase());const search = async (query: string): Promise<void> => { latest.value = query; loading.value = true; await promiseTimeout(400); if (query !== latest.value) { return; } found.value = catalog.filter((option) => matches(option, query)); loading.value = false;};</script>export interface Product { readonly sku: string; readonly name: string; readonly category: string; readonly price: number; readonly stock: number;}export interface Category { readonly value: string; readonly label: string; readonly children?: readonly Category[];}export const categories: readonly Category[] = [ { value: 'furniture', label: 'Furniture', children: [ { value: 'seating', label: 'Chairs' }, { value: 'tables', label: 'Tables' }, { value: 'storage', label: 'Storage' }, ], }, { value: 'lighting', label: 'Lighting', children: [ { value: 'lamps', label: 'Lamps' }, { value: 'pendants', label: 'Pendants' }, ], }, { value: 'textile', label: 'Textiles', children: [ { value: 'throws', label: 'Throws' }, { value: 'pillows', label: 'Pillows' }, ], },];export const products: readonly Product[] = [ { sku: 'S-101', name: 'Oslo armchair', category: 'seating', price: 249, stock: 12 }, { sku: 'S-104', name: 'Bergen chair', category: 'seating', price: 89, stock: 34 }, { sku: 'S-110', name: 'Loft stool', category: 'seating', price: 45, stock: 0 }, { sku: 'T-201', name: 'Malmo desk', category: 'tables', price: 329, stock: 7 }, { sku: 'T-207', name: 'Tromso coffee table', category: 'tables', price: 139, stock: 15 }, { sku: 'H-301', name: 'Vik shelving unit', category: 'storage', price: 199, stock: 9 }, { sku: 'H-305', name: 'Fjord dresser', category: 'storage', price: 299, stock: 4 }, { sku: 'L-401', name: 'Nord table lamp', category: 'lamps', price: 69, stock: 41 }, { sku: 'L-408', name: 'Arc floor lamp', category: 'lamps', price: 119, stock: 18 }, { sku: 'L-412', name: 'Sphere pendant', category: 'pendants', price: 99, stock: 22 }, { sku: 'X-501', name: 'Wool throw', category: 'throws', price: 59, stock: 56 }, { sku: 'X-507', name: 'Linen pillow', category: 'pillows', price: 29, stock: 80 },];Keep loading on while the request runs, and ignore answers that arrive for an older query.
Show more data in an option
Describe the row in the option slot.
- Peter WolfeBoston, 14 orders
- Lena OrtizChicago, 3 orders
- Tim SayedDenver, 8 orders
- Vera LaneBoston, 1 order
- Glen NashAustin, 5 orders
- Evan PorterSeattle, 2 orders
- Mary CooperPortland, 11 orders
- Paul HawkinsBoston, 6 orders
- Olga GrahamChicago, 4 orders
- Sam EllisDenver, 9 orders
- Nina HartAustin, 2 orders
- Alina YusufPortland, 7 orders
<template> <NuiCombobox v-model="customer" :options="buyers" label="Customer" placeholder="Search by name"> <template #option="{ option }"> <span class="flex items-baseline justify-between gap-4"> <span>{{ option.label }}</span> <span class="text-xs text-muted-foreground">{{ summaryOf(option.value) }}</span> </span> </template> </NuiCombobox></template><script setup lang="ts">import { ref } from 'vue';import { NuiCombobox, plural } from '@numior/kit';import { customers, type Customer } from './customers';const customer = ref<Customer | null>(null);const buyers = customers.map((person) => ({ value: person, label: person.name }));const summaryOf = (person: Customer): string => `${person.city}, ${person.orders} ${plural('en', person.orders, { one: 'order', other: 'orders' })}`;</script>export interface Customer { readonly id: number; readonly name: string; readonly city: string; readonly email: string; readonly phone: string; readonly orders: number; readonly spent: number;}export const customers: readonly Customer[] = [ { id: 1, name: 'Peter Wolfe', city: 'Boston', email: 'peter.wolfe@example.com', phone: '(617) 555-0101', orders: 14, spent: 2480, }, { id: 2, name: 'Lena Ortiz', city: 'Chicago', email: 'lena.ortiz@example.com', phone: '(312) 555-0102', orders: 3, spent: 295, }, { id: 3, name: 'Tim Sayed', city: 'Denver', email: 'tim.sayed@example.com', phone: '(303) 555-0103', orders: 8, spent: 1920, }, { id: 4, name: 'Vera Lane', city: 'Boston', email: 'vera.lane@example.com', phone: '(617) 555-0104', orders: 1, spent: 180, }, { id: 5, name: 'Glen Nash', city: 'Austin', email: 'glen.nash@example.com', phone: '(512) 555-0105', orders: 5, spent: 645, }, { id: 6, name: 'Evan Porter', city: 'Seattle', email: 'evan.porter@example.com', phone: '(206) 555-0106', orders: 2, spent: 440, }, { id: 7, name: 'Mary Cooper', city: 'Portland', email: 'mary.cooper@example.com', phone: '(503) 555-0107', orders: 11, spent: 1300, }, { id: 8, name: 'Paul Hawkins', city: 'Boston', email: 'paul.hawkins@example.com', phone: '(617) 555-0108', orders: 6, spent: 965, }, { id: 9, name: 'Olga Graham', city: 'Chicago', email: 'olga.graham@example.com', phone: '(312) 555-0109', orders: 4, spent: 530, }, { id: 10, name: 'Sam Ellis', city: 'Denver', email: 'sam.ellis@example.com', phone: '(303) 555-0110', orders: 9, spent: 2190, }, { id: 11, name: 'Nina Hart', city: 'Austin', email: 'nina.hart@example.com', phone: '(512) 555-0111', orders: 2, spent: 755, }, { id: 12, name: 'Alina Yusuf', city: 'Portland', email: 'alina.yusuf@example.com', phone: '(503) 555-0112', orders: 7, spent: 820, },];Accessibility
The focus stays in the field while the arrow keys move through the options, and a screen reader reads the highlighted one. Tab skips the clear, list and chip remove buttons: Backspace in an empty field removes the last chip.
Keys | Action | |
|---|---|---|
↓ | Opens the list and highlights the next option. | |
↑ | Opens the list and highlights the previous option. | |
↵ | Selects the highlighted option. With customValue, adds the typed text. | |
Esc | Closes the list. A dialog around the field stays open. | |
⇥ | Closes the list and moves the focus on. | |
⌫ | In an empty field with multiple, removes the last chip. |
API
Props
Name | Type | Default | Description | |
|---|---|---|---|---|
options*required required | NuiOptionSource<T> | — | List options, optionally in groups. | |
multiple | boolean | false | Lets the user pick several values. They show as chips, and the list stays open after each pick. | |
clearable | boolean | false | Shows a button in the field that clears the selection. | |
placeholder | string | — | Text shown in the empty field. | |
name | string | — | The name attribute for form submission. Each value gets its own hidden input. | |
getKey | NuiOptionKey<T> | — | Function that returns the key of an object value. Without it the JSON of the value is the key. | |
filter | false | NuiComboboxFilter<T> | — | Custom function that matches options to the query. false turns matching off when the server searches. | |
loading | boolean | false | Shows a "Loading" row in the list. | |
loop | boolean | true | Arrow keys wrap from the last option to the first and back. On by default. | |
locale | string | — | Locale of the text search and the built-in texts, such as tr-TR. Defaults to the locale from createNuiConfig. | |
customValue | ((text: string) => T) | — | Function that turns the typed text into a value. Enter adds it when no option is highlighted. | |
placement | Placement | "bottom-start" | Where the list opens, such as bottom-start or top. | |
emptyLabel | string | — | Text when nothing matches. Defaults to "No matches" from the dictionary. | |
loadingLabel | string | — | Text of the loading row. Defaults to "Loading" from the dictionary. | |
clearLabel | string | — | Clear button label for screen readers. Defaults to "Clear" from the dictionary. | |
removeLabel | string | — | Chip remove button label for screen readers. Defaults to "Remove" from the dictionary. | |
toggleLabel | string | — | List button label for screen readers. Defaults to "Show options" from the dictionary. | |
id | string | — | Id of the control. Defaults to the one from NuiField or is generated automatically. | |
size | NuiSize | "md" | Changes the size of the control: sm, md or lg. The theme density shifts the height of each size. | |
disabled | boolean | — | Disables the control. Inside NuiField it defaults to the field value. | |
readonly | boolean | — | Prevents changes to the value, which stays selectable and copyable. Inside NuiField it defaults to the field value. | |
required | boolean | — | Marks the control as required. Inside NuiField it defaults to the field value. | |
invalid | boolean | — | Shows the error state without a text. With error it turns on by itself. | |
label | string | — | Label of the control. Inside NuiField the field provides the label. | |
description | string | — | Hint below the control. Screen readers announce it with the control. | |
error | string | — | Error text below the control. Puts the control into the error state. | |
reserveError | boolean | false | Reserves space for the error so the form does not shift when it appears. |
Models
Name | Type | Default | Description | |
|---|---|---|---|---|
v-model | T | null | null | Selected value, or null, when multiple is off. | |
v-model:values | T[] | [] | Array of selected values when multiple is on. |
Events
Name | Payload | Description | |
|---|---|---|---|
search | [query: string] | Fires on every input with the query text. |
Slots
Name | Slot props | Description | |
|---|---|---|---|
option | { option: NuiOption<T>; selected: boolean; active: boolean; } | Custom option markup. Receives option, selected and active. |
Texts
Key | Russian | English | |
|---|---|---|---|
clear | Очистить | Clear | |
empty | Ничего не найдено | No matches | |
remove | Убрать | Remove | |
toggle | Показать варианты | Show options |