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

[Autocomplete] the first option is not highlighted for autoHighlight using filterOptions #36336

Closed
2 tasks done
tmtk75 opened this issue Feb 26, 2023 · 5 comments · Fixed by #37012
Closed
2 tasks done
Assignees
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!

Comments

@tmtk75
Copy link

tmtk75 commented Feb 26, 2023

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Link to live example: https://codesandbox.io/s/mui-autocomplete-bug-forked-vj99f8?file=/src/AutocompleteDemo.js:1001-1026

Steps:

  1. Click the autocomplete to focus.
  2. Move the mouse cursor further from the autocomplete to avoid unintentional focus.
  3. Type "god"
  4. Expect "The Godfather" at the top is highlighted but "Goodfellas" is highlighted1.

Current behavior 😯

With v5.11.10, The 4th item, "Goodfellas", is highlighted.
autocomplete-5 11 10

Expected behavior 🤔

With v5.11.6, the 1st item is highlighted expectedly.
autocomplete-5 11 6

Context 🔦

I'm using Fuse for fuzzy searching in filterOptions.

autoHighlight says:

If true, the first option is automatically highlighted.

Currently it behaves unintentionally when Fuse sorts the order of the options.
If I replace Fuse with simple match, it works well. The 1st option is highlighted.
I'm feeling this issue happens when the order of the given options is changed in filterOptions.

It worked with v5.11.6 as I wrote as Expected behavior.

Your environment 🌎

I'm using Chrome 110.0.5481.177.

npx @mui/envinfo
  System:
    OS: macOS 12.6.3
  Binaries:
    Node: 16.14.2 - ~/.anyenv/envs/nodenv/versions/16.14.2/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.5.0 - ~/.anyenv/envs/nodenv/versions/16.14.2/bin/npm
  Browsers:
    Chrome: 110.0.5481.177
    Edge: 110.0.1587.50
    Firefox: Not Found
    Safari: 16.3
  npmPackages:
    @emotion/react: ^11.4.0 => 11.10.6
    @emotion/styled: ^11.3.0 => 11.10.6
    @mui/base:  5.0.0-alpha.115
    @mui/core-downloads-tracker:  5.11.9
    @mui/icons-material: ^5 => 5.11.9
    @mui/lab: 5.0.0-alpha.120 => 5.0.0-alpha.120
    @mui/material: 5.11.6 => 5.11.6
    @mui/private-theming:  5.11.9
    @mui/styled-engine:  5.11.9
    @mui/styles: ^5.11.7 => 5.11.9
    @mui/system:  5.11.9
    @mui/types:  7.2.3
    @mui/utils:  5.11.9
    @types/react: ^18 => 18.0.28
    react: ^18 => 18.2.0
    react-dom: ^18 => 18.2.0
    typescript: ^4.0.5 => 4.9.5

Footnotes

  1. When type enter, "The Godfather" is selected :-)

@tmtk75 tmtk75 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Feb 26, 2023
@tmtk75 tmtk75 changed the title [Autocomplete] the first option is not highlighted for autoHighlight [Autocomplete] the first option is not highlighted for autoHighlight using filterOptions Feb 26, 2023
@zannager zannager added the component: autocomplete This is the name of the generic UI component, not the React module! label Feb 27, 2023
@sai6855
Copy link
Contributor

sai6855 commented Mar 6, 2023

@tmtk75 in v5.11.6 highlighted option would always reset whenever options are changed but from v5.11.7 highlighted option would only reset if previously highlighted option is not present in new options to support #29508

So the behaviour you are expericing is expected behaviour

@tmtk75
Copy link
Author

tmtk75 commented Mar 6, 2023

@sai6855 thanks for your reply.

I'm not sure of #29508 well but autoHighlight says like this.

https://mui.com/material-ui/api/autocomplete/#props

If true, the first option is automatically highlighted.

The current behavior looks weird to me.

Also I wrote as a footnote, in the example to reproduce Godfather is entered when I type enter even though Goodfellas is highlighted.

This is unnatural. I don't think it's expected behavior.

@sai6855
Copy link
Contributor

sai6855 commented Mar 6, 2023

If true, the first option is automatically highlighted.

i think this applies only on initial render of listbox.

This is unnatural. I don't think it's expected behavior.

yes you right, there is bug somewhere.

i think this issue is related to #33634

@michaldudak
Copy link
Member

I suppose we can only run the highlight restoration logic when the options change without changing the input. It's how browser address fields work.
Then, the lazy loading of options would still work without resetting the highlight.

@michaldudak michaldudak added bug 🐛 Something doesn't work and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Mar 13, 2023
@bencevoros
Copy link
Contributor

Is there any update on this issue? I'd like to fix it if no one has started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: autocomplete This is the name of the generic UI component, not the React module!
Projects
None yet
5 participants