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); SelectedOptionItems.Add(selectOption);
} }
selectOption.IsSelected = true; selectOption.IsSelected = true;
CurrentValue = selectOption.Value; CurrentValue = selectOption.Value;
InvokeOnSelectedItemChanged(selectOption); InvokeOnSelectedItemChanged(selectOption);

View File

@ -25,7 +25,7 @@
<div class="" style="max-height: @PopupContainerMaxHeight; overflow-y: auto;"> <div class="" style="max-height: @PopupContainerMaxHeight; overflow-y: auto;">
<div> <div>
<div class="" role="listbox" style="display: flex; flex-direction: column;"> <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" <Tree @ref="_tree"
TItem="TItem" TItem="TItem"
BlockNode BlockNode

View File

@ -14,7 +14,7 @@ using System.Linq;
namespace AntDesign 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; [Parameter] public bool ShowExpand { get; set; } = true;
@ -137,6 +137,7 @@ namespace AntDesign
private readonly string _dir = "ltr"; private readonly string _dir = "ltr";
private Tree<TItem> _tree; private Tree<TItem> _tree;
[Parameter]
public override string Value public override string Value
{ {
get => base.Value; get => base.Value;
@ -156,6 +157,7 @@ namespace AntDesign
} }
} }
[Parameter]
public override IEnumerable<string> Values public override IEnumerable<string> Values
{ {
get => base.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() private void UpdateValueAndSelection()
{ {
if (SelectOptionItems.Any(o => o.Value == Value)) if (SelectOptionItems.Any(o => o.Value == Value))

View File

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

View File

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