Skip to content

Commit 0053ba7

Browse files
Pass through arbitrary attributes to QuickGrid (#50051)
Co-authored-by: James Yeung <[email protected]>
1 parent 81f23df commit 0053ba7

File tree

8 files changed

+26
-4
lines changed

8 files changed

+26
-4
lines changed

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/Microsoft.AspNetCore.Components.QuickGrid.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<!-- Bundle the theme CSS files as if they were scoped, even though they aren't -->
1616
<ThemeCssFiles Include="Themes\*.css" />
1717
<_CurrentProjectDiscoveredScopedCssFiles Include="@(ThemeCssFiles)" RelativePath="%(Identity)" BasePath="_content/$(AssemblyName)" />
18+
19+
<Compile Include="$(ComponentsSharedSourceRoot)src\AttributeUtilities.cs" LinkBase="Infrastructure" />
1820
</ItemGroup>
1921

2022
<ItemGroup>

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Theme.get -> stri
115115
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Theme.set -> void
116116
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Virtualize.get -> bool
117117
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Virtualize.set -> void
118+
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.AdditionalAttributes.get -> System.Collections.Generic.IReadOnlyDictionary<string!, object!>?
119+
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.AdditionalAttributes.set -> void
118120
Microsoft.AspNetCore.Components.QuickGrid.SortDirection
119121
Microsoft.AspNetCore.Components.QuickGrid.SortDirection.Ascending = 1 -> Microsoft.AspNetCore.Components.QuickGrid.SortDirection
120122
Microsoft.AspNetCore.Components.QuickGrid.SortDirection.Auto = 0 -> Microsoft.AspNetCore.Components.QuickGrid.SortDirection

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@{ FinishCollectingColumns(); }
1111
<ColumnsCollectedNotifier TGridItem="TGridItem" />
1212

13-
<table class="@GridClass()" theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions">
13+
<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions" @attributes="AdditionalAttributes" class="@GridClass()">
1414
<thead>
1515
<tr>
1616
@_renderColumnHeaders
@@ -39,7 +39,7 @@
3939
private void RenderNonVirtualizedRows(RenderTreeBuilder __builder)
4040
{
4141
var initialRowIndex = 2; // aria-rowindex is 1-based, plus the first row is the header
42-
var rowIndex = initialRowIndex;
42+
var rowIndex = initialRowIndex;
4343
foreach (var item in _currentNonVirtualizedViewItems)
4444
{
4545
RenderRow(__builder, rowIndex++, item);

src/Components/QuickGrid/Microsoft.AspNetCore.Components.QuickGrid/src/QuickGrid.razor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Components.QuickGrid.Infrastructure;
66
using Microsoft.AspNetCore.Components.Web.Virtualization;
77
using Microsoft.JSInterop;
8+
using Microsoft.AspNetCore.Components.Forms;
89

910
namespace Microsoft.AspNetCore.Components.QuickGrid;
1011

@@ -90,6 +91,11 @@ public partial class QuickGrid<TGridItem> : IAsyncDisposable
9091
/// </summary>
9192
[Parameter] public PaginationState? Pagination { get; set; }
9293

94+
/// <summary>
95+
/// Gets or sets a collection of additional attributes that will be applied to the created element.
96+
/// </summary>
97+
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }
98+
9399
[Inject] private IServiceProvider Services { get; set; } = default!;
94100
[Inject] private IJSRuntime JS { get; set; } = default!;
95101

@@ -387,7 +393,10 @@ private string AriaSortValue(ColumnBase<TGridItem> column)
387393
: ColumnClass(column);
388394

389395
private string GridClass()
390-
=> $"quickgrid {Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}";
396+
{
397+
var gridClass = $"quickgrid {Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}";
398+
return AttributeUtilities.CombineClassNames(AdditionalAttributes, gridClass) ?? string.Empty;
399+
}
391400

392401
private static string? ColumnClass(ColumnBase<TGridItem> column) => column.Align switch
393402
{

src/Components/Web/src/Microsoft.AspNetCore.Components.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16+
<Compile Include="$(ComponentsSharedSourceRoot)src\AttributeUtilities.cs" LinkBase="Forms" />
1617
<Compile Include="$(ComponentsSharedSourceRoot)src\ExpressionFormatting\**\*.cs" LinkBase="Forms\ExpressionFommatting" />
1718
<Compile Include="$(ComponentsSharedSourceRoot)src\DefaultAntiforgeryStateProvider.cs" LinkBase="Forms" />
1819
</ItemGroup>

src/Components/test/E2ETest/Tests/QuickGridTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,12 @@ public void PaginatorDisplaysCorrectItemCount()
113113
Assert.Equal("1", currentPageNumber);
114114
Assert.Equal("5", totalPageNumber);
115115
}
116+
117+
[Fact]
118+
public void AdditionalAttributesApplied()
119+
{
120+
var grid = app.FindElement(By.CssSelector("#grid > table"));
121+
Assert.Equal("somevalue", grid.GetAttribute("custom-attrib"));
122+
Assert.Contains("custom-class-attrib", grid.GetAttribute("class")?.Split(" "));
123+
}
116124
}

src/Components/test/testassets/BasicTestApp/QuickGridTest/SampleQuickGridComponent.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h3>Sample QuickGrid Component</h3>
44

55
<div id="grid">
6-
<QuickGrid Items="@FilteredPeople" Pagination="@pagination">
6+
<QuickGrid Items="@FilteredPeople" Pagination="@pagination" custom-attrib="somevalue" class="custom-class-attrib">
77
<PropertyColumn Property="@(p => p.PersonId)" Sortable="true" />
88
<PropertyColumn Property="@(p => p.firstName)" Sortable="true">
99
<ColumnOptions>

0 commit comments

Comments
 (0)