Skip to content

Conversation

@spingee
Copy link

@spingee spingee commented Oct 27, 2025

Checklist:

  • The PR is submitted to the correct branch (dev).
  • My code follows the style of this project.
  • I've added relevant tests or confirmed existing ones.

Fixes #6657 , i think bunit doesnt support testing bubling enter key event for submit submit, so no tests

@spingee spingee changed the title Autocomplete prevent submit on enter key when popup open MudAutoComplete: Prevent submit on enter key when popup open Oct 27, 2025
gemini-code-assist[bot]

This comment was marked as spam.

@mudbot mudbot bot added the bug Unexpected behavior or functionality not working as intended label Oct 28, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds keyboard interception functionality to the MudAutocomplete component to handle special key combinations (Ctrl+Shift+Backspace for clearing, Escape/Tab for closing menu) and dynamically manage Enter key behavior based on the menu state.

Key changes:

  • Integrated IKeyInterceptorService for keyboard event handling
  • Added dynamic Enter/NumpadEnter key prevention based on menu open state
  • Implemented special keyboard shortcuts (Ctrl+Shift+Backspace to clear, Escape/Tab to close menu)

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
MudAutocomplete.razor.cs Added KeyInterceptorService injection, key handling logic, and calls to HandleKeyInterception when menu state changes
MudAutocomplete.razor Added id attribute to outer div to enable key interception targeting
Comments suppressed due to low confidence (1)

src/MudBlazor/Components/Autocomplete/MudAutocomplete.razor.cs:1204

  • The KeyInterceptorService subscription is not being unsubscribed in the disposal method. This can lead to memory leaks as the service will retain references to this component. Add await KeyInterceptorService.UnsubscribeAsync(_elementId); before calling base.DisposeAsyncCore(), similar to how MudSelect handles disposal (see MudSelect.razor.cs line 1373).
        protected override async ValueTask DisposeAsyncCore()
        {
            if (_debounceTimer is not null)
            {
                await _debounceTimer.DisposeAsync();
            }

            if (_cancellationTokenSrc is not null)
            {
                try
                {
                    await _cancellationTokenSrc.CancelAsync();
                }
                catch { /*ignored*/ }
                try
                {
                    _cancellationTokenSrc.Dispose();
                }
                catch { /*ignored*/ }
            }

            await base.DisposeAsyncCore();
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

public partial class MudAutocomplete<T> : MudBaseInput<T>
{
/// <summary>
/// We need a random id for the year items in the year list so we can scroll to the item safely in every DatePicker.
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment incorrectly references 'DatePicker' when this is an Autocomplete component. This appears to be copied from another component and should be updated to describe the purpose of _componentId in the context of MudAutocomplete.

Suggested change
/// We need a random id for the year items in the year list so we can scroll to the item safely in every DatePicker.
/// Unique component id used for referencing this Autocomplete instance in the DOM (e.g., for scrolling, focus management, or accessibility).

Copilot uses AI. Check for mistakes.

_keyInterceptorObserving = true;
var options = new KeyInterceptorOptions(
"mud-input-slot",
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The target class 'mud-input-slot' may not work correctly with the current element structure. MudSelect uses 'mud-input-control' as the target class because it directly contains the MudInputControl component with that class. Consider using 'mud-input-control' instead to match the pattern used in MudSelect, or verify that 'mud-input-slot' is the correct target for the keyboard events you want to intercept.

Suggested change
"mud-input-slot",
"mud-input-control",

Copilot uses AI. Check for mistakes.
/// We need a random id for the year items in the year list so we can scroll to the item safely in every DatePicker.
/// </summary>
private readonly string _componentId = Identifier.Create();
private string _elementId = Identifier.Create("autocomplete");
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field '_elementId' can be 'readonly'.

Suggested change
private string _elementId = Identifier.Create("autocomplete");
private readonly string _elementId = Identifier.Create("autocomplete");

Copilot uses AI. Check for mistakes.
@danielchalmers
Copy link
Member

Could you add some tests to ensure it doesn't get reverted in the future?

@mudbot mudbot bot added the needs: tests A maintainer has explicitly asked for associated test cases to be added to this pull request label Oct 31, 2025
@danielchalmers danielchalmers changed the title MudAutoComplete: Prevent submit on enter key when popup open MudAutocomplete: Prevent submit on enter key when popup open Oct 31, 2025
@spingee
Copy link
Author

spingee commented Oct 31, 2025

I tried but bunit seems to doesnt support these bubling behaviors

@versile2 versile2 self-requested a review October 31, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended needs: tests A maintainer has explicitly asked for associated test cases to be added to this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MudAutocomplete submits form when item is selected with Enter-key

2 participants