I am trying to use a custom autocomplete inside a dialog, but it the autocomplete wont open.
Open the codesandbox below.
You'll see:
Click on "labels" of 1. And you see the autocomplete. Good. Click on "Open" then try to open the autocomplete from the dialog. Impossible. Not Good.
https://codesandbox.io/s/autocomplete-x-dialog-x-autofocus-758fn?file=/src/GitHubLabel.tsx
I believe the culprit is the input's autofocus (from 'renderInput').
For some reason, the autocomplete blurs as soon as it opens, and closes immediatly. Removing the autofocus helps just a bit, you can then open the autocomplete, but it will disappear when clicking the input.
Related closed issue on github: https://github.com/mui-org/material-ui/issues/20915
The issue is that clicking on "Labels" is opening a Popper within a Dialog. By default, Dialog enforces keeping focus on itself. Opening the Popper is taking focus away from the Dialog. When Dialog brings focus back on itself, it is causing the Popper to close.
Adding the disableEnforceFocus prop on the Dialog fixes the problem. Here is a fixed version of your sandbox that includes some console logs that make it easier to see what is happening: https://codesandbox.io/s/autocomplete-x-dialog-x-autofocus-jk0ql?file=/src/InDialog.jsx.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With