Skip to content

Commit d18c380

Browse files
fix: focus and ordering in column filter popup
1 parent 9ad9ca0 commit d18c380

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎Components/FancyColumnFilterPopup.razor‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
aria-haspopup="dialog"
1111
aria-expanded="@(isOpen ? "true" : "false")"
1212
@ref="anchorElement"
13-
@onclick="OpenAsync">
13+
@onclick="Open">
1414
<i class="bi @(ActiveFilter is null ? "bi-funnel" : "bi-funnel-fill")" aria-hidden="true"></i>
1515
</button>
1616

@@ -61,6 +61,7 @@
6161
private FancyColumnFilterOperator draftOperator = FancyColumnFilterOperator.Contains;
6262
private string draftValue = string.Empty;
6363
private bool isOpen;
64+
private bool showPending;
6465

6566
[Parameter, EditorRequired]
6667
public string ColumnKey { get; set; } = string.Empty;
@@ -87,12 +88,22 @@
8788

8889
private bool CanApply => Draft.IsEffective();
8990

90-
private async Task OpenAsync()
91+
private void Open()
9192
{
9293
draftOperator = ActiveFilter?.Operator ?? FancyColumnFilterOperator.Contains;
9394
draftValue = ActiveFilter?.Value ?? string.Empty;
9495
isOpen = true;
96+
showPending = true;
97+
}
98+
99+
protected override async Task OnAfterRenderAsync(bool firstRender)
100+
{
101+
if (!showPending)
102+
{
103+
return;
104+
}
95105

106+
showPending = false;
96107
module ??= await JS.InvokeAsync<IJSObjectReference>("import", "./_content/SoPro.FancyTable/fancy-table-filter.js");
97108
dotNetReference ??= DotNetObjectReference.Create(this);
98109
await module.InvokeVoidAsync("show", anchorElement, popupElement, dotNetReference);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,21 @@ public void Open_CallsJsShow()
163163

164164
module.VerifyInvoke("show");
165165
}
166+
167+
[Fact]
168+
public async Task Reopen_ShowsAfterRenderWithResetDraft()
169+
{
170+
BunitJSModuleInterop module = JSInterop.SetupModule(ModulePath);
171+
module.Mode = JSRuntimeMode.Loose;
172+
IRenderedComponent<FancyColumnFilterPopup> cut = RenderPopup(new FancyColumnFilter("name", FancyColumnFilterOperator.Equals, "Anna"));
173+
174+
Open(cut);
175+
cut.Find("select").Change("IsEmpty");
176+
Assert.Empty(cut.FindAll("input"));
177+
await cut.InvokeAsync(() => cut.Instance.OnPopoverClosed());
178+
Open(cut);
179+
180+
Assert.Single(cut.FindAll("input"));
181+
Assert.Equal(2, module.Invocations["show"].Count);
182+
}
166183
}

‎wwwroot/fancy-table-filter.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ export function show(anchor, popup, dotNet) {
1212
popup.addEventListener("toggle", event => {
1313
if (event.newState !== "closed") return;
1414
dispose(popup);
15-
anchor.focus();
15+
const active = document.activeElement;
16+
if (!active || active === document.body || popup.contains(active)) {
17+
anchor.focus({ preventScroll: true });
18+
}
1619
dotNet?.invokeMethodAsync("OnPopoverClosed").catch(() => { });
1720
}, { signal });
1821

@@ -40,7 +43,7 @@ export function show(anchor, popup, dotNet) {
4043
popup.style.top = `${rect.bottom + 4}px`;
4144
popup.style.left = `${left}px`;
4245

43-
popup.querySelector("input, select")?.focus();
46+
(popup.querySelector("input") ?? popup.querySelector("select"))?.focus();
4447
}
4548

4649
export function hide(popup) {

0 commit comments

Comments
 (0)