Skip to content

Pass through arbitrary attributes to QuickGrid #50051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<!-- Bundle the theme CSS files as if they were scoped, even though they aren't -->
<ThemeCssFiles Include="Themes\*.css" />
<_CurrentProjectDiscoveredScopedCssFiles Include="@(ThemeCssFiles)" RelativePath="%(Identity)" BasePath="_content/$(AssemblyName)" />

<Compile Include="$(ComponentsSharedSourceRoot)src\AttributeUtilities.cs" LinkBase="Infrastructure" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Theme.get -> stri
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Theme.set -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Virtualize.get -> bool
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.Virtualize.set -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.AdditionalAttributes.get -> System.Collections.Generic.IReadOnlyDictionary<string!, object!>?
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.AdditionalAttributes.set -> void
Microsoft.AspNetCore.Components.QuickGrid.SortDirection
Microsoft.AspNetCore.Components.QuickGrid.SortDirection.Ascending = 1 -> Microsoft.AspNetCore.Components.QuickGrid.SortDirection
Microsoft.AspNetCore.Components.QuickGrid.SortDirection.Auto = 0 -> Microsoft.AspNetCore.Components.QuickGrid.SortDirection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@{ FinishCollectingColumns(); }
<ColumnsCollectedNotifier TGridItem="TGridItem" />

<table class="@GridClass()" theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions">
<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions" @attributes="AdditionalAttributes" class="@GridClass()">
<thead>
<tr>
@_renderColumnHeaders
Expand Down Expand Up @@ -39,7 +39,7 @@
private void RenderNonVirtualizedRows(RenderTreeBuilder __builder)
{
var initialRowIndex = 2; // aria-rowindex is 1-based, plus the first row is the header
var rowIndex = initialRowIndex;
var rowIndex = initialRowIndex;
foreach (var item in _currentNonVirtualizedViewItems)
{
RenderRow(__builder, rowIndex++, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Components.QuickGrid.Infrastructure;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components.Forms;

namespace Microsoft.AspNetCore.Components.QuickGrid;

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

/// <summary>
/// Gets or sets a collection of additional attributes that will be applied to the created element.
/// </summary>
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

[Inject] private IServiceProvider Services { get; set; } = default!;
[Inject] private IJSRuntime JS { get; set; } = default!;

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

private string GridClass()
=> $"quickgrid {Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}";
{
var gridClass = $"quickgrid {Class} {(_pendingDataLoadCancellationTokenSource is null ? null : "loading")}";
return AttributeUtilities.CombineClassNames(AdditionalAttributes, gridClass) ?? string.Empty;
}

private static string? ColumnClass(ColumnBase<TGridItem> column) => column.Align switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(ComponentsSharedSourceRoot)src\AttributeUtilities.cs" LinkBase="Forms" />
<Compile Include="$(ComponentsSharedSourceRoot)src\ExpressionFormatting\**\*.cs" LinkBase="Forms\ExpressionFommatting" />
<Compile Include="$(ComponentsSharedSourceRoot)src\DefaultAntiforgeryStateProvider.cs" LinkBase="Forms" />
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/Components/test/E2ETest/Tests/QuickGridTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ public void PaginatorDisplaysCorrectItemCount()
Assert.Equal("1", currentPageNumber);
Assert.Equal("5", totalPageNumber);
}

[Fact]
public void AdditionalAttributesApplied()
{
var grid = app.FindElement(By.CssSelector("#grid > table"));
Assert.Equal("somevalue", grid.GetAttribute("custom-attrib"));
Assert.Contains("custom-class-attrib", grid.GetAttribute("class")?.Split(" "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h3>Sample QuickGrid Component</h3>

<div id="grid">
<QuickGrid Items="@FilteredPeople" Pagination="@pagination">
<QuickGrid Items="@FilteredPeople" Pagination="@pagination" custom-attrib="somevalue" class="custom-class-attrib">
<PropertyColumn Property="@(p => p.PersonId)" Sortable="true" />
<PropertyColumn Property="@(p => p.firstName)" Sortable="true">
<ColumnOptions>
Expand Down