Skip to content

Commit

Permalink
MudElement: Fix ElementReferenceCapture (MudBlazor#9547)
Browse files Browse the repository at this point in the history
Co-authored-by: Artyom M. <wordkap@gmail.com>
  • Loading branch information
danielchalmers and ScarletKuro committed Aug 2, 2024
1 parent 4b61df0 commit 42be9f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@namespace MudBlazor.UnitTests.TestComponents

@* https://github.com/MudBlazor/MudBlazor/issues/9544 *@

<MudButton id="element-button"
Variant="@Variant.Filled"
Color="@Color.Primary"
OnClick="@ButtonOnClick"
tabindex="@_tabindex">
Error
</MudButton>

@code {
private string _tabindex = "-1";

void ButtonOnClick()
{
_tabindex = null;
}
}
7 changes: 7 additions & 0 deletions src/MudBlazor.UnitTests/Components/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElementReferenceExceptionTest>();
comp.Find("#element-button").Click();
}
}
}
11 changes: 1 addition & 10 deletions src/MudBlazor/Components/Element/MudElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 42be9f4

Please sign in to comment.