Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the text in a MUI Autocomplete?

I have an Autocomplete field which uses:

searchText = {this.state.searchText}

like this;

<AutoComplete
  floatingLabelText='agent input'
  ref='agentInput'
  hintText="type response"
  multiLine = {true}
  fullWidth = {true}
  searchText = {this.state.searchText}
  onNewRequest={this.sendAgentInput}
  dataSource={this.agentCommands}
/>

But when I update the this.setState({searchText: null }) it will clear the autoComplete once, but not the second time.

Not sure if this is a bug or if there's another way to reset the field.

I also tried looking for the field and adding a ref but no luck.

Filed here in case it's a bug https://github.com/callemall/material-ui/issues/2615

like image 267
dcsan Avatar asked Dec 21 '15 01:12

dcsan


People also ask

How do you clear the selected value in autocomplete?

$('selector'). autocomplete('close'). val(''); This will also reset the current search of the autocomplete (in addition to clearing the input).

How do I clear TextField MUI?

There are two methods for clearing the MUI TextField: Setting a state value in the value prop and clearing it. Clearing the inputRef value.

How do I clear select material UI?

The Material-UI Select component is a great user input component with a list of options. However, it is a bad experience to require a user to click into the component and press “backspace” to clear it.

How do I get value from Mui autocomplete?

MUI Autocomplete Get Selected Value I used the onChange handler to pass the selected option to the state value's setter. If we want to get the Autocomplete's selected value for use with a form submission, we only need to wrap the Autocomplete in a form element and add a Button with type="submit" .


1 Answers

Try also to change searchText on every input update:

onUpdateInput={this.handleUpdateInput}

This function should change searchText whenever user changes the input:

handleUpdateInput(text) {
  this.setState({
    searchText: text
  })
}

My code looks as follows (ES6):

class MyComponent extends Component {
  constructor (props) {
    super(props)

    this.dataSource = ['a', 'b' ,'c']
    this.state = {
        searchText: ''
    }

  }

  handleUpdateInput (t) { this.setState({ searchText: t }) }

  handleSelect (t) { this.setState( { searchText: '' }) }

  render () {
    return <AutoComplete dataSource={this.dataSource}
                         searchText={this.state.searchText}
                         onNewRequest={this.handleSelect.bind(this)}
                         onUpdateInput={this.handleUpdateInput.bind(this)}
    />
  }      
}

Here I want to clear input when user presses enter or chooses some item from the list (so I clear searchText in handleSelect) but I also change state of searchText on every input update (handleUpdateInput).

I hope it will work for you!

like image 163
Magda Chmielewska Avatar answered Sep 20 '22 15:09

Magda Chmielewska