2020-06-02 13:34:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using System.Linq;
|
2021-03-12 17:02:11 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-05-08 15:31:38 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
|
|
|
|
using Microsoft.JSInterop;
|
|
|
|
|
using AntDesign.JsInterop;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Cascader : AntInputComponentBase<string>
|
|
|
|
|
{
|
|
|
|
|
[Parameter] public bool AllowClear { get; set; } = true;
|
|
|
|
|
|
2021-09-10 19:06:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Overlay adjustment strategy (when for example browser resize is happening)
|
|
|
|
|
/// </summary>
|
2023-01-09 15:25:19 +08:00
|
|
|
|
[Parameter] public TriggerBoundaryAdjustMode BoundaryAdjustMode { get; set; } = TriggerBoundaryAdjustMode.InView;
|
2021-09-10 19:06:50 +08:00
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
[Parameter] public bool ChangeOnSelect { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter] public string DefaultValue { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter] public string ExpandTrigger { get; set; }
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
[Parameter] public string NotFoundContent { get; set; } = LocaleProvider.CurrentLocale.Empty.Description;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
[Parameter] public string Placeholder { get => _placeHolder; set => _placeHolder = value; }
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-02-07 18:13:27 +08:00
|
|
|
|
[Parameter] public string PopupContainerSelector { get; set; } = "body";
|
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
[Parameter] public bool ShowSearch { get; set; }
|
|
|
|
|
|
2022-10-24 13:28:20 +08:00
|
|
|
|
[Parameter] public bool Disabled { get; set; }
|
|
|
|
|
|
2021-04-29 14:31:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Please use SelectedNodesChanged instead.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Obsolete("Instead use SelectedNodesChanged.")]
|
2020-06-02 13:34:26 +08:00
|
|
|
|
[Parameter] public Action<List<CascaderNode>, string, string> OnChange { get; set; }
|
|
|
|
|
|
2021-04-29 14:31:03 +08:00
|
|
|
|
[Parameter] public EventCallback<CascaderNode[]> SelectedNodesChanged { get; set; }
|
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
[Parameter]
|
2021-05-08 15:31:38 +08:00
|
|
|
|
public IEnumerable<CascaderNode> Options
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
2022-05-17 23:23:29 +08:00
|
|
|
|
get => _nodelist ?? Enumerable.Empty<CascaderNode>();
|
2020-06-02 13:34:26 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-05-17 23:23:29 +08:00
|
|
|
|
if (value == null && _nodelist == null) return;
|
|
|
|
|
if (value != null && _nodelist?.SequenceEqual(value) == true) return;
|
|
|
|
|
|
|
|
|
|
_nodelist = _nodelist ?? new List<CascaderNode>();
|
|
|
|
|
_nodelist?.Clear();
|
|
|
|
|
_searchList?.Clear();
|
|
|
|
|
if (value != null)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
2022-05-17 23:23:29 +08:00
|
|
|
|
_nodelist.AddRange(value);
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
2021-03-23 12:59:20 +08:00
|
|
|
|
ProcessParentAndDefault();
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 12:14:29 +08:00
|
|
|
|
private List<CascaderNode> _nodelist = new List<CascaderNode>();
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private List<CascaderNode> _selectedNodes = new List<CascaderNode>();
|
|
|
|
|
private List<CascaderNode> _hoverSelectedNodes = new List<CascaderNode>();
|
|
|
|
|
private List<CascaderNode> _renderNodes = new List<CascaderNode>();
|
|
|
|
|
private List<CascaderNode> _searchList = new List<CascaderNode>();
|
|
|
|
|
private IEnumerable<CascaderNode> _matchList;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
private ClassMapper _menuClassMapper = new ClassMapper();
|
|
|
|
|
private ClassMapper _inputClassMapper = new ClassMapper();
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private EventCallbackFactory _callbackFactory = new EventCallbackFactory();
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private bool _dropdownOpened;
|
|
|
|
|
private bool _isOnCascader;
|
|
|
|
|
private SelectedTypeEnum _selectedType;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private bool _showClearIcon;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
private string _displayText;
|
2021-04-29 14:31:03 +08:00
|
|
|
|
private bool _initialized;
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private string _searchValue;
|
|
|
|
|
private ElementReference _inputRef;
|
|
|
|
|
private string _placeHolder = LocaleProvider.CurrentLocale.Global.Placeholder;
|
|
|
|
|
|
|
|
|
|
private bool _focused;
|
|
|
|
|
private string _menuStyle;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
private static Dictionary<string, string> _sizeMap = new Dictionary<string, string>()
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
2021-03-23 12:59:20 +08:00
|
|
|
|
["large"] = "lg",
|
|
|
|
|
["small"] = "sm"
|
|
|
|
|
};
|
2020-06-07 00:47:18 +08:00
|
|
|
|
|
2021-03-23 12:59:20 +08:00
|
|
|
|
protected override void OnInitialized()
|
2020-06-07 00:47:18 +08:00
|
|
|
|
{
|
2021-03-23 12:59:20 +08:00
|
|
|
|
base.OnInitialized();
|
2021-05-08 15:31:38 +08:00
|
|
|
|
string prefixCls = "ant-cascader";
|
2023-01-13 23:27:35 +08:00
|
|
|
|
string selectCls = "ant-select";
|
2020-06-07 00:47:18 +08:00
|
|
|
|
|
2021-03-12 17:02:11 +08:00
|
|
|
|
ClassMapper
|
2023-01-13 23:27:35 +08:00
|
|
|
|
.Add("ant-select ant-cascader ant-select-single ant-select-show-arrow")
|
2021-05-08 15:31:38 +08:00
|
|
|
|
.Add($"{prefixCls}-picker")
|
|
|
|
|
.GetIf(() => $"{prefixCls}-picker-{Size}", () => _sizeMap.ContainsKey(Size))
|
2022-08-25 19:38:01 +08:00
|
|
|
|
.If("ant-select-open", () => _dropdownOpened)
|
|
|
|
|
.If("ant-select-focused", () => _focused)
|
|
|
|
|
.If($"{prefixCls}-picker-show-search ant-select-show-search", () => ShowSearch)
|
2021-05-08 15:31:38 +08:00
|
|
|
|
.If($"{prefixCls}-picker-with-value", () => !string.IsNullOrEmpty(_searchValue))
|
2022-08-25 19:38:01 +08:00
|
|
|
|
.If($"ant-select-lg", () => Size == "large")
|
|
|
|
|
.If($"ant-select-sm", () => Size == "small")
|
2022-10-24 13:28:20 +08:00
|
|
|
|
.If($"ant-select-disabled", () => Disabled)
|
2023-01-13 23:27:35 +08:00
|
|
|
|
.If("ant-select-allow-clear ", () => AllowClear)
|
|
|
|
|
.If($"{selectCls}-in-form-item ", () => FormItem != null)
|
|
|
|
|
.If($"{selectCls}-has-feedback", () => FormItem?.HasFeedback == true)
|
|
|
|
|
.GetIf(() => $"{selectCls}-status-{FormItem?.ValidateStatus.ToString().ToLowerInvariant()}", () => FormItem is { ValidateStatus: not FormValidateStatus.Default })
|
2021-05-08 15:31:38 +08:00
|
|
|
|
.If($"{prefixCls}-picker-rtl", () => RTL);
|
2021-01-16 21:54:57 +08:00
|
|
|
|
|
2021-03-23 12:59:20 +08:00
|
|
|
|
_inputClassMapper
|
|
|
|
|
.Add("ant-input")
|
2021-04-28 22:42:29 +08:00
|
|
|
|
.GetIf(() => $"ant-input-{_sizeMap[Size]}", () => _sizeMap.ContainsKey(Size))
|
2021-05-08 15:31:38 +08:00
|
|
|
|
.Add($"{prefixCls}-input")
|
|
|
|
|
.If($"{prefixCls}-input-rtl", () => RTL);
|
2021-03-12 17:02:11 +08:00
|
|
|
|
|
|
|
|
|
_menuClassMapper
|
2021-05-08 15:31:38 +08:00
|
|
|
|
.Add($"{prefixCls}-menu")
|
|
|
|
|
.If($"{prefixCls}-menu-rtl", () => RTL);
|
2021-03-12 17:02:11 +08:00
|
|
|
|
|
2021-04-29 14:31:03 +08:00
|
|
|
|
SetDefaultValue(Value ?? DefaultValue);
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
protected override void OnValueChange(string value)
|
|
|
|
|
{
|
|
|
|
|
base.OnValueChange(value);
|
|
|
|
|
RefreshNodeValue(value);
|
2022-07-18 13:23:39 +08:00
|
|
|
|
RefreshDisplayText();
|
2020-07-11 23:03:13 +08:00
|
|
|
|
}
|
2021-01-16 21:54:57 +08:00
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 输入框单击(显示/隐藏浮层)
|
|
|
|
|
/// </summary>
|
2022-03-26 13:58:37 +08:00
|
|
|
|
private async Task InputOnToggle()
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
2022-10-24 13:28:20 +08:00
|
|
|
|
if (Disabled)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_selectedType = SelectedTypeEnum.Click;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
_hoverSelectedNodes.Clear();
|
2021-05-08 15:31:38 +08:00
|
|
|
|
if (!_dropdownOpened)
|
|
|
|
|
{
|
2021-11-15 12:21:17 +08:00
|
|
|
|
var inputElemnet = await Js.InvokeAsync<HtmlElement>(JSInteropConstants.GetDomInfo, _inputRef);
|
|
|
|
|
_menuStyle = $"width:{inputElemnet.ClientWidth}px;";
|
|
|
|
|
if (!_nodelist.Any())
|
|
|
|
|
{
|
|
|
|
|
_menuStyle += "height:auto;";
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_dropdownOpened = true;
|
2022-08-25 19:38:01 +08:00
|
|
|
|
_focused = true;
|
2021-05-08 15:31:38 +08:00
|
|
|
|
}
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 输入框/浮层失去焦点(隐藏浮层)
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CascaderOnBlur()
|
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
if (!_isOnCascader)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_dropdownOpened = false;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
_renderNodes = _selectedNodes;
|
2022-08-25 19:38:01 +08:00
|
|
|
|
_searchValue = string.Empty;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 19:38:01 +08:00
|
|
|
|
_focused = false;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清除已选择项
|
|
|
|
|
/// </summary>
|
2022-08-13 12:27:20 +08:00
|
|
|
|
private async Task ClearSelected()
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
_selectedNodes.Clear();
|
|
|
|
|
_hoverSelectedNodes.Clear();
|
2020-06-27 18:24:21 +08:00
|
|
|
|
_displayText = string.Empty;
|
2021-04-29 14:31:03 +08:00
|
|
|
|
SetValue(string.Empty);
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_dropdownOpened = false;
|
2022-08-25 19:38:01 +08:00
|
|
|
|
|
2022-08-13 12:27:20 +08:00
|
|
|
|
_searchValue = string.Empty;
|
|
|
|
|
await this.FocusAsync(_inputRef);
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 浮层移入
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void NodesOnMouseOver()
|
|
|
|
|
{
|
|
|
|
|
if (!AllowClear) return;
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_isOnCascader = true;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 浮层移出
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void NodesOnMouseOut()
|
|
|
|
|
{
|
|
|
|
|
if (!AllowClear) return;
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_isOnCascader = false;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 下拉节点单击
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="node"></param>
|
|
|
|
|
private void NodeOnClick(CascaderNode node)
|
|
|
|
|
{
|
|
|
|
|
if (node.Disabled) return;
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_searchValue = string.Empty;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
SetSelectedNode(node, SelectedTypeEnum.Click);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 下拉节点移入
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="node"></param>
|
|
|
|
|
private void NodeOnMouseOver(CascaderNode node)
|
|
|
|
|
{
|
|
|
|
|
if (ExpandTrigger != "hover") return;
|
|
|
|
|
|
|
|
|
|
if (node.Disabled) return;
|
|
|
|
|
if (!node.HasChildren) return;
|
|
|
|
|
|
|
|
|
|
SetSelectedNode(node, SelectedTypeEnum.Hover);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private void OnSearchInput(ChangeEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_searchValue = e.Value?.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnSearchKeyUp(KeyboardEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(_searchValue))
|
|
|
|
|
{
|
|
|
|
|
_showClearIcon = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var inputElemnet = await Js.InvokeAsync<HtmlElement>(JSInteropConstants.GetDomInfo, _inputRef);
|
|
|
|
|
_menuStyle = $"width:{inputElemnet.ClientWidth}px;";
|
|
|
|
|
_matchList = _searchList.Where(x => x.Label.Contains(_searchValue, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
_showClearIcon = true;
|
|
|
|
|
if (!_matchList.Any())
|
|
|
|
|
{
|
|
|
|
|
_menuStyle += "height:auto;";
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Selected nodes
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cascaderNode"></param>
|
|
|
|
|
/// <param name="selectedType"></param>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private void SetSelectedNode(CascaderNode cascaderNode, SelectedTypeEnum selectedType)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
if (cascaderNode == null) return;
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_selectedType = selectedType;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
if (selectedType == SelectedTypeEnum.Click)
|
|
|
|
|
{
|
|
|
|
|
_selectedNodes.Clear();
|
|
|
|
|
SetSelectedNodeWithParent(cascaderNode, ref _selectedNodes);
|
|
|
|
|
_renderNodes = _selectedNodes;
|
|
|
|
|
|
|
|
|
|
if (ChangeOnSelect || !cascaderNode.HasChildren)
|
2021-05-08 15:31:38 +08:00
|
|
|
|
{
|
2020-06-02 13:34:26 +08:00
|
|
|
|
SetValue(cascaderNode.Value);
|
2021-05-08 15:31:38 +08:00
|
|
|
|
}
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_hoverSelectedNodes.Clear();
|
|
|
|
|
SetSelectedNodeWithParent(cascaderNode, ref _hoverSelectedNodes);
|
|
|
|
|
_renderNodes = _hoverSelectedNodes;
|
|
|
|
|
}
|
|
|
|
|
_renderNodes.Sort((x, y) => x.Level.CompareTo(y.Level)); //Level 升序排序
|
|
|
|
|
|
|
|
|
|
if (!cascaderNode.HasChildren)
|
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_dropdownOpened = false;
|
|
|
|
|
_isOnCascader = false;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Set all parent nodes to be selected
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="node"></param>
|
|
|
|
|
/// <param name="list"></param>
|
|
|
|
|
private void SetSelectedNodeWithParent(CascaderNode node, ref List<CascaderNode> list)
|
|
|
|
|
{
|
|
|
|
|
if (node == null) return;
|
|
|
|
|
|
|
|
|
|
list.Add(node);
|
|
|
|
|
SetSelectedNodeWithParent(node.ParentNode, ref list);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-16 21:54:57 +08:00
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// handles parent nodes and defaults after Options updating
|
2021-01-16 21:54:57 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private void ProcessParentAndDefault()
|
|
|
|
|
{
|
2021-03-23 12:59:20 +08:00
|
|
|
|
InitCascaderNodeState(_nodelist, null, 0);
|
|
|
|
|
SetDefaultValue(Value ?? DefaultValue);
|
2021-01-16 21:54:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Initialize nodes (Level, ParentNode)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list"></param>
|
|
|
|
|
/// <param name="parentNode"></param>
|
|
|
|
|
/// <param name="level"></param>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private void InitCascaderNodeState(List<CascaderNode> list, CascaderNode parentNode, int level, bool recursive = false)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
if (list == null) return;
|
|
|
|
|
|
|
|
|
|
foreach (var node in list)
|
|
|
|
|
{
|
|
|
|
|
node.Level = level;
|
|
|
|
|
node.ParentNode = parentNode;
|
|
|
|
|
|
|
|
|
|
if (node.HasChildren)
|
2021-05-08 15:31:38 +08:00
|
|
|
|
{
|
|
|
|
|
InitCascaderNodeState(node.Children.ToList(), node, level + 1, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_searchList.Add(new CascaderNode
|
|
|
|
|
{
|
|
|
|
|
Label = node.Label,
|
|
|
|
|
ParentNode = node.ParentNode,
|
|
|
|
|
Value = node.Value,
|
|
|
|
|
Disabled = node.Disabled,
|
|
|
|
|
Level = node.Level
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!recursive)
|
|
|
|
|
{
|
|
|
|
|
_searchList.ForEach(node =>
|
|
|
|
|
{
|
|
|
|
|
var pathList = new List<CascaderNode>();
|
|
|
|
|
SetSelectedNodeWithParent(node, ref pathList);
|
|
|
|
|
pathList.Reverse();
|
|
|
|
|
node.Label = string.Join(" / ", pathList.Select(x => x.Label));
|
|
|
|
|
});
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Refresh the selected value
|
2020-07-11 23:03:13 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
private void RefreshNodeValue(string value)
|
|
|
|
|
{
|
|
|
|
|
_selectedNodes.Clear();
|
|
|
|
|
var node = GetNodeByValue(_nodelist, value);
|
|
|
|
|
SetSelectedNodeWithParent(node, ref _selectedNodes);
|
|
|
|
|
_renderNodes = _selectedNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Set the default value
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="defaultValue"></param>
|
|
|
|
|
private void SetDefaultValue(string defaultValue)
|
|
|
|
|
{
|
2021-04-29 14:31:03 +08:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(defaultValue))
|
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
RefreshNodeValue(defaultValue);
|
|
|
|
|
SetValue(defaultValue);
|
2021-04-29 14:31:03 +08:00
|
|
|
|
}
|
2020-06-02 13:34:26 +08:00
|
|
|
|
|
2021-04-29 14:31:03 +08:00
|
|
|
|
_initialized = true;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Set the binding value
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
private void SetValue(string value)
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
RefreshDisplayText();
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
if (Value != value)
|
|
|
|
|
{
|
|
|
|
|
CurrentValueAsString = value;
|
2021-04-29 14:31:03 +08:00
|
|
|
|
|
|
|
|
|
if (_initialized && SelectedNodesChanged.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
SelectedNodesChanged.InvokeAsync(_selectedNodes.ToArray());
|
|
|
|
|
}
|
2021-05-08 15:31:38 +08:00
|
|
|
|
|
|
|
|
|
OnChange?.Invoke(_selectedNodes, value, _displayText);
|
2020-07-11 23:03:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// rebuild the display text
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RefreshDisplayText()
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
_selectedNodes.Sort((x, y) => x.Level.CompareTo(y.Level)); //Level 升序排序
|
2021-05-08 15:31:38 +08:00
|
|
|
|
_displayText = string.Join(" / ", _selectedNodes.Where(x => x != null).Select(x => x.Label));
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
/// Get the node based on the specified value
|
2020-06-02 13:34:26 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="list"></param>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-05-08 15:31:38 +08:00
|
|
|
|
private CascaderNode GetNodeByValue(IEnumerable<CascaderNode> list, string value)
|
2020-06-02 13:34:26 +08:00
|
|
|
|
{
|
|
|
|
|
if (list == null) return null;
|
|
|
|
|
|
|
|
|
|
foreach (var node in list)
|
|
|
|
|
{
|
|
|
|
|
if (node.Value == value)
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
|
|
if (node.HasChildren)
|
|
|
|
|
{
|
2021-05-08 15:31:38 +08:00
|
|
|
|
var nd = GetNodeByValue(node.Children, value);
|
2020-06-02 13:34:26 +08:00
|
|
|
|
if (nd != null)
|
2021-05-08 15:31:38 +08:00
|
|
|
|
{
|
|
|
|
|
return nd;
|
|
|
|
|
}
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-08 15:31:38 +08:00
|
|
|
|
return null;
|
2020-06-02 13:34:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|