fix(module: TreeSelect): value binding on datasource was changed (#3492)

* fix(module: TreeSelect): value binding on datasource was changed

* update selection after value changed

* tree datasource change

* invoke state changed

* clean up

* clean up
This commit is contained in:
James Yeung 2023-11-08 00:24:48 +08:00 committed by GitHub
parent ede6b57f36
commit 6f6b75835a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 9 deletions

View File

@ -751,6 +751,7 @@ namespace AntDesign
{
SelectedOptionItems.Add(selectOption);
}
selectOption.IsSelected = true;
CurrentValue = selectOption.Value;
InvokeOnSelectedItemChanged(selectOption);

View File

@ -25,7 +25,7 @@
<div class="" style="max-height: @PopupContainerMaxHeight; overflow-y: auto;">
<div>
<div class="" role="listbox" style="display: flex; flex-direction: column;">
<CascadingValue Name="IsTreeSelect" Value="true" IsFixed="true">
<CascadingValue Name="TreeSelect" Value="this" IsFixed="true">
<Tree @ref="_tree"
TItem="TItem"
BlockNode

View File

@ -14,7 +14,7 @@ using System.Linq;
namespace AntDesign
{
public partial class TreeSelect<TItem> : SelectBase<string, TItem> where TItem : class
public partial class TreeSelect<TItem> : SelectBase<string, TItem>
{
[Parameter] public bool ShowExpand { get; set; } = true;
@ -137,6 +137,7 @@ namespace AntDesign
private readonly string _dir = "ltr";
private Tree<TItem> _tree;
[Parameter]
public override string Value
{
get => base.Value;
@ -156,6 +157,7 @@ namespace AntDesign
}
}
[Parameter]
public override IEnumerable<string> Values
{
get => base.Values;
@ -442,6 +444,23 @@ namespace AntDesign
;
}
// bind the option once after fetching the data source asynchronously
// fixed https://github.com/ant-design-blazor/ant-design-blazor/issues/3446
internal void UpdateValueAfterDataSourceChanged()
{
if (Value != null)
{
UpdateValueAndSelection();
StateHasChanged();
}
if (Values != null)
{
UpdateValuesSelection();
StateHasChanged();
}
}
private void UpdateValueAndSelection()
{
if (SelectOptionItems.Any(o => o.Value == Value))

View File

@ -16,8 +16,8 @@ namespace AntDesign
{
public partial class Tree<TItem> : AntDomComponentBase
{
[CascadingParameter(Name = "IsTreeSelect")]
public bool IsTreeSelect { get; set; }
[CascadingParameter(Name = "TreeSelect")]
public TreeSelect<TItem> TreeSelect { get; set; }
#region fields
@ -31,6 +31,10 @@ namespace AntDesign
/// </summary>
private ConcurrentDictionary<long, TreeNode<TItem>> _checkedNodes = new ConcurrentDictionary<long, TreeNode<TItem>>();
bool _nodeHasChanged;
#endregion fields
#region Tree
@ -139,12 +143,26 @@ namespace AntDesign
/// Add a node
/// </summary>
/// <param name="treeNode"></param>
internal void AddNode(TreeNode<TItem> treeNode)
internal void AddChildNode(TreeNode<TItem> treeNode)
{
treeNode.NodeIndex = ChildNodes.Count;
ChildNodes.Add(treeNode);
}
internal void AddNode(TreeNode<TItem> treeNode)
{
_allNodes.Add(treeNode);
_nodeHasChanged = true;
CallAfterRender(async () =>
{
if (_nodeHasChanged)
{
_nodeHasChanged = false;
TreeSelect?.UpdateValueAfterDataSourceChanged();
}
});
}
#endregion Node
#region Selected
@ -818,7 +836,7 @@ namespace AntDesign
{
if (firstRender)
{
if (IsTreeSelect)
if (TreeSelect is not null)
{
IsCtrlKeyDown = true;
}
@ -854,4 +872,4 @@ namespace AntDesign
base.Dispose(disposing);
}
}
}
}

View File

@ -933,11 +933,12 @@ namespace AntDesign
ParentNode.AddNode(this);
else
{
TreeComponent.AddNode(this);
TreeComponent.AddChildNode(this);
if (!TreeComponent.DefaultExpandAll && TreeComponent.DefaultExpandParent)
Expand(true);
}
TreeComponent._allNodes.Add(this);
TreeComponent.AddNode(this);
if (TreeComponent.DisabledExpression != null)
Disabled = TreeComponent.DisabledExpression(this);