question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Autocomplete] unexpectly changes field value on mouse hover

See original GitHub issue

An Autocomplete component with freeSolo and autoSelect changes the field value by simply hovering the mouse over the dropdown menu and then bluring the field.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

The problem is illustrated in this sandbox: https://codesandbox.io/s/thirsty-hodgkin-n1ype

In the first field, click the popup arrow icon to open the dropdown menu, then hover the mouse down such that menu items are successively highlighted. Continue to move mouse down outside of the menu area and click anywhere to blur the field.

The field value (and associated state) will be changed to the last menu item that was hovered (and highlighted).

Expected Behavior 🤔

As the user hasn’t made any selection in the dropdown menu, the field value shouldn’t change.

Context 🔦

The use case is to create a free text entry with a set of suggested values for quick entry.

Your Environment 🌎

Tech Version
Material-UI v4.9.10
Material-UI lab v4.0.0-alpha.49
React 16.12.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:23 (20 by maintainers)

github_iconTop GitHub Comments

2reactions
oliviertassinaricommented, Apr 16, 2020

@goffioul This is one of the reasons why this props combination isn’t used by default by the Autocomplete. However, I think that there is room for improvement here. What do you think of this patch?

diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 3cb1ca0c5..4de19514a 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -790,6 +790,10 @@ export default function useAutocomplete(props) {
     setHighlightedIndex(index, 'mouse');
   };

+  const handleListboxMouseLeave = () => {
+    changeHighlightedIndex('reset', 'next');
+  };
+
   const handleOptionTouchStart = () => {
     isTouch.current = true;
   };
@@ -955,6 +959,7 @@ export default function useAutocomplete(props) {
         // Prevent blur
         event.preventDefault();
       },
+      onMouseLeave: handleListboxMouseLeave,
     }),
     getOptionProps: ({ index, option }) => {
       const selected = (multiple ? value : [value]).some(
Read more comments on GitHub >

github_iconTop Results From Across the Web

[Autocomplete] unexpectly changes field value on mouse hover
An Autocomplete component with freeSolo and autoSelect changes the field value by simply hovering the mouse over the dropdown menu and then ...
Read more >
JQuery UI Autocomplete focus() being called unexpectedly in ...
focus() to fire when I click on an item in the menu e.g. I enter "J", click on "JavaScript" and "JavaScript" should now...
Read more >
Form Input | Components - BootstrapVue
The custom input and change events receive a single argument of the current value (after any formatting has been applied), and are triggered...
Read more >
Hovering over a menu item should not change the text field
Autocomplete : Hovering over a menu item should not change the text field ... (is really common) I pass over with my mouse...
Read more >
Behavior Change Of AutoComplete = "Username" In Recent ...
Upon digging into the HTML to see if something in the markup had changed, I noticed that our login form was using autocomplete="username"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found