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

[Auto Complete] Clear value issue #4213

Closed
simonelp opened this issue May 9, 2016 · 9 comments
Closed

[Auto Complete] Clear value issue #4213

simonelp opened this issue May 9, 2016 · 9 comments
Labels
bug 🐛 Something doesn't work

Comments

@simonelp
Copy link

simonelp commented May 9, 2016

Problem description

When i try to clear the value of the TextField passing searchText props, after select one item from the menu, the value want change.

I'm using the onNewRequest callback, but it's called before setState with selected value.
Also trying to change searchText props with a setTimeout (after the setState function has been called), the check in componentWillReceiveProps doesn't let the state to be changed.

Steps to reproduce

  • create an Autocomplete controlled component passing it searchText, onNewRequest and dataSource
  • in onNewRequest callback get the selected value and call a function that clear the searchText value in your state (the same searchText you pass to the component)
  • search and select one value in your app

You can see that the value doesn't change as its searchText props.

Versions

  • Material-UI: 0.15.0.beta-2
  • React: 15.0.2
  • Browser: Google Chrome v 50.0.2661.94
@ochowie
Copy link

ochowie commented May 11, 2016

I'm having the same issue in the 0.15.0 release. I'm encountering this under slightly different circumstances where I was trimming the text in searchText after selection and it works but then something seems to overwrite it and the entire selected text comes back. I see something similar when I try to completely clear the field. The field is cleared temporarily and then the value selected from the dropdown returns.

@ochowie
Copy link

ochowie commented May 11, 2016

I've created a simple example using the example code from the documentation. This will first clear the control then write back the selected value.

ochowie added a commit to ochowie/material-ui that referenced this issue May 11, 2016
Removed setting the searchText state from the menu close function and moved the newRequest event after the menu close so that it can be used to overwrite the selected value.

Closes mui#4213
ochowie added a commit to ochowie/material-ui that referenced this issue May 11, 2016
Removed setting the searchText state from the menu close function and moved the newRequest event after the menu close so that it can be used to overwrite the selected value.

Closes mui#4213
@mauvew
Copy link

mauvew commented Jun 30, 2016

any further action on this issue? Clearing the display value after selecting seems quite important, if the auto complete component is used as a search box

@priezz
Copy link

priezz commented Jul 13, 2016

As a workaround I do

const autoComplete = <your ref to AutoComplete>
setTimeout( () => autoComplete && autoComplete.setState({ searchText: "" }), 500 )

in the callback. In this case it works.

@nicodee
Copy link

nicodee commented Jul 16, 2016

@priezz Thanks a lot man. Spent a whole day trying to figure this out.

@oliviertassinari
Copy link
Member

It should be fixed now. The setTimeout hack should no longer be needed.

@FrancoisJULIENNE
Copy link

FrancoisJULIENNE commented Sep 7, 2017

Try this :
this.setState({ searchText: "\r" });

because I think the function should test the length of the string (BUG ?)

@TheCodeNinja254
Copy link

TheCodeNinja254 commented May 29, 2020

I could use some help,
I am able to clear the value after submit by clearing the Form. The previously selected item, however, still remains in the text field. I need to clear this since I am loading the list dynamically and 'refetching' after every submit. See code below:

<Autocomplete
                        onChange={(event, selectedValue) => setFieldValue('SerialNumber', selectedValue, true)}
                        clearText = 'Clear'
                        options={getSerials.serials.map((routers) => serials.SerialNumber)}
                        autoComplete
                        openOnFocus
                        noOptionsText = 'No serial number to match your search'
                        renderInput={(params) => (
                          <TextField
                            {...params}
                            label="Search/Enter serial number"
                            required
                            name="SerialNumber"
                            helperText={
                              errors.SerialNumber || null
                            }
                            variant="outlined"
                            value={values.SerialNumber}
                            SelectProps={{ native: true }}
                          />
                        )}
                      />

@oliviertassinari
Copy link
Member

@TheCodeNinja254 Look into how you can control the value of the component, alternatively, you can ask on Stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work
Projects
None yet
9 participants