InputText component should provide a FocusAsync() method
See original GitHub issueThe 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:
- Created 3 years ago
- Reactions:15
- Comments:18 (17 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 aninput
element. I think it should be fine to expose it. Could we call itsomething other than
BaseElementReference? e.g.
InputElementor
ElementReference(does that mean it's called
SelectElementfor
InputSelect`?)Usage:
Thank you so much for your help, I am going to proceed according with the discussion