Skip to main content

RadioGroupField

A customizable and accessible radio button group for single selection, fully integrated with react-hook-form. Designed with Tailwind classes and flexibility in mind, it provides clear error feedback and styling overrides.

๐Ÿ“ฆ Importโ€‹

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

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringโ€”Optional group label displayed above the radios
namestringโ€” (required)Name of the field (used by react-hook-form)
optionsarrayโ€” (required)List of radio options: { label, value }[]
registerfunctionโ€” (required)react-hook-form register method
errorMessagestringundefinedError message to display under the group
outerContainerClassNamestring""Additional class for outermost container
inputContainerClassNamestring""Class for the container wrapping the radio buttons
inputClassNamestring""Class applied to each radio input
labelClassNamestring""Class for the group label element
optionLabelClassNamestring""Class applied to the label for each radio option
errorClassNamestring""Class applied to the error message

๐Ÿ’… Styling Notesโ€‹

  • Base Inputs:
    • Size: h-4 w-4
    • Color: text-blue-600
    • Focus: focus:ring-blue-500
  • Error State:
    • border-red-500 focus:ring-red-500
  • Layout:
    • Default horizontal/vertical spacing via space-y-2 space-x-2

๐Ÿงช Exampleโ€‹

<RadioGroupField
name="gender"
label="Select your gender"
options={[
{ label: "Male", value: "male" },
{ label: "Female", value: "female" },
{ label: "Other", value: "other" },
]}
register={register("gender", {
required: "Please select a gender",
})}
errorMessage={errors.gender?.message}
/>

๐Ÿง  Tipsโ€‹

  • Useful for any single-choice selection.
  • For inline display, apply flex styles via inputContainerClassName.
  • Combine with InputField, CheckboxGroupField for a complete form UI.