Skip to content

Commit 9ad9ca0

Browse files
feat: add column filter popup component
1 parent 4de6c3a commit 9ad9ca0

8 files changed

Lines changed: 531 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
@implements IAsyncDisposable
2+
@using System.Globalization
3+
@using Microsoft.AspNetCore.Components.Web
4+
@using Microsoft.JSInterop
5+
@inject IJSRuntime JS
6+
7+
<button type="button"
8+
class="btn btn-link p-0 border-0 fancy-filter-button"
9+
aria-label="@FormatColumnLabel(Texts.FilterColumnFormat, ColumnTitle)"
10+
aria-haspopup="dialog"
11+
aria-expanded="@(isOpen ? "true" : "false")"
12+
@ref="anchorElement"
13+
@onclick="OpenAsync">
14+
<i class="bi @(ActiveFilter is null ? "bi-funnel" : "bi-funnel-fill")" aria-hidden="true"></i>
15+
</button>
16+
17+
<div popover="auto" id="@popupId" class="fancy-filter-popup" @ref="popupElement">
18+
<div class="d-flex flex-column gap-2 p-2">
19+
<select class="form-select form-select-sm" @bind="draftOperator">
20+
@foreach (FancyColumnFilterOperator op in Operators)
21+
{
22+
<option value="@op.ToString()">@GetOperatorLabel(op)</option>
23+
}
24+
</select>
25+
26+
@if (draftOperator.RequiresValue())
27+
{
28+
<input type="text"
29+
class="form-control form-control-sm"
30+
placeholder="@Texts.FilterValuePlaceholder"
31+
value="@draftValue"
32+
@oninput="OnValueInput"
33+
@onkeydown="OnValueKeyDown" />
34+
}
35+
36+
<div class="d-flex justify-content-end gap-2">
37+
<button type="button"
38+
class="btn btn-sm btn-outline-secondary"
39+
disabled="@(ActiveFilter is null)"
40+
@onclick="ClearAsync">
41+
@Texts.ClearFilterLabel
42+
</button>
43+
<button type="button"
44+
class="btn btn-sm btn-primary"
45+
disabled="@(!CanApply)"
46+
@onclick="ApplyAsync">
47+
@Texts.ApplyFilterLabel
48+
</button>
49+
</div>
50+
</div>
51+
</div>
52+
53+
@code {
54+
private static readonly FancyColumnFilterOperator[] Operators = Enum.GetValues<FancyColumnFilterOperator>();
55+
56+
private readonly string popupId = $"fancy-filter-{Guid.NewGuid():N}";
57+
private ElementReference anchorElement;
58+
private ElementReference popupElement;
59+
private IJSObjectReference? module;
60+
private DotNetObjectReference<FancyColumnFilterPopup>? dotNetReference;
61+
private FancyColumnFilterOperator draftOperator = FancyColumnFilterOperator.Contains;
62+
private string draftValue = string.Empty;
63+
private bool isOpen;
64+
65+
[Parameter, EditorRequired]
66+
public string ColumnKey { get; set; } = string.Empty;
67+
68+
[Parameter, EditorRequired]
69+
public string ColumnTitle { get; set; } = string.Empty;
70+
71+
[Parameter]
72+
public FancyColumnFilter? ActiveFilter { get; set; }
73+
74+
[Parameter, EditorRequired]
75+
public FancyTableTexts Texts { get; set; } = FancyTableTexts.Default;
76+
77+
[Parameter]
78+
public EventCallback<FancyColumnFilter> OnApply { get; set; }
79+
80+
[Parameter]
81+
public EventCallback OnClear { get; set; }
82+
83+
private FancyColumnFilter Draft => new(
84+
ColumnKey,
85+
draftOperator,
86+
draftOperator.RequiresValue() ? draftValue : null);
87+
88+
private bool CanApply => Draft.IsEffective();
89+
90+
private async Task OpenAsync()
91+
{
92+
draftOperator = ActiveFilter?.Operator ?? FancyColumnFilterOperator.Contains;
93+
draftValue = ActiveFilter?.Value ?? string.Empty;
94+
isOpen = true;
95+
96+
module ??= await JS.InvokeAsync<IJSObjectReference>("import", "./_content/SoPro.FancyTable/fancy-table-filter.js");
97+
dotNetReference ??= DotNetObjectReference.Create(this);
98+
await module.InvokeVoidAsync("show", anchorElement, popupElement, dotNetReference);
99+
}
100+
101+
private void OnValueInput(ChangeEventArgs args)
102+
{
103+
draftValue = args.Value?.ToString() ?? string.Empty;
104+
}
105+
106+
private async Task OnValueKeyDown(KeyboardEventArgs args)
107+
{
108+
if (args.Key == "Enter" && CanApply)
109+
{
110+
await ApplyAsync();
111+
}
112+
}
113+
114+
private async Task ApplyAsync()
115+
{
116+
if (!CanApply)
117+
{
118+
return;
119+
}
120+
121+
await OnApply.InvokeAsync(Draft);
122+
await HideAsync();
123+
}
124+
125+
private async Task ClearAsync()
126+
{
127+
await OnClear.InvokeAsync();
128+
await HideAsync();
129+
}
130+
131+
private async Task HideAsync()
132+
{
133+
if (module is not null)
134+
{
135+
await module.InvokeVoidAsync("hide", popupElement);
136+
}
137+
}
138+
139+
[JSInvokable]
140+
public Task OnPopoverClosed()
141+
{
142+
isOpen = false;
143+
StateHasChanged();
144+
return Task.CompletedTask;
145+
}
146+
147+
private string GetOperatorLabel(FancyColumnFilterOperator op) => op switch
148+
{
149+
FancyColumnFilterOperator.Equals => Texts.FilterOperatorEqualsLabel,
150+
FancyColumnFilterOperator.NotEquals => Texts.FilterOperatorNotEqualsLabel,
151+
FancyColumnFilterOperator.Contains => Texts.FilterOperatorContainsLabel,
152+
FancyColumnFilterOperator.NotContains => Texts.FilterOperatorNotContainsLabel,
153+
FancyColumnFilterOperator.StartsWith => Texts.FilterOperatorStartsWithLabel,
154+
FancyColumnFilterOperator.EndsWith => Texts.FilterOperatorEndsWithLabel,
155+
FancyColumnFilterOperator.IsEmpty => Texts.FilterOperatorIsEmptyLabel,
156+
FancyColumnFilterOperator.IsNotEmpty => Texts.FilterOperatorIsNotEmptyLabel,
157+
_ => op.ToString()
158+
};
159+
160+
private static string FormatColumnLabel(string format, string title) =>
161+
string.Format(CultureInfo.CurrentCulture, format, title);
162+
163+
public async ValueTask DisposeAsync()
164+
{
165+
if (module is not null)
166+
{
167+
try
168+
{
169+
await module.InvokeVoidAsync("dispose", popupElement);
170+
await module.DisposeAsync();
171+
}
172+
catch (JSDisconnectedException)
173+
{
174+
}
175+
}
176+
177+
dotNetReference?.Dispose();
178+
}
179+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.fancy-filter-popup {
2+
min-width: 14rem;
3+
padding: 0;
4+
background: #fff;
5+
border: 1px solid #dee2e6;
6+
border-radius: 6px;
7+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
8+
}

‎Components/FancyTableTexts.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,16 @@ public sealed record FancyTableTexts
2020
public string ShowingVisibleRowsFormat { get; init; } = "Showing {0}-{1} of {2} visible rows";
2121
public string ResizeColumnFormat { get; init; } = "Resize column {0}";
2222
public string ReorderColumnFormat { get; init; } = "Reorder column {0}";
23+
public string FilterColumnFormat { get; init; } = "Filter column {0}";
24+
public string FilterValuePlaceholder { get; init; } = "Value";
25+
public string ApplyFilterLabel { get; init; } = "Apply";
26+
public string ClearFilterLabel { get; init; } = "Clear";
27+
public string FilterOperatorEqualsLabel { get; init; } = "Is";
28+
public string FilterOperatorNotEqualsLabel { get; init; } = "Is not";
29+
public string FilterOperatorContainsLabel { get; init; } = "Contains";
30+
public string FilterOperatorNotContainsLabel { get; init; } = "Does not contain";
31+
public string FilterOperatorStartsWithLabel { get; init; } = "Starts with";
32+
public string FilterOperatorEndsWithLabel { get; init; } = "Ends with";
33+
public string FilterOperatorIsEmptyLabel { get; init; } = "Is empty";
34+
public string FilterOperatorIsNotEmptyLabel { get; init; } = "Is not empty";
2335
}

‎FancyTable.de.resx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,40 @@
6666
<data name="ReorderColumnFormat" xml:space="preserve">
6767
<value>Spalte {0} neu anordnen</value>
6868
</data>
69+
<data name="FilterColumnFormat" xml:space="preserve">
70+
<value>Spalte {0} filtern</value>
71+
</data>
72+
<data name="FilterValuePlaceholder" xml:space="preserve">
73+
<value>Wert</value>
74+
</data>
75+
<data name="ApplyFilterLabel" xml:space="preserve">
76+
<value>Anwenden</value>
77+
</data>
78+
<data name="ClearFilterLabel" xml:space="preserve">
79+
<value>Zurücksetzen</value>
80+
</data>
81+
<data name="FilterOperatorEqualsLabel" xml:space="preserve">
82+
<value>Ist</value>
83+
</data>
84+
<data name="FilterOperatorNotEqualsLabel" xml:space="preserve">
85+
<value>Ist nicht</value>
86+
</data>
87+
<data name="FilterOperatorContainsLabel" xml:space="preserve">
88+
<value>Enthält</value>
89+
</data>
90+
<data name="FilterOperatorNotContainsLabel" xml:space="preserve">
91+
<value>Enthält nicht</value>
92+
</data>
93+
<data name="FilterOperatorStartsWithLabel" xml:space="preserve">
94+
<value>Beginnt mit</value>
95+
</data>
96+
<data name="FilterOperatorEndsWithLabel" xml:space="preserve">
97+
<value>Endet mit</value>
98+
</data>
99+
<data name="FilterOperatorIsEmptyLabel" xml:space="preserve">
100+
<value>Ist leer</value>
101+
</data>
102+
<data name="FilterOperatorIsNotEmptyLabel" xml:space="preserve">
103+
<value>Ist nicht leer</value>
104+
</data>
69105
</root>

‎FancyTable.en.resx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,40 @@
6666
<data name="ReorderColumnFormat" xml:space="preserve">
6767
<value>Reorder column {0}</value>
6868
</data>
69+
<data name="FilterColumnFormat" xml:space="preserve">
70+
<value>Filter column {0}</value>
71+
</data>
72+
<data name="FilterValuePlaceholder" xml:space="preserve">
73+
<value>Value</value>
74+
</data>
75+
<data name="ApplyFilterLabel" xml:space="preserve">
76+
<value>Apply</value>
77+
</data>
78+
<data name="ClearFilterLabel" xml:space="preserve">
79+
<value>Clear</value>
80+
</data>
81+
<data name="FilterOperatorEqualsLabel" xml:space="preserve">
82+
<value>Is</value>
83+
</data>
84+
<data name="FilterOperatorNotEqualsLabel" xml:space="preserve">
85+
<value>Is not</value>
86+
</data>
87+
<data name="FilterOperatorContainsLabel" xml:space="preserve">
88+
<value>Contains</value>
89+
</data>
90+
<data name="FilterOperatorNotContainsLabel" xml:space="preserve">
91+
<value>Does not contain</value>
92+
</data>
93+
<data name="FilterOperatorStartsWithLabel" xml:space="preserve">
94+
<value>Starts with</value>
95+
</data>
96+
<data name="FilterOperatorEndsWithLabel" xml:space="preserve">
97+
<value>Ends with</value>
98+
</data>
99+
<data name="FilterOperatorIsEmptyLabel" xml:space="preserve">
100+
<value>Is empty</value>
101+
</data>
102+
<data name="FilterOperatorIsNotEmptyLabel" xml:space="preserve">
103+
<value>Is not empty</value>
104+
</data>
69105
</root>

‎FancyTable.resx‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,40 @@
6666
<data name="ReorderColumnFormat" xml:space="preserve">
6767
<value>Reorder column {0}</value>
6868
</data>
69+
<data name="FilterColumnFormat" xml:space="preserve">
70+
<value>Filter column {0}</value>
71+
</data>
72+
<data name="FilterValuePlaceholder" xml:space="preserve">
73+
<value>Value</value>
74+
</data>
75+
<data name="ApplyFilterLabel" xml:space="preserve">
76+
<value>Apply</value>
77+
</data>
78+
<data name="ClearFilterLabel" xml:space="preserve">
79+
<value>Clear</value>
80+
</data>
81+
<data name="FilterOperatorEqualsLabel" xml:space="preserve">
82+
<value>Is</value>
83+
</data>
84+
<data name="FilterOperatorNotEqualsLabel" xml:space="preserve">
85+
<value>Is not</value>
86+
</data>
87+
<data name="FilterOperatorContainsLabel" xml:space="preserve">
88+
<value>Contains</value>
89+
</data>
90+
<data name="FilterOperatorNotContainsLabel" xml:space="preserve">
91+
<value>Does not contain</value>
92+
</data>
93+
<data name="FilterOperatorStartsWithLabel" xml:space="preserve">
94+
<value>Starts with</value>
95+
</data>
96+
<data name="FilterOperatorEndsWithLabel" xml:space="preserve">
97+
<value>Ends with</value>
98+
</data>
99+
<data name="FilterOperatorIsEmptyLabel" xml:space="preserve">
100+
<value>Is empty</value>
101+
</data>
102+
<data name="FilterOperatorIsNotEmptyLabel" xml:space="preserve">
103+
<value>Is not empty</value>
104+
</data>
69105
</root>

0 commit comments

Comments
 (0)