Skip to main content

TypeAheadSelectField

A customizable react-select powered dropdown with search functionality, integrated with react-hook-form. Supports label, error message display, and Tailwind CSS class overrides.

๐Ÿ“ฆ Importโ€‹

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

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringundefinedOptional label text shown above the dropdown
idstringโ€” (required)Unique id for the input field and label
optionsArray<{ label: string, value: string }>โ€” (required)Options available for selection
controlanyโ€”react-hook-form control object (passed for consistency, but internally handled by 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 react-select

๐Ÿ’… Styling Notesโ€‹

  • Uses react-select under the hood with Tailwind-compatible class overrides.
  • Default react-select style classes 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 country = [
{label: "India", value:"IN"},
{label: "USA", value:"USA"},
];

<Controller
defaultValue="IN"
name="country"
control={control}
render={({ field }) => (
<TypeAheadSelectField
id="country"
label="Country"
options={country}
{...field}
value={country.find(c => c.value === field.value)}
onChange={val => field.onChange(val.value)}
errorMessage={errors.country?.message}
/>
)}
/>

๐Ÿง  Tipsโ€‹

  • Great for searchable dropdowns where users can quickly find options by typing.
  • Works seamlessly with Controller from react-hook-form.
  • You can fully customize the behavior and appearance of the dropdown using additional props supported by react-select.