Skip to content

Fix: InputRadioGroup not registering initial click in some circumstances#39208

Closed
MariovanZeist wants to merge 2 commits into
dotnet:mainfrom
MariovanZeist:mvz/InputRadioGroup-preventRecreate
Closed

Fix: InputRadioGroup not registering initial click in some circumstances#39208
MariovanZeist wants to merge 2 commits into
dotnet:mainfrom
MariovanZeist:mvz/InputRadioGroup-preventRecreate

Conversation

@MariovanZeist
Copy link
Copy Markdown
Contributor

Fixes InputRadioGroup not registering initial click

I have created a simple repo to show the issue: https://github.com/MariovanZeist/InputRadioGroupDoubleClick

The problem was the use of an InputContext as a key for the renderer.
As this Context was recreated every time the InputRadioGroup was rendered, all its Content was also recreated.

When you have an InputText for example, and you would edit the text and without pressing enter or tab out of the input would click on a InputRadio element, first the OnChange of the InputText would fire, forcing a rerender of the InputRadioGroup which in turn would rebuild its content, and would lose the click event (As the HTML that was clicked on is now gone)

Description

The solution was to change the way InputContext was created, but also honor #24762 where the issue was introduced.

The biggest hurdle was to fix the Issue with no breaking changes,
Most of the changes are to internal classes so won't affect other developers.
The only change that could be considered breaking is the addition of the IDisposable interface to a InputRadio component, but I could not find a solution without it.

2 e2e tests have been added

Fixes #31610

Fixes: #39145 (comment)

@MariovanZeist MariovanZeist requested a review from a team as a code owner December 28, 2021 00:13
@ghost ghost added area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member labels Dec 28, 2021
@MariovanZeist
Copy link
Copy Markdown
Contributor Author

The main challenge with this PR was the fact that if you use a primitive value for TValue Due to how blazor detects changes in ChangeDetection

private static bool IsKnownImmutableType(Type type)
=> type.IsPrimitive
|| type == typeof(string)
|| type == typeof(DateTime)
|| type == typeof(Type)
|| type == typeof(decimal)
|| type == typeof(Guid);

An InputRadio component would never rerender as its parameters would not change (When using an enum as a TValue, the InputRadio would rerender), creating an issue that #24762 was created to fix.

The solution here was to maintain a list of actions to call when an InputRadioGroup was rerendered, so it could also rerender its children.

@mkArtakMSFT
Copy link
Copy Markdown
Contributor

Thanks for your PR, @MariovanZeist.
@SteveSandersonMS could you please review this when you're back? Thanks!

@SteveSandersonMS
Copy link
Copy Markdown
Member

Just a note for anyone reviewing this: we should compare the underlying issue against the one reported at #39145. If it's the same underlying problem, we might want to look for a non-radiogroup-specific fix.

@MariovanZeist
Copy link
Copy Markdown
Contributor Author

@SteveSandersonMS The issue #39145 has 2 different (Nonrelated issues)
That's why I am referring to the comment in that Issue which states a different issue than the one in the Title, #39145 (comment)
Which describes the problem I am fixing here.
Sorry for the confusion.

@MariovanZeist
Copy link
Copy Markdown
Contributor Author

Fixed in @40148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InputRadioGroup not registering initial click in some circumstances InputRadioGroup does not support state preservation for child content

3 participants