Skip to content

Commit fa4df4b

Browse files
committed
Revert "refactor: 移除 dropdown-item 点击逻辑"
This reverts commit d62cbe5.
1 parent c367af3 commit fa4df4b

File tree

4 files changed

+14
-28
lines changed

4 files changed

+14
-28
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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"
1516
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement"/>
1617
<span class="form-select-append"><i class="@Icon"></i></span>
1718
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>
@@ -26,7 +27,7 @@
2627
<div class="dropdown-menu-body">
2728
@foreach (var item in Rows)
2829
{
29-
<div @key="item" class="dropdown-item" data-bb-val="@item">
30+
<div @key="item" class="dropdown-item" @onclick="() => OnClickItem(item)">
3031
@if (ItemTemplate == null)
3132
{
3233
<div>@item</div>

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,37 +119,24 @@ 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-
128122
/// <summary>
129123
/// Callback method when a candidate item is clicked
130124
/// </summary>
131-
[JSInvokable]
132-
public async Task TriggerClick(int index)
125+
private async Task OnClickItem(string val)
133126
{
134-
if (index < 0 || index >= Rows.Count)
135-
{
136-
return;
137-
}
138-
139-
var item = Rows[index];
140-
CurrentValueAsString = item;
127+
CurrentValue = val;
141128

142129
if (OnSelectedItemChanged != null)
143130
{
144-
await OnSelectedItemChanged(Value);
131+
await OnSelectedItemChanged(val);
145132
}
146133

147134
if (OnBlurAsync != null)
148135
{
149136
await OnBlurAsync(Value);
150137
}
151138

152-
await TriggerFilter(Value);
139+
await TriggerFilter(val);
153140
}
154141

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

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ 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, value) {
8+
export function init(id, invoke) {
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-
1715
const isPopover = input.getAttribute('data-bs-toggle') === 'bb.dropdown';
1816
if (isPopover) {
1917
ac.popover = Popover.init(el, { toggleClass: '[data-bs-toggle="bb.dropdown"]' });
@@ -52,14 +50,7 @@ export function init(id, invoke, value) {
5250
}
5351

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

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

8879
el.classList.add('is-loading');
8980
filterCallback(v);
81+
9082
});
9183

9284
ac.show = () => {

src/BootstrapBlazor/Components/AutoComplete/PopoverCompleteBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ 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+
161167
/// <summary>
162168
/// 触发 OnBlur 回调方法 由 Javascript 触发
163169
/// </summary>

0 commit comments

Comments
 (0)