Using @mui/material/Autocomplete, I cannot for the life of me make the component read only!
Even if the renderInput function returns a <TextField readOnly /> component, the drop down menu still trigger onChange events.
I tried <Autocomplete readOnly />, but the property gets ignored.
This seems to be a gross design oversight, or am I missing something? Not all components support the same properties.
Currently, I found that using disabled instead of readOnly yields the desired result, however this creates a non uniform form; certain fields are read only, others are disabled.
** Edit **
Those finding this issue should upvote and follow the ongoing issue on Github.
** Solution **
This is the working solution I have used :
import React, { forwardRef, useState } from 'react';
import PropType from 'prop-types';
import Autocomplete from '@mui/material/Autocomplete';
const AutocompleteEx = forwardRef(({
readOnly,
renderInput,
...props
}, ref) => {
const [open, setOpen] = useState(false);
return (
<Autocomplete ref={ ref }
open={ open }
onOpen={ () => !readOnly && setOpen(true) }
onClose={ () => setOpen(false) }
disableClearable={ readOnly }
renderInput={ ({ inputProps, ...params }) => renderInput({ ...params, inputProps: { readOnly, ...inputProps } }) }
{ ...props }
/>
);
});
AutocompleteEx.propTypes = {
readOnly: PropType.bool,
renderInput: PropType.func.isRequired
};
AutocompleteEx.defaultProps = {
readOnly: false
};
export default AutocompleteEx