MUI Components
NPMGithub

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.

NameTypeDescription
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.
refRef<HTMLInputElement>
Forwarded ref for the underlying MUI TextField's input element — use it to imperatively focus the field or read its DOM node.
valueOption | 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.
multipleboolean
When true, allows selecting multiple values.
disableClearableboolean
When true, the selected value cannot be cleared from the input.
Default: false
limitTagsnumber
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.
textFieldPropsTextFieldProps
TextFieldProps forwarded to the internal MUI TextField.
ChipPropsChipProps
ChipProps forwarded to chips rendered for selected values.
labelReactNode
Label displayed for the field. Defaults to a human-readable label derived from fieldName, e.g. firstName becomes First Name
showLabelAboveFormFieldboolean
When true, renders the field label above the form field in the FormLabel component, instead of inside or beside it.
formLabelPropsFormLabelProps
FormLabelProps forwarded to the internal FormLabel. The id is managed by the component. Multiple fields can be configured using the ConfigProvider component.
hideLabelboolean
When true, hides the rendered field label while preserving accessible labeling where possible.
requiredboolean
Indicates that the field is mandatory by adding an asterisk symbol (*) to the form label and setting the relevant accessibility attributes.
errorMessagestring | 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.
hideErrorMessageboolean
If true, hides the error message text while keeping the field in an error state.
helperTextReactNode
Content displayed in the FormHelperText component below the field when there is no visible validation error.
formHelperTextPropsFormHelperTextProps
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.

Reference#