RHFCheckboxGroup
The RHFCheckboxGroup component renders multiple checkboxes allowing the user to select one or more items from a set. It supports both arrays and arrays of objects for options, and each checkbox can be customized as needed.
Usage
import RHFCheckboxGroup, { RHFCheckboxGroupProps } from '@nish1896/rhf-mui-components/mui/checkbox-group';
Example with an array of strings:
<RHFCheckboxGroup
fieldName="toDo"
control={control}
label="Tasks to do"
options={['Read Mails', 'Go for a hike', 'Bring groceries']}
errorMessage={errors?.toDo?.message}
/>
Example with an array of objects:
<RHFCheckboxGroup
fieldName="countries"
control={control}
label="Select Countries You've visited"
options={[
{ code: 'AUS', country: 'Australia' },
{ code: 'IN', country: 'India' },
{ code: 'UAE', country: 'United Arab Emirates' }
]}
labelKey="country"
valueKey="code"
errorMessage={errors?.countries?.message}
/>
warning
When using an array of objects for options, both labelKey
and valueKey
are required. If either is missing, an error will be thrown.
Examples
API
RHFCheckboxGroupProps
accepts the following props:
Name | Type | Required | Description |
---|---|---|---|
fieldName | string | ✅ | React Hook Form requires name as a key for the registration process. This is a required prop for all components. |
control | UseFormControl | ✅ | The control option yielded on calling the useForm hook. |
registerOptions | RegisterOptions | Register options for validation if using react-hook-form without any validation libraries like yup or Joi. | |
options | string[] or object[] | ✅ | An array with string or object values. Make sure to pass labelKey and valueKey when options is an array of objects. |
labelKey | string | ✅ | The key of object in options array, whose value would be shown as the label in the formfield. Only required when input options is an array of objects. |
valueKey | string | ✅ | The key of object in options array, whose value would be actual value of the option selected in the formfield. Only required when input options is an array of objects. |
required | boolean | Indicates that the field is mandatory by adding an asterisk symbol (*) to the formLabel . This visual cue helps users quickly identify required fields in the form. | |
onValueChange | (selectedItemValue, value, event) => void | An optional callback function triggered upon selection. The selectedItemValue parameter provides the value of the item being checked, while the value parameter returns the updated complete value of the form field. | |
disabled | boolean | A boolean value to enable or disable editing of the form field. | |
label | ReactNode | The text to render in FormLabel component. By default, the value of fieldName such as firstName will be transformed to display "First Name". | |
showLabelAboveFormField | boolean | Renders the form label above the field by default. Set this prop to false to hide the label. | |
formLabelProps | FormLabelProps | FormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component. | |
checkboxProps | CheckboxProps | Checkbox Props to customise each checkbox in checkbox group. | |
formControlLabelProps | FormControlLabelProps | FormControlLabelProps to customise FormControlLabel component for a field. Multiple fields can be configured using the ConfigProvider component. | |
helperText | ReactNode | The content to display within the FormHelperText component below the field. If the field validation fails, this content will be overridden by the corresponding error message. | |
errorMessage | ReactNode | Error message to be shown for a field in FormHelperText component. | |
hideErrorMessage | boolean | A flag to prevent replacement of helper text of a field by the error message when the validation is triggered. | |
formHelperTextProps | FormHelperTextProps | FormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component. |