Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoComplete] List is not filtered when using onChange and onInputChange together #19318

Closed
2 tasks
Aerowiel opened this issue Jan 20, 2020 · 9 comments
Closed
2 tasks
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: Stack Overflow Please ask the community on Stack Overflow

Comments

@Aerowiel
Copy link

Aerowiel commented Jan 20, 2020

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When i'm using both onChange and onInputChange in the AutoComplete component it breaks the filtering. I'm forced to use both of them because if you don't use onInputChange the state is not updated when the user write something and exit the textfield without pressing enter.

Expected Behavior 🤔

The list should get filtered while the user is typing more text.

Steps to Reproduce 🕹

Steps:

  1. Use AutoComplete component
  2. Add onChange and onInputChange on the component (both mapped to the same onChange function)
  3. Try to enter some text in the textfield
  4. Doesn't get filtered...

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v4.8.0
React v16.12.0
Browser
TypeScript
etc.
@oliviertassinari oliviertassinari added status: waiting for author Issue with insufficient information support: Stack Overflow Please ask the community on Stack Overflow and removed status: waiting for author Issue with insufficient information labels Jan 20, 2020
@support
Copy link

support bot commented Jan 20, 2020

👋 Thanks for using Material-UI!

We use GitHub issues exclusively as a bug and feature requests tracker, however,
this issue appears to be a support request.

For support, please check out https://material-ui.com/getting-started/support/. Thanks!

If you have a question on StackOverflow, you are welcome to link to it here, it might help others.
If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

@support support bot closed this as completed Jan 20, 2020
@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Jan 20, 2020
@willwill96

This comment has been minimized.

@PhilSchwerttraeger
Copy link

#18492 (comment)

@watkinshughes
Copy link

We are seeing the same issue in our implementation. Not sure how this is a support issue instead of a bug. Thanks.

@oliviertassinari
Copy link
Member

@watkinshughes Without reproduction and working demos in the documentation, we assume it's a support request.

@atpollmann
Copy link

@oliviertassinari Here is a working demo using both handlers together
If I use onInputChange to update the value state, it autocomplete doesn't filter the list, and when pressing enter, it doesn't trigger the onChange handler.

You can remove onInputChange and everything works fine. The problem with this is that there's no way to update the state without pressing enter.

Thank you

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 16, 2020

@atpollmann You need to have two different states (value + inputValue).

@atpollmann
Copy link

@atpollmann You need to have two different states (value + inputValue).

Thank you @oliviertassinari, my mistake. It is clearly stated in the documentation

@zcemycl
Copy link

zcemycl commented Jun 20, 2021

@Aerowiel I have a solution for this, instead of using filterOptions.

Instead of

const filterOptions = createFilterOptions({
  matchFrom: 'start',
  stringify: option => option.title,
});

<Autocomplete filterOptions={filterOptions} />

You can use filter in options

const [opts,setOpts] = useState({ip:''})
const keyIPs = ['a','aaa','ab','b']
<Autocomplete freeSolo 
value={opts.ip}
onChange={(event, value) => {
       console.log(value)
       setOpts(prev=>{return {...prev,
       ip:value?value:""}})}}
       inputValue={opts.ip}
onInputChange={(event, newInputValue) => {
        console.log(newInputValue)
        setOpts(prev=>{return {...prev,ip:newInputValue}})}}
options={keyIPs.filter((item)=>item.indexOf(opts.ip)===0)} 
getOptionLabel={(option) => option}
       renderInput={(params) => (
       <TextField  {...params} label="IP" margin="normal" variant="outlined"/> )}
 />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests

7 participants