Skip to content

Commit 59b1440

Browse files
jorivermAI\jvermeyl
andauthored
RadzenTocItem: take in attributes to pass on to the list item element (#2360)
Co-authored-by: AI\jvermeyl <[email protected]>
1 parent 98d6729 commit 59b1440

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

Radzen.Blazor.Tests/TocTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Bunit;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
namespace Radzen.Blazor.Tests
6+
{
7+
public class TocTests
8+
{
9+
[Fact]
10+
public void TocItem_Renders_With_Attributes()
11+
{
12+
using var ctx = new TestContext();
13+
var component = ctx.RenderComponent<RadzenTocItem>(parameters =>
14+
{
15+
parameters.Add(p => p.Attributes, new Dictionary<string, object>
16+
{
17+
{ "data-enhance-nav", "false" },
18+
{ "aria-label", "Table of Contents Item" }
19+
});
20+
});
21+
22+
Assert.Contains("data-enhance-nav=\"false\"", component.Markup);
23+
Assert.Contains("aria-label=\"Table of Contents Item\"", component.Markup);
24+
}
25+
}
26+
}
27+

Radzen.Blazor/RadzenTocItem.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<li class=@Class role="presentation">
1+
<li class=@GetCssClass() @attributes=@Attributes role="presentation">
22
<div class=@WrapperClass>
33
<a href=@Selector class=@LinkClass @onclick:preventDefault @onclick=@OnClickAsync>@if (Template is null) {@Text} else {@Template}</a>
44
</div>

Radzen.Blazor/RadzenTocItem.razor.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System;
2-
using System.Threading.Tasks;
31
using Microsoft.AspNetCore.Components;
42
using Radzen.Blazor.Rendering;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Collections.ObjectModel;
6+
using System.Threading.Tasks;
57
namespace Radzen.Blazor;
68

79
#nullable enable
@@ -11,6 +13,15 @@ namespace Radzen.Blazor;
1113
/// </summary>
1214
public partial class RadzenTocItem : ComponentBase, IAsyncDisposable
1315
{
16+
/// <summary>
17+
/// Gets or sets a dictionary of additional HTML attributes that will be applied to the component's root element.
18+
/// Any attributes not explicitly defined as parameters will be captured here and rendered on the element.
19+
/// Use this to add data-* attributes, ARIA attributes, or any custom HTML attributes.
20+
/// </summary>
21+
/// <value>The unmatched attributes dictionary.</value>
22+
[Parameter(CaptureUnmatchedValues = true)]
23+
public IReadOnlyDictionary<string, object> Attributes { get; set; } = new ReadOnlyDictionary<string, object>(new Dictionary<string, object>());
24+
1425
/// <summary>
1526
/// Gets or sets the child content.
1627
/// </summary>
@@ -43,10 +54,26 @@ public partial class RadzenTocItem : ComponentBase, IAsyncDisposable
4354

4455
private bool selected;
4556

46-
private string Class => ClassList.Create("rz-toc-item")
57+
/// <summary>
58+
/// Gets the final CSS class rendered by the component. Combines it with a <c>class</c> custom attribute.
59+
/// </summary>
60+
protected string GetCssClass()
61+
{
62+
if (Attributes != null && Attributes.TryGetValue("class", out var @class) && !string.IsNullOrEmpty(Convert.ToString(@class)))
63+
{
64+
return $"{GetComponentCssClass()} {@class}";
65+
}
66+
67+
return GetComponentCssClass();
68+
}
69+
70+
/// <summary>
71+
/// Gets the component CSS class.
72+
/// </summary>
73+
protected string GetComponentCssClass() => ClassList.Create("rz-toc-item")
4774
.Add("rz-toc-item-selected", selected)
4875
.ToString();
49-
76+
5077
private string WrapperClass => ClassList.Create("rz-toc-item-wrapper")
5178
.Add(Level switch
5279
{
@@ -59,7 +86,7 @@ public partial class RadzenTocItem : ComponentBase, IAsyncDisposable
5986

6087
private string LinkClass => ClassList.Create("rz-toc-link")
6188
.ToString();
62-
89+
6390
internal void Activate()
6491
{
6592
selected = true;

0 commit comments

Comments
 (0)