Skip to main content

AsyncTypeAheadSelectField

An asynchronous react-select/async powered searchable dropdown that fetches options dynamically. Supports label, error message display, and Tailwind CSS class overrides.

๐Ÿ“ฆ Importโ€‹

import { AsyncTypeAheadSelectField } from "@sahilphondekar/react-component-library";

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringundefinedOptional label text shown above the dropdown
idstringโ€” (required)Unique id for the input field and label
loadOptions(inputValue: string) => Promise<Array<{ label: string, value: string }>>โ€” (required)Async function to load options based on user input
controlanyโ€”react-hook-form control object (passed for consistency, handled via Controller)
errorMessagestringundefinedDisplays error message below the dropdown if provided
outerContainerClassNamestring""Extra classes for the outer wrapper <div>
labelClassNamestring""Extra classes for the <label> element
selectClassNamestring""Extra classes for the react-select component
errorClassNamestring""Extra classes for the error <p> element
placeholderstring"Select..."Placeholder text shown when no option is selected
...restanyโ€”Any additional props are spread to AsyncSelect

๐Ÿ’… Styling Notesโ€‹

  • Built on top of react-select/async with Tailwind-compatible class overrides.
  • Default react-select class prefix applied via classNamePrefix="react-select".
  • Error state highlights the select input with a border-red-500 color.

๐Ÿงช Exampleโ€‹

import { Controller } from "react-hook-form";

const loadOptions = async (inputValue) => {
if (!inputValue) return [];

try {
const response = await fetch(
`https://jsonplaceholder.typicode.com/users`
);
const users = await response.json();

// Filter users by inputValue (simulate backend search)
const filtered = users.filter((user) =>
user.name.toLowerCase().includes(inputValue.toLowerCase())
);

// Map to react-select format
return filtered.map((user) => ({
label: user.name,
value: user.id,
}));
} catch (error) {
console.error("Error fetching users:", error);
return [];
}
};

<Controller
name="users"
control={control}
render={({ field }) => (
<AsyncTypeAheadSelectField
id="users"
label="Users (Async)"
loadOptions={loadOptions}
{...field}
value={undefined}
onChange={val => field.onChange(val?.value)}
errorMessage={errors.users?.message}
/>
)}
/>

๐Ÿง  Tipsโ€‹

  • Ideal for large datasets where you don't want to load all options at once.
  • Works great with paginated APIs or search endpoints.
  • Use cacheOptions and defaultOptions for better user experience with async loading.