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
| Feature | RHFAutocomplete | RHFAutocompleteObject |
|---|---|---|
| Stored value | string / string[] | object / object[] |
Requires labelKey | Optional | Required |
Requires valueKey | Optional | Required |
| Use case | Simple forms | Metadata-driven forms |
Custom values (freeSolo) | Users can enter values not present in options | Not 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
/>Both labelKey and valueKey props are mandatory. Since this component works
exclusively with object-based options, omitting either will result in an error.
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.
| Name | Type | Description |
|---|---|---|
fieldName* | string | Name of the field registered with React Hook Form. This prop is required for all components. |
control* | UseFormControl | |
registerOptions | RegisterOptions | 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. |
multiple | boolean | 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. |
label | ReactNode | 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. |
showLabelAboveFormField | boolean | Render form label above the form field in FormLabel component. |
formLabelProps | FormLabelProps | FormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component. |
hideLabel | boolean | 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. |
circularProgressProps | CircularProgressProps | Props forwarded to the CircularProgress displayed in the autocomplete input when it is loading.Added in v4.3. |
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. |
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. |
hideErrorMessage | boolean | A flag to prevent replacement of helper text of a field by the errorMessage when the validation is triggered. |
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. |
formHelperTextProps | FormHelperTextProps | FormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component. |
textFieldProps | TextFieldProps | Props to customise the Autocomplete Textfield. |
ChipProps | ChipProps | 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. |

