Async Select
AsyncSelect is a controlled combobox for options that come from a search. fetcher runs when the popover opens and again as you type. onChange receives the whole option, or null when you clear it. Search for a person, or for an email. Pick the selected row again to clear it.
@dentil/async-selectOn this page
—'use client';
import { useState } from 'react';
import { AsyncSelect } from '@/components/dentil/async-select/async-select';
type Person = { id: string; name: string; email: string };
const people: Person[] = [
{ id: 'ada', name: 'Ada Lovelace', email: 'ada@analytical.engine' },
{ id: 'grace', name: 'Grace Hopper', email: 'grace@navy.mil' },
{ id: 'katherine', name: 'Katherine Johnson', email: 'katherine@nasa.gov' },
{ id: 'margaret', name: 'Margaret Hamilton', email: 'margaret@mit.edu' },
{ id: 'annie', name: 'Annie Easley', email: 'annie@nasa.gov' },
{ id: 'dorothy', name: 'Dorothy Vaughan', email: 'dorothy@nasa.gov' },
];
function searchPeople(query: string): Promise<Person[]> {
const q = query.trim().toLowerCase();
const matches = people.filter(
(person) => person.name.toLowerCase().includes(q) || person.email.toLowerCase().includes(q)
);
return new Promise((resolve) => setTimeout(() => resolve(matches), 800));
}
export function AsyncSelectOverviewDemo() {
const [person, setPerson] = useState<Person | null>(null);
return (
<div className="flex w-full max-w-xs flex-col gap-4">
<AsyncSelect
fetcher={searchPeople}
value={person?.id}
defaultOption={person}
onChange={setPerson}
getOptionValue={(option) => option.id}
getDisplayValue={(option) => option.name}
renderOption={(option) => (
<span className="flex min-w-0 flex-col">
<span className="truncate">{option.name}</span>
<span className="text-muted-foreground truncate text-xs">{option.email}</span>
</span>
)}
placeholder="Select a person"
searchPlaceholder="Search people..."
emptyText="No person found."
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{person?.name || '—'}</code>
</span>
</div>
);
}Installation
pnpm dlx shadcn@latest add @dentil/async-selectnpx shadcn@latest add @dentil/async-selectyarn dlx shadcn@latest add @dentil/async-selectbunx --bun shadcn@latest add @dentil/async-selectFiles land in components/dentil/async-select/. Uses the stock shadcn button, command and popover, so it follows your theme. For a list you already have in memory, use Select Search.
Usage
value is the selected id. Store the option and pass it as defaultOption, so the trigger keeps its label before the list has loaded and after a search that leaves it out. A thrown error replaces the list with the message and a retry. With preload, fetcher runs once per mount.
'use client';
import { useState } from 'react';
import { AsyncSelect } from '@/components/dentil/async-select/async-select';
type Customer = { id: string; name: string; email: string };
// searchCustomers is your request. Reject to show the error and a retry.
export function CustomerSelect() {
const [customer, setCustomer] = useState<Customer | null>(null);
return (
<AsyncSelect
fetcher={(query, signal) => searchCustomers(query, signal)}
value={customer?.id}
defaultOption={customer}
onChange={setCustomer}
getOptionValue={(option) => option.id}
getDisplayValue={(option) => option.name}
renderOption={(option) => (
<span className="flex min-w-0 flex-col">
<span className="truncate">{option.name}</span>
<span className="text-muted-foreground truncate text-xs">{option.email}</span>
</span>
)}
placeholder="Select a customer"
searchPlaceholder="Search customers..."
emptyText="No customer found."
/>
);
}Examples
Preload
Options load once. Further typing filters them in the browser, and Initech cannot be picked.
—'use client';
import { useState } from 'react';
import { AsyncSelect } from '@/components/dentil/async-select/async-select';
type Account = { id: string; name: string; added?: boolean };
const accounts: Account[] = [
{ id: 'acme', name: 'Acme Ltd' },
{ id: 'globex', name: 'Globex' },
{ id: 'initech', name: 'Initech', added: true },
{ id: 'umbrella', name: 'Umbrella' },
{ id: 'hooli', name: 'Hooli' },
];
function loadAccounts(): Promise<Account[]> {
return new Promise((resolve) => setTimeout(() => resolve(accounts), 800));
}
export function AsyncSelectPreloadDemo() {
const [account, setAccount] = useState<Account | null>(null);
return (
<div className="flex w-full max-w-xs flex-col gap-4">
<AsyncSelect
fetcher={loadAccounts}
preload
getSearchValue={(option) => option.name}
value={account?.id}
defaultOption={account}
onChange={setAccount}
getOptionValue={(option) => option.id}
getDisplayValue={(option) => option.name}
isOptionDisabled={(option) => option.added === true}
renderOption={(option) => (
<span className="flex min-w-0 items-center gap-2">
<span className="truncate">{option.name}</span>
{option.added ? <span className="text-muted-foreground ml-auto text-xs">Added</span> : null}
</span>
)}
placeholder="Select an account"
searchPlaceholder="Search accounts..."
emptyText="No account found."
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{account?.name || '—'}</code>
</span>
</div>
);
}Empty state
Search for a name that is not listed, then use it anyway. The popover closes and the result is that string.
—'use client';
import { useState } from 'react';
import { AsyncSelect } from '@/components/dentil/async-select/async-select';
import { Button } from '@/components/ui/button';
const teams = ['Design', 'Finance', 'Support'];
function searchTeams(query: string): Promise<string[]> {
const q = query.trim().toLowerCase();
const matches = teams.filter((team) => team.toLowerCase().includes(q));
return new Promise((resolve) => setTimeout(() => resolve(matches), 800));
}
export function AsyncSelectEmptyDemo() {
const [team, setTeam] = useState<string | null>(null);
return (
<div className="flex w-full max-w-xs flex-col gap-4">
<AsyncSelect
fetcher={searchTeams}
value={team}
defaultOption={team}
onChange={setTeam}
getOptionValue={(option) => option}
getDisplayValue={(option) => option}
renderOption={(option) => <span className="truncate">{option}</span>}
placeholder="Select a team"
searchPlaceholder="Search teams..."
emptyText={({ query, close }) => (
<div className="flex flex-col items-center gap-2 px-3 py-6 text-center">
<p className="text-muted-foreground text-sm">{query ? `No team named ${query}.` : 'No teams found.'}</p>
{query ? (
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
setTeam(query);
close();
}}>
Use {query}
</Button>
) : null}
</div>
)}
/>
<span className="text-muted-foreground text-sm">
Result: <code className="text-foreground">{team || '—'}</code>
</span>
</div>
);
}API reference
AsyncSelect
| Prop | Type | Default | Description |
|---|---|---|---|
| fetcher | (query: string, signal: AbortSignal) => Promise<T[]> | — | Loads options for a search. '' asks for the unfiltered list. signal aborts the previous request. The latest function is used on the next search, so remount the component when the same query should return a different set. |
| value | string | null | undefined | — | The selected option's value. null, undefined and '' are empty. |
| onChange | (option: T | null) => void | — | Called with the picked option, or null when the selection is cleared. |
| getOptionValue | (option: T) => string | — | Unique value for an option. This is what value is compared to. |
| getDisplayValue | (option: T) => ReactNode | — | Trigger contents for the selected option. |
| renderOption | (option: T) => ReactNode | — | Contents of the option row. A check mark is added after it. |
| defaultOption | T | null | — | Shown on the trigger when value matches it and that option is not in the current results. Pass the selection you already have so its label shows before the list loads. |
| preload | boolean | false | Load once with an empty query, then filter in the browser. Otherwise each search calls fetcher after debounce. |
| filterFn | (option: T, query: string) => boolean | — | Client filter used when preload is set. Return true to keep the option. When omitted, the filter matches getOptionValue and getSearchValue. |
| getSearchValue | (option: T) => string | — | Extra text the default preload filter matches, besides the option value. |
| isOptionDisabled | (option: T) => boolean | — | Disabled options stay in the list and cannot be picked. |
| placeholder | string | 'Select...' | Trigger label when nothing is selected. |
| searchPlaceholder | string | 'Search...' | Search field placeholder. |
| emptyText | ReactNode | ((state: { query: string; close: () => void }) => ReactNode) | 'No results.' | Shown when the list is empty. A function receives the current query and a way to close the popover. |
| loadingSkeleton | ReactNode | — | Replaces the three loading rows shown while the first results are in flight. |
| debounce | number | 300 | Wait before calling fetcher after a keystroke, in milliseconds. Ignored when preload is set. Clearing the field fetches immediately. |
| clearable | boolean | true | Picking the selected option again clears it and calls onChange with null. |
| …props | ComponentProps<'button'> | — | Other props (id, aria-invalid, aria-describedby, onBlur…) are passed to the trigger button. |