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] Clear AutoComplete Value on click of Button #4736

Closed
dhavalsoni2001 opened this issue Jul 18, 2016 · 10 comments
Closed

[AutoComplete] Clear AutoComplete Value on click of Button #4736

dhavalsoni2001 opened this issue Jul 18, 2016 · 10 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@dhavalsoni2001
Copy link

dhavalsoni2001 commented Jul 18, 2016

I want to store data in list with AutoComplete Component. User write text in AutoComplete Component and clicking on add Button data should stored in list and after that I want to clear AutoComplete Text.
But there is no method or properties to clear Input of AutoComplete.

Version :
0.15.0

@itsjimbo
Copy link

itsjimbo commented Jul 19, 2016

I got it working by using setState on the searchText property with refs as in

import AutoComplete from 'material-ui/AutoComplete';
...
 handleNewRequest(chosenRequest,idx){
        if (idx>-1)  //only allow selected items to be added
        {
            //do something with the chosenRequest, eg rest request
             setTimeout(()=>{
                    this.refs[`autocomplete`].setState({searchText:''});
                    this.refs[`autocomplete`].focus();
                }, 700);
        }
    }

and the render

render(){
 return (<AutoComplete
                       ref={`autocomplete`}
                       hintText="Enter Some Tezt..."
                       dataSource={...}
                       onNewRequest={this.handleNewRequest.bind(this)}
                       fullWidth={true}
                   />);
}

@aahan96
Copy link
Contributor

aahan96 commented Jul 20, 2016

Duplicate of #2615

@aahan96 aahan96 closed this as completed Jul 20, 2016
@Stosiu
Copy link

Stosiu commented Sep 1, 2016

@itsjimbo Thanks! Your code solved my problem ;)

@jaggu07
Copy link

jaggu07 commented Jun 11, 2020

Simple if clear button is enabled

this.refs[autocomplete ].getElementsByClassName('MuiAutocomplete-clearIndicator')[0].click();

@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Jun 11, 2020
@Sakthivelgovindan
Copy link

Sakthivelgovindan commented Apr 25, 2021

Hi @dhavalsoni2001,

Try this, it's working for me,

< Autocomplete inputValue={this.state.labelName} />

Refer :
https://stackoverflow.com/questions/58626031/how-to-clear-the-autocompletes-input-in-material-ui-after-an-onchange

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Apr 25, 2021
@gowthamvbhat
Copy link

Is there any straightforward way to achieve this when using autocomplete as an uncontrolled component ?

@nihalvaria
Copy link

I had similar usecase to clear the Autocomplete component using external button,
I also tried this solution , but with this the component gets in focus and dropdown opens, I didn't want that because I had multiple Autocomplete components.

Finally I solved it by changing the key for Autocomplete component,
It forces it to remount (not the most optimal solution), hence the component resets.

@johnvonneumann7
Copy link

I got it working by using setState on the searchText property with refs as in

import AutoComplete from 'material-ui/AutoComplete';
...
 handleNewRequest(chosenRequest,idx){
        if (idx>-1)  //only allow selected items to be added
        {
            //do something with the chosenRequest, eg rest request
             setTimeout(()=>{
                    this.refs[`autocomplete`].setState({searchText:''});
                    this.refs[`autocomplete`].focus();
                }, 700);
        }
    }

and the render

render(){
 return (<AutoComplete
                       ref={`autocomplete`}
                       hintText="Enter Some Tezt..."
                       dataSource={...}
                       onNewRequest={this.handleNewRequest.bind(this)}
                       fullWidth={true}
                   />);
}

How can this be accomplished with a function component?

@jlev
Copy link

jlev commented Oct 6, 2022

+1 to resetting the key. That's the only thing that worked for my use case

@ZeInhumane
Copy link

life is full of ups and downs

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: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests