Skip to main content
Version: v3

RHFSelect

RHFSelect is an extended version of Select, allowing users to select one or more options. One significant advantage is its ability to accept both arrays and arrays of objects, making it easier to render options with differing labels and values without custom logic.

Usage​

import RHFSelect, { RHFSelectProps } from '@nish1896/rhf-mui-components/mui/select';

Here’s a basic example with an array of strings:

<RHFSelect
fieldName="color"
control={control}
options={['Red', 'Blue', 'Orange']}
errorMessage={errors?.color?.message}
/>

For options as an array of objects:

<RHFSelect
fieldName="country"
control={control}
options={[
{ code: 'AUS', country: 'Australia' },
{ code: 'IN', country: 'India' },
{ code: 'UAE', country: 'United Arab Emirates' },
]}
labelKey="country"
valueKey="code"
errorMessage={errors?.country?.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.

You can extend or reuse the RHFSelect component by creating your own wrapper component.

πŸ‘‰ StyledSelect Example (GitHub)

Examples​

API​

The RHFSelectProps interface extends SelectProps and accepts the following additional props:

NameTypeRequiredDescription
fieldNamestringβœ…React Hook Form requires name as a key for the registration process. This is a required prop for all components.
controlUseFormControlβœ…The 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.
optionsstring[] or number[] or object[]βœ…An array with string, numeric or object values. Make sure to pass labelKey and valueKey when options is an array of objects.
labelKeystringβœ…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.
valueKeystringβœ…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.
showDefaultOptionbooleanShow default Label of the disabled option when value of RHFSelect or RHFNativeSelect is ''. This text can be changed using the defaultOptionText prop.
defaultOptionTextstringCustom text to replace the default text when showDefaultOption is true for RHFSelect or RHFNativeSelect.
onValueChange(newValue, event, child) => voidAn optional callback function when an option is selected. The latest value can be obtained from newValue argument.
showLabelAboveFormFieldbooleanRender form label above the form field in FormLabel component.
formLabelPropsFormLabelPropsFormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
placeholderstringThe placeholder text to be shown when no option is selected in the select field. Available from version 3.1.0 and above.
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.