Skip to content

Commit e54e37b

Browse files
celadarisArgoZhang
andauthored
refactor(AutoComplete): improved the TriggerChange/TriggerFilter method to update CurrentValue (#5531)
* Update AutoComplete.razor.cs fixes the same problem I was having in issue #5110 * chore: bump version 9.4.7-beta01 * doc: update comment to english --------- Co-authored-by: Argo Zhang <argo@live.ca>
1 parent 5ddb33d commit e54e37b

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.4.6</Version>
4+
<Version>9.4.7-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

+36-20
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,82 @@
88
namespace BootstrapBlazor.Components;
99

1010
/// <summary>
11-
/// AutoComplete 组件
11+
/// AutoComplete component
1212
/// </summary>
1313
public partial class AutoComplete
1414
{
1515
/// <summary>
16-
/// 获得 组件样式
16+
/// Gets the component style
1717
/// </summary>
1818
private string? ClassString => CssBuilder.Default("auto-complete")
1919
.AddClassFromAttributes(AdditionalAttributes)
2020
.Build();
2121

2222
/// <summary>
23-
/// 获得/设置 通过输入字符串获得匹配数据集合
23+
/// Gets or sets the collection of matching data obtained by inputting a string
2424
/// </summary>
2525
[Parameter]
2626
[NotNull]
2727
public IEnumerable<string>? Items { get; set; }
2828

2929
/// <summary>
30-
/// 获得/设置 自定义集合过滤规则 默认 null
30+
/// Gets or sets custom collection filtering rules, default is null
3131
/// </summary>
3232
[Parameter]
3333
public Func<string, Task<IEnumerable<string>>>? OnCustomFilter { get; set; }
3434

3535
/// <summary>
36-
/// 获得/设置 图标
36+
/// Gets or sets the icon
3737
/// </summary>
3838
[Parameter]
3939
public string? Icon { get; set; }
4040

4141
/// <summary>
42-
/// 获得/设置 加载图标
42+
/// Gets or sets the loading icon
4343
/// </summary>
4444
[Parameter]
4545
public string? LoadingIcon { get; set; }
4646

4747
/// <summary>
48-
/// 获得/设置 匹配数据时显示的数量
48+
/// Gets or sets the number of items to display when matching data
4949
/// </summary>
5050
[Parameter]
5151
[NotNull]
5252
public int? DisplayCount { get; set; }
5353

5454
/// <summary>
55-
/// 获得/设置 是否开启模糊查询,默认为 false
55+
/// Gets or sets whether to enable fuzzy search, default is false
5656
/// </summary>
5757
[Parameter]
5858
public bool IsLikeMatch { get; set; }
5959

6060
/// <summary>
61-
/// 获得/设置 匹配时是否忽略大小写,默认为 true
61+
/// Gets or sets whether to ignore case when matching, default is true
6262
/// </summary>
6363
[Parameter]
6464
public bool IgnoreCase { get; set; } = true;
6565

6666
/// <summary>
67-
/// 获得/设置 获得焦点时是否展开下拉候选菜单 默认 true
67+
/// Gets or sets whether to expand the dropdown candidate menu when focused, default is true
6868
/// </summary>
6969
[Parameter]
7070
public bool ShowDropdownListOnFocus { get; set; } = true;
7171

7272
/// <summary>
73-
/// 获得/设置 是否显示无匹配数据选项 默认 true 显示
73+
/// Gets or sets whether to show the no matching data option, default is true
7474
/// </summary>
7575
[Parameter]
7676
public bool ShowNoDataTip { get; set; } = true;
7777

7878
/// <summary>
79-
/// IStringLocalizer 服务实例
79+
/// IStringLocalizer service instance
8080
/// </summary>
8181
[Inject]
8282
[NotNull]
8383
private IStringLocalizer<AutoComplete>? Localizer { get; set; }
8484

8585
/// <summary>
86-
/// 获得 获得焦点自动显示下拉框设置字符串
86+
/// Gets the string setting for automatically displaying the dropdown when focused
8787
/// </summary>
8888
private string? ShowDropdownListOnFocusString => ShowDropdownListOnFocus ? "true" : null;
8989

@@ -115,7 +115,7 @@ protected override void OnParametersSet()
115115
}
116116

117117
/// <summary>
118-
/// 鼠标点击候选项时回调此方法
118+
/// Callback method when a candidate item is clicked
119119
/// </summary>
120120
private async Task OnClickItem(string val)
121121
{
@@ -129,12 +129,15 @@ private async Task OnClickItem(string val)
129129
private List<string> Rows => _filterItems ?? [.. Items];
130130

131131
/// <summary>
132-
/// TriggerFilter 方法
132+
/// TriggerFilter method
133133
/// </summary>
134134
/// <param name="val"></param>
135135
[JSInvokable]
136136
public override async Task TriggerFilter(string val)
137137
{
138+
// Store the current input value to prevent it from being overwritten
139+
var currentInputValue = val;
140+
138141
if (OnCustomFilter != null)
139142
{
140143
var items = await OnCustomFilter(val);
@@ -157,20 +160,33 @@ public override async Task TriggerFilter(string val)
157160
{
158161
_filterItems = [.. _filterItems.Take(DisplayCount.Value)];
159162
}
160-
await TriggerChange(val);
163+
164+
// Use currentInputValue here instead of potentially stale val
165+
CurrentValue = currentInputValue;
166+
167+
// Only trigger StateHasChanged if no binding is present
168+
if (!ValueChanged.HasDelegate)
169+
{
170+
StateHasChanged();
171+
}
161172
}
162173

163174
/// <summary>
164-
/// TriggerChange 方法
175+
/// TriggerChange method
165176
/// </summary>
166177
/// <param name="val"></param>
167178
[JSInvokable]
168179
public override Task TriggerChange(string val)
169180
{
170-
CurrentValue = val;
171-
if (!ValueChanged.HasDelegate)
181+
// Only update CurrentValue if the value has actually changed
182+
// This prevents overwriting the user's input
183+
if (CurrentValue != val)
172184
{
173-
StateHasChanged();
185+
CurrentValue = val;
186+
if (!ValueChanged.HasDelegate)
187+
{
188+
StateHasChanged();
189+
}
174190
}
175191
return Task.CompletedTask;
176192
}

0 commit comments

Comments
 (0)