Skip to main content

CheckboxGroupField

A flexible group of checkboxes, designed for multiple selection fields and easily integrated with react-hook-form. Fully customizable with Tailwind utility overrides and displays inline error messaging.

๐Ÿ“ฆ Importโ€‹

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

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringโ€”Optional group label displayed above the checkboxes
namestringโ€” (required)The name key used by react-hook-form
optionsarrayโ€” (required)List of checkbox options, each with { label, value }
registerfunctionโ€” (required)react-hook-form register method
errorMessagestringundefinedError message for validation feedback
outerContainerClassNamestring""Class for the outermost wrapper <div>
inputContainerClassNamestring""Class for the wrapper around all checkboxes
inputClassNamestring""Class for each <input type="checkbox">
labelClassNamestring""Class for the group label
optionLabelClassNamestring""Class for each label next to a checkbox
errorClassNamestring""Class for the error message <p>

๐Ÿ’… Styling Notesโ€‹

  • Checkboxes:
    • h-4 w-4 text-blue-600 border-gray-300
    • Focus ring on selection: focus:ring-blue-500
    • Error styles: border-red-500 focus:ring-red-500
  • Group layout: space-y-2 space-x-2 with inline-flex styling per option

๐Ÿงช Exampleโ€‹

<CheckboxGroupField
name="interests"
label="Select your interests"
options={[
{ label: "Music", value: "music" },
{ label: "Travel", value: "travel" },
{ label: "Reading", value: "reading" },
]}
register={register("interests", {
validate: (value) => value.length > 0 || "Select at least one option",
})}
errorMessage={errors.interests?.message}
/>

๐Ÿง  Tipsโ€‹

  • Best suited for multiple checkbox values under one field name.
  • Combine with InputField and SelectField for complete form solutions.
  • Works great with Tailwind-based responsive forms.