Skip to content

InputRadioGroup does not support state preservation for child content #31610

@Sebazzz

Description

@Sebazzz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DoneThis issue has been fixedarea-blazorIncludes: Blazor, Razor Componentsfeature-blazor-builtin-componentsFeatures related to the built in components we ship or could ship in the futureinvestigate

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions