Material-ui: [Autocomplete] disableCloseOnSelect + selecting using mouse = crash

Created on 19 Nov 2019  路  3Comments  路  Source: mui-org/material-ui

The scenario is pretty common, in case a user tries to select multiple items in a dropdown list using a mouse.

Based on the official demo:
https://codesandbox.io/s/material-demo-2c4ph

Using a mouse:

  • expand dropdown
  • select an item (still ok)
  • without moving a mouse click on another item (crashes)

Works if the mouse pointer is moved, and the item is selected before clicking on it.

bug 馃悰 Autocomplete good first issue

Most helpful comment

I will work on it :)

All 3 comments

@TomasB Nice! We haven't seen a crash for quite some time. The problem is that we reset the highlightedIndex to -1 after the filter listed of options changes. However, if we don't move the mouse, not mouseover event is triggered, we can't rely on the ref value.

What do you think of this diff?

diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
index 2bdc11a27..eb3a12168 100644
--- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
+++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
@@ -630,7 +630,8 @@ export default function useAutocomplete(props) {
   };

   const handleOptionClick = event => {
-    selectNewValue(event, filteredOptions[highlightedIndexRef.current]);
+    const index = Number(event.currentTarget.getAttribute('data-option-index'));
+    selectNewValue(event, filteredOptions[index]);
   };

   const handleTagDelete = index => event => {
@@ -762,6 +763,12 @@ export default function useAutocomplete(props) {
         // Prevent blur
         event.preventDefault();
       },

Do you want to work on it (we would need a test) :)?

I will work on it :)

@weslenng We should be able to add a test case for this one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FranBran picture FranBran  路  3Comments

sys13 picture sys13  路  3Comments

ryanflorence picture ryanflorence  路  3Comments

rbozan picture rbozan  路  3Comments

ghost picture ghost  路  3Comments