RHF-MUI Components
NPM

RHFMultiAutocomplete

RHFMultiAutocomplete is a multi-select Autocomplete that renders each option with a checkbox and an optional "Select All" row. It stores an array of primitive values.

This is particularly useful when users handle bulk selections, such as assigning multiple tags, selecting categories, or choosing items in batch operations. It keeps the same validation, customization and styling options as RHFAutocomplete.

Use RHFMultiAutocompleteObject when you need to store full option objects.

Usage

import RHFMultiAutocomplete, { RHFMultiAutocompleteProps } from '@nish1896/rhf-mui-components/mui/multi-autocomplete';
import { useForm } from 'react-hook-form';

type FormValues = {
  skills: string[];
};

const { control, handleSubmit } = useForm<FormValues>({
  defaultValues: { skills: [] }
});

<form onSubmit={handleSubmit(values => console.log(values))}>
  <RHFMultiAutocomplete
    fieldName="skills"
    control={control}
    options={['React', 'Vue', 'Angular', 'Svelte']}
    registerOptions={{
      validate: value => value.length > 0 || 'Pick at least one skill'
    }}
  />
</form>

The stored value is a string[] when options is an array of strings, or the valueKey property for an array of objects. Clearing the field gives []. Every option has a checkbox, the menu stays open while the user picks, and selected values are shown as chips.

onValueChange receives { newValue, selectedOption }: the next array, and the value that triggered the change. selectedOption is '__ALL__' when "Select All" is toggled, and undefined when the field is cleared.

The label, helper text and error props work as described in RHFTextField.

With an array of objects

When options is an array of objects, both labelKey and valueKey are required. The stored value is a string[] of the valueKey properties of the selected options, not the whole objects.

<RHFMultiAutocomplete
  fieldName="plans"
  control={control}
  options={[
    { id: 'free', name: 'Free' },
    { id: 'pro', name: 'Pro' },
    { id: 'team', name: 'Team' }
  ]}
  labelKey="name"
  valueKey="id"
/>

Select All

A "Select All" row is added at the top of the menu. selectAllText changes its text (Select All by default) and hideSelectAllOption removes it. It is also hidden automatically when there are fewer than two options that can be selected, or when freeSolo is on. Its checkbox is checked when every option is selected and indeterminate when only some are, and clicking it again deselects everything.

Disabled options (see getOptionDisabled below) are left alone: Select All adds every selectable option and keeps the disabled ones as they are, and deselecting keeps only the disabled ones that were already selected.

The internally injected "Select All" option is NOT included in the final form value. Only actual options are returned.

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  selectAllText="All skills"
/>

Customizing individual options

renderOptionLabel customizes the label shown next to an option's checkbox. It receives the option and a state object with selected, index, inputValue and disabled. getOptionDisabled receives an option and returns true to disable it.

<RHFMultiAutocomplete
  fieldName="plans"
  control={control}
  options={[
    { id: 'free', name: 'Free', soldOut: false },
    { id: 'pro', name: 'Pro', soldOut: false },
    { id: 'team', name: 'Team', soldOut: true }
  ]}
  labelKey="name"
  valueKey="id"
  getOptionDisabled={option => option.soldOut}
  renderOptionLabel={(option, { disabled }) => (
    <span style={{ opacity: disabled ? 0.5 : 1 }}>
      {option.name}{disabled ? ' (sold out)' : ''}
    </span>
  )}
/>

Styling the checkboxes

checkboxProps is forwarded to every Checkbox, and formControlLabelProps to every FormControlLabel that wraps it. The sx of formControlLabelProps is merged with the default FormControlLabel styles from ConfigProvider.

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  checkboxProps={{ size: 'small', color: 'secondary' }}
  formControlLabelProps={{ sx: { m: 0 } }}
/>

Limiting the visible chips

While the field is not focused, only the first limitTags chips (default 2) are shown, followed by a "+N More" chip. limitTags={-1} shows all of them, and getLimitTagsText receives the number of hidden chips and returns your own text. ChipProps customizes the chips.

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  limitTags={3}
  getLimitTagsText={hiddenCount => `+${hiddenCount} skills`}
  ChipProps={{ color: 'primary', size: 'small' }}
/>

Free text entry

freeSolo lets the user add a value that isn't in options. The typed string is stored as it is, and shown as a chip. autoSelect defaults to true with freeSolo, so text typed in the field is committed when it loses focus. Pass autoSelect={false} to require Enter.

<RHFMultiAutocomplete
  fieldName="tags"
  control={control}
  options={['bug', 'feature', 'docs']}
  freeSolo
/>
Note

freeSolo is not compatible with the Select All row, which is hidden when it is on. The field can hold both predefined and custom values, so selecting "all" would be confusing.

When using freeSolo with object-based options, custom renderers such as renderValue, renderTags, or renderOptionLabel should handle both option objects and user-entered string values. A common approach is:

typeof value === 'string'

to distinguish free-form input from predefined option objects.

Clearing, loading and placeholder

A clear button is shown once there is a selection, and disableClearable removes it. Set loading while the options are being fetched: a small spinner appears at the end of the input (customize it with circularProgressProps). A placeholder in textFieldProps is shown only while nothing is selected.

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  loading={isFetchingSkills}
  textFieldProps={{ placeholder: 'Pick your skills' }}
/>

Handling value changes

onValueChange runs after the new array has been stored in the form. It receives { newValue, selectedOption }; selectedOption is '__ALL__' when "Select All" is toggled, and undefined when the field is cleared:

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  onValueChange={({ newValue, selectedOption }) => {
    if (selectedOption === '__ALL__') {
      console.log('Everything selected:', newValue);
    }
  }}
/>

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 limits the selection to three values: onValueChange is not called when customOnChange is used.

<RHFMultiAutocomplete
  fieldName="skills"
  control={control}
  options={['React', 'Vue', 'Angular', 'Svelte']}
  customOnChange={({ rhfOnChange, newValue, selectedOption }) => {
    if (newValue.length <= 3) {
      rhfOnChange(newValue);
    }
  }}
/>

Examples

API

The RHFMultiAutocompleteProps 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*string[] / object[]
An array with string or object values. Make sure to pass labelKey and valueKey when options is an array of objects.
labelKeystring
Property name used as the visible label for each option.
Required when options is an array of objects.
valueKeystring
Property name used as the stored value for each option.
Required when options is an array of objects.
freeSoloboolean
Allows users to enter values that are not present in the autocomplete options. Supported from v4 onwards.
customOnChange({ rhfOnChange, newValue, selectedOption }) => void
Override the default onChange behavior of the multi-autocomplete.
onValueChange({ newValue, selectedOption }) => void
Returns the latest value of the field in newValue parameter. The last selected option can be obtained from selectedOption.
selectAllTextstring
Custom text to render in place of the "Select All" option that enables user to select all available options in the Autocomplete.
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.
renderOptionLabel(option, state) => ReactNode
Render the option label content corresponding to each checkbox.
checkboxPropsCheckboxProps
Checkbox Props used to customize the checkbox rendered alongside each autocomplete option.
formControlLabelPropsFormControlLabelProps
FormControlLabelProps to customise FormControlLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
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.

Source Code

View the full implementation of this component on GitHub.