dentil

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-select
On this page
Result: —

Installation

pnpm dlx shadcn@latest add @dentil/async-select
npx shadcn@latest add @dentil/async-select
yarn dlx shadcn@latest add @dentil/async-select
bunx --bun shadcn@latest add @dentil/async-select

Files 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.

Result: —

Empty state

Search for a name that is not listed, then use it anyway. The popover closes and the result is that string.

Result: —

API reference

AsyncSelect

PropTypeDefaultDescription
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.
valuestring | 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.
defaultOptionT | 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.
preloadbooleanfalseLoad 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.
placeholderstring'Select...'Trigger label when nothing is selected.
searchPlaceholderstring'Search...'Search field placeholder.
emptyTextReactNode | ((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.
loadingSkeletonReactNode—Replaces the three loading rows shown while the first results are in flight.
debouncenumber300Wait before calling fetcher after a keystroke, in milliseconds. Ignored when preload is set. Clearing the field fetches immediately.
clearablebooleantruePicking the selected option again clears it and calls onChange with null.
…propsComponentProps<'button'>—Other props (id, aria-invalid, aria-describedby, onBlur…) are passed to the trigger button.