简体   繁体   中英

How to disable filtering options in material-ui Autocomplete?

I'm using material-ui Autocomplete . When the user changes input, it fetches suggestions from a backend asynchronously. This is part of the code:

const [options, setOptions] = useState([]);

<Autocomplete
  ...
  freeSolo={true}
  options={options}
  renderInput={params => (
    <TextField
      ...
      {...params}
      onChange={async (e) => {
          // get suggestions from backend
          const suggestions = await getSuggestions(e.target.value);

          // update autocomplete options
          setOptions(suggestions);

          ...
      }}
      InputProps={{
        ...params.InputProps,
        endAdornment: (
          <React.Fragment>
            {loading ? <CircularProgress color="inherit" size={20} /> : null}
            {params.InputProps.endAdornment}
          </React.Fragment>
        ),
      }}
    />
  )}
/>

The problem is that material-ui Autocomplete doesn't show all of the options that are set using "setOptions". It filters them.

for example: Suppose that the user enters "appl" and getSuggestions returns ["apple", "orange", "potato"]. But It only shows "apple" because it filters out "orange" and "potato".

How can I disable filtering?

The filterOptions method is intended to give you the freedom to decide which options will be available and which will be hidden.

If you just want to show all options - just implement the filterOptions to return all the values:

filterOptions={(options, state) => options}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM