diff --git a/src/MudBlazor.UnitTests.Viewer/TestComponents/Element/ElementReferenceExceptionTest.razor b/src/MudBlazor.UnitTests.Viewer/TestComponents/Element/ElementReferenceExceptionTest.razor new file mode 100644 index 000000000000..1109e571568b --- /dev/null +++ b/src/MudBlazor.UnitTests.Viewer/TestComponents/Element/ElementReferenceExceptionTest.razor @@ -0,0 +1,20 @@ +@namespace MudBlazor.UnitTests.TestComponents + +@* https://github.com/MudBlazor/MudBlazor/issues/9544 *@ + + + Error + + +@code { + private string _tabindex = "-1"; + + void ButtonOnClick() + { + _tabindex = null; + } +} diff --git a/src/MudBlazor.UnitTests/Components/ElementTests.cs b/src/MudBlazor.UnitTests/Components/ElementTests.cs index a89ef58dc066..85e0a78c767b 100644 --- a/src/MudBlazor.UnitTests/Components/ElementTests.cs +++ b/src/MudBlazor.UnitTests/Components/ElementTests.cs @@ -48,5 +48,12 @@ public void MudElement_Should_Not_Attach_A_Null_Event() comp2.Find("span").MouseOver(); comp2.Instance.WasHovered.Should().BeTrue(); } + + [Test] + public void ElementReferenceCapture() + { + var comp = Context.RenderComponent(); + comp.Find("#element-button").Click(); + } } } diff --git a/src/MudBlazor/Components/Element/MudElement.cs b/src/MudBlazor/Components/Element/MudElement.cs index c19231760389..30905efc69b4 100644 --- a/src/MudBlazor/Components/Element/MudElement.cs +++ b/src/MudBlazor/Components/Element/MudElement.cs @@ -75,16 +75,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder) builder.OpenElement(seq++, HtmlTag); // Splatted attributes. - foreach (var attribute in UserAttributes) - { - // Check if the attribute value is not null before adding it to the builder. - // This avoids adding null event handlers, such as `@onmouseenter=@(Open ? HandleEnter : null)`. - // This is useful because Blazor always adds the attribute value and creates an EventCallback in normal HTML elements. - if (attribute.Value is not null) - { - builder.AddAttribute(seq++, attribute.Key, attribute.Value); - } - } + builder.AddMultipleAttributes(seq++, UserAttributes!); // Add class and style attributes. builder.AddAttribute(seq++, "class", Class);