Skip to content

Commit bf32cd4

Browse files
committed
feat: 增加 OnCollapsed 回调方法
1 parent 016ce93 commit bf32cd4

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/BootstrapBlazor/Components/Select/Select.razor.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public partial class Select<TValue> : ISelect, ILookup
117117
[Parameter]
118118
public bool IsAutoClearSearchTextWhenCollapsed { get; set; }
119119

120+
/// <summary>
121+
/// Gets or sets the dropdown collapsed callback method.
122+
/// </summary>
123+
[Parameter]
124+
public Func<Task>? OnCollapsed { get; set; }
125+
120126
IEnumerable<SelectedItem>? ILookup.Lookup { get => Items; set => Items = value; }
121127

122128
StringComparison ILookup.LookupStringComparison { get => StringComparison; set => StringComparison = value; }
@@ -267,9 +273,30 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue
267273
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new
268274
{
269275
ConfirmMethodCallback = nameof(ConfirmSelectedItem),
270-
SearchMethodCallback = nameof(TriggerOnSearch)
276+
SearchMethodCallback = nameof(TriggerOnSearch),
277+
TriggerCollapsed = (OnCollapsed != null || IsAutoClearSearchTextWhenCollapsed) ? nameof(TriggerCollapsed) : null
271278
});
272279

280+
/// <summary>
281+
/// Trigger <see cref="OnCollapsed"/> event callback method. called by JavaScript.
282+
/// </summary>
283+
/// <returns></returns>
284+
[JSInvokable]
285+
public async Task TriggerCollapsed()
286+
{
287+
if (OnCollapsed != null)
288+
{
289+
await OnCollapsed();
290+
}
291+
292+
if (IsAutoClearSearchTextWhenCollapsed)
293+
{
294+
SearchText = string.Empty;
295+
_itemsCache = null;
296+
StateHasChanged();
297+
}
298+
}
299+
273300
/// <summary>
274301
/// <inheritdoc/>
275302
/// </summary>

src/BootstrapBlazor/Components/Select/Select.razor.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export function init(id, invoke, options) {
1010
}
1111

1212
const search = el.querySelector(".search-text")
13-
const popover = Popover.init(el)
13+
const popover = Popover.init(el, {
14+
hideCallback: () => {
15+
if (options.triggerCollapsed) {
16+
invoke.invokeMethodAsync(options.triggerCollapsed);
17+
}
18+
}
19+
});
1420
const input = el.querySelector(`#${id}_input`);
1521
const select = {
1622
el, invoke, options,

0 commit comments

Comments
 (0)