RHFMultiAutocomplete
RHFMultiAutocomplete is an enhanced version of RHFAutocomplete that supports selecting multiple options from the dataset. This component also includes a "Select All" feature, allowing users to quickly select all available options with a single click.
This feature is particularly useful for scenarios where users need to handle bulk selections,
such as assigning multiple tags, selecting categories, or choosing items in batch operations.
The implementation ensures flexibility while maintaining a seamless user experience, with the
same validation, customization, and styling options as RHFAutocomplete
.
The freeSolo
prop of Autocomplete is not supported for this component.
Usage
import RHFMultiAutocomplete, { RHFMultiAutocompleteProps } from '@nish1896/rhf-mui-components/mui/multi-autocomplete';
<RHFMultiAutocomplete
fieldName="countriesVisited"
control={control}
options={['Azerbaijan', 'Canada', 'India']}
errorMessage={errors?.countriesVisited?.message}
/>
For options as an array of objects:
<RHFMultiAutocomplete
fieldName="countriesVisited"
control={control}
options={countryList} // imported from RHFCountrySelect
labelKey="name"
valueKey="iso"
errorMessage={errors?.countriesVisited?.message}
/>
When using an array of objects for options, both labelKey
and valueKey
are required. If either is missing, an error will be thrown.
Examples
API
The RHFMultiAutocomplete
interface extends AutocompleteProps
and accepts the following additional props:
Name | Type | Required | Description |
---|---|---|---|
fieldName | string | ✅ | React Hook Form requires name as a key for the registration process. This is a required prop for all components. |
control | UseFormControl | ✅ | The control option yielded on calling the useForm hook. |
registerOptions | RegisterOptions | Register options for validation if using react-hook-form without any validation libraries like yup or Joi. | |
options | string[] or object[] | ✅ | An array with string or object values. Make sure to pass labelKey and valueKey when options is an array of objects. |
labelKey | string | ✅ | The key of object in options array, whose value would be shown as the label in the formfield. Only required when input options is an array of objects. |
valueKey | string | ✅ | The key of object in options array, whose value would be actual value of the option selected in the formfield. Only required when input options is an array of objects. |
selectAllText | string | Custom text to render in place of the "Select All" option that enables user to select all available options in the Autocomplete. | |
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. | |
onValueChange | (fieldValue: string[], targetValue?: string) => void | Callback function that returns the latest fieldValue and value of the item selected. | |
label | ReactNode | The text to render in FormLabel component. By default, the value of fieldName such as firstName will be transformed to display "First Name". | |
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. | |
checkboxProps | CheckboxProps | Checkbox Props to customise each checkbox in checkbox group. | |
formControlLabelProps | FormControlLabelProps | FormControlLabelProps to customise FormControlLabel component for a field. Multiple fields can be configured using the ConfigProvider component. | |
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. | |
errorMessage | ReactNode | Error message to be shown for a field in FormHelperText component. | |
hideErrorMessage | boolean | A flag to prevent replacement of helper text of a field by the error message when the validation is triggered. | |
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 to customise the Chip component for each input tag. |