Skip to content
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

Add QuickGrid RowClass parameter #59901

Merged
merged 4 commits into from
Jan 16, 2025
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
@@ -1 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.get -> System.Func<TGridItem, string?>?
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.set -> void
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@

private void RenderRow(RenderTreeBuilder __builder, int rowIndex, TGridItem item)
{
<tr @key="@(ItemKey(item))" aria-rowindex="@rowIndex">
var rowClass = RowClass?.Invoke(item);
<tr @key="@(ItemKey(item))" aria-rowindex="@rowIndex" class="@rowClass">
@foreach (var col in _columns)
{
<td class="@ColumnClass(col)" @key="@col">@{ col.CellContent(__builder, item); }</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public partial class QuickGrid<TGridItem> : IAsyncDisposable
/// </summary>
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

/// <summary>
/// Optional. A callback to be invoked for each rendered row to specify a CSS class.
/// </summary>
[Parameter] public Func<TGridItem, string?>? RowClass { get; set; }

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

Expand Down
27 changes: 27 additions & 0 deletions src/Components/test/E2ETest/Tests/QuickGridTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,31 @@ public void AdditionalAttributesApplied()
Assert.Equal("somevalue", grid.GetDomAttribute("custom-attrib"));
Assert.Contains("custom-class-attrib", grid.GetDomAttribute("class")?.Split(" "));
}

[Fact]
public void RowClassApplied()
{
var grid = app.FindElement(By.CssSelector("#grid > table"));
var rows = grid.FindElements(By.CssSelector("tbody > tr"));

bool isJulieRowFound = false;
foreach (var row in rows)
{
var firstName = row.FindElement(By.CssSelector("td:nth-child(2)")).Text;
if (firstName == "Julie")
{
isJulieRowFound = true;
Assert.Equal("highlight", row.GetDomAttribute("class"));
}
else
{
Assert.Null(row.GetDomAttribute("class"));
}
}
danroth27 marked this conversation as resolved.
Show resolved Hide resolved

if (!isJulieRowFound)
{
Assert.Fail("No row found for Julie to highlight.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<h3>Sample QuickGrid Component</h3>

<div id="grid">
<QuickGrid Items="@FilteredPeople" Pagination="@pagination" custom-attrib="somevalue" class="custom-class-attrib">
<QuickGrid Items="@FilteredPeople" Pagination="@pagination" RowClass="HighlightJulie" custom-attrib="somevalue" class="custom-class-attrib">
<PropertyColumn Property="@(p => p.PersonId)" Sortable="true" />
<PropertyColumn Property="@(p => p.firstName)" Sortable="true">
<ColumnOptions>
<div class="search-box">
<input type="search" autofocus @bind="firstNameFilter" @bind:event="oninput" placeholder="First name..." />
</div>
</ColumnOptions>
</PropertyColumn>
<ColumnOptions>
<div class="search-box">
<input type="search" autofocus @bind="firstNameFilter" @bind:event="oninput" placeholder="First name..." />
</div>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Property="@(p => p.lastName)" Sortable="true" />
<PropertyColumn Property="@(p => p.BirthDate)" Format="yyyy-MM-dd" Sortable="true" />
<PropertyColumn Title="Age in years" Property="@(p => ComputeAge(p.BirthDate))" Sortable="true"/>
Expand All @@ -27,6 +27,8 @@
int ComputeAge(DateOnly birthDate)
=> DateTime.Now.Year - birthDate.Year - (birthDate.DayOfYear < DateTime.Now.DayOfYear ? 0 : 1);

string HighlightJulie(Person person) => person.firstName == "Julie" ? "highlight" : null;

IQueryable<Person> FilteredPeople
{
get
Expand Down
Loading