RHFAutocompleteObject
RHFAutocompleteObject is an
Autocomplete that stores the
complete option object in your form, rather than a primitive.
A common use case is picking a record when you need more than its ID later. In
an "Assign reviewer" form, the options are users loaded from an API, such as { id, name, email, role }. RHFAutocompleteObject shows each name and stores
the selected user object itself, so the rest of the form can use the email for
a confirmation message or the role for validation, without searching the list
again for the matching ID.
It also suits edit forms. The saved reviewer can come back from the API as a
fresh object and still be shown as selected, because options are matched by
valueKey rather than by object identity.
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 |
freeSolo is not supported in RHFAutocompleteObject as mixing typed
strings with option objects would make the stored value type unpredictable.
Use RHFAutocomplete or
RHFMultiAutocomplete instead.
Usage
import RHFAutocompleteObject, { RHFAutocompleteObjectProps } from '@nish1896/rhf-mui-components/mui/autocomplete-object';options must be an array of objects, and both labelKey and valueKey
are required. labelKey is the property used as each option's visible label,
and valueKey identifies an option. The stored value is the whole option
object.
import { useForm } from 'react-hook-form';
type User = {
id: number;
name: string;
email: string;
role: string
};
type FormValues = {
reviewer: User | null;
};
const { control, handleSubmit } = useForm<FormValues>({
defaultValues: { reviewer: null }
});
<form onSubmit={handleSubmit(values => console.log(values.reviewer?.email))}>
<RHFAutocompleteObject
fieldName="reviewer"
control={control}
options={users}
labelKey="name"
valueKey="id"
registerOptions={{ required: 'Please pick a reviewer' }}
/>
</form>Given these options:
const countryList = [
{ name: 'India', code: 'IN', currency: 'INR' },
{ name: 'Canada', code: 'CA', currency: 'CAD' }
];the stored value is a single object ({ name: 'India', code: 'IN', currency: 'INR' }) or, with multiple, an array of them.
The label, helper text and error props work as described in RHFTextField.
Multiple selection
Set multiple to store an array of objects, shown as chips. Use [] as the
default value. limitTags, getLimitTagsText and ChipProps work as in
RHFAutocomplete.
<RHFAutocompleteObject
fieldName="reviewers"
control={control}
options={users}
labelKey="name"
valueKey="id"
multiple
limitTags={3}
registerOptions={{
validate: value => value.length > 0 || 'Pick at least one reviewer'
}}
/>Reading the selected object
Because the full object is stored, you can derive other UI from it with
useWatch:
const reviewer = useWatch({ control, name: 'reviewer' });
<RHFAutocompleteObject
fieldName="reviewer"
control={control}
options={users}
labelKey="name"
valueKey="id"
/>
{reviewer && <p>We will email {reviewer.email}</p>}Clearing, loading and the input
disableClearable removes the clear button, so give the field an initial value.
loading shows a spinner while options are fetched, and circularProgressProps
customizes it. textFieldProps is forwarded to the underlying TextField.
<RHFAutocompleteObject
fieldName="reviewer"
control={control}
options={users}
labelKey="name"
valueKey="id"
loading={isFetchingUsers}
circularProgressProps={{ size: 16 }}
textFieldProps={{ placeholder: 'Search reviewers', size: 'small' }}
/>Handling value changes
onValueChange runs after the selected object has been stored in the form. It
receives { newValue, event, reason, details }, so you can use the whole
object, not just an ID:
<RHFAutocompleteObject
fieldName="reviewer"
control={control}
options={users}
labelKey="name"
valueKey="id"
onValueChange={({ newValue, event, reason, details }) => setValue('reviewerEmail', newValue?.email ?? '')}
/>Use customOnChange when you need to change what gets stored. It replaces the
default handling, so you must call rhfOnChange yourself with the value to
store. This example refuses to select an inactive user: onValueChange is not
called when customOnChange is used.
<RHFAutocompleteObject
fieldName="reviewer"
control={control}
options={users}
labelKey="name"
valueKey="id"
customOnChange={({ rhfOnChange, newValue, event, reason, details }) => {
if (newValue?.role === 'REVIEWER') {
rhfOnChange(newValue);
}
}}
/>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. |
Source Code
View the full implementation of this component on GitHub.

