Skip to content

Commit b4f17f2

Browse files
fix: toggle column filter popup from its funnel button
1 parent 36b9a4e commit b4f17f2

3 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎Components/FancyColumnFilterPopup.razor‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
aria-label="@FormatColumnLabel(Texts.FilterColumnFormat, ColumnTitle)"
1010
aria-haspopup="dialog"
1111
aria-expanded="@(isOpen ? "true" : "false")"
12+
popovertarget="@popupId"
1213
@ref="anchorElement"
1314
@onclick="Open">
1415
<i class="bi @(ActiveFilter is null ? "bi-funnel" : "bi-funnel-fill")" aria-hidden="true"></i>
@@ -90,6 +91,13 @@
9091

9192
private void Open()
9293
{
94+
// The button is a native popover invoker: while open, this click is closed by the browser
95+
// (reported back through OnPopoverClosed), so the open path must not run again.
96+
if (isOpen)
97+
{
98+
return;
99+
}
100+
93101
draftOperator = ActiveFilter?.Operator ?? FancyColumnFilterOperator.Contains;
94102
draftValue = ActiveFilter?.Value ?? string.Empty;
95103
isOpen = true;

‎tests/SoPro.FancyTable.Tests/FancyColumnFilterPopupTests.cs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,44 @@ public async Task Reopen_ShowsAfterRenderWithResetDraft()
180180
Assert.Single(cut.FindAll("input"));
181181
Assert.Equal(2, module.Invocations["show"].Count);
182182
}
183+
184+
[Fact]
185+
public void Button_IsNativeInvokerForPopover()
186+
{
187+
IRenderedComponent<FancyColumnFilterPopup> cut = RenderPopup();
188+
189+
string target = cut.Find("button.fancy-filter-button").GetAttribute("popovertarget")!;
190+
191+
Assert.False(string.IsNullOrEmpty(target));
192+
Assert.Equal(target, cut.Find("div[popover]").GetAttribute("id"));
193+
}
194+
195+
[Fact]
196+
public void ClickWhileOpen_DoesNotShowAgainNorResetDraft()
197+
{
198+
BunitJSModuleInterop module = JSInterop.SetupModule(ModulePath);
199+
module.Mode = JSRuntimeMode.Loose;
200+
IRenderedComponent<FancyColumnFilterPopup> cut = RenderPopup(new FancyColumnFilter("name", FancyColumnFilterOperator.Equals, "Anna"));
201+
202+
Open(cut);
203+
cut.Find("input").Input("xyz");
204+
Open(cut);
205+
206+
Assert.Single(module.Invocations["show"]);
207+
Assert.Equal("true", cut.Find("button.fancy-filter-button").GetAttribute("aria-expanded"));
208+
cut.Find("select").Change("Equals");
209+
Assert.Equal("xyz", cut.Find("input").GetAttribute("value"));
210+
}
211+
212+
[Fact]
213+
public async Task OnPopoverClosed_ResetsAriaExpanded()
214+
{
215+
IRenderedComponent<FancyColumnFilterPopup> cut = RenderPopup();
216+
217+
Open(cut);
218+
Assert.Equal("true", cut.Find("button.fancy-filter-button").GetAttribute("aria-expanded"));
219+
await cut.InvokeAsync(() => cut.Instance.OnPopoverClosed());
220+
221+
Assert.Equal("false", cut.Find("button.fancy-filter-button").GetAttribute("aria-expanded"));
222+
}
183223
}

‎wwwroot/fancy-table-filter.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const instances = new WeakMap();
22

3-
export function show(anchor, popup, dotNet) {
3+
export async function show(anchor, popup, dotNet) {
44
if (!anchor || !popup) return;
55

6+
// The anchor's popovertarget opens the popover natively when its click finishes. Wait one task so
7+
// that activation has run; otherwise showPopover() here would be undone by the native toggle.
8+
await new Promise(resolve => setTimeout(resolve));
9+
610
dispose(popup);
711

812
const abort = new AbortController();
@@ -26,10 +30,12 @@ export function show(anchor, popup, dotNet) {
2630
close();
2731
}, { signal, capture: true });
2832

29-
try {
30-
popup.showPopover();
31-
} catch (error) {
32-
if (error?.name !== "InvalidStateError") throw error;
33+
if (!popup.matches(":popover-open")) {
34+
try {
35+
popup.showPopover();
36+
} catch (error) {
37+
if (error?.name !== "InvalidStateError") throw error;
38+
}
3339
}
3440

3541
const rect = anchor.getBoundingClientRect();

0 commit comments

Comments
 (0)