Describe the bug
A InputRadioGroup will always re-render all children and does not support state preservation at all.
The consequence of this, is that focus within a InputRadioGroup will not be retained.
To Reproduce
https://gist.github.com/Sebazzz/dc33bb9e984774407aa9b9644f356cca
Run and click the button. You will see that the child components of the radio button will show a new date, indicating a re-render happens.
Further technical details
- ASP.NET Core version: 5.0
This happens because in OnParametersSet always a new _context is initialized:
|
protected override void OnParametersSet() |
|
{ |
|
var groupName = !string.IsNullOrEmpty(Name) ? Name : _defaultGroupName; |
|
var fieldClass = EditContext.FieldCssClass(FieldIdentifier); |
|
var changeEventCallback = EventCallback.Factory.CreateBinder<string?>(this, __value => CurrentValueAsString = __value, CurrentValueAsString); |
|
|
|
_context = new InputRadioContext(CascadedContext, groupName, CurrentValue, fieldClass, changeEventCallback); |
|
} |
In the rendering procedure, this is used as a key value. Always being a new instance, the children will always be deleted and new children created:
|
protected override void BuildRenderTree(RenderTreeBuilder builder) |
|
{ |
|
Debug.Assert(_context != null); |
|
|
|
builder.OpenComponent<CascadingValue<InputRadioContext>>(0); |
|
builder.SetKey(_context); |
Possible fix
_context should be initialized once in the OnInitialized or only be changed if the groupname or other parameters change.
Describe the bug
A
InputRadioGroupwill always re-render all children and does not support state preservation at all.The consequence of this, is that focus within a InputRadioGroup will not be retained.
To Reproduce
https://gist.github.com/Sebazzz/dc33bb9e984774407aa9b9644f356cca
Run and click the button. You will see that the child components of the radio button will show a new date, indicating a re-render happens.
Further technical details
This happens because in
OnParametersSetalways a new_contextis initialized:aspnetcore/src/Components/Web/src/Forms/InputRadioGroup.cs
Lines 32 to 39 in 368fa1d
In the rendering procedure, this is used as a key value. Always being a new instance, the children will always be deleted and new children created:
aspnetcore/src/Components/Web/src/Forms/InputRadioGroup.cs
Lines 42 to 47 in 368fa1d
Possible fix
_contextshould be initialized once in theOnInitializedor only be changed if the groupname or other parameters change.