I'm looking at this example of Autocomplete provided by MaterialUI
https://codesandbox.io/s/81qc1
I was wondering how I can display a "No options found" message if no results are found.
I'm looking at this example of Autocomplete provided by MaterialUI
https://codesandbox.io/s/81qc1
I was wondering how I can display a "No options found" message if no results are found.
Use props noOptionsText for Material-UI Autocomplete
Text to display when there are no options. For localization purposes, you can use the provided translations.
import Autocomplete from '@material-ui/lab/Autocomplete';
<Autocomplete
noOptionsText={'Your Customized No Options Text'}
...
/>
For those who don't want "No options" to show up at all,
<Autocomplete freeSolo />
freeSolo
is enough –
Christiansand You can use a combination of a conditional on freeSolo
and the noOptionsText
field... like so:
<Autocomplete
freeSolo={inputValue?.length ? false : true}
loading={isMatchingUsersLoading}
loadingText={"Searching..."}
options={matchingUsers}
noOptionsText={"No matches..."}
....
/>
This will keep the 'empty box' from showing up AND still give you the noOptionsText
and the loading
text at appropriate times.
In the documentation looking for noOptionsText
prop: https://material-ui.com/api/autocomplete/
const theme = createTheme(
{
components: {
MuiAutocomplete: {
defaultProps: {
noOptionsText: 'any text you want',
},
},
},
}
);
This my solution for removing the No Options
text without freeSolo:
PaperComponent={({ children }) => {
let hasOptions = true
if (React.isValidElement(children)) {
const { className } = children.props
hasOptions = className !== "MuiAutocomplete-noOptions"
}
return (
<Paper>
{hasOptions && children}
</Paper>
)
}}
What we're doing here is simply removing the only listed item from children which is the element with the text No Options
. If there is no class by this string then children will be presented as normal.
© 2022 - 2025 — McMap. All rights reserved.