ant-design-blazor/components/tree/TreeNodeTitle.razor
如易君 7af4adc145
fix(module: tree): Fix search crash issue after Tree custom SearchExpression (#3274)
* 修复Tree自定义SearchExpression后Title中不包括过滤内容导致的bug

* 删除空行,修复格式

---------

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2023-06-11 23:34:14 +08:00

137 lines
6.1 KiB
C#

@namespace AntDesign
@inherits ComponentBase
@typeparam TItem
@using System.Text.RegularExpressions;
@if (CanDraggable)
{
<span class="@TitleClassMapper.Class" title="@SelfNode.Title" @onclick="OnClick" @ondblclick="OnDblClick" draggable="true"
@oncontextmenu="OnContextMenu"
@oncontextmenu:preventDefault="TreeComponent.OnContextMenu.HasDelegate"
@ondragover:preventDefault @ondragover="OnDragOver" @ondragleave="OnDragLeave" @ondragenter="OnDragEnter" @ondrop:preventDefault @ondrop="OnDrop"
@ondragstart="OnDragStart" @ondragend="OnDragEnd" aria-grabbed="true">
@if (TreeComponent.TitleIconTemplate != null && TreeComponent.ShowIcon)
{
<span class="ant-tree-iconEle @(IsSwitcherOpen?"ant-tree-icon__open":"") @(IsSwitcherClose?"ant-tree-icon__close":"") @(SelfNode.Loading?"ant-tree-icon_loading":"")">
<span class="ant-tree-iconEle ant-tree-icon__customize">
@TreeComponent.TitleIconTemplate(SelfNode)
</span>
</span>
}
else if (string.IsNullOrWhiteSpace(SelfNode.Icon) == false && TreeComponent.ShowIcon)
{
<span class="ant-tree-iconEle @(IsSwitcherOpen?"ant-tree-icon__open":"") @(IsSwitcherClose?"ant-tree-icon__close":"") @(SelfNode.Loading?"ant-tree-icon_loading":"")">
<span class="ant-tree-iconEle ant-tree-icon__customize">
@if (string.IsNullOrWhiteSpace(SelfNode.Icon) == false)
{
<Icon Type="@SelfNode.Icon" Theme="outline" />
}
</span>
</span>
}
@if (TreeComponent.TitleTemplate != null)
{
@TreeComponent.TitleTemplate(SelfNode)
}
else
{
<span class="ant-tree-title" style="pointer-events: none;">
@if (SelfNode.TitleTemplate != null)
{
@SelfNode.TitleTemplate
}
else
{
bool shouldMarkup = SelfNode.Matched && !string.IsNullOrWhiteSpace(TreeComponent.SearchValue);
int index = -1;
if (shouldMarkup)
{
index = SelfNode.Title.IndexOf(TreeComponent.SearchValue, StringComparison.InvariantCultureIgnoreCase);
}
if (shouldMarkup && (index > -1))
{
var start = SelfNode.Title.Substring(0, index);
var match = SelfNode.Title.Substring(index, TreeComponent.SearchValue.Length);
var end = SelfNode.Title.Substring(index + TreeComponent.SearchValue.Length);
var value = $"{start}<span class=\"{TreeComponent.MatchedClass}\" style=\"{TreeComponent.MatchedStyle}\">{match}</span>{end}";
<span>
@((MarkupString)value)
</span>
}
else
{
@SelfNode.Title
}
}
</span>
}
@if (SelfNode.DragTarget)
{
<div class="ant-tree-drop-indicator" style="left:@(SelfNode.DragTargetBottom?"4px":"28px");right:0;bottom:-3px;"></div>
}
</span>
}
else
{
<span class="@TitleClassMapper.Class" title="@SelfNode.Title" @onclick="OnClick" @ondblclick="OnDblClick"
@oncontextmenu="OnContextMenu" @oncontextmenu:preventDefault="TreeComponent.OnContextMenu.HasDelegate">
@if (TreeComponent.TitleIconTemplate != null && TreeComponent.ShowIcon)
{
<span class="ant-tree-iconEle @(IsSwitcherOpen?"ant-tree-icon__open":"") @(IsSwitcherClose?"ant-tree-icon__close":"") @(SelfNode.Loading?"ant-tree-icon_loading":"")">
<span class="ant-tree-iconEle ant-tree-icon__customize">
@TreeComponent.TitleIconTemplate(SelfNode)
</span>
</span>
}
else if ((!string.IsNullOrWhiteSpace(SelfNode.Icon) || SelfNode.IconTemplate != null) && TreeComponent.ShowIcon)
{
<span class="ant-tree-iconEle @(IsSwitcherOpen?"ant-tree-icon__open":"") @(IsSwitcherClose?"ant-tree-icon__close":"") @(SelfNode.Loading?"ant-tree-icon_loading":"")">
<span class="ant-tree-iconEle ant-tree-icon__customize">
@if (SelfNode.IconTemplate != null)
{
@SelfNode.IconTemplate(SelfNode)
}
else if (!string.IsNullOrWhiteSpace(SelfNode.Icon))
{
<Icon Type="@SelfNode.Icon" Theme="outline" />
}
</span>
</span>
}
@if (TreeComponent.TitleTemplate != null)
{
@TreeComponent.TitleTemplate(SelfNode)
}
else if (SelfNode.TitleTemplate != null)
{
@SelfNode.TitleTemplate
}
else
{
<span class="ant-tree-title">
@if (SelfNode.Matched && !string.IsNullOrWhiteSpace(TreeComponent.SearchValue))
{
int index = SelfNode.Title.IndexOf(TreeComponent.SearchValue, StringComparison.InvariantCultureIgnoreCase);
var start = SelfNode.Title.Substring(0, index);
var match = SelfNode.Title.Substring(index, TreeComponent.SearchValue.Length);
var end = SelfNode.Title.Substring(index + TreeComponent.SearchValue.Length);
var value = $"{start}<span class=\"{TreeComponent.MatchedClass}\" style=\"{TreeComponent.MatchedStyle}\">{match}</span>{end}";
<span>
@((MarkupString)value)
</span>
}
else
{
@SelfNode.Title
}
</span>
}
@if (SelfNode.DragTarget)
{
<div class="ant-tree-drop-indicator" style="left:@(SelfNode.DragTargetBottom?"4px":"28px");right:0;bottom:-3px;"></div>
}
</span>
}