-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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] unexpectly changes field value on mouse hover #20602
Comments
@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( |
Hi, i can make a PR for this 👯 editeted
|
@marcosvega91 Thanks for the interest in this issue :). The proposed change impacts all the combination of props. It's meant to be a broader fix. The You will find a similar behavior on Kendo-UI jQuery, Downshift, a native select, or event Chrome's autocomplete box, Chrome's URL bar, and Google search autocomplete bar. |
The proposed patch seems to fix the problem in my use case. Thanks. |
Giving up on the issue, for now. It doesn't work #20615 |
@oliviertassinari Not sure I understand. Does that mean it's gonna stay like that? |
@goffioul If you have a concern with this behavior, favor |
@oliviertassinari That is not really an option. Try again in my sandbox, I've disabled |
Use the two states (value/onChange and inputValue/onInputChange) in this case. |
|
Does that mean the app needs 2 state value? If yes, how does the application know which one to use? Could you provide an example of how to use the 2 states of |
Maybe we should have a live demo in https://material-ui.com/components/autocomplete/#controllable-states https://codesandbox.io/s/admiring-dust-9j3dh?file=/src/App.js |
Thanks for the demo. With 2 state values, how does the app know the value to use? E.g. select all text in the field, type |
@goffioul What's your use case? "value" is the selected value by the user, "inputValue" is what is currently rendered in the textbox. Maybe we should better describe this too in the documentation. Enter |
I want a field that can accept free text and also provide a list of predefined value for quick selection. The field also needs to have a non-empty value on mount. And whatever is currently in the field is the wanted value, whether entered with a keyboard or selected from the dropdown list. This is about editing an object that contains a text field, but the user needs to be able to select a value from a predefined set of entries, instead of having to enter the text value every time. Usually, the text field value is part of the predefined set, but one must retain the ability to enter arbitrary text. This is illustrated with this video: |
Thanks for the video, it's perfect, all you need is in the documentation. I'm working on a patch to translate the learnings of this discussion into a better docs. |
Please note that in the video, it only works because I'm using a combination So I'm left with your suggestion to force the app to use 2 state values, one of which is only used to set the value on mount. |
In which version of material-ui-lab this patch is present? Currently I'm using "@material-ui/lab": "^4.0.0-alpha.58" and this patch is missing there. |
We likely want to make this happen, with #23916. |
I have solved this problem by removing the autoSelect property, or by setting autoSelect={false}. |
Totally agree this one would be great to fix, we are struggling with it as well |
An
Autocomplete
component withfreeSolo
andautoSelect
changes the field value by simply hovering the mouse over the dropdown menu and then bluring the field.Current Behavior 😯
The problem is illustrated in this sandbox: https://codesandbox.io/s/trusting-dewdney-cvj7ss?file=/src/App.js
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 🌎
The text was updated successfully, but these errors were encountered: