microsoft / fluentui-blazor

Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
https://www.fluentui-blazor.net
MIT License
3.78k stars 363 forks source link

fix: double submit with Enter #1050

Open iotalambda opened 10 months ago

iotalambda commented 10 months ago

🐛 Bug Report

When navigating to an EditForm's submit button by keyboard and hitting Enter, submit occurs twice.

💻 Repro or Code Sample

dotnet new install Microsoft.FluentUI.AspNetCore.Templates::4.1.1 dotnet new fluentblazor

Replace Counter.razor content with the following, tab your way to the FluentButton and hit Enter -> current count is incremented by 2.

@page "/counter"
@rendermode InteractiveServer

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: <FluentBadge Appearance="Appearance.Neutral">@currentCount</FluentBadge></p>

<EditForm OnValidSubmit=@IncrementCount Model="new { }">
    <button type="submit">Submit with normal button -> +1</button>
    <FluentButton Appearance="Appearance.Accent" Type=@ButtonType.Submit>Submit with FluentButton -> +2</FluentButton>
</EditForm>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

🌍 Your Environment

Occurs at least in Edge and Firefox

vnbaaij commented 10 months ago

I just tried it with same template and your exact code. Not seeing this behavior: counter-#1050

iotalambda commented 10 months ago

I just tried it with same template and your exact code. Not seeing this behavior: counter-#1050 counter-#1050

Did you submit by clicking the button? The bug does not occur when submitting by clicking - only when submitting by activating the button and then hitting Enter on your keyboard.

vnbaaij commented 10 months ago

No, did not do that. Will try that next

vnbaaij commented 10 months ago

Ok, can reproduce with Enter keypress...weird. While we search for a solution, a workaround is to remove the Type=ButtonType.Submit part from the code. The IncrementCount method will then only be called once.

vnbaaij commented 10 months ago

Of course, removing the submit is not alway going to be a viable solution.

I've traced back the bug all the way to the FAST source code (which is what is used to build the fluent-button web component that we wrap in our FluentButton component): https://github.com/microsoft/fast/issues/6704.

Unfortunately, it needs to be fixed there first. After that we need to get a new release of the web components script and integrate that in our library. I'm in contact with the FAST maintainers to see if and how this can be expidited but I'm afraid this behavior can not be changed/circumvented when usen the keyboard until the scripts get fixed.

iotalambda commented 10 months ago

Thanks @vnbaaij. Too bad the problem lies so deep.

Also, another workaround is to use a non-fluent submit button for the time being.

grantcolley commented 1 week ago

@vnbaaij do you have a solution in sight?

vnbaaij commented 1 week ago

No, nothing has changed. We anticipate it wont be fixed until we have the new web components