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 public method to QuickGrid to close column options UI #57904

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.OverscanCount.get -> int
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.OverscanCount.set -> void
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.CloseColumnOptionsAsync() -> System.Threading.Tasks.Task!
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@{ FinishCollectingColumns(); }
<ColumnsCollectedNotifier TGridItem="TGridItem" />

<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions" @attributes="AdditionalAttributes" class="@GridClass()">
<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptionsAsync" @attributes="AdditionalAttributes" class="@GridClass()">
<thead>
<tr>
@_renderColumnHeaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ public Task ShowColumnOptionsAsync(ColumnBase<TGridItem> column)
return Task.CompletedTask;
}

/// <summary>
/// Closes the <see cref="ColumnBase{TGridItem}.ColumnOptions"/> UI that was previously displayed.
/// </summary>
public Task CloseColumnOptionsAsync()
{
_displayOptionsForColumn = null;
StateHasChanged();
return Task.CompletedTask;
}

/// <summary>
/// Instructs the grid to re-fetch and render the current data from the supplied data source
/// (either <see cref="Items"/> or <see cref="ItemsProvider"/>).
Expand Down Expand Up @@ -440,9 +450,4 @@ public async ValueTask DisposeAsync()
// the client disconnected. This is not an error.
}
}

private void CloseColumnOptions()
{
_displayOptionsForColumn = null;
}
}
42 changes: 42 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,46 @@ public void AdditionalAttributesApplied()
Assert.Equal("somevalue", grid.GetAttribute("custom-attrib"));
Assert.Contains("custom-class-attrib", grid.GetAttribute("class")?.Split(" "));
}

[Fact]
public void CanOpenColumnOptions()
{
var grid = app.FindElement(By.CssSelector("#grid > table"));
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));

firstNameColumnOptionsButton.Click();

var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
Browser.Exists(By.CssSelector(firstNameSearchSelector));
}

[Fact]
public void CanCloseColumnOptionsByBlurring()
{
var grid = app.FindElement(By.CssSelector("#grid > table"));
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));

firstNameColumnOptionsButton.Click();

// Click outside the column options to close
grid.Click();

var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
Browser.DoesNotExist(By.CssSelector(firstNameSearchSelector));
}

[Fact]
public void CanCloseColumnOptionsByCloseColumnOptionsAsync()
{
var grid = app.FindElement(By.CssSelector("#grid > table"));
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));

firstNameColumnOptionsButton.Click();

// Click the button inside the column options popup to close, which calls QuickGrid.CloseColumnOptionsAsync
grid.FindElement(By.CssSelector("#close-column-options")).Click();

var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
Browser.DoesNotExist(By.CssSelector(firstNameSearchSelector));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
<h3>Sample QuickGrid Component</h3>

<div id="grid">
<QuickGrid Items="@FilteredPeople" Pagination="@pagination" custom-attrib="somevalue" class="custom-class-attrib">
<QuickGrid @ref="@quickGridRef" 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>
<div class="search-box">
<input type="search" autofocus @bind="firstNameFilter" @bind:event="oninput" placeholder="First name..." />
</div>
<button type="button" id="close-column-options" @onclick="@(() => quickGridRef.CloseColumnOptionsAsync())">Close Column Options</button>
</ColumnOptions>
</PropertyColumn>
<PropertyColumn Property="@(p => p.lastName)" Sortable="true" />
Expand All @@ -23,6 +24,7 @@
record Person(int PersonId, string firstName, string lastName, DateOnly BirthDate);
PaginationState pagination = new PaginationState { ItemsPerPage = 10 };
string firstNameFilter;
QuickGrid<Person> quickGridRef;

int ComputeAge(DateOnly birthDate)
=> DateTime.Now.Year - birthDate.Year - (birthDate.DayOfYear < DateTime.Now.DayOfYear ? 0 : 1);
Expand Down
Loading