I would like components like the TextBox, NumericTextBox, TextArea, Button to have a method in their reference similar to the FocusAsync() which Microsoft included to the ElementReference in .NET5.
---
ADMIN EDIT
For the time being, you can use JS interop and prepare a suitable selector. Here is a basic example for the button:
@inject IJSRuntime _js
Notes:<br />
- Move this script to a proper place, it is hacked into the component to make this snippet short
- Make sure the ID is unique if you use IDs. THere are other selectors you can use (such as classes, or you can even cascade your selectors to make them more specific)
<br /><br />
<script suppress-error="BL9992">
function focusElement(selector) {
var elem = document.querySelector(selector);
if (elem && elem.focus) {
setTimeout(function () {
elem.focus();
}, 30);
}
}
</script>
<TelerikButton OnClick="@FocusBtn">Focus the other button</TelerikButton>
<br /><br />
<TelerikButton Id="@btnId" OnClick="@SpecialBtnAction">I will be focused programmatically</TelerikButton>
@code{
string btnId = "my-special-btn";
async Task FocusBtn()
{
await _js.InvokeVoidAsync("focusElement", $"#{btnId}");
}
async Task SpecialBtnAction()
{
Console.WriteLine("special button clicked");
}
}
---