RHF-MUI Components
NPM

RHFAutocompleteObject

RHFAutocompleteObject extends Autocomplete and is designed for cases where you need to work with the entire selected option object instead of just a primitive value.

Unlike RHFAutocomplete, which stores only the value derived from valueKey, this component stores and returns the full option object (or array of objects). This is particularly useful when additional metadata from the selected option is required elsewhere in your form or application.

Key Differences

FeatureRHFAutocompleteRHFAutocompleteObject
Stored valuestring / string[]object / object[]
Requires labelKeyOptionalRequired
Requires valueKeyOptionalRequired
Use caseSimple formsMetadata-driven forms
Custom values (freeSolo)Users can enter values not present in optionsNot supported

Usage

import RHFAutocompleteObject, { RHFAutocompleteObjectProps } from '@nish1896/rhf-mui-components/mui/autocomplete-object';

<RHFAutocompleteObject
  fieldName="country"
  control={control}
  options={countryList}
  labelKey="name"
  valueKey="code"
  multiple           // omit this prop for single selection
/>
Warning

Both labelKey and valueKey props are mandatory. Since this component works exclusively with object-based options, omitting either will result in an error.

Note

freeSolo is not supported in RHFAutocompleteObject.

This component stores complete option objects in form state, whereas enabling freeSolo would introduce string values alongside option objects, resulting in mixed value types.

If you need users to enter custom values that are not present in the provided options, use RHFAutocomplete or RHFMultiAutocomplete instead.

Example Option Structure

const countryList = [
  { name: 'India', code: 'IN', currency: 'INR' },
  { name: 'Canada', code: 'CA', currency: 'CAD' },
  { name: 'Germany', code: 'DE', currency: 'EUR' }
];

Stored Value

// Single
{ name: 'India', code: 'IN', currency: 'INR' },

// Multiple
[
  { name: 'India', code: 'IN', currency: 'INR' },
  { name: 'Canada', code: 'CA', currency: 'CAD' }
]

When to Use

Use RHFAutocompleteObject when:

  • You need access to additional fields from the selected option (e.g. currency, id, etc.)
  • You want to avoid mapping IDs back to objects
  • Your form logic depends on rich object data
  • You are working with APIs that expect full objects instead of IDs

Examples

API

The RHFAutocompleteObjectProps interface extends AutocompleteProps and accepts the following additional props. Props marked with * are required.

NameTypeDescription
fieldName*string
Name of the field registered with React Hook Form. This prop is required for all components.
control*UseFormControl
The control option yielded on calling the useForm hook.
registerOptionsRegisterOptions
React Hook Form validation rules. Useful when not using a schema validation library such as Yup or Joi.
options*object[]
An array of objects. labelKey and valueKey are required so the component knows which properties to use for the visible label and the stored value.
labelKey*string
Property name used as the visible label for each option.
valueKey*string
Property name used as the stored value for each option.
multipleboolean
Allows multiple values to be selected.
customOnChange({ rhfOnChange, newValue, event, reason, details }) => void
Override the default onChange behavior of the autocomplete.
onValueChange({ newValue, event, reason, details }) => void
Returns the entire object option(s) selected by the user in newValue parameter. The last selected option can be obtained from details.
labelReactNode
The text to render in the FormLabel component. By default, the value of fieldName is transformed (e.g., "firstName" to "First Name") using the fieldNameToLabel function.
showLabelAboveFormFieldboolean
Render form label above the form field in FormLabel component.
formLabelPropsFormLabelProps
FormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
hideLabelboolean
Hides the FormLabel component if you don’t want to display the default form label component or prefer to render a fully custom label instead.
circularProgressPropsCircularProgressProps
Props forwarded to the CircularProgress displayed in the autocomplete input when it is loading.
Added in v4.3.
requiredboolean
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.
renderError(error: FieldError) => ReactNode
Custom renderer for the React Hook Form field error. Receives the current field error and returns the content to display, such as error.message or a custom React element in the HelperText component.
Added in v4.1.0.
hideErrorMessageboolean
A flag to prevent replacement of helper text of a field by the errorMessage when the validation is triggered.
helperTextReactNode
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.
formHelperTextPropsFormHelperTextProps
FormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.
textFieldPropsTextFieldProps
Props to customise the Autocomplete Textfield.
ChipPropsChipProps
Props applied to the Chip component used to render selected values.
customIds{ field, label, helperText, error }
Overrides the default field, label, helper text, and error IDs used for accessibility.