Skip to content

Commit e8e7bbf

Browse files
authored
fix(AutoComplete): click event also trigger focus event (#4588)
* fix(AutoComplete): remove focus client event * feat: 增加 triggerFocus 方法 * refactor: 精简代码 * refactor: 更改为私有变量 * test: 更新单元测试
1 parent 896a3ba commit e8e7bbf

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public partial class AutoComplete
1818
/// <summary>
1919
/// 获得/设置 当前下拉框是否显示
2020
/// </summary>
21-
private bool IsShown { get; set; }
21+
private bool _isShown;
2222

2323
/// <summary>
2424
/// 获得 组件样式
2525
/// </summary>
2626
protected virtual string? ClassString => CssBuilder.Default("auto-complete")
2727
.AddClass("is-loading", IsLoading)
28-
.AddClass("show", IsShown && !IsPopover)
28+
.AddClass("show", _isShown && !IsPopover)
2929
.Build();
3030

3131
/// <summary>
@@ -149,7 +149,7 @@ protected override void OnParametersSet()
149149
protected override async Task OnBlur()
150150
{
151151
CurrentSelectedItem = "";
152-
IsShown = false;
152+
_isShown = false;
153153

154154
if (OnBlurAsync != null)
155155
{
@@ -185,7 +185,12 @@ protected virtual async Task OnFocus(FocusEventArgs args)
185185
else
186186
{
187187
FilterItems = DisplayCount == null ? Items.ToList() : Items.Take(DisplayCount.Value).ToList();
188-
IsShown = true;
188+
_isShown = true;
189+
190+
if (IsPopover)
191+
{
192+
await InvokeVoidAsync("triggerFocus", Id);
193+
}
189194
}
190195
}
191196
}
@@ -217,7 +222,7 @@ public virtual async Task OnKeyUp(string key)
217222
IsLoading = false;
218223
}
219224

220-
IsShown = true;
225+
_isShown = true;
221226

222227
var source = FilterItems;
223228
if (source.Count > 0)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ export function init(id, invoke) {
1414

1515
if (el.querySelector('[data-bs-toggle="bb.dropdown"]')) {
1616
ac.popover = Popover.init(el, { toggleClass: '[data-bs-toggle="bb.dropdown"]' });
17-
EventHandler.on(input, 'focus', e => {
18-
input.click();
19-
});
2017
}
2118

2219
// debounce
@@ -71,6 +68,11 @@ export function composition(id) {
7168
}
7269
}
7370

71+
export function triggerFocus(id) {
72+
const ac = Data.get(id)
73+
ac.popover?.show();
74+
}
75+
7476
export function dispose(id) {
7577
const ac = Data.get(id)
7678
Data.remove(id)

test/UnitTest/Components/AutoCompleteTest.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.AspNetCore.Components.Web;
7+
using System.Reflection;
78

89
namespace UnitTest.Components;
910

@@ -179,7 +180,7 @@ public void Enter_Test()
179180
}
180181

181182
[Fact]
182-
public void ShowDropdownListOnFocus_Ok()
183+
public async Task ShowDropdownListOnFocus_Ok()
183184
{
184185
IEnumerable<string> items = new List<string>() { "test1", "test2" };
185186
var cut = Context.RenderComponent<AutoComplete>(pb =>
@@ -189,26 +190,20 @@ public void ShowDropdownListOnFocus_Ok()
189190
});
190191

191192
// 获得焦点时不会自动弹出下拉框
192-
cut.InvokeAsync(() =>
193-
{
194-
var input = cut.Find("input");
195-
input.FocusAsync(new FocusEventArgs());
196-
var menu = cut.Find("ul");
197-
Assert.Equal("dropdown-menu", menu.ClassList.ToString());
198-
});
193+
var input = cut.Find("input");
194+
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
195+
var menu = cut.Find("ul");
196+
Assert.Equal("dropdown-menu", menu.ClassList.ToString());
199197

200198
// 获得焦点时自动弹出下拉框
201199
cut.SetParametersAndRender(pb =>
202200
{
203201
pb.Add(a => a.ShowDropdownListOnFocus, true);
204202
});
205-
cut.InvokeAsync(() =>
206-
{
207-
var input = cut.Find("input");
208-
input.FocusAsync(new FocusEventArgs());
209-
var menu = cut.Find("ul");
210-
Assert.Equal("dropdown-menu show", menu.ClassList.ToString());
211-
});
203+
input = cut.Find("input");
204+
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
205+
// IsShown = true
206+
Assert.True(GetShownValue(cut.Instance));
212207

213208
var filter = false;
214209
cut.SetParametersAndRender(pb =>
@@ -222,12 +217,25 @@ public void ShowDropdownListOnFocus_Ok()
222217
});
223218

224219
// trigger focus
225-
cut.InvokeAsync(() =>
220+
input = cut.Find("input");
221+
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
222+
Assert.True(filter);
223+
224+
cut.SetParametersAndRender(pb =>
226225
{
227-
var input = cut.Find("input");
228-
input.FocusAsync(new FocusEventArgs());
229-
Assert.True(filter);
226+
pb.Add(a => a.IsPopover, true);
227+
pb.Add(a => a.OnFocusFilter, false);
230228
});
229+
input = cut.Find("input");
230+
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
231+
// IsShown = true
232+
Assert.True(GetShownValue(cut.Instance));
233+
}
234+
235+
private static bool GetShownValue(AutoComplete instance)
236+
{
237+
var fieldInfo = instance.GetType().GetField("_isShown", BindingFlags.NonPublic | BindingFlags.Instance);
238+
return fieldInfo?.GetValue(instance) is true;
231239
}
232240

233241
[Fact]

0 commit comments

Comments
 (0)