Skip to content

Commit 2f08627

Browse files
committed
feat: 增加 FilterProvider 组件
1 parent 9abd346 commit 2f08627

6 files changed

Lines changed: 150 additions & 39 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// Filter component
10+
/// </summary>
11+
public class Filter<TFilter> : FilterProvider where TFilter : IComponent
12+
{
13+
/// <summary>
14+
/// 获得/设置 过滤器组件参数集合 Default is null
15+
/// </summary>
16+
[Parameter]
17+
public IDictionary<string, object>? FilterParameters { get; set; }
18+
19+
/// <summary>
20+
/// 渲染自定义过滤器方法
21+
/// </summary>
22+
/// <returns></returns>
23+
protected override RenderFragment RenderFilter() => builder =>
24+
{
25+
var filterType = typeof(TFilter);
26+
builder.OpenComponent<TFilter>(0);
27+
if (filterType.IsSubclassOf(typeof(FilterBase)))
28+
{
29+
builder.AddAttribute(1, nameof(FilterBase.FieldKey), FieldKey);
30+
builder.AddAttribute(2, nameof(FilterBase.IsHeaderRow), IsHeaderRow);
31+
}
32+
if (filterType.IsSubclassOf(typeof(MultipleFilterBase)))
33+
{
34+
builder.AddAttribute(10, nameof(MultipleFilterBase.Count), Count);
35+
}
36+
37+
if (FilterParameters != null)
38+
{
39+
builder.AddMultipleAttributes(100, FilterParameters);
40+
}
41+
builder.CloseComponent();
42+
};
43+
}

src/BootstrapBlazor/Components/Filters/FilterBase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction
3838
[CascadingParameter, NotNull]
3939
protected TableColumnFilter? TableColumnFilter { get; set; }
4040

41+
/// <summary>
42+
/// Gets or sets the <see cref="FilterContext"/> instance from cascading parameter.
43+
/// </summary>
44+
[CascadingParameter]
45+
protected FilterContext? FilterContext { get; set; }
46+
4147
/// <summary>
4248
/// <inheritdoc/>
4349
/// </summary>
@@ -51,6 +57,20 @@ protected override void OnInitialized()
5157
}
5258
}
5359

60+
/// <summary>
61+
/// <inheritdoc/>
62+
/// </summary>
63+
protected override void OnParametersSet()
64+
{
65+
base.OnParametersSet();
66+
67+
if (FilterContext != null)
68+
{
69+
FieldKey = FilterContext.FieldKey;
70+
IsHeaderRow = FilterContext.IsHeaderRow;
71+
}
72+
}
73+
5474
/// <summary>
5575
/// 重置按钮回调方法
5676
/// </summary>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+
using System.Runtime.CompilerServices;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// FilterContext class
12+
/// </summary>
13+
public class FilterContext
14+
{
15+
/// <summary>
16+
/// Gets or sets whether the filter is header row. Default is false.
17+
/// </summary>
18+
public bool IsHeaderRow { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the column field key. Default is null.
22+
/// </summary>
23+
public string? FieldKey { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the filter counter. Default is 0.
27+
/// </summary>
28+
public int Count { get; set; }
29+
}

src/BootstrapBlazor/Components/Filters/Filter.razor renamed to src/BootstrapBlazor/Components/Filters/FilterProvider.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
@namespace BootstrapBlazor.Components
22
@inherits ComponentBase
3-
@typeparam TFilter
43

5-
@if (_isHeaderRow)
4+
@if (IsHeaderRow)
65
{
76
@RenderFilter()
87
}
@@ -17,12 +16,13 @@ else
1716
<div class="d-flex flex-fill">
1817
@if (ShowMoreButton)
1918
{
20-
<Button Color="Color.None" OnClick="OnClickPlus" Icon="@PlusIcon" IsDisabled="@(_count == 1)"></Button>
21-
<Button Color="Color.None" OnClick="OnClickMinus" Icon="@MinusIcon" IsDisabled="@(_count == 0)"></Button>
19+
<Button Color="Color.None" OnClick="OnClickPlus" Icon="@PlusIcon" IsDisabled="@(Count == 1)"></Button>
20+
<Button Color="Color.None" OnClick="OnClickMinus" Icon="@MinusIcon" IsDisabled="@(Count == 0)"></Button>
2221
}
2322
</div>
2423
<Button Color="Color.None" class="filter-dismiss" OnClick="OnClickReset" Text="@ClearButtonText"></Button>
2524
<Button Color="Color.None" class="filter-dismiss" OnClick="OnClickConfirm" Text="@FilterButtonText"></Button>
2625
</div>
2726
</div>
2827
}
28+

src/BootstrapBlazor/Components/Filters/Filter.razor.cs renamed to src/BootstrapBlazor/Components/Filters/FilterProvider.razor.cs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
namespace BootstrapBlazor.Components;
99

1010
/// <summary>
11-
/// Filter 组件
11+
/// FilterProvider component
1212
/// </summary>
13-
public partial class Filter<TFilter> where TFilter : IComponent
13+
public partial class FilterProvider
1414
{
15-
/// <summary>
16-
/// 获得/设置 过滤器组件参数集合 Default is null
17-
/// </summary>
18-
[Parameter]
19-
public IDictionary<string, object>? FilterParameters { get; set; }
20-
2115
/// <summary>
2216
/// 获得/设置 重置按钮文本
2317
/// </summary>
@@ -53,10 +47,20 @@ public partial class Filter<TFilter> where TFilter : IComponent
5347
/// <summary>
5448
/// Gets or sets the filter title. Default is null.
5549
/// </summary>
50+
[Parameter]
5651
public string? Title { get; set; }
5752

53+
/// <summary>
54+
/// Gets or sets the child content. Default is null.
55+
/// </summary>
56+
[Parameter]
57+
public RenderFragment? ChildContent { get; set; }
58+
59+
/// <summary>
60+
/// Gets or sets the <see cref="TableColumnFilter"/> instance from cascading parameter.
61+
/// </summary>
5862
[CascadingParameter]
59-
private TableColumnFilter? TableColumnFilter { get; set; }
63+
protected TableColumnFilter? TableColumnFilter { get; set; }
6064

6165
[Inject]
6266
[NotNull]
@@ -66,9 +70,20 @@ public partial class Filter<TFilter> where TFilter : IComponent
6670
[NotNull]
6771
private IIconTheme? IconTheme { get; set; }
6872

69-
private int _count;
70-
private string? _fieldKey;
71-
private bool _isHeaderRow = false;
73+
/// <summary>
74+
/// Gets or sets the filter counter. Default is 0.
75+
/// </summary>
76+
protected int Count { get; set; }
77+
78+
/// <summary>
79+
/// Gets or sets the column field key. Default is null.
80+
/// </summary>
81+
protected string? FieldKey { get; set; }
82+
83+
/// <summary>
84+
/// Gets or sets whether the filter is header row. Default is false.
85+
/// </summary>
86+
protected bool IsHeaderRow { get; set; }
7287

7388
/// <summary>
7489
/// <inheritdoc/>
@@ -84,9 +99,8 @@ protected override void OnParametersSet()
8499
ClearButtonText ??= Localizer[nameof(ClearButtonText)];
85100

86101
Title ??= TableColumnFilter.GetFilterTitle();
87-
88-
_isHeaderRow = TableColumnFilter.IsHeaderRow();
89-
_fieldKey = TableColumnFilter.GetFieldKey();
102+
FieldKey ??= TableColumnFilter.GetFieldKey();
103+
IsHeaderRow = TableColumnFilter.IsHeaderRow();
90104
}
91105

92106
/// <summary>
@@ -95,7 +109,7 @@ protected override void OnParametersSet()
95109
/// <returns></returns>
96110
private async Task OnClickReset()
97111
{
98-
_count = 0;
112+
Count = 0;
99113
if (TableColumnFilter != null)
100114
{
101115
await TableColumnFilter.Reset();
@@ -121,9 +135,9 @@ protected async Task OnClickConfirm()
121135
/// <returns></returns>
122136
private void OnClickPlus()
123137
{
124-
if (_count == 0)
138+
if (Count == 0)
125139
{
126-
_count++;
140+
Count++;
127141
}
128142
}
129143

@@ -133,9 +147,9 @@ private void OnClickPlus()
133147
/// <returns></returns>
134148
private void OnClickMinus()
135149
{
136-
if (_count == 1)
150+
if (Count == 1)
137151
{
138-
_count--;
152+
Count--;
139153
}
140154
}
141155

@@ -145,22 +159,14 @@ private void OnClickMinus()
145159
/// <returns></returns>
146160
protected virtual RenderFragment RenderFilter() => builder =>
147161
{
148-
var filterType = typeof(TFilter);
149-
builder.OpenComponent<TFilter>(0);
150-
if (filterType.IsSubclassOf(typeof(FilterBase)))
162+
builder.OpenComponent<CascadingValue<FilterContext>>(0);
163+
builder.AddAttribute(1, nameof(CascadingValue<FilterContext>.Value), new FilterContext()
151164
{
152-
builder.AddAttribute(1, nameof(FilterBase.FieldKey), _fieldKey);
153-
builder.AddAttribute(2, nameof(FilterBase.IsHeaderRow), _isHeaderRow);
154-
}
155-
if (filterType.IsSubclassOf(typeof(MultipleFilterBase)))
156-
{
157-
builder.AddAttribute(10, nameof(MultipleFilterBase.Count), _count);
158-
}
159-
160-
if (FilterParameters != null)
161-
{
162-
builder.AddMultipleAttributes(100, FilterParameters);
163-
}
165+
Count = Count,
166+
FieldKey = FieldKey,
167+
IsHeaderRow = IsHeaderRow
168+
});
169+
builder.AddAttribute(2, nameof(CascadingValue<FilterContext>.ChildContent), ChildContent);
164170
builder.CloseComponent();
165171
};
166172
}

src/BootstrapBlazor/Components/Filters/MultipleFilterBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,17 @@ public abstract class MultipleFilterBase : FilterBase
2020
/// 获得/设置 多个条件逻辑关系符号
2121
/// </summary>
2222
protected FilterLogic Logic { get; set; }
23+
24+
/// <summary>
25+
/// <inheritdoc/>
26+
/// </summary>
27+
protected override void OnParametersSet()
28+
{
29+
base.OnParametersSet();
30+
31+
if (FilterContext != null)
32+
{
33+
Count = FilterContext.Count;
34+
}
35+
}
2336
}

0 commit comments

Comments
 (0)