Skip to content

Commit

Permalink
feat(MultiFilter): Update components after changes in the Items data …
Browse files Browse the repository at this point in the history
…source (#3739)

* refactor(MultiFilter): 重构代码

* refactor: 精简代码

* Revert "refactor: 精简代码"

This reverts commit c7011af.

* Revert "refactor(MultiFilter): 重构代码"

This reverts commit f1c5af7.

* refactor: 修复搜索结果为空时全选被选中问题

* refactor: 重构代码

* chore: bump version 8.6.5-beta03

---------

Co-authored-by: Argo-AscioTech <[email protected]>
  • Loading branch information
h2ls and ArgoZhang authored Jun 28, 2024
1 parent 74e1700 commit dba666a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.6.5-beta02</Version>
<Version>8.6.5-beta03</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Filters/MultiFilter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
}
<div class="bb-multi-filter-list">
<div class="bb-multi-filter-header">
<Checkbox Value="checkAll" ShowAfterLabel="true" ShowLabel="false" DisplayText="@SelectAllText" OnStateChanged="@OnStateChanged" State="GetState()" />
<Checkbox Value="GetAllState()" ShowAfterLabel="true" ShowLabel="false" DisplayText="@SelectAllText" OnStateChanged="@OnStateChanged" State="_selectAllState" />
</div>
<div class="bb-multi-filter-body scroll">
@foreach (var item in GetItems())
Expand Down
59 changes: 32 additions & 27 deletions src/BootstrapBlazor/Components/Filters/MultiFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,16 @@ public partial class MultiFilter

private string? _searchText;

private bool checkAll = false;
private List<MultiFilterItem> _source = [];

private readonly List<MultiFilterItem> _source = [];

private IEnumerable<MultiFilterItem>? _items;
private List<MultiFilterItem>? _items;

/// <summary>
/// OnInitialized 方法
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

if (Items != null)
{
_source.AddRange(Items.Select(item => new MultiFilterItem() { Value = item.Value, Text = item.Text }));
}
if (TableFilter != null)
{
TableFilter.ShowMoreButton = false;
Expand All @@ -61,14 +54,18 @@ protected override void OnParametersSet()

SearchPlaceHolderText ??= Localizer["MultiFilterSearchPlaceHolderText"];
SelectAllText ??= Localizer["MultiFilterSelectAllText"];

if (Items != null)
{
_source = Items.Select(item => new MultiFilterItem() { Value = item.Value, Text = item.Text }).ToList();
}
}

/// <summary>
/// 重置过滤条件方法
/// </summary>
public override void Reset()
{
checkAll = false;
_searchText = string.Empty;
foreach (var item in _source)
{
Expand Down Expand Up @@ -97,26 +94,34 @@ public override FilterKeyValueAction GetFilterConditions()
return filter;
}

private CheckboxState GetState() => GetItems().All(i => i.Checked)
? CheckboxState.Checked
: GetItems().All(i => !i.Checked) ? CheckboxState.UnChecked : CheckboxState.Indeterminate;
private CheckboxState _selectAllState = CheckboxState.UnChecked;

private Task OnStateChanged(CheckboxState state, bool val)
private CheckboxState GetState()
{
checkAll = val;
if (state == CheckboxState.Checked)
var state = CheckboxState.UnChecked;
var items = GetItems();
if (items.Count > 0)
{
foreach (var item in _source)
{
item.Checked = true;
}
state = items.All(i => i.Checked)
? CheckboxState.Checked
: items.Any(i => i.Checked)
? CheckboxState.Indeterminate
: CheckboxState.UnChecked;
}
else
return state;
}

private bool GetAllState()
{
_selectAllState = GetState();
return _selectAllState == CheckboxState.Checked;
}

private Task OnStateChanged(CheckboxState state, bool val)
{
foreach (var item in _source)
{
foreach (var item in _source)
{
item.Checked = false;
}
item.Checked = state == CheckboxState.Checked;
}
StateHasChanged();
return Task.CompletedTask;
Expand All @@ -132,7 +137,7 @@ private Task OnSearchValueChanged(string? val)
_searchText = val;
if (!string.IsNullOrEmpty(_searchText))
{
_items = _source.Where(i => i.Text.Contains(_searchText));
_items = _source.Where(i => i.Text.Contains(_searchText)).ToList();
}
else
{
Expand All @@ -142,7 +147,7 @@ private Task OnSearchValueChanged(string? val)
return Task.CompletedTask;
}

private IEnumerable<MultiFilterItem> GetItems() => _items ?? _source;
private List<MultiFilterItem> GetItems() => _items ?? _source;

class MultiFilterItem
{
Expand Down

0 comments on commit dba666a

Please sign in to comment.