Skip to main content

CheckboxField

A styled and accessible checkbox input component built with Tailwind CSS, integrated with react-hook-form. Allows full customization through class overrides and displays validation errors.

๐Ÿ“ฆ Importโ€‹

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

๐Ÿงฑ Propsโ€‹

NameTypeDefaultDescription
labelstringโ€” (required)Text label shown next to the checkbox
idstringโ€” (required)The unique id for the checkbox input and label association
registeranyโ€”react-hook-form register object
errorMessagestringundefinedOptional error message to display below the checkbox
outerContainerClassNamestring""Custom class for the outermost wrapper <div>
inputContainerClassNamestring""Custom class for the inner container holding input and label
inputClassNamestring""Custom class for the <input type="checkbox">
labelClassNamestring""Custom class for the checkbox label
errorClassNamestring""Custom class for the error <p> element

๐Ÿ’… Styling Notesโ€‹

  • Checkbox base styles:
    • h-4 w-4 rounded border-gray-300 text-blue-600
    • Focus ring: focus:ring-blue-500
  • Error state:
    • Error message styled as text-red-500 by default

๐Ÿงช Exampleโ€‹

<CheckboxField
id="agreeTerms"
label="I agree to the terms and conditions"
register={register("agreeTerms", { required: "You must agree to continue" })}
errorMessage={errors.agreeTerms?.message}
/>

๐Ÿง  Tipsโ€‹

  • Integrates seamlessly with react-hook-form's validation.
  • Ideal for consent checkboxes, feature toggles, or boolean form controls.
  • Combine with InputField or SelectField for full form coverage.