Skip to content

Commit 0e8d5a3

Browse files
committed
refactor: 精简代码
1 parent bfcf6d0 commit 0e8d5a3

File tree

4 files changed

+29
-91
lines changed

4 files changed

+29
-91
lines changed

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,7 @@
5757
}
5858
else
5959
{
60-
<CascadingValue Value="_items" IsFixed="true">
61-
@ItemsTemplate
62-
</CascadingValue>
63-
<RenderTemplate>
64-
@foreach(var item in _items)
65-
{
66-
switch (item)
67-
{
68-
case DropdownDivider:
69-
<Divider></Divider>
70-
break;
71-
case DropdownItem t:
72-
{
73-
var disabled = GetItemTriggerClick(t);
74-
if (t.ChildContent != null)
75-
{
76-
<div class="@GetItemClassString(disabled)">
77-
@t.ChildContent
78-
</div>
79-
}
80-
else
81-
{
82-
<DynamicElement @key="t" class="@GetItemClassString(disabled)"
83-
TriggerClick="!disabled" OnClick="() => OnClickItem(t)">
84-
<i class="@GetItemIconString(t)"></i>
85-
<span>@t.Text</span>
86-
</DynamicElement>
87-
}
88-
89-
break;
90-
}
91-
default:
92-
<DynamicComponent Type="item.GetType()"></DynamicComponent>
93-
break;
94-
}
95-
}
96-
</RenderTemplate>
60+
@ItemsTemplate
9761
}
9862
</div>
9963
</div>

src/BootstrapBlazor/Components/Dropdown/Dropdown.razor.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,4 @@ private async Task HandlerClick()
292292
await OnClick.InvokeAsync();
293293
}
294294
}
295-
296-
private static string? GetItemIconString(DropdownItem item) => CssBuilder.Default("dropdown-item-icon")
297-
.AddClass(item.Icon, !string.IsNullOrEmpty(item.Icon))
298-
.Build();
299-
300-
private static string? GetItemClassString(bool disabled) => CssBuilder.Default("dropdown-item")
301-
.AddClass("disabled", disabled)
302-
.Build();
303-
304-
private static bool GetItemTriggerClick(DropdownItem item) => item.OnDisabledCallback?.Invoke(item) ?? item.Disabled;
305-
306-
private static async Task OnClickItem(DropdownItem item)
307-
{
308-
if (item.OnClick != null)
309-
{
310-
await item.OnClick();
311-
}
312-
}
313295
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@namespace BootstrapBlazor.Components
2+
3+
@if (ChildContent != null)
4+
{
5+
<div class="@ItemClassString">
6+
@t.ChildContent
7+
</div>
8+
}
9+
else
10+
{
11+
<DynamicElement class="@ItemClassString"
12+
TriggerClick="CanTriggerClick" OnClick="OnClickItem">
13+
<i class="@ItemIconString"></i>
14+
<span>@Text</span>
15+
</DynamicElement>
16+
}

src/BootstrapBlazor/Components/Dropdown/DropdownItem.cs renamed to src/BootstrapBlazor/Components/Dropdown/DropdownItem.razor.cs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// DropdownItem 组件
1010
/// </summary>
11-
public class DropdownItem : ComponentBase, IDisposable
11+
public partial class DropdownItem
1212
{
1313
/// <summary>
1414
/// 获得/设置 显示文本
@@ -32,7 +32,7 @@ public class DropdownItem : ComponentBase, IDisposable
3232
/// 获得/设置 是否被禁用回调方法 默认 null 优先级高于 <see cref="Disabled"/>
3333
/// </summary>
3434
[Parameter]
35-
public Func<DropdownItem, bool>? OnDisabledCallback { get; set; }
35+
public Func<bool>? OnDisabledCallback { get; set; }
3636

3737
/// <summary>
3838
/// 获得/设置 点击回调方法 默认 null
@@ -46,45 +46,21 @@ public class DropdownItem : ComponentBase, IDisposable
4646
[Parameter]
4747
public RenderFragment? ChildContent { get; set; }
4848

49-
[CascadingParameter]
50-
[NotNull]
51-
private List<ComponentBase>? Items { get; set; }
49+
private string? ItemIconString => CssBuilder.Default("dropdown-item-icon")
50+
.AddClass(Icon, !string.IsNullOrEmpty(Icon))
51+
.Build();
5252

53-
/// <summary>
54-
/// <inheritdoc/>
55-
/// </summary>
56-
protected override void OnInitialized()
57-
{
58-
base.OnInitialized();
53+
private string? ItemClassString => CssBuilder.Default("dropdown-item")
54+
.AddClass("disabled", CanTriggerClick)
55+
.Build();
5956

60-
Items.Add(this);
61-
}
57+
private bool CanTriggerClick => OnDisabledCallback?.Invoke() ?? Disabled;
6258

63-
private bool _disposedValue;
64-
65-
/// <summary>
66-
/// 释放资源方法
67-
/// </summary>
68-
/// <param name="disposing"></param>
69-
protected virtual void Dispose(bool disposing)
59+
private async Task OnClickItem()
7060
{
71-
if (!_disposedValue)
61+
if (OnClick != null)
7262
{
73-
if (disposing)
74-
{
75-
Items.Remove(this);
76-
}
77-
78-
_disposedValue = true;
63+
await OnClick();
7964
}
8065
}
81-
82-
/// <summary>
83-
/// <inheritdoc/>
84-
/// </summary>
85-
public void Dispose()
86-
{
87-
Dispose(true);
88-
GC.SuppressFinalize(this);
89-
}
9066
}

0 commit comments

Comments
 (0)