Skip to main content

SelectField

A customizable and accessible select dropdown input integrated with react-hook-form. Supports label, error message, and Tailwind CSS class overrides.

๐Ÿ“ฆ Importโ€‹

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

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringundefinedOptional label text shown above the dropdown
idstringโ€” (required)The unique id for the <select> element and its label
optionsArray<{ label: string, value: string }>โ€” (required)Options to show in the dropdown
registeranyโ€”react-hook-form register object
errorMessagestringundefinedDisplays error message below dropdown if provided
outerContainerClassNamestring""Extra classes for the outer wrapper <div>
inputClassNamestring""Extra classes for the <select> element
labelClassNamestring""Extra classes for the <label> element
optionLabelClassNamestring""Extra classes for each <option> element
errorClassNamestring""Extra classes for the error <p> element

๐Ÿ’… Styling Notesโ€‹

  • Uses Tailwind CSS for all base styles.
  • Default appearance:
    • border-gray-300 with focus:ring-blue-500
  • Error state:
    • border-red-500 with focus:ring-red-500

๐Ÿงช Exampleโ€‹

<SelectField
id="country"
label="Country"
options={[
{ label: "India", value: "IN" },
{ label: "United States", value: "US" },
]}
register={register("country", { required: "Country is required" })}
errorMessage={errors.country?.message}
/>

๐Ÿง  Tipsโ€‹

  • Useful for dropdown menus in forms.
  • Works well with InputField to create complete form UIs.
  • You can dynamically generate the options from an API or local constants.