RadzenProfileMenuItem Template property added

Fix #1754
This commit is contained in:
Vladimir Enchev
2025-10-23 12:45:26 +03:00
parent e6baaa6184
commit efd3d18484
3 changed files with 144 additions and 3 deletions

View File

@@ -0,0 +1,120 @@
using Bunit;
using Microsoft.AspNetCore.Components;
using Xunit;
namespace Radzen.Blazor.Tests
{
public class ProfileMenuItemTests
{
[Fact]
public void ProfileMenuItem_Renders_TextParameter()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenProfileMenu>(parameters =>
{
parameters.Add(p => p.ChildContent, builder =>
{
builder.OpenComponent<RadzenProfileMenuItem>(0);
builder.AddAttribute(1, "Text", "Profile");
builder.CloseComponent();
});
});
Assert.Contains("Profile", component.Markup);
}
[Fact]
public void ProfileMenuItem_Renders_IconParameter()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenProfileMenu>(parameters =>
{
parameters.Add(p => p.ChildContent, builder =>
{
builder.OpenComponent<RadzenProfileMenuItem>(0);
builder.AddAttribute(1, "Icon", "account_circle");
builder.AddAttribute(2, "Text", "Profile");
builder.CloseComponent();
});
});
Assert.Contains("account_circle", component.Markup);
}
[Fact]
public void ProfileMenuItem_Template_OverridesText()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenProfileMenu>(parameters =>
{
parameters.Add(p => p.ChildContent, builder =>
{
builder.OpenComponent<RadzenProfileMenuItem>(0);
builder.AddAttribute(1, "Text", "This should not appear");
builder.AddAttribute(2, "Template", (RenderFragment)((templateBuilder) =>
{
templateBuilder.OpenElement(0, "span");
templateBuilder.AddAttribute(1, "class", "template-content");
templateBuilder.AddContent(2, "Template Content");
templateBuilder.CloseElement();
}));
builder.CloseComponent();
});
});
// Template should be rendered
Assert.Contains("template-content", component.Markup);
// Text should not be rendered in navigation-item-text span when Template is present
Assert.DoesNotContain("<span class=\"rz-navigation-item-text\">This should not appear</span>", component.Markup);
}
[Fact]
public void ProfileMenuItem_Renders_TemplateWithSwitch()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenProfileMenu>(parameters =>
{
parameters.Add(p => p.ChildContent, builder =>
{
builder.OpenComponent<RadzenProfileMenuItem>(0);
builder.AddAttribute(1, "Icon", "settings");
builder.AddAttribute(2, "Template", (RenderFragment)((templateBuilder) =>
{
templateBuilder.OpenComponent<RadzenSwitch>(0);
templateBuilder.CloseComponent();
}));
builder.CloseComponent();
});
});
// Icon should still be rendered
Assert.Contains("settings", component.Markup);
// Switch should be rendered from template
Assert.Contains("rz-switch", component.Markup);
}
[Fact]
public void ProfileMenuItem_Renders_PathParameter()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenProfileMenu>(parameters =>
{
parameters.Add(p => p.ChildContent, builder =>
{
builder.OpenComponent<RadzenProfileMenuItem>(0);
builder.AddAttribute(1, "Text", "Settings");
builder.AddAttribute(2, "Path", "/settings");
builder.CloseComponent();
});
});
Assert.Contains("href=\"/settings\"", component.Markup);
}
}
}

View File

@@ -15,7 +15,14 @@
{
<img class="notranslate rzi rz-navigation-item-icon" src="@Image" alt=@ImageAlternateText />
}
<span class="rz-navigation-item-text">@Text</span>
@if (Template != null)
{
@Template
}
else
{
<span class="rz-navigation-item-text">@Text</span>
}
</NavLink>
}
else
@@ -29,7 +36,14 @@
{
<img class="notranslate rzi rz-navigation-item-icon" src="@Image" alt=@ImageAlternateText />
}
<span class="rz-navigation-item-text">@Text</span>
@if (Template != null)
{
@Template
}
else
{
<span class="rz-navigation-item-text">@Text</span>
}
</div>
}
</div>

View File

@@ -79,6 +79,13 @@ namespace Radzen.Blazor
[Parameter]
public string Value { get; set; }
/// <summary>
/// Gets or sets the template.
/// </summary>
/// <value>The template.</value>
[Parameter]
public RenderFragment Template { get; set; }
/// <summary>
/// Gets or sets the menu.
/// </summary>
@@ -124,7 +131,7 @@ namespace Radzen.Blazor
internal string GetItemCssClass()
{
return $"{GetCssClass()} {(Parent.IsFocused(this) ? "rz-state-focused" : "")}".Trim();
return $"{GetCssClass()} {(Parent?.IsFocused(this) == true ? "rz-state-focused" : "")}".Trim();
}
}
}