-
-
Notifications
You must be signed in to change notification settings - Fork 364
feat(MultiSelectFilter): add MultiSelectFilter component #6097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4f194f
refactor: 移除 bind-value event 语句
ArgoZhang 7299c52
refactor: 增加 MultiSelectFilter 组件
ArgoZhang 8c4d96d
test: 增加单元测试
ArgoZhang 40a3aff
test: 更新单元测试
ArgoZhang 5498a05
test: 更新单元测试
ArgoZhang 02d6b81
test: 更新单元测试
ArgoZhang 22583d3
test: 更新单元测试
ArgoZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/BootstrapBlazor/Components/Filters/MultiSelectFilter.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| @namespace BootstrapBlazor.Components | ||
| @inherits FilterBase | ||
| @typeparam TType | ||
|
|
||
| @if (IsHeaderRow) | ||
| { | ||
| <div class="@FilterRowClassString"> | ||
| <MultiSelect class="is-filter" Items="@Items" @bind-Value="_value1" OnValueChanged="_ => OnFilterAsync()" | ||
| ShowLabel="false" SkipValidate="true" IsPopover="true"></MultiSelect> | ||
| </div> | ||
| } | ||
| else | ||
| { | ||
| <MultiSelect Items="@Items" @bind-Value="@_value1" IsPopover="true" ShowLabel="false" SkipValidate="true"></MultiSelect> | ||
| } | ||
90 changes: 90 additions & 0 deletions
90
src/BootstrapBlazor/Components/Filters/MultiSelectFilter.razor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // 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([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
||
| /// <summary> | ||
| /// 多项选择下拉框过滤组件 | ||
| /// </summary> | ||
| public partial class MultiSelectFilter<TType> | ||
| { | ||
| private string? FilterRowClassString => CssBuilder.Default("filter-row") | ||
| .AddClass("active", TableColumnFilter.HasFilter()) | ||
| .Build(); | ||
|
|
||
| private TType? _value1; | ||
| private FilterAction _action1 = FilterAction.Equal; | ||
ArgoZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the filter items. | ||
| /// </summary> | ||
| [Parameter] | ||
| public List<SelectedItem>? Items { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public override void Reset() | ||
| { | ||
| _value1 = default; | ||
| _action1 = FilterAction.Equal; | ||
| Count = 0; | ||
| StateHasChanged(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| public override FilterKeyValueAction GetFilterConditions() | ||
| { | ||
| var filter = new FilterKeyValueAction() { FilterLogic = FilterLogic.Or }; | ||
| if (_value1 is string v) | ||
| { | ||
| var items = v.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
| foreach (var item in items) | ||
| { | ||
| filter.Filters.Add(new FilterKeyValueAction | ||
| { | ||
| FieldKey = FieldKey, | ||
| FieldValue = item, | ||
| FilterAction = _action1 | ||
| }); | ||
| } | ||
| } | ||
| else if (_value1 is IEnumerable<string> values) | ||
| { | ||
| foreach (var item in values) | ||
| { | ||
| filter.Filters.Add(new FilterKeyValueAction | ||
| { | ||
| FieldKey = FieldKey, | ||
| FieldValue = item, | ||
| FilterAction = _action1 | ||
| }); | ||
| } | ||
| } | ||
| return filter; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| public override async Task SetFilterConditionsAsync(FilterKeyValueAction filter) | ||
| { | ||
| var first = filter.Filters.FirstOrDefault() ?? filter; | ||
| if (first.FieldValue is TType value) | ||
| { | ||
| _value1 = value; | ||
| } | ||
| else | ||
| { | ||
| _value1 = default; | ||
| } | ||
| _action1 = first.FilterAction; | ||
|
|
||
| await base.SetFilterConditionsAsync(filter); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // 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([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| namespace UnitTest.Components; | ||
|
|
||
| public class TableMultiSelectFilterTest : BootstrapBlazorTestBase | ||
| { | ||
| [Fact] | ||
| public async Task IsHeaderRow_Ok() | ||
| { | ||
| var cut = Context.RenderComponent<TableColumnFilter>(pb => | ||
| { | ||
| pb.Add(a => a.Table, new MockTable()); | ||
| pb.Add(a => a.Column, new MockColumn()); | ||
| pb.Add(a => a.IsHeaderRow, true); | ||
| }); | ||
| cut.Contains("filter-row"); | ||
|
|
||
| var actions = cut.FindAll(".dropdown-item"); | ||
| await cut.InvokeAsync(() => actions[1].Click()); | ||
|
|
||
| // check filter | ||
| var filter = cut.Instance; | ||
| var conditions = filter.FilterAction.GetFilterConditions(); | ||
| Assert.Single(conditions.Filters); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task OnFilterAsync_Ok() | ||
| { | ||
| var cut = Context.RenderComponent<TableColumnFilter>(pb => | ||
| { | ||
| pb.Add(a => a.Table, new MockTable()); | ||
| pb.Add(a => a.Column, new MockColumn()); | ||
| pb.Add(a => a.IsHeaderRow, false); | ||
| }); | ||
| var item = cut.Find(".dropdown-menu .dropdown-item"); | ||
| await cut.InvokeAsync(() => item.Click()); | ||
|
|
||
| var filter = cut.FindComponent<MultiSelectFilter<string>>(); | ||
| var conditions = filter.Instance.GetFilterConditions(); | ||
|
|
||
| Assert.Single(conditions.Filters); | ||
|
|
||
| await cut.InvokeAsync(() => filter.Instance.Reset()); | ||
| conditions = filter.Instance.GetFilterConditions(); | ||
| Assert.Empty(conditions.Filters); | ||
|
|
||
| await cut.InvokeAsync(() => filter.Instance.SetFilterConditionsAsync(new FilterKeyValueAction() | ||
| { | ||
| FieldValue = "v1,v2", | ||
| })); | ||
| conditions = filter.Instance.GetFilterConditions(); | ||
| Assert.Equal(2, conditions.Filters.Count); | ||
|
|
||
| await cut.InvokeAsync(() => filter.Instance.SetFilterConditionsAsync(new FilterKeyValueAction() | ||
| { | ||
| FieldValue = true, | ||
| })); | ||
| conditions = filter.Instance.GetFilterConditions(); | ||
| Assert.Empty(conditions.Filters); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task OnFilterAsync_List() | ||
| { | ||
| var cut = Context.RenderComponent<TableColumnFilter>(pb => | ||
| { | ||
| pb.Add(a => a.Table, new MockTable()); | ||
| pb.Add(a => a.Column, new MockColumnList()); | ||
| pb.Add(a => a.IsHeaderRow, false); | ||
| }); | ||
| var item = cut.Find(".dropdown-menu .dropdown-item"); | ||
| await cut.InvokeAsync(() => item.Click()); | ||
|
|
||
| var filter = cut.FindComponent<MultiSelectFilter<List<string>>>(); | ||
| var conditions = filter.Instance.GetFilterConditions(); | ||
|
|
||
| Assert.Single(conditions.Filters); | ||
|
|
||
| await cut.InvokeAsync(() => filter.Instance.Reset()); | ||
| conditions = filter.Instance.GetFilterConditions(); | ||
| Assert.Empty(conditions.Filters); | ||
| } | ||
|
|
||
| class MockTable : ITable | ||
| { | ||
| public Dictionary<string, IFilterAction> Filters { get; set; } = []; | ||
|
|
||
| public Func<Task>? OnFilterAsync { get; set; } | ||
|
|
||
| public List<ITableColumn> Columns => []; | ||
|
|
||
| public IEnumerable<ITableColumn> GetVisibleColumns() => Columns; | ||
| } | ||
|
|
||
| class MockColumn : TableColumn<Foo, string> | ||
| { | ||
| public MockColumn() | ||
| { | ||
| PropertyType = typeof(string); | ||
| FieldName = "MultiSelect"; | ||
|
|
||
| FilterTemplate = new RenderFragment(builder => | ||
| { | ||
| builder.OpenComponent<FilterProvider>(0); | ||
| builder.AddAttribute(1, nameof(FilterProvider.ChildContent), new RenderFragment(builder => | ||
| { | ||
| builder.OpenComponent<MultiSelectFilter<string>>(2); | ||
| builder.AddAttribute(3, nameof(MultiSelectFilter<string>.Items), new List<SelectedItem> | ||
| { | ||
| new("v1", "Test-1"), | ||
| new("v2", "Test-2") | ||
| }); | ||
| builder.CloseComponent(); | ||
| })); | ||
| builder.CloseComponent(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class MockColumnList : TableColumn<Foo, List<string>> | ||
| { | ||
| public MockColumnList() | ||
| { | ||
| PropertyType = typeof(List<string>); | ||
| FieldName = "MultiSelect"; | ||
|
|
||
| FilterTemplate = new RenderFragment(builder => | ||
| { | ||
| builder.OpenComponent<FilterProvider>(0); | ||
| builder.AddAttribute(1, nameof(FilterProvider.ChildContent), new RenderFragment(builder => | ||
| { | ||
| builder.OpenComponent<MultiSelectFilter<List<string>>>(2); | ||
| builder.AddAttribute(3, nameof(MultiSelectFilter<List<string>>.Items), new List<SelectedItem> | ||
| { | ||
| new("v1", "Test-1"), | ||
| new("v2", "Test-2") | ||
| }); | ||
| builder.CloseComponent(); | ||
| })); | ||
| builder.CloseComponent(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.