Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-ui autocomplete clear value

I have one problem in my react code.

I use Material-ui and redux-form. I have select input like and after change this select i should reset value in . I use action 'change' from react-form and set value for textfield. But label in still remains. Can i clear or reset value in ?

<Autocomplete     options={list}     getOptionLabel={option => option.name}     onInputChange={onChange}     onChange={onChangeAutoComplete}     noOptionsText='Нет доступных вариантов'     loadingText='Загрузка...'     openText='Открыть'     renderInput={params => (         <Field             {...params}             label={label}             name={fieldName}             variant="outlined"             fullWidth             component={renderTextField}             className={classes.textField}             margin="normal"         />     )} /> 
like image 920
Kirill Avatar asked Jan 17 '20 16:01

Kirill


People also ask

How do I turn off input in autocomplete MUI?

To hide browser autocomplete with React Material UI Autocomplete and TextField, we can set inputProps. autocomplete to 'off' . We set inputProps. autoComplete to 'off' in the renderInput function to disable browser autocomplete in the text field.

What is autocomplete MUI?

The autocomplete is a normal text input enhanced by a panel of suggested options.


2 Answers

Using hooks on the value prop breaks the functionality of the autocomplete component ( at least for me ). Using class, and setting the local state is the same.

Luckily it is a react component, so it have a "key" prop. When the key prop changes, the component is re-rendered with the default values ( which is an empty array since nothing is selected). I used hooks in the parent component and passed the values to the key prop, whenever reset is needed.

<Autocomplete     key={somethingMeaningful} // Bool, or whatever just change it to re-render the component //...other props /> 

Hope this helps!

like image 128
Milan Neninger Avatar answered Sep 20 '22 17:09

Milan Neninger


use value in your <Autocomplete /> like this:

<Autocomplete     value={this.state.value} //insert your state key here //...other props /> 

Then clear state of that key, to clear the autocomplete field value

like image 20
Wasim Abuzaher Avatar answered Sep 20 '22 17:09

Wasim Abuzaher