Skip to content

Commit f9fda76

Browse files
authored
feat(AutoComplete): use javascript update input value (#6709)
* refactor: 移除 dropdown-item 点击逻辑 * test: 更新单元测试 * feat: 增加无数据处理逻辑 * Revert "feat: 增加无数据处理逻辑" This reverts commit b2cbd76. * Revert "test: 更新单元测试" This reverts commit 057ad97. * Revert "refactor: 移除 dropdown-item 点击逻辑" This reverts commit d62cbe5. * refactor: 增加客户端设置值逻辑 * refactor: 更新清空逻辑 * refactor: 更新赋值逻辑 * test: 更新单元测试 * refactor: 更新 Search 组件
1 parent e6d73bd commit f9fda76

File tree

9 files changed

+42
-10
lines changed

9 files changed

+42
-10
lines changed

src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor

Lines changed: 0 additions & 1 deletion
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>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ 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>
@@ -137,6 +143,9 @@ private async Task OnClickItem(string val)
137143
}
138144

139145
await TriggerFilter(val);
146+
147+
// 使用脚本更新 input 值
148+
await InvokeVoidAsync("setValue", Id, val);
140149
}
141150

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ const handlerKeyup = (ac, e) => {
175175
}
176176
}
177177

178+
export function setValue(id, value) {
179+
const ac = Data.get(id)
180+
const { input } = ac;
181+
if (input) {
182+
input.value = value;
183+
}
184+
}
185+
178186
export function dispose(id) {
179187
const ac = Data.get(id)
180188
Data.remove(id)

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>

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
data-bb-auto-dropdown-focus="@ShowDropdownListOnFocusString" data-bb-debounce="@DurationString"
1515
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString"
1616
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
17-
@bind="@_displayText"
1817
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement" />
1918
<span class="form-select-append"><i class="@Icon"></i></span>
2019
<span class="form-select-append ac-loading"><i class="@LoadingIcon"></i></span>

src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ protected override void OnParametersSet()
184184
Items ??= [];
185185
}
186186

187+
/// <summary>
188+
/// <inheritdoc/>
189+
/// </summary>
190+
/// <returns></returns>
191+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, _displayText);
192+
187193

188194
private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;
189195

@@ -199,6 +205,9 @@ protected override void OnParametersSet()
199205
/// <returns></returns>
200206
private async Task OnClearValue()
201207
{
208+
// 使用脚本更新 input 值
209+
await InvokeVoidAsync("setValue", Id, "");
210+
202211
if (OnClearAsync != null)
203212
{
204213
await OnClearAsync();
@@ -223,6 +232,9 @@ private async Task OnClickItem(TValue val)
223232
CurrentValue = val;
224233
_displayText = GetDisplayText(val);
225234

235+
// 使用脚本更新 input 值
236+
await InvokeVoidAsync("setValue", Id, _displayText);
237+
226238
if (OnSelectedItemChanged != null)
227239
{
228240
await OnSelectedItemChanged(val);

src/BootstrapBlazor/Components/Search/Search.razor

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
data-bb-skip-esc="@SkipEscString" data-bb-skip-enter="@SkipEnterString" data-bb-blur="@TriggerBlurString"
3030
data-bb-scroll-behavior="@ScrollIntoViewBehaviorString"
3131
data-bb-input="@UseInputString"
32-
@bind="@_displayText"
3332
placeholder="@PlaceHolder" disabled="@Disabled" @ref="FocusElement" />
3433
@if (IsClearable)
3534
{

src/BootstrapBlazor/Components/Search/Search.razor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ protected override void OnParametersSet()
209209
}
210210
}
211211

212+
/// <summary>
213+
/// <inheritdoc/>
214+
/// </summary>
215+
/// <returns></returns>
216+
protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, _displayText);
217+
212218
private string? _displayText;
213219
private async Task OnSearchClick()
214220
{
@@ -229,6 +235,9 @@ private async Task OnSearchClick()
229235

230236
private async Task OnClearClick()
231237
{
238+
// 使用脚本更新 input 值
239+
await InvokeVoidAsync("setValue", Id, "");
240+
232241
_displayText = null;
233242
if (OnClear != null)
234243
{
@@ -252,6 +261,9 @@ private async Task OnClickItem(TValue val)
252261
CurrentValue = val;
253262
_displayText = GetDisplayText(val);
254263

264+
// 使用脚本更新 input 值
265+
await InvokeVoidAsync("setValue", Id, _displayText);
266+
255267
if (OnSelectedItemChanged != null)
256268
{
257269
await OnSelectedItemChanged(val);

test/UnitTest/Components/AutoFillTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public void OnGetDisplayText_Ok()
194194
pb.Add(a => a.OnGetDisplayText, foo => foo?.Name);
195195
});
196196
var input = cut.Find("input");
197-
Assert.Equal("张三 1000", input.Attributes["value"]?.Value);
197+
Assert.Null(input.Attributes["value"]?.Value);
198198
}
199199

200200
[Fact]

0 commit comments

Comments
 (0)