MUIAutocompleteObject
MUIAutocompleteObject is an Autocomplete that stores the complete option object rather than a primitive.
Note
freeSolo is intentionally unsupported to keep the value type predictable — use MUIAutocomplete
or MUIMultiAutocomplete instead.
Usage#
import MUIAutocompleteObject, {
MUIAutocompleteObjectProps
} from '@nish1896/mui-components/mui/autocomplete-object';const [user, setUser] = useState<User | null>(null);
<MUIAutocompleteObject
fieldName="user"
value={user}
onValueChange={({ newValue }) => setUser(newValue)}
options={users}
labelKey="name"
valueKey="id"
/>Props#
MUIAutocompleteObjectProps also accepts most AutocompleteProps.
Props marked with * are required.
| Name | Type | Description |
|---|---|---|
fieldName* | string | Name/path of the field. Used to derive the id, the default label, and the name attribute. This prop is required for all components. |
ref | Ref<HTMLInputElement> | Forwarded ref for the underlying MUI TextField's input element — use it to imperatively focus the field or read its DOM node. |
value | Option | Option[] | null | Currently selected option object(s): Option[] when multiple is true, otherwise a single Option. A cleared single selection emits null (unless disableClearable); a cleared multi-selection emits [], never null. |
onValueChange* | ({ newValue, event, reason, details }) => void | Called on every selection change with the selected object value(s) from MUI, without reducing them to valueKey. |
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 to compare options with the current value. |
multiple | boolean | When true, allows selecting multiple values. |
disableClearable | boolean | When true, the selected value cannot be cleared from the input. Default: false |
limitTags | number | Maximum number of selected values shown as chips when the input is not focused. Set -1 to disable the limit.Default: 2 |
getLimitTagsText | (more: number) => ReactNode | Custom label rendered for the hidden selections counter. Receives the number of hidden values. |
textFieldProps | TextFieldProps | TextFieldProps forwarded to the internal MUI TextField. |
ChipProps | ChipProps | ChipProps forwarded to chips rendered for selected values. |
label | ReactNode | Label displayed for the field. Defaults to a human-readable label derived from fieldName, e.g. firstName becomes First Name |
showLabelAboveFormField | boolean | When true, renders the field label above the form field in the FormLabel component, instead of inside or beside it. |
formLabelProps | FormLabelProps | FormLabelProps forwarded to the internal FormLabel. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component. |
hideLabel | boolean | When true, hides the rendered field label while preserving accessible labeling where possible. |
required | boolean | Indicates that the field is mandatory by adding an asterisk symbol (*) to the form label and setting the relevant accessibility attributes. |
errorMessage | string | string[] | Validation error for the field — pass a single message string, or a string[] when the field can fail multiple rules at once (every message is shown together). A non-empty string or array puts the field in an error state; undefined/''/[] clear it. Normalize your form library's error shape to this at the call site (e.g. an RHF FieldError via its .message). Use renderError to customize how the message(s) are rendered. |
renderError | (errors: string[]) => ReactNode | Custom renderer for the resolved error message(s), called only when the field is in an error state. Always receives a string[] — use errors[0] for the common single-message case, or map over errors when a field fails several rules. By default a single message renders as text and multiple messages render on separate lines. |
hideErrorMessage | boolean | If true, hides the error message text while keeping the field in an error state. |
helperText | ReactNode | Content displayed in the FormHelperText component below the field when there is no visible validation error. |
formHelperTextProps | FormHelperTextProps | FormHelperTextProps forwarded to the internal FormHelperText. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component. |
customIds | { field, label, helperText, error } | Overrides the default field, label, helper text, and error IDs used for accessibility. |
Playground#
Driven by plain React state — stores the whole selected object; a single
select (labelKey / valueKey) and a multi-select with limitTags and ChipProps.