Skip to content

Commit e778430

Browse files
committed
feat: 增加 OnItemShownAsync 回调方法
1 parent da9de81 commit e778430

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/BootstrapBlazor/Components/DropdownWidget/DropdownWidget.razor.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public sealed partial class DropdownWidget
3131
[Parameter]
3232
public Func<DropdownWidgetItem, Task>? OnItemCloseAsync { get; set; }
3333

34+
/// <summary>
35+
/// 获得/设置 下拉项关闭回调方法
36+
/// </summary>
37+
[Parameter]
38+
public Func<DropdownWidgetItem, Task>? OnItemShownAsync { get; set; }
39+
3440
private List<DropdownWidgetItem> Childs { get; } = new List<DropdownWidgetItem>(20);
3541

3642
/// <summary>
@@ -54,15 +60,23 @@ internal void Add(DropdownWidgetItem item)
5460
/// Widget 下拉项关闭回调方法 由 JavaScript 调用
5561
/// </summary>
5662
/// <param name="index"></param>
63+
/// <param name="shown"></param>
5764
/// <returns></returns>
5865
[JSInvokable]
59-
public async Task OnWidgetItemClosed(int index)
66+
public async Task OnWidgetItemClosed(int index, bool shown)
6067
{
6168
var items = GetItems().ToList();
6269
var item = index < items.Count ? items[index] : null;
63-
if (item != null && OnItemCloseAsync != null)
70+
if (item != null)
6471
{
65-
await OnItemCloseAsync(item);
72+
if (OnItemCloseAsync != null && !shown)
73+
{
74+
await OnItemCloseAsync(item);
75+
}
76+
else if (OnItemShownAsync != null && shown)
77+
{
78+
await OnItemShownAsync(item);
79+
}
6680
}
6781
}
6882
}

src/BootstrapBlazor/Components/DropdownWidget/DropdownWidget.razor.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ export function init(id, invoke, options) {
77
return;
88
}
99

10-
const { method } = options;
11-
EventHandler.on(el, 'hidden.bs.dropdown', e => {
10+
const invokeMethod = e => {
1211
const item = e.target;
1312
const items = [...el.querySelectorAll("[data-bs-toggle=\"dropdown\"]")];
1413
const index = items.indexOf(item);
15-
invoke.invokeMethodAsync(method, index);
16-
});
14+
invoke.invokeMethodAsync(method, index, e.type === 'shown.bs.dropdown');
15+
}
16+
17+
const { method } = options;
18+
EventHandler.on(el, 'hidden.bs.dropdown', invokeMethod);
19+
EventHandler.on(el, 'shown.bs.dropdown', invokeMethod);
1720
}
1821

1922
export function dispose(id) {

0 commit comments

Comments
 (0)