React Material-UI Autocomplete customize `no options` text
Asked Answered
P

7

26

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.

Possessory answered 3/4, 2020 at 21:21 Comment(0)
M
43

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'}
  ...
/>

enter image description here

Mychael answered 4/4, 2020 at 4:32 Comment(3)
Is there any option if I don't want to show the noOption popper? If I set it to null or false it still shows blank popperPeeved
@Peeved Maybe you could add a js conditional style to the dropdown area via its className (using MUI style nesting selector).Mychael
@Mychael could you elaborate of how that would work? Maybe even show an example?Pastiche
B
33

For those who don't want "No options" to show up at all,

<Autocomplete freeSolo />
Belter answered 9/4, 2021 at 14:14 Comment(6)
Mister, you deserve a bag of cookies!Nearly
This is exactly what I wanted, so THANKS!!!!!!!!! Also instead of true, I added a condition to not show this before 6 characters to fetch the option list. Basically show it after I really know there are no options.Similitude
Omg, i got what i needed, thanks a million!!!Waylay
You don't need to set true. freeSolo is enoughChristiansand
Chocolate chip cookiesBrindled
No longer supported: codesandbox.io/s/elated-goodall-f4vn9w?file=/src/Demo.tsx This works tho: codesandbox.io/s/elated-goodall-f4vn9w?file=/src/Demo.tsxOutstation
P
4

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.

Possess answered 14/4, 2023 at 16:36 Comment(0)
D
1

In the documentation looking for noOptionsText prop: https://material-ui.com/api/autocomplete/

Devilry answered 4/8, 2020 at 12:43 Comment(0)
D
0
const theme = createTheme(
  {
    components: {
      MuiAutocomplete: {
        defaultProps: {
          noOptionsText: 'any text you want',
        },
      },
    },
  }
);
Donnie answered 5/12, 2022 at 6:59 Comment(2)
Reviewed it in low quality answers queue. While it's code-only, in my opinion it's also self explanatoryGyno
Please don't post only code as an answer, but also provide an explanation of what your code does and how it solves the problem of the question.Limeade
S
0

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.

Sawmill answered 30/7, 2024 at 19:56 Comment(0)
P
-1

I would recommend you to check this link for fast and easy Autocomplete implementation. But if you want to build your own Autocomplete component using useAutocomplete check this

Pituitary answered 3/4, 2020 at 21:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.