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

AutocompleteInput selects a value other than the highlighted one #9593

Open
MatthiasLinster opened this issue Jan 18, 2024 · 2 comments
Open

Comments

@MatthiasLinster
Copy link

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 the onInputChange 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:

  1. Go to https://stackblitz.com/edit/github-fvmfaw?file=src%2FCreateView.tsx
  2. Click on the add button.
  3. Type "duc" into the autocomplete field. The option "duck1" will be correctly highlighted.
  4. Press backspace to remove "uc" so that you end up with "d" in the autocomplete field. Now, "duck1" is highlighted although it is not the first option.
  5. Press enter. "dog1" is selected although "duck1" is highlighted.

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

  • React-admin version: 4.16.0
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18.2.0
  • Browser: Chrome
  • Stack trace (in case of a JS error):
@slax57
Copy link
Contributor

slax57 commented Jan 22, 2024

Idk why, trying your stackblitz raised an error

image

Although I managed to create a working stackblitz with the same code, and indeed I can reproduce the issue there.

Thanks for the report!

@slax57 slax57 added the bug label Jan 22, 2024
@adguernier adguernier self-assigned this Apr 5, 2024
@adguernier
Copy link
Contributor

adguernier commented Apr 5, 2024

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.

@adguernier adguernier removed their assignment Apr 5, 2024
@fzaninotto fzaninotto removed the bug label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants