Components
Combobox
Picks from a list that can be searched, for more options than a select can hold.
'use client'
import { useState } from 'react'
import { Combobox } from '@dex/shared/components/combobox'
const CITIES = [
{ value: 'london', label: 'London' },
{ value: 'manchester', label: 'Manchester' },
{ value: 'edinburgh', label: 'Edinburgh' },
{ value: 'bristol', label: 'Bristol' },
{ value: 'cambridge', label: 'Cambridge' },
{ value: 'dublin', label: 'Dublin' },
{ value: 'berlin', label: 'Berlin' },
{ value: 'amsterdam', label: 'Amsterdam' },
{ value: 'lisbon', label: 'Lisbon' },
{ value: 'new-york', label: 'New York' },
]
export function ComboboxDefault() {
const [city, setCity] = useState<string | null>(null)
return (
<div className="w-full max-w-sm">
<Combobox
aria-label="City"
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value={city}
onValueChange={setCity}
/>
</div>
)
}
'use client'
import { MapPin } from 'lucide-react'
import { useState } from 'react'
import { Combobox } from '@dex/shared/components/combobox'
const CITIES = [
{ value: 'london', label: 'London', detail: 'England, UK' },
{ value: 'manchester', label: 'Manchester', detail: 'England, UK' },
{ value: 'edinburgh', label: 'Edinburgh', detail: 'Scotland, UK' },
{ value: 'cardiff', label: 'Cardiff', detail: 'Wales, UK' },
{ value: 'dublin', label: 'Dublin', detail: 'Ireland' },
{ value: 'berlin', label: 'Berlin', detail: 'Germany' },
{ value: 'amsterdam', label: 'Amsterdam', detail: 'Netherlands' },
{ value: 'new-york', label: 'New York', detail: 'New York, USA' },
]
export function ComboboxIconAndDetail() {
const [city, setCity] = useState<string | null>('manchester')
return (
<div className="w-full max-w-sm">
<Combobox
aria-label="City"
icon={<MapPin />}
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value={city}
onValueChange={setCity}
/>
</div>
)
}
We use this to find roles you could commute to.
'use client'
import { MapPin } from 'lucide-react'
import { useState } from 'react'
import { Combobox } from '@dex/shared/components/combobox'
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
const CITIES = [
{ value: 'london', label: 'London', detail: 'England, UK' },
{ value: 'manchester', label: 'Manchester', detail: 'England, UK' },
{ value: 'edinburgh', label: 'Edinburgh', detail: 'Scotland, UK' },
{ value: 'cardiff', label: 'Cardiff', detail: 'Wales, UK' },
{ value: 'dublin', label: 'Dublin', detail: 'Ireland' },
{ value: 'berlin', label: 'Berlin', detail: 'Germany' },
{ value: 'amsterdam', label: 'Amsterdam', detail: 'Netherlands' },
{ value: 'new-york', label: 'New York', detail: 'New York, USA' },
]
export function ComboboxInAField() {
const [city, setCity] = useState<string | null>(null)
return (
<Field className="max-w-sm">
<FieldLabel>Where are you based?</FieldLabel>
<FieldControl>
<Combobox
icon={<MapPin />}
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value={city}
onValueChange={setCity}
/>
</FieldControl>
<FieldMessage description="We use this to find roles you could commute to." />
</Field>
)
}
'use client'
import { MapPin } from 'lucide-react'
import { useEffect, useState } from 'react'
import { Combobox, type ComboboxOption } from '@dex/shared/components/combobox'
const CITIES: ComboboxOption[] = [
{ value: 'london', label: 'London', detail: 'England, UK' },
{ value: 'leeds', label: 'Leeds', detail: 'England, UK' },
{ value: 'liverpool', label: 'Liverpool', detail: 'England, UK' },
{ value: 'lisbon', label: 'Lisbon', detail: 'Portugal' },
{ value: 'lyon', label: 'Lyon', detail: 'France' },
{ value: 'manchester', label: 'Manchester', detail: 'England, UK' },
{ value: 'madrid', label: 'Madrid', detail: 'Spain' },
{ value: 'munich', label: 'Munich', detail: 'Germany' },
]
export function ComboboxServerSearch() {
const [text, setText] = useState('')
const [city, setCity] = useState<ComboboxOption | null>(null)
const [answered, setAnswered] = useState({ prefix: '', results: [] as ComboboxOption[] })
const prefix = text.trim().toLowerCase()
// Stands in for a request: the results arrive 400ms after the last keystroke.
useEffect(() => {
if (!prefix) return
const timeout = setTimeout(() => {
const results = CITIES.filter((option) => option.label.toLowerCase().startsWith(prefix))
setAnswered({ prefix, results })
}, 400)
return () => clearTimeout(timeout)
}, [prefix])
const results = prefix ? answered.results : []
return (
<div className="w-full max-w-sm">
<Combobox
aria-label="City"
icon={<MapPin />}
placeholder="Search for a city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
focusSearchOnOpen
options={results}
value={city?.value ?? null}
selectedOption={city}
onValueChange={(value) => setCity(results.find((option) => option.value === value) ?? null)}
search={{
value: text,
onValueChange: setText,
isSearching: Boolean(prefix) && answered.prefix !== prefix,
}}
/>
</div>
)
}
'use client'
import { useState } from 'react'
import { Chip, ChipAction, ChipGroup } from '@dex/shared/components/chip-group'
import { MultiCombobox } from '@dex/shared/components/combobox'
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
const LANGUAGES = ['Python', 'TypeScript', 'Go', 'Rust', 'Kotlin', 'Scala', 'Elixir', 'Swift']
const SUGGESTED = LANGUAGES.slice(0, 3)
export function ComboboxSeveralValues() {
const [chosen, setChosen] = useState(['Python'])
const shown = [...SUGGESTED, ...chosen.filter((language) => !SUGGESTED.includes(language))]
const toggle = (language: string) =>
setChosen((current) =>
current.includes(language)
? current.filter((item) => item !== language)
: [...current, language],
)
return (
<FieldSet>
<FieldLegend>Which languages do you work in?</FieldLegend>
<ChipGroup type="multiple" value={chosen} onValueChange={setChosen}>
{shown.map((language) => (
<Chip key={language} value={language}>
{language}
</Chip>
))}
<MultiCombobox
trigger={<ChipAction>More</ChipAction>}
options={LANGUAGES.map((language) => ({ value: language, label: language }))}
selected={chosen}
onToggle={toggle}
searchPlaceholder="Search languages"
emptyText="No matching language"
/>
</ChipGroup>
</FieldSet>
)
}
'use client'
import { useState } from 'react'
import { Chip, ChipAction, ChipGroup } from '@dex/shared/components/chip-group'
import { MultiCombobox } from '@dex/shared/components/combobox'
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
const INDUSTRIES = ['AI', 'Climate', 'Developer tools', 'Fintech', 'Health', 'Robotics', 'Security']
const MOST = 3
export function ComboboxWithALimit() {
const [chosen, setChosen] = useState(['AI', 'Fintech'])
const toggle = (industry: string) =>
setChosen((current) =>
current.includes(industry)
? current.filter((item) => item !== industry)
: [...current, industry],
)
return (
<FieldSet>
<FieldLegend>Pick up to three industries</FieldLegend>
<ChipGroup type="multiple" value={chosen} onValueChange={setChosen}>
{chosen.map((industry) => (
<Chip key={industry} value={industry}>
{industry}
</Chip>
))}
<MultiCombobox
trigger={<ChipAction>Add</ChipAction>}
options={INDUSTRIES.map((industry) => ({ value: industry, label: industry }))}
selected={chosen}
onToggle={toggle}
isFull={chosen.length >= MOST}
searchPlaceholder="Search industries"
emptyText="No matching industry"
/>
</ChipGroup>
</FieldSet>
)
}
Choose where you’re based.
'use client'
import { MapPin } from 'lucide-react'
import { useState } from 'react'
import { Combobox } from '@dex/shared/components/combobox'
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
const CITIES = [
{ value: 'london', label: 'London', detail: 'England, UK' },
{ value: 'manchester', label: 'Manchester', detail: 'England, UK' },
{ value: 'edinburgh', label: 'Edinburgh', detail: 'Scotland, UK' },
{ value: 'dublin', label: 'Dublin', detail: 'Ireland' },
]
export function ComboboxInvalid() {
const [city, setCity] = useState<string | null>(null)
return (
<Field invalid={!city} className="max-w-sm">
<FieldLabel>Where are you based?</FieldLabel>
<FieldControl>
<Combobox
icon={<MapPin />}
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value={city}
onValueChange={setCity}
/>
</FieldControl>
<FieldMessage error={city ? undefined : 'Choose where you’re based.'} />
</Field>
)
}
'use client'
import { MapPin } from 'lucide-react'
import { useState } from 'react'
import { Combobox } from '@dex/shared/components/combobox'
import { Field, FieldControl, FieldLabel } from '@dex/shared/components/field'
const CITIES = [
{ value: 'london', label: 'London', detail: 'England, UK' },
{ value: 'manchester', label: 'Manchester', detail: 'England, UK' },
]
export function ComboboxDisabled() {
const [city, setCity] = useState<string | null>('london')
return (
<Field disabled className="max-w-sm">
<FieldLabel>Where are you based?</FieldLabel>
<FieldControl>
<Combobox
icon={<MapPin />}
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value={city}
onValueChange={setCity}
disabled
/>
</FieldControl>
</Field>
)
}
From your LinkedIn profile.
'use client'
import { MapPin } from 'lucide-react'
import { Combobox } from '@dex/shared/components/combobox'
import { Field, FieldControl, FieldDescription, FieldLabel } from '@dex/shared/components/field'
const CITIES = [{ value: 'london', label: 'London', detail: 'England, UK' }]
export function ComboboxReadOnly() {
return (
<Field className="max-w-sm">
<FieldLabel>Where are you based?</FieldLabel>
<FieldControl>
<Combobox
icon={<MapPin />}
placeholder="Search for your city"
searchPlaceholder="Type a city or town"
emptyText="No matching city or town"
options={CITIES}
value="london"
onValueChange={() => {}}
readOnly
/>
</FieldControl>
<FieldDescription>From your LinkedIn profile.</FieldDescription>
</Field>
)
}
Below 640px the list opens in a bottom sheet instead of a popover. The sheet is the full width of the screen, its rows are 44px tall, it lifts above the keyboard while you type, and a swipe down closes it. MultiCombobox stays open while rows are ticked, so its sheet also has a Done button beside the search.
Narrow the window and open any example on this page to see it.
- Use a combobox when there are more options than a select can hold, such as cities or programming languages. For a short, fixed list, use a select.
Comboboxpicks one value and is a field, like an input.MultiComboboxpicks several and opens from any trigger, such as a chip action.- Use
detailfor a second, muted piece of text at the end of a row, such as a city's region. It also shows in the field once the option is chosen. - When the options come from the server, pass
searchand filter them yourself. PassselectedOptiontoo, so the field can show the chosen option after the results have moved on. - Add
focusSearchOnOpenwhen nothing can be picked before typing. On a phone the sheet then opens with the keyboard up. - Say what was not found in
emptyText: "No matching city or town", not "No results".