question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InputText component should provide a FocusAsync() method

See original GitHub issue

The PR #23316 introduced ElementReference.FocusAsync() which is convenient to focus an html element. In my case I need to focus an <InputText> component in a form. There are 2 cases where I want to focus the element:

  • In OnAfterRenderAsync when firstRender is true (similar to autofocus)
  • On form submit to focus the element when the user doesn’t provide a value

I currently use a custom component that inherits from InputText and provide a FocusAsync method.

@inherits InputText
@inject IJSRuntime JSRuntime

<input @ref="inputElement"
       @attributes="AdditionalAttributes"
       class="@CssClass"
       value="@CurrentValue"
       @oninput="EventCallback.Factory.CreateBinder<string>(this, __value => CurrentValueAsString = __value, CurrentValueAsString)" />

@code {
    private ElementReference inputElement;

    public ValueTask FocusAsync()
    {
        // could be replaced with inputElement.FocusAsync() with the next release
        return JSRuntime.InvokeVoidAsync("BlazorFocus", inputElement);
    }
}

In the parent component I can use FocusAsync when needed which works well.

I think InputBase<T> or each component should have a FocusAsync method, so I don’t need to override components:

namespace Microsoft.AspNetCore.Components.Forms
{
    public class InputBase<T>
    {
+        public ValueTask FocusAsync();
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:18 (17 by maintainers)

github_iconTop GitHub Comments

6reactions
pranavkmcommented, Aug 13, 2020

Do you think exposing an ElementReference to the wrapped HTML element

I’m slightly partial to not exposing the implementation details of components, but it’s unlikely we would have ever an InputText that does not use an input element. I think it should be fine to expose it. Could we call it something other than BaseElementReference? e.g. InputElementorElementReference(does that mean it's calledSelectElementforInputSelect`?)

class InputText
{
+ ElementReference InputElement { get; }
}

Usage:

@code{
  InputText inputText;
  protected override async Task OnAfterRenderAsync(bool firstRender)
  {
       if(firstRender)
            await inputText.InputElement.FocusAsync();
   }
}
3reactions
uabarahonacommented, Aug 13, 2020

Thank you so much for your help, I am going to proceed according with the discussion

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor's FocusAsync doesn't always set focus
I can't make .NET 6's Blazor AsyncFocus method work. Near as I can tell, AsyncFocus only works when a component value isn't null....
Read more >
ElementReferenceExtensions.FocusAsync Method
FocusAsync (ElementReference, Boolean)​​ Gives focus to an element given its ElementReference.
Read more >
Auto focus an input element in a Blazor form on page load
The workaround is to use the focus() function once the page is rendered to focus the desired element. #Focus an <input> element. First,...
Read more >
Blazor .NET 5 - Easy Focus with FocusAsync (No ... - YouTube
referralCode=8EFA9D9FF38E3065DF0C In this video we'll use the FocusAsync method to focus on an HTML Element.
Read more >
Focus textbox or input programmatically - Telerik UI for Blazor
The Telerik textboxes and inputs offer a FocusAsync method that lets you focus them with code. The example below showcases it for a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found