I want to create a components by following this steps:
- I have a list of items.
- I want to loop in this list and create a component like
InputNumber. - Add
EventCallbackto the generic createdInputNumberthat acceptrefof thisInputtextbecause I want to use thisrefto set the focus on thisInputNumber. - I have also
onbluremethod that execute some code for me, and I am using theonfocusto return focus to the input after execute this code byonblure
My question
How can I get this ref and send it as parameter of EventCallback?
The problem here that this components have been generated by loop, so I don't want to create by hand hundred variables to represent ref's.
My concept code like this:
@code{
private void OnFocus(MyInputNumber<double?> obj)
{
if (obj is not null)
{
obj!.Element.Value.FocusAsync();
}
}
}
@foreach(var MyItem in MyList)
{
<EditForm Model="MyItem">
//Some components ..
<label>
Test
<InputNumber @bind-Value="MyItem.MyVal"
@onfocus="@((InputNumber<double?> obj @*wrong*@) =>
OnFocus(obj))"
@onblur=@(() => OnblureHandler(context))
</label>
</EditForm>
}
If you see up the parameter InputNumber<double?> obj, this way is wrong, usually I use @ref=SomeVariable but becasue I created in generic way, I can not do that.
Note:
I don't to adjust my list to be dictionary<MYItemType,InputNumber<double?>>, or create a new class that has InputNumber<double?> as property. I am searching for different way, like go from editcontext to any input has been modified and reset focus on it, I don't know if that possible !