fix: In autocomplete dropdown, the item’s event has been changed from click to mousedown #16649
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#16625
In an autocomplete input component, events can often lead to unintended order of execution, particularly when handling change and click events. When a user types in the input and then selects an item from the dropdown list, the change event fires first because it’s triggered as soon as the input field loses focus or updates its content. Then, the click event on the dropdown item is executed.
Problem:
change Event: This triggers first when the input value changes, such as when the user clicks outside the input or the dropdown, updating the internal value.
click Event on Dropdown Item: This triggers after the change event, potentially overriding or undoing the intended selection behavior.
Solution with mousedown Event:
To change the order and have the dropdown selection (click) execute before the change event, you can replace click with the mousedown event on the dropdown items. This way:
mousedown Event on Dropdown Item: Fires when the user presses the mouse button down on the dropdown item, executing before the input loses focus.
change Event: Now, the change event fires after mousedown, updating the input field only after the item is selected.