Material-ui: [Autocomplete] onOpen callback invoked twice

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

  • [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 馃槸

I'm using @material-ui/lab v4.0.0-alpha.36. It seems that onOpen callback of Autocomplete components is getting invoked twice when clicking into field.

Example of the component usage:

<Autocomplete
      value={props.value}
      options={props.options}
      loading={props.loading}
      multiple={props.multiple}
      renderInput={params => (
        <TextField
          {...params}
          fullWidth
          name={props.name}
          label={props.label}
          onBlur={props.onBlur}
          InputProps={{
            ...params.InputProps,
            endAdornment: (
              <>
                {props.loading ? <CircularProgress color="inherit" size={20} /> : null}
                {params.InputProps.endAdornment}
              </>
            ),
          }}
        />
      )}
      onOpen={() => console.log('on open')} <-- this is printed twice
      onChange={handleChange}
      getOptionLabel={option => option.label}
      getOptionSelected={(option, value) => option.value === value}
    />

Expected Behavior 馃

onOpen callback should be invoked only once.

Steps to Reproduce 馃暪

Steps:

  1. Use Autocomplete component.
  2. Provide onOpen callback.
  3. Click into the field.
  4. See that onOpen invoked twice.

Context 馃敠

Your Environment 馃寧

| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.0.0-alpha.36 |
| React | v16.12.0 |
| Browser | Chrome |
| TypeScript | - |
| etc. | |

Autocomplete incomplete

All 10 comments

@Mindtraveller Why is this an issue?

@Mindtraveller Why is this an issue?

Because Input was opened once whereas callback is invoked twice.
It's the same like invoking onChange or any other callback twice.

Do you think this is expected behaviour?

I have added the waiting for users upvotes tag. I'm closing the issue as we are not sure it's important. So please upvote this issue if you are. We will prioritize our effort based on the number of upvotes.

Hi! I still have same problem.
So I want to share one of solutions. You can control "open" state.
Also you can call this in events of Autocomplete component: onClose, onClickCapture, onOpen (actually work without this event in my code)

```const [isOpen, changeIsOpen] = useState(false);
const openAutocomplete = (newOpenState: boolean) => {
if (isOpen === false && newOpenState !== isOpen) {
onOpen && onOpen();
}
if (isOpen === true && newOpenState !== isOpen) {
onClose && onClose();
}
changeIsOpen(newOpenState);
};

What's the issue? Why does it matter how many time the callback is fired?

What's the issue? Why does it matter how many time the callback is fired?

I want to load data from server when i click Autocomplete component: https://material-ui.com/ru/components/autocomplete/#Asynchronous.tsx

Issue: double trigger of "onOpen" event. So, when i click Autocomplete element its trigger twice "fetch" to my server.

To be sure:
Component render once, but onOpen triggered twice, so it's not re-render mistake

Does anyone has a reproduction? I can't reproduce.

Does anyone has a reproduction? I can't reproduce.

I just want to share some more information.
If put alert('on open') into event handler, handler will invoke infinitely. Maybe its related with animation, I don't know.
Also my component wrapped by mobx.

Example:

<Autocomplete
      value={props.value}
      options={props.options}
      loading={props.loading}
      multiple={props.multiple}
      renderInput={params => (
        <TextField
          {...params}
          fullWidth
          name={props.name}
          label={props.label}
          onBlur={props.onBlur}
          InputProps={{
            ...params.InputProps,
            endAdornment: (
              <>
                {props.loading ? <CircularProgress color="inherit" size={20} /> : null}
                {params.InputProps.endAdornment}
              </>
            ),
          }}
        />
      )}
      onOpen={() => alert('on open')} <-- this is alert infinitely 
      onChange={handleChange}
      getOptionLabel={option => option.label}
      getOptionSelected={(option, value) => option.value === value}
    />

Please provide a minimal reproduction test case. This would help a lot 馃懛 .
A live example would be perfect. This codesandbox.io template _may_ be a good starting point. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

ghost picture ghost  路  3Comments

revskill10 picture revskill10  路  3Comments

FranBran picture FranBran  路  3Comments

newoga picture newoga  路  3Comments