RHFMultiAutocompleteObject
RHFMultiAutocompleteObject is a multi-select
Autocomplete for object-based
options. Each option has a checkbox, there is a "Select All" row, and the
stored value is the array of full option objects, not primitives.
Use RHFMultiAutocomplete when a
string[] of one property is enough.
freeSolo is not supported in RHFMultiAutocompleteObject. Mixing typed
strings with option objects would make the stored value type unpredictable.
Use RHFAutocomplete or
RHFMultiAutocomplete instead.
Usage
import RHFMultiAutocompleteObject, { RHFMultiAutocompleteObjectProps } from '@nish1896/rhf-mui-components/mui/multi-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.
import { useForm } from 'react-hook-form';
type Country = { name: string; code: string; currency: string };
type FormValues = {
countries: Country[];
};
const { control, handleSubmit } = useForm<FormValues>({
defaultValues: { countries: [] }
});
<form onSubmit={handleSubmit(values => console.log(values.countries))}>
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
registerOptions={{
validate: value => value.length > 0 || 'Select at least one country'
}}
/>
</form>Both labelKey and valueKey are required. This component only supports
object-based options.
Select All
A "Select All" row is added at the top of the menu. selectAllText changes
its text and hideSelectAllOption removes it.
- Clicking it selects every available option and stores the full array of objects.
- When all options are selected, clicking it again clears the selection.
- Its checkbox is indeterminate when some, but not all, options are selected.
The internally injected "Select All" option is NOT included in the final form value. Only actual options are returned.
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
selectAllText="All countries"
/>Customizing individual options
renderOptionLabel customizes the label next to an option's checkbox, and
getOptionDisabled disables options. checkboxProps and
formControlLabelProps style every option row.
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
renderOptionLabel={option => `${option.name} (${option.currency})`}
getOptionDisabled={option => option.currency === 'EUR'}
checkboxProps={{ size: 'small' }}
/>Limiting chips, loading and the input
limitTags, getLimitTagsText and ChipProps control the chips while the
field is not focused. loading shows a spinner while options are fetched,
disableClearable removes the clear button, and textFieldProps is forwarded
to the underlying TextField.
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
limitTags={3}
loading={isFetchingCountries}
textFieldProps={{ placeholder: 'Search countries' }}
/>Handling value changes
onValueChange runs after the new array of objects has been stored in the
form. It receives { newValue, selectedOption }:
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
onValueChange={({ newValue, selectedOption }) => {
setCurrencies(newValue.map(country => country.currency));
}}
/>Use customOnChange when you need to change what gets stored. It replaces the
default handling, so you must call rhfOnChange yourself with the array to
store. This example limits the selection to three countries: onValueChange is
not called when customOnChange is used.
<RHFMultiAutocompleteObject
fieldName="countries"
control={control}
options={countryList}
labelKey="name"
valueKey="code"
customOnChange={({ rhfOnChange, newValue, selectedOption }) => {
if (newValue.length <= 3) {
rhfOnChange(newValue);
}
}}
/>The label, helper text and error props work as described in RHFTextField.
Examples
API
The RHFMultiAutocompleteObjectProps 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. |
customOnChange | ({ rhfOnChange, newValue, selectedOption }) => void | Override the default onChange behavior of the autocomplete. |
onValueChange | ({ newValue, selectedOption }) => void | Returns the latest value of the field in newValue parameter. The last selected option can be obtained from selectedOption. |
selectAllText | string | Custom text to render in place of the "Select All" option that enables user to select all available options in the Autocomplete. |
hideSelectAllOption | boolean | A flag to hide the "Select All" option that enables user to select all available options. This option will be automatically hidden when there are less than 2 options to select from. |
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. |
renderOptionLabel | (option, state) => ReactNode | Render the option label content corresponding to each checkbox. |
checkboxProps | CheckboxProps | Checkbox Props used to customize the checkbox rendered alongside each autocomplete option. |
formControlLabelProps | FormControlLabelProps | FormControlLabelProps to customise FormControlLabel component for a field. Multiple fields can be configured using the ConfigProvider component. |
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.

