Search code examples
c#blazormudblazor

Clear MudTextField after Enter key pressed


I am taking input from user into MudTextField. When user input some value and press ENTER key, the a method (OnKeyPressed) is called and some task is done and same MudTextField is remained focused. I want when execution of this method is done, then MudTextField should remain focused but all input text should be cleared. I have set the bind value to null, but it is not working. Please guide me how to do this.

<MudTextField Required="true"
    Disabled="true"
    @onkeypress="OnKeyPressed"
    Immediate="true"
    Margin="Margin.Dense"
    @bind-Value="Barcode"
    Label=""
    Variant="Variant.Outlined"
    Adornment="Adornment.Start"
    AdornmentIcon="@Icons.Material.Filled.Person">
</MudTextField>

public async void OnKeyPressed(KeyboardEventArgs e)
{
    if (e.Key == "Enter")
    {
        // some login is implemented here
        Barcode = null;
    }
}

Please guide me how to do this.


Solution

    1. Use the OnKeyPress property instead of @onkeypress
    2. Set TextUpdateSuppression property to false.
    <MudTextField Required="true"
                  Disabled="false"
                  TextUpdateSuppression="false"
                  OnKeyPress="OnKeyPressed"
                  Immediate="true"
                  Margin="Margin.Dense"
                  @bind-Value="Barcode"
                  Label=""
                  Variant="Variant.Outlined"
                  Adornment="Adornment.Start"
                  AdornmentIcon="@Icons.Material.Filled.Person">
    </MudTextField>