diff --git a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor index d061f3129c6..35c33af51c3 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor @@ -65,7 +65,9 @@ - + + + @@ -211,22 +213,30 @@ - + + + - + + + - + + + - + + + diff --git a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor.cs index 218df0223b0..79a6cd4e579 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesFilter.razor.cs @@ -29,31 +29,14 @@ public partial class TablesFilter [NotNull] private Table? TableSetFilter { get; set; } - private readonly Dictionary _multiFilterParameter1 = new(); - private readonly Dictionary _multiFilterParameter2 = new(); - private readonly Dictionary _multiFilterParameter3 = new(); - private readonly Dictionary _multiFilterParameter4 = new(); - /// - /// OnInitialized 方法 + /// /// protected override void OnInitialized() { base.OnInitialized(); Items = Foo.GenerateFoo(FooLocalizer); - var items1 = Items.Select(i => new SelectedItem(i.Name!, i.Name!)).DistinctBy(i => i.Value); - _multiFilterParameter1.Add(nameof(MultiFilter.Items), items1); - - _multiFilterParameter2.Add(nameof(MultiFilter.OnGetItemsAsync), new Func>>(OnGetAddressItemsAsync)); - - var items3 = Items.Select(i => new SelectedItem(i.Complete.ToString(), i.Complete.ToString())).DistinctBy(i => i.Value); - _multiFilterParameter3.Add(nameof(MultiFilter.Items), items3); - _multiFilterParameter3.Add(nameof(MultiFilter.ShowSearch), false); - - var items4 = Items.Select(i => new SelectedItem(i.Education.ToString()!, i.Education.ToString()!)).DistinctBy(i => i.Value); - _multiFilterParameter4.Add(nameof(MultiFilter.Items), items4); - _multiFilterParameter4.Add(nameof(MultiFilter.ShowSearch), false); } private async Task> OnGetAddressItemsAsync() diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index b745ba90652..9829dee67d3 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@  - 9.6.2 + 9.6.3 diff --git a/src/BootstrapBlazor/Components/Filters/FilterBase.cs b/src/BootstrapBlazor/Components/Filters/FilterBase.cs index 6dd3345a20c..948ea03e499 100644 --- a/src/BootstrapBlazor/Components/Filters/FilterBase.cs +++ b/src/BootstrapBlazor/Components/Filters/FilterBase.cs @@ -38,6 +38,12 @@ public abstract class FilterBase : BootstrapModuleComponentBase, IFilterAction [CascadingParameter, NotNull] protected TableColumnFilter? TableColumnFilter { get; set; } + /// + /// Gets or sets the instance from cascading parameter. + /// + [CascadingParameter] + protected FilterContext? FilterContext { get; set; } + /// /// /// @@ -51,6 +57,20 @@ protected override void OnInitialized() } } + /// + /// + /// + protected override void OnParametersSet() + { + base.OnParametersSet(); + + if (FilterContext != null) + { + FieldKey = FilterContext.FieldKey; + IsHeaderRow = FilterContext.IsHeaderRow; + } + } + /// /// 重置按钮回调方法 /// diff --git a/src/BootstrapBlazor/Components/Filters/FilterContext.cs b/src/BootstrapBlazor/Components/Filters/FilterContext.cs new file mode 100644 index 00000000000..2ae9e7d8f08 --- /dev/null +++ b/src/BootstrapBlazor/Components/Filters/FilterContext.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using System.Runtime.CompilerServices; + +namespace BootstrapBlazor.Components; + +/// +/// FilterContext class +/// +public class FilterContext +{ + /// + /// Gets or sets whether the filter is header row. Default is false. + /// + public bool IsHeaderRow { get; set; } + + /// + /// Gets or sets the column field key. Default is null. + /// + public string? FieldKey { get; set; } + + /// + /// Gets or sets the filter counter. Default is 0. + /// + public int Count { get; set; } +} diff --git a/src/BootstrapBlazor/Components/Filters/Filter.razor b/src/BootstrapBlazor/Components/Filters/FilterProvider.razor similarity index 83% rename from src/BootstrapBlazor/Components/Filters/Filter.razor rename to src/BootstrapBlazor/Components/Filters/FilterProvider.razor index e987fe3ed0b..34d962cf662 100644 --- a/src/BootstrapBlazor/Components/Filters/Filter.razor +++ b/src/BootstrapBlazor/Components/Filters/FilterProvider.razor @@ -1,8 +1,7 @@ @namespace BootstrapBlazor.Components @inherits ComponentBase -@typeparam TFilter -@if (_isHeaderRow) +@if (IsHeaderRow) { @RenderFilter() } @@ -17,8 +16,8 @@ else
@if (ShowMoreButton) { - - + + }
diff --git a/src/BootstrapBlazor/Components/Filters/Filter.razor.cs b/src/BootstrapBlazor/Components/Filters/FilterProvider.razor.cs similarity index 69% rename from src/BootstrapBlazor/Components/Filters/Filter.razor.cs rename to src/BootstrapBlazor/Components/Filters/FilterProvider.razor.cs index 46eaa79235d..4a4deac853a 100644 --- a/src/BootstrapBlazor/Components/Filters/Filter.razor.cs +++ b/src/BootstrapBlazor/Components/Filters/FilterProvider.razor.cs @@ -8,16 +8,10 @@ namespace BootstrapBlazor.Components; /// -/// Filter 组件 +/// FilterProvider component /// -public partial class Filter where TFilter : IComponent +public partial class FilterProvider { - /// - /// 获得/设置 过滤器组件参数集合 Default is null - /// - [Parameter] - public IDictionary? FilterParameters { get; set; } - /// /// 获得/设置 重置按钮文本 /// @@ -53,10 +47,20 @@ public partial class Filter where TFilter : IComponent /// /// Gets or sets the filter title. Default is null. /// + [Parameter] public string? Title { get; set; } + /// + /// Gets or sets the child content. Default is null. + /// + [Parameter] + public RenderFragment? ChildContent { get; set; } + + /// + /// Gets or sets the instance from cascading parameter. + /// [CascadingParameter] - private TableColumnFilter? TableColumnFilter { get; set; } + protected TableColumnFilter? TableColumnFilter { get; set; } [Inject] [NotNull] @@ -66,9 +70,20 @@ public partial class Filter where TFilter : IComponent [NotNull] private IIconTheme? IconTheme { get; set; } - private int _count; - private string? _fieldKey; - private bool _isHeaderRow = false; + /// + /// Gets or sets the filter counter. Default is 0. + /// + protected int Count { get; set; } + + /// + /// Gets or sets the column field key. Default is null. + /// + protected string? FieldKey { get; set; } + + /// + /// Gets or sets whether the filter is header row. Default is false. + /// + protected bool IsHeaderRow { get; set; } /// /// @@ -84,9 +99,8 @@ protected override void OnParametersSet() ClearButtonText ??= Localizer[nameof(ClearButtonText)]; Title ??= TableColumnFilter.GetFilterTitle(); - - _isHeaderRow = TableColumnFilter.IsHeaderRow(); - _fieldKey = TableColumnFilter.GetFieldKey(); + FieldKey ??= TableColumnFilter.GetFieldKey(); + IsHeaderRow = TableColumnFilter.IsHeaderRow(); } /// @@ -95,7 +109,7 @@ protected override void OnParametersSet() /// private async Task OnClickReset() { - _count = 0; + Count = 0; if (TableColumnFilter != null) { await TableColumnFilter.Reset(); @@ -121,9 +135,9 @@ protected async Task OnClickConfirm() /// private void OnClickPlus() { - if (_count == 0) + if (Count == 0) { - _count++; + Count++; } } @@ -133,9 +147,9 @@ private void OnClickPlus() /// private void OnClickMinus() { - if (_count == 1) + if (Count == 1) { - _count--; + Count--; } } @@ -145,22 +159,14 @@ private void OnClickMinus() /// protected virtual RenderFragment RenderFilter() => builder => { - var filterType = typeof(TFilter); - builder.OpenComponent(0); - if (filterType.IsSubclassOf(typeof(FilterBase))) + builder.OpenComponent>(0); + builder.AddAttribute(1, nameof(CascadingValue.Value), new FilterContext() { - builder.AddAttribute(1, nameof(FilterBase.FieldKey), _fieldKey); - builder.AddAttribute(2, nameof(FilterBase.IsHeaderRow), _isHeaderRow); - } - if (filterType.IsSubclassOf(typeof(MultipleFilterBase))) - { - builder.AddAttribute(10, nameof(MultipleFilterBase.Count), _count); - } - - if (FilterParameters != null) - { - builder.AddMultipleAttributes(100, FilterParameters); - } + Count = Count, + FieldKey = FieldKey, + IsHeaderRow = IsHeaderRow + }); + builder.AddAttribute(2, nameof(CascadingValue.ChildContent), ChildContent); builder.CloseComponent(); }; } diff --git a/src/BootstrapBlazor/Components/Filters/MultipleFilterBase.cs b/src/BootstrapBlazor/Components/Filters/MultipleFilterBase.cs index 637a798c327..111d7d7a2c4 100644 --- a/src/BootstrapBlazor/Components/Filters/MultipleFilterBase.cs +++ b/src/BootstrapBlazor/Components/Filters/MultipleFilterBase.cs @@ -20,4 +20,17 @@ public abstract class MultipleFilterBase : FilterBase /// 获得/设置 多个条件逻辑关系符号 /// protected FilterLogic Logic { get; set; } + + /// + /// + /// + protected override void OnParametersSet() + { + base.OnParametersSet(); + + if (FilterContext != null) + { + Count = FilterContext.Count; + } + } } diff --git a/src/BootstrapBlazor/Components/Filters/TableColumnFilter.razor b/src/BootstrapBlazor/Components/Filters/TableColumnFilter.razor index 35e63ed3615..b71429cdb7e 100644 --- a/src/BootstrapBlazor/Components/Filters/TableColumnFilter.razor +++ b/src/BootstrapBlazor/Components/Filters/TableColumnFilter.razor @@ -27,11 +27,15 @@ else { @if (Column.PropertyType.IsEnum()) { - + + + } else if (Column.IsLookup()) { - + + + } else { @@ -39,31 +43,49 @@ else switch (fieldType.Name) { case nameof(String): - + + + break; case nameof(Boolean): - + + + break; case nameof(DateTime): - + + + break; case nameof(Int16): - + + + break; case nameof(Int32): - + + + break; case nameof(Int64): - + + + break; case nameof(Single): - + + + break; case nameof(Double): - + + + break; case nameof(Decimal): - + + + break; default:
@NotSupportedMessage
diff --git a/test/UnitTest/Components/FilterProviderTest.cs b/test/UnitTest/Components/FilterProviderTest.cs new file mode 100644 index 00000000000..f8398d8be67 --- /dev/null +++ b/test/UnitTest/Components/FilterProviderTest.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +namespace UnitTest.Components; + +public class FilterProviderTest : BootstrapBlazorTestBase +{ + [Fact] + public async Task FilterProvider_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.Add(a => a.ShowMoreButton, true); + pb.AddChildContent(); + }); + + var filter = cut.FindComponent(); + var context = filter.Instance.GetFilterContext(); + Assert.NotNull(context); + Assert.Equal(0, context.Count); + + // 点击 + + var plus = cut.Find(".card-footer button"); + await cut.InvokeAsync(() => plus.Click()); + context = filter.Instance.GetFilterContext(); + Assert.NotNull(context); + Assert.Equal(1, context.Count); + } + + class MockFilter : StringFilter + { + public FilterContext? GetFilterContext() => FilterContext; + } +} diff --git a/test/UnitTest/Components/TableColumnFilterTest.cs b/test/UnitTest/Components/TableColumnFilterTest.cs index 4a880484432..9ce0bfd42f0 100644 --- a/test/UnitTest/Components/TableColumnFilterTest.cs +++ b/test/UnitTest/Components/TableColumnFilterTest.cs @@ -4,7 +4,6 @@ // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone using Microsoft.Extensions.Localization; -using System.Threading.Tasks; namespace UnitTest.Components; @@ -57,7 +56,7 @@ public void TableColumnFilter_Ok() } [Fact] - public void Filter_Ok() + public void FilterProvider_Ok() { var cut = Context.RenderComponent(pb => { @@ -71,14 +70,6 @@ public void Filter_Ok() pb.Add(a => a.TableColumns, CreateCatTableColumns()); }); }); - - Context.RenderComponent>(pb => - { - pb.Add(a => a.FilterParameters, new Dictionary() - { - { "Items", new List() } - }); - }); } [Fact]