Skip to content

Commit d62cbe5

Browse files
committed
refactor: 移除 dropdown-item 点击逻辑
1 parent e6d73bd commit d62cbe5

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
1313
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString" data-bb-blur="@TriggerBlurString"
1414
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString" data-bb-trigger-delete="true"
15-
@bind="@CurrentValueAsString"
1615
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement"/>
1716
<span class="form-select-append"><i class="@Icon"></i></span>
1817
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
@@ -27,7 +26,7 @@
2726
<div class="dropdown-menu-body">
2827
@foreach (var item in Rows)
2928
{
30-
<div @key="item" class="dropdown-item" @onclick="() => OnClickItem(item)">
29+
<div @key="item" class="dropdown-item" data-bb-val="@item">
3130
@if (ItemTemplate == null)
3231
{
3332
<div>@item</div>

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,37 @@ protected override void OnParametersSet()
119119
LoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.LoadingIcon);
120120
}
121121

122+
/// <summary>
123+
/// <inheritdoc/>
124+
/// </summary>
125+
/// <returns></returns>
126+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, Value);
127+
122128
/// <summary>
123129
/// Callback method when a candidate item is clicked
124130
/// </summary>
125-
private async Task OnClickItem(string val)
131+
[JSInvokable]
132+
public async Task TriggerClick(int index)
126133
{
127-
CurrentValue = val;
134+
if (index < 0 || index >= Rows.Count)
135+
{
136+
return;
137+
}
138+
139+
var item = Rows[index];
140+
CurrentValueAsString = item;
128141

129142
if (OnSelectedItemChanged != null)
130143
{
131-
await OnSelectedItemChanged(val);
144+
await OnSelectedItemChanged(Value);
132145
}
133146

134147
if (OnBlurAsync != null)
135148
{
136149
await OnBlurAsync(Value);
137150
}
138151

139-
await TriggerFilter(val);
152+
await TriggerFilter(Value);
140153
}
141154

142155
private List<string> Rows => _filterItems ?? [.. Items];

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import EventHandler from "../../modules/event-handler.js"
55
import Input from "../../modules/input.js"
66
import Popover from "../../modules/base-popover.js"
77

8-
export function init(id, invoke) {
8+
export function init(id, invoke, value) {
99
const el = document.getElementById(id)
1010
const menu = el.querySelector('.dropdown-menu')
1111
const input = document.getElementById(`${id}_input`)
1212
const ac = { el, invoke, menu, input }
1313
Data.set(id, ac)
1414

15+
input.value = value;
16+
1517
const isPopover = input.getAttribute('data-bs-toggle') === 'bb.dropdown';
1618
if (isPopover) {
1719
ac.popover = Popover.init(el, { toggleClass: '[data-bs-toggle="bb.dropdown"]' });
@@ -50,7 +52,14 @@ export function init(id, invoke) {
5052
}
5153

5254
EventHandler.on(menu, 'click', '.dropdown-item', e => {
55+
const item = e.delegateTarget;
56+
const val = item.getAttribute('data-bb-val');
57+
input.value = val;
5358
ac.close();
59+
60+
const items = [...item.parentElement.children];
61+
const index = items.indexOf(item);
62+
invoke.invokeMethodAsync('TriggerClick', index);
5463
});
5564

5665
EventHandler.on(input, 'focus', e => {
@@ -78,7 +87,6 @@ export function init(id, invoke) {
7887

7988
el.classList.add('is-loading');
8089
filterCallback(v);
81-
8290
});
8391

8492
ac.show = () => {

src/BootstrapBlazor/Components/AutoComplete/PopoverCompleteBase.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ protected override void OnParametersSet()
158158
Offset ??= "[0, 6]";
159159
}
160160

161-
/// <summary>
162-
/// <inheritdoc/>
163-
/// </summary>
164-
/// <returns></returns>
165-
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop);
166-
167161
/// <summary>
168162
/// 触发 OnBlur 回调方法 由 Javascript 触发
169163
/// </summary>

0 commit comments

Comments
 (0)