Skip to content

Commit 6d7f53c

Browse files
matthetheringtoncaptainsafia
authored andcommitted
[Blazor] Add public method to QuickGrid to close column options UI (#57904)
* Adds a public method to QuickGrid to close the column options UI
1 parent 96ac227 commit 6d7f53c

File tree

5 files changed

+58
-8
lines changed

5 files changed

+58
-8
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#nullable enable
2+
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.CloseColumnOptionsAsync() -> System.Threading.Tasks.Task!
23
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.get -> System.Func<TGridItem, string?>?
3-
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.set -> void
4+
Microsoft.AspNetCore.Components.QuickGrid.QuickGrid<TGridItem>.RowClass.set -> void

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

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

13-
<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptions" @attributes="AdditionalAttributes" class="@GridClass()">
13+
<table theme="@Theme" aria-rowcount="@(_ariaBodyRowCount + 1)" @ref="_tableReference" @onclosecolumnoptions="CloseColumnOptionsAsync" @attributes="AdditionalAttributes" class="@GridClass()">
1414
<thead>
1515
<tr>
1616
@_renderColumnHeaders

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,16 @@ public Task ShowColumnOptionsAsync(ColumnBase<TGridItem> column)
277277
return Task.CompletedTask;
278278
}
279279

280+
/// <summary>
281+
/// Closes the <see cref="ColumnBase{TGridItem}.ColumnOptions"/> UI that was previously displayed.
282+
/// </summary>
283+
public Task CloseColumnOptionsAsync()
284+
{
285+
_displayOptionsForColumn = null;
286+
StateHasChanged();
287+
return Task.CompletedTask;
288+
}
289+
280290
/// <summary>
281291
/// Instructs the grid to re-fetch and render the current data from the supplied data source
282292
/// (either <see cref="Items"/> or <see cref="ItemsProvider"/>).
@@ -445,9 +455,4 @@ public async ValueTask DisposeAsync()
445455
// the client disconnected. This is not an error.
446456
}
447457
}
448-
449-
private void CloseColumnOptions()
450-
{
451-
_displayOptionsForColumn = null;
452-
}
453458
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,46 @@ public void RowClassApplied()
148148
Assert.Fail("No row found for Julie to highlight.");
149149
}
150150
}
151+
152+
[Fact]
153+
public void CanOpenColumnOptions()
154+
{
155+
var grid = app.FindElement(By.CssSelector("#grid > table"));
156+
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));
157+
158+
firstNameColumnOptionsButton.Click();
159+
160+
var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
161+
Browser.Exists(By.CssSelector(firstNameSearchSelector));
162+
}
163+
164+
[Fact]
165+
public void CanCloseColumnOptionsByBlurring()
166+
{
167+
var grid = app.FindElement(By.CssSelector("#grid > table"));
168+
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));
169+
170+
firstNameColumnOptionsButton.Click();
171+
172+
// Click outside the column options to close
173+
grid.Click();
174+
175+
var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
176+
Browser.DoesNotExist(By.CssSelector(firstNameSearchSelector));
177+
}
178+
179+
[Fact]
180+
public void CanCloseColumnOptionsByCloseColumnOptionsAsync()
181+
{
182+
var grid = app.FindElement(By.CssSelector("#grid > table"));
183+
var firstNameColumnOptionsButton = grid.FindElement(By.CssSelector("thead > tr > th:nth-child(2) > div > button[title=\"Column options\"]"));
184+
185+
firstNameColumnOptionsButton.Click();
186+
187+
// Click the button inside the column options popup to close, which calls QuickGrid.CloseColumnOptionsAsync
188+
grid.FindElement(By.CssSelector("#close-column-options")).Click();
189+
190+
var firstNameSearchSelector = "#grid > table > thead > tr > th:nth-child(2) input[type=search]";
191+
Browser.DoesNotExist(By.CssSelector(firstNameSearchSelector));
192+
}
151193
}

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

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

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

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

0 commit comments

Comments
 (0)