Skip to main content
Version: v2

RHFSwitch

Switches toggle the state of a single setting on or off. RHFSwitch extends the Switch component and can be customized in a similar way.

Usage

import RHFSwitch, { RHFSwitchProps } from '@nish1896/rhf-mui-components/mui/switch';
<RHFSwitch
fieldName="darkTheme"
control={control}
label="Enable Dark Theme?"
/>

Examples

API

RHFSwitchProps extends SwitchProps and accepts the following additional values.

NameTypeRequiredDescription
fieldNamestringReact Hook Form requires name as a key for the registration process. This is a required prop for all components.
controlUseFormControlThe control option yielded on calling the useForm hook.
registerOptionsRegisterOptionsRegister options for validation if using react-hook-form without any validation libraries like yup or Joi.
onValueChange(isChecked: boolean, e: ChangeEvent) => voidA callback function that triggers when the switch is toggled, providing a boolean indicating whether the switch is on or off.
labelReactNodeThe text to render in FormLabel component. By default, the value of fieldName such as firstName will be transformed to display "First Name" using the fieldNameToLabel function.
formControlLabelPropsFormControlLabelPropsFormControlLabelProps to customise FormControlLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
helperTextReactNodeThe 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.
errorMessageReactNodeError message to be shown for a field in FormHelperText component.
hideErrorMessagebooleanA flag to prevent replacement of helper text of a field by the error message when the validation is triggered.
formHelperTextPropsFormHelperTextPropsFormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.