Open MatthiasLinster opened 8 months ago
Idk why, trying your stackblitz raised an error
Although I managed to create a working stackblitz with the same code, and indeed I can reproduce the issue there.
Thanks for the report!
IMO and after some investigations, setting your own state to change the choices collides with the <AutocompleteInput>
logic and it leads to this behavior.
I think a good way to achieve what you want to do, is to follow the the React-admin way, taking inspiration from the <ReferenceInput>
. This component fetches choices and creates a ChoicesContext
used by <AutocompleteInput>
.
To conclude, for me this is not a bug.
What you were expecting:
We are using an
AutcompleteInput
with options provided by an external backend. In particular, the backend yields filtered and sorted options based on the user's input. We keep track of the user input within a React state (updated by theonInputChange
callback), which is in turn passed to React/Tanstack query to fetch the options. The order of the search results is only stable for the same query string, i.e. can change if you type further characters.While this works fine, we get some flaky highlighting behavior when we set
autoHighlight
to true. According to the MUI documentation, this prop selects the first element in the list. That is, once you press "Enter", you select the first element. While the selection works fine, the first option is not always highlighted although it will be selected when you press "Enter".What happened instead: The highlighted option is not the first one. Sometimes, there is even no highlighted option at all.
Steps to reproduce:
Related code: https://stackblitz.com/edit/github-fvmfaw?file=src%2FCreateView.tsx
Other information: The same example works with Material UI, see https://stackblitz.com/edit/stackblitz-starters-wxu1kv?file=src%2FApp.tsx
Environment