Material-ui: [Autocomplete] Clear button does not disappear when the input is cleaned manually

Created on 17 Dec 2019  路  2Comments  路  Source: mui-org/material-ui

The clear button inside the input (the button used to clear the input) is displayed even if the input is empty (only if you clear it manually not using the button)

  • [x] The issue is present in the latest release.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 馃槸

The clear button is displayed even if the input is empty

Expected Behavior 馃

The clear button should not be displayed if the input is empty.

Steps to Reproduce 馃暪

Steps:

  1. Create a Autocomplete component with freeSolo bool to true
<Autocomplete
                      freeSolo
                      autoHighlight
                      options={fins.map(fin => fin.name)}
                      value={values.fin}
                      onChange={(event, newValue) => {
                        handleChange({target:{
                          name: "fin",
                          value: newValue,
                        }})
                      }}
                      onInputChange={(event, newValue) => {
                        handleChange({target:{
                          name: "fin",
                          value: newValue,
                        }})
                      }}
                      renderInput={params => (
                        <TextField
                          {...params}
                          required
                          fullWidth
                          className={classes.textField}
                          name="fin"
                          label="FIN"
                          type="text"
                          margin='normal'
                          error={errors.fin ? true : false}
                          helperText={errors.fin}
                        />
                      )}
                    />
  1. Write anything in the input
  2. Delete manually by using "suppr" button on your keyboard
  3. Notice that the clear button is displayed even if the input is empty

SOLUTION 馃敠

Check if the value is an empty string OR null, atm the Autocomplete component is only checking if the input value is null...

Your Environment 馃寧

The environment is not important here obv.

| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.8.0 |
| React | v16.12.0 |
| Browser | Chrome latest version |
| TypeScript | NO |
| etc. | |

Autocomplete question

Most helpful comment

@Aerowiel It seems that you are not using the controllable API correctly.
@AlfredoGJ Your example looks OK. You can ask on StackOverflow how to get the behavior you desire. tip: check the concept of controllable/uncontrolled component out.

All 2 comments

@Aerowiel it looks like this has more to do with the value prop than the freeSolo mode. When you set the value prop and then you try to clear the value means the keyboard or the clear button it clears the text but internally the component keeps the previous value. Using freeSolo or not. This happens in the multiple select mode too.

You can see this here: https://codesandbox.io/s/select-in-appbar-ixwq4

I think that the user must be able to change the value of the autocomplete component although it was set in with the value prop. but I don't know. Is this the expected behavior @oliviertassinari ?

@Aerowiel It seems that you are not using the controllable API correctly.
@AlfredoGJ Your example looks OK. You can ask on StackOverflow how to get the behavior you desire. tip: check the concept of controllable/uncontrolled component out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

finaiized picture finaiized  路  3Comments

ghost picture ghost  路  3Comments

revskill10 picture revskill10  路  3Comments

rbozan picture rbozan  路  3Comments

mb-copart picture mb-copart  路  3Comments