Skip to content
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

[HxAutosuggest] [HxInputTags] Stop propagation of Enter key event when selecting an item #584

Closed
TPIvan opened this issue Aug 24, 2023 · 4 comments
Labels
bug Something isn't working up for grabs

Comments

@TPIvan
Copy link
Contributor

TPIvan commented Aug 24, 2023

I have HxAutosuggest in a Form:

<EditForm Model="exampleModel" OnValidSubmit="HandleValidSubmit">   
    <HxAutosuggest Label="Employee"
                   Placeholder="Start typing to search by name"
                   TItem="EmployeeDto"
                   TValue="int?"
                   @bind-Value="@selectedEmployeeId"
                   DataProvider="ProvideSuggestions"
                   MinimumLength="0"
                   ValueSelector="employee => employee.Id"
                   TextSelector="employee => employee.Name"
                   ItemFromValueResolver="ResolveAutosuggestItemFromValue">
        <EmptyTemplate>
            <span class="p-2">Couldn't find any matching employee</span>
        </EmptyTemplate>
    </HxAutosuggest>    
    <p class="mt-3">Selected employee ID: @selectedEmployeeId</p>      
</EditForm>    

@code {
    private ExampleModel exampleModel = new();
    private int? selectedEmployeeId = 1;
    private void HandleValidSubmit()
    {
        System.Diagnostics.Debugger.Break();
    }

    private async Task<AutosuggestDataProviderResult<EmployeeDto>> ProvideSuggestions(AutosuggestDataProviderRequest request)
    {
        var matchingEmployees = await DemoDataService.FindEmployeesByNameAsync(request.UserInput, limitCount: 10, request.CancellationToken);
        return new AutosuggestDataProviderResult<EmployeeDto> { Data = matchingEmployees };
    }

    private async Task<EmployeeDto> ResolveAutosuggestItemFromValue(int? value)
    {
        if (value is null)
        {
            return null;
        }    
        return await DemoDataService.GetEmployeeByIdAsync(value.Value);
    }    
    private class ExampleModel {}
}

The problem is that when I select an item from the drop-down list using the arrow keys and then press the Enter key, the form is submitted automatically. This is not the expected behavior, as I only want to submit the form when I click the submit button. I think that HxAutosuggest should stop the propagation of the Enter key event when an item is selected, so that it does not trigger the form submission. Is there a way to achieve this or is it a bug?

@hakenr
Copy link
Member

hakenr commented Sep 25, 2023

@TPIvan Are you sure? We are using HxAutosuggest in our applications on daily basis and I'm not able to reproduce the form submisssion with Enter key when selecting a suggested item from list.

... I will try to test your repro-code.

@hakenr hakenr added the bug Something isn't working label Sep 25, 2023
@hakenr hakenr added this to the Priority 2 - Normal milestone Sep 25, 2023
@hakenr
Copy link
Member

hakenr commented Sep 25, 2023

In the end I was able to reproduce your issue using the code sample provider. Thank you!

In our code, we usually do not use <EditForm OnValidSubmit="..." /> but we use <HxButton EditContext="editContext" OnValidSubmit="..." /> instead, where it is not affected.

...will track this as a bug to resolve.

@TPIvan
Copy link
Contributor Author

TPIvan commented Sep 25, 2023

I observed this issue with HxInputTags in a rather complex form, so I created a code sample to isolate it. It seems to be reproducible. I found the testing project and I tried it again. The break in HandleValidSubmit was hit when I navigated by arrows in the suggest list and pressed enter. The component version was 4.0.?, so I updated it to the current 4.2.1, but the problem is still there. I can attach the whole project if needed.

@hakenr
Copy link
Member

hakenr commented Sep 25, 2023

Notes for Future Fixes:

  • It appears that the submit action is triggered by the input element, which retains focus while navigating through suggested items.
  • Generally, pressing the Enter key on an input field submits the corresponding form.
  • We'll likely need to manage the onkeydown event within HxAutosuggestInputInternal and prevent the default event behavior when the dropdown menu is open.

(If anyone is interested in addressing this and creating a pull request, your contribution would be greatly appreciated.)

@hakenr hakenr closed this as completed in 334046b Dec 30, 2023
@hakenr hakenr changed the title [HxAutosuggest] Stop propagation of Enter key event when selecting an item [HxAutosuggest] [HxInputTags] Stop propagation of Enter key event when selecting an item Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working up for grabs
Projects
None yet
Development

No branches or pull requests

2 participants