Material-ui: [AutoComplete] Clear AutoComplete Value on click of Button

Created on 18 Jul 2016  路  4Comments  路  Source: mui-org/material-ui

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

Autocomplete

Most helpful comment

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}
                   />);
}

All 4 comments

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}
                   />);
}

Duplicate of #2615

@itsjimbo Thanks! Your code solved my problem ;)

Simple if clear button is enabled

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zabojad picture zabojad  路  3Comments

FranBran picture FranBran  路  3Comments

reflog picture reflog  路  3Comments

finaiized picture finaiized  路  3Comments

ericraffin picture ericraffin  路  3Comments