From 4b060b0de2826a4820bec333bc36bf4b6195f97b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 20:48:33 +0800 Subject: [PATCH 01/14] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=20SkipEnte?= =?UTF-8?q?r/SkipEsc=20=E4=B8=BA=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AutoFill/AutoFill.razor.cs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index 142146169..3ce4253ac 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -88,6 +88,18 @@ public partial class AutoFill [NotNull] public Func? OnGetDisplayText { get; set; } + /// + /// 获得/设置 是否跳过 Enter 按键处理 默认 false + /// + [Parameter] + public bool SkipEnter { get; set; } + + /// + /// 获得/设置 是否跳过 Esc 按键处理 默认 false + /// + [Parameter] + public bool SkipEsc { get; set; } + /// /// 获得/设置 选项改变回调方法 默认 null /// @@ -171,16 +183,6 @@ public partial class AutoFill } } - /// - /// 获得/设置 是否跳过 Enter 按键处理 默认 false - /// - protected bool SkipEnter { get; set; } - - /// - /// 获得/设置 是否跳过 Esc 按键处理 默认 false - /// - protected bool SkipEsc { get; set; } - /// /// OnKeyUp 方法 /// -- Gitee From 45b7f523faf726c35265bd574cc8c707f00d87c7 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 20:48:59 +0800 Subject: [PATCH 02/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20AutoFill=20?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 75 ++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 test/UnitTest/Components/AutoFillTest.cs diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs new file mode 100644 index 000000000..cf6587c02 --- /dev/null +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -0,0 +1,75 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +using BootstrapBlazor.Shared; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Localization; + +namespace UnitTest.Components; + +public class AutoFillTest : BootstrapBlazorTestBase +{ + private IStringLocalizer Localizer { get; } + + private Foo Model { get; } + + private List Items { get; } + + public AutoFillTest() + { + Localizer = Context.Services.GetRequiredService>(); + Model = Foo.Generate(Localizer); + Items = Foo.GenerateFoo(Localizer, 2); + } + + [Fact] + public void ShowLabel_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.ShowLabel, true); + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.DisplayText, "AutoFillDisplayText"); + }); + Assert.Contains("AutoFillDisplayText", cut.Markup); + } + + [Fact] + public void OnCustomFilter_Ok() + { + var filtered = false; + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.OnCustomFilter, new Func>>(key => + { + filtered = true; + var items = Foo.GenerateFoo(Localizer, 3); + return Task.FromResult(items.AsEnumerable()); + })); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + Assert.True(filtered); + } + + [Fact] + public void KeyUp_Test() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowUp" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowUp" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowUp" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); + } +} -- Gitee From 6616ddae22f2ccefc38fcd574b396d754257cbeb Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 20:59:30 +0800 Subject: [PATCH 03/14] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20isShown?= =?UTF-8?q?=20=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AutoFill/AutoFill.razor.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index 3ce4253ac..a2f4520ec 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -19,7 +19,6 @@ namespace BootstrapBlazor.Components; public partial class AutoFill { private bool _isLoading; - private bool _isShown; private string? _lastFilterText; /// @@ -27,7 +26,6 @@ public partial class AutoFill /// protected virtual string? ClassString => CssBuilder.Default("auto-complete") .AddClass("is-loading", _isLoading) - .AddClass("is-complete", _isShown) .Build(); /// @@ -138,7 +136,15 @@ public partial class AutoFill PlaceHolder ??= Localizer[nameof(PlaceHolder)]; Items ??= Enumerable.Empty(); FilterItems ??= new List(); - OnGetDisplayText ??= v => v?.ToString() ?? ""; + OnGetDisplayText ??= v => + { + string? ret = null; + if (v is not null) + { + ret = v.ToString(); + } + return ret ?? ""; + }; } /// @@ -161,7 +167,6 @@ public partial class AutoFill /// protected async Task OnBlur() { - _isShown = false; if (OnSelectedItemChanged != null && ActiveSelectedItem != null) { await OnSelectedItemChanged(ActiveSelectedItem); @@ -210,10 +215,8 @@ public partial class AutoFill var source = FilterItems; if (source.Any()) { - _isShown = true; - // 键盘向上选择 - if (_isShown && args.Key == "ArrowUp") + if (args.Key == "ArrowUp") { var index = 0; if (ActiveSelectedItem != null) @@ -227,7 +230,7 @@ public partial class AutoFill ActiveSelectedItem = source[index]; CurrentItemIndex = index; } - else if (_isShown && args.Key == "ArrowDown") + else if (args.Key == "ArrowDown") { var index = 0; if (ActiveSelectedItem != null) -- Gitee From 54d373ba974c971600f07f70a045906ab4c584a8 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:00:06 +0800 Subject: [PATCH 04/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Null=20Items?= =?UTF-8?q?=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index cf6587c02..1a0a8212b 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -37,6 +37,13 @@ public class AutoFillTest : BootstrapBlazorTestBase Assert.Contains("AutoFillDisplayText", cut.Markup); } + [Fact] + public void NullItems_Ok() + { + var cut = Context.RenderComponent>(); + Assert.Contains("dropdown-list", cut.Markup); + } + [Fact] public void OnCustomFilter_Ok() { -- Gitee From 033b400189055c1bbf67536dfa591f44179f8484 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:01:12 +0800 Subject: [PATCH 05/14] =?UTF-8?q?Revert=20"refactor:=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=20isShown=20=E5=8F=98=E9=87=8F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6616ddae22f2ccefc38fcd574b396d754257cbeb. --- .../Components/AutoFill/AutoFill.razor.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index a2f4520ec..3ce4253ac 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -19,6 +19,7 @@ namespace BootstrapBlazor.Components; public partial class AutoFill { private bool _isLoading; + private bool _isShown; private string? _lastFilterText; /// @@ -26,6 +27,7 @@ public partial class AutoFill /// protected virtual string? ClassString => CssBuilder.Default("auto-complete") .AddClass("is-loading", _isLoading) + .AddClass("is-complete", _isShown) .Build(); /// @@ -136,15 +138,7 @@ public partial class AutoFill PlaceHolder ??= Localizer[nameof(PlaceHolder)]; Items ??= Enumerable.Empty(); FilterItems ??= new List(); - OnGetDisplayText ??= v => - { - string? ret = null; - if (v is not null) - { - ret = v.ToString(); - } - return ret ?? ""; - }; + OnGetDisplayText ??= v => v?.ToString() ?? ""; } /// @@ -167,6 +161,7 @@ public partial class AutoFill /// protected async Task OnBlur() { + _isShown = false; if (OnSelectedItemChanged != null && ActiveSelectedItem != null) { await OnSelectedItemChanged(ActiveSelectedItem); @@ -215,8 +210,10 @@ public partial class AutoFill var source = FilterItems; if (source.Any()) { + _isShown = true; + // 键盘向上选择 - if (args.Key == "ArrowUp") + if (_isShown && args.Key == "ArrowUp") { var index = 0; if (ActiveSelectedItem != null) @@ -230,7 +227,7 @@ public partial class AutoFill ActiveSelectedItem = source[index]; CurrentItemIndex = index; } - else if (args.Key == "ArrowDown") + else if (_isShown && args.Key == "ArrowDown") { var index = 0; if (ActiveSelectedItem != null) -- Gitee From 32eff11f6690c29109959602c1300f6d17ecbb5b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:06:20 +0800 Subject: [PATCH 06/14] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E4=B8=8D?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E7=9A=84=E6=9D=A1=E4=BB=B6=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index 3ce4253ac..d7d89120f 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -211,9 +211,8 @@ public partial class AutoFill if (source.Any()) { _isShown = true; - // 键盘向上选择 - if (_isShown && args.Key == "ArrowUp") + if (args.Key == "ArrowUp") { var index = 0; if (ActiveSelectedItem != null) @@ -227,7 +226,7 @@ public partial class AutoFill ActiveSelectedItem = source[index]; CurrentItemIndex = index; } - else if (_isShown && args.Key == "ArrowDown") + else if (args.Key == "ArrowDown") { var index = 0; if (ActiveSelectedItem != null) -- Gitee From bc389df2a039dfb4f28049073d4ad54c01e2a1a4 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:20:01 +0800 Subject: [PATCH 07/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Esc=20?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 27 ++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index 1a0a8212b..938a6dd86 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -63,6 +63,33 @@ public class AutoFillTest : BootstrapBlazorTestBase Assert.True(filtered); } + [Fact] + public void Escape_Ok() + { + var escTrigger = false; + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.SkipEsc, true); + pb.Add(a => a.OnEscAsync, new Func(foo => + { + escTrigger = true; + return Task.CompletedTask; + })); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Escape" }); + Assert.False(escTrigger); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.SkipEsc, false); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Escape" }); + Assert.True(escTrigger); + } [Fact] public void KeyUp_Test() { -- Gitee From 6ad8d17a9cb38f131c4622dea2f004168368c608 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:20:20 +0800 Subject: [PATCH 08/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20Enter=20?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index 938a6dd86..bd67cdb59 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -90,6 +90,42 @@ public class AutoFillTest : BootstrapBlazorTestBase cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Escape" }); Assert.True(escTrigger); } + + [Fact] + public void Enter_Ok() + { + var enterTrigger = false; + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.SkipEnter, true); + pb.Add(a => a.OnEnterAsync, new Func(foo => + { + enterTrigger = true; + return Task.CompletedTask; + })); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Enter" }); + Assert.False(enterTrigger); + + Foo? selectedItem = null; + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.SkipEnter, false); + pb.Add(a => a.OnSelectedItemChanged, new Func(foo => + { + selectedItem = foo; + return Task.CompletedTask; + })); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Enter" }); + Assert.True(enterTrigger); + Assert.NotNull(selectedItem); + } + [Fact] public void KeyUp_Test() { -- Gitee From 62ec19463e26b3b4defa0808c567c0d28b692d08 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:23:23 +0800 Subject: [PATCH 09/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20OnSelectedIt?= =?UTF-8?q?emChanged=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index bd67cdb59..ebf5b71d4 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -126,6 +126,25 @@ public class AutoFillTest : BootstrapBlazorTestBase Assert.NotNull(selectedItem); } + [Fact] + public void OnSelectedItemChanged_Ok() + { + Foo? selectedItem = null; + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.OnSelectedItemChanged, new Func(foo => + { + selectedItem = foo; + return Task.CompletedTask; + })); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "t" }); + cut.Find(".dropdown-item").MouseDown(new MouseEventArgs()); + Assert.NotNull(selectedItem); + } + [Fact] public void KeyUp_Test() { -- Gitee From e6a63885693d193b4d810f0abfa439486c0ffc44 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:37:57 +0800 Subject: [PATCH 10/14] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=B2=BE=E7=AE=80=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AutoFill/AutoFill.razor.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index d7d89120f..015aa53e3 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -20,7 +20,6 @@ public partial class AutoFill { private bool _isLoading; private bool _isShown; - private string? _lastFilterText; /// /// 获得 组件样式 @@ -138,7 +137,15 @@ public partial class AutoFill PlaceHolder ??= Localizer[nameof(PlaceHolder)]; Items ??= Enumerable.Empty(); FilterItems ??= new List(); - OnGetDisplayText ??= v => v?.ToString() ?? ""; + OnGetDisplayText ??= v => + { + string? ret = null; + if (v is not null) + { + ret = v.ToString(); + } + return ret ?? ""; + }; } /// @@ -190,10 +197,9 @@ public partial class AutoFill /// protected virtual async Task OnKeyUp(KeyboardEventArgs args) { - if (!_isLoading && _lastFilterText != InputString) + if (!_isLoading) { _isLoading = true; - _lastFilterText = InputString; if (OnCustomFilter != null) { var items = await OnCustomFilter(InputString); -- Gitee From 8b7193342152e37246ded4a186b18cebf0a5fb86 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 21:38:26 +0800 Subject: [PATCH 11/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20GetDisplayTe?= =?UTF-8?q?xt=20=E4=B8=BA=E7=A9=BA=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index ebf5b71d4..5d1736475 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -161,4 +161,39 @@ public class AutoFillTest : BootstrapBlazorTestBase cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); } + + [Fact] + public void OnGetDisplayText_Null() + { + var v = new AutoFillNullStringMock(); + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, v); + pb.Add(a => a.Items, new List + { + new AutoFillNullStringMock() { Value = "1" }, + new AutoFillNullStringMock() { Value = "2" }, + }); + pb.Add(a => a.Template, v => builder => + { + builder.OpenElement(0, "div"); + builder.AddContent(1, v.Value); + builder.CloseElement(); + }); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "1" }); + cut.Find(".dropdown-item").MouseDown(new MouseEventArgs()); + Assert.Equal("1", cut.Instance.Value.Value); + } + + class AutoFillNullStringMock + { + [NotNull] + public string? Value { get; set; } + + public override string? ToString() + { + return null; + } + } } -- Gitee From 854e948191284538a09e032221485e80bf4c030f Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 22:08:24 +0800 Subject: [PATCH 12/14] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/AutoFill/AutoFill.razor.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs index 015aa53e3..6d7626ca9 100644 --- a/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs +++ b/src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs @@ -137,15 +137,7 @@ public partial class AutoFill PlaceHolder ??= Localizer[nameof(PlaceHolder)]; Items ??= Enumerable.Empty(); FilterItems ??= new List(); - OnGetDisplayText ??= v => - { - string? ret = null; - if (v is not null) - { - ret = v.ToString(); - } - return ret ?? ""; - }; + OnGetDisplayText ??= v => v?.ToString() ?? ""; } /// -- Gitee From dc4c7d40fcc5a04b846f884209ba165fd049fb87 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 22:08:44 +0800 Subject: [PATCH 13/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=20DisplayCount?= =?UTF-8?q?=20=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index 5d1736475..41567c067 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -162,6 +162,21 @@ public class AutoFillTest : BootstrapBlazorTestBase cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "ArrowDown" }); } + [Fact] + public void DisplayCount_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.Value, Model); + pb.Add(a => a.Items, Items); + pb.Add(a => a.IsLikeMatch, true); + pb.Add(a => a.IgnoreCase, false); + pb.Add(a => a.DisplayCount, 2); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "Z" }); + Assert.Equal(2, cut.FindAll(".dropdown-item").Count); + } + [Fact] public void OnGetDisplayText_Null() { -- Gitee From 9887c60989c539027d9c01e69107d074be225521 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 1 Feb 2022 22:09:10 +0800 Subject: [PATCH 14/14] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E9=9B=86?= =?UTF-8?q?=E5=90=88=E9=A1=B9=E4=B8=AD=E6=9C=89=E7=A9=BA=E5=80=BC=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/AutoFillTest.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/UnitTest/Components/AutoFillTest.cs b/test/UnitTest/Components/AutoFillTest.cs index 41567c067..2d44f73ad 100644 --- a/test/UnitTest/Components/AutoFillTest.cs +++ b/test/UnitTest/Components/AutoFillTest.cs @@ -181,8 +181,9 @@ public class AutoFillTest : BootstrapBlazorTestBase public void OnGetDisplayText_Null() { var v = new AutoFillNullStringMock(); - var cut = Context.RenderComponent>(pb => + var cut = Context.RenderComponent>(pb => { + pb.Add(a => a.IgnoreCase, true); pb.Add(a => a.Value, v); pb.Add(a => a.Items, new List { @@ -198,7 +199,19 @@ public class AutoFillTest : BootstrapBlazorTestBase }); cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "1" }); cut.Find(".dropdown-item").MouseDown(new MouseEventArgs()); - Assert.Equal("1", cut.Instance.Value.Value); + Assert.Equal("1", cut.Instance.Value!.Value); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.Items, new List + { + null, + new AutoFillNullStringMock() { Value = "2" }, + }); + pb.Add(a => a.Template, (RenderFragment?)null); + }); + cut.Find(".form-control").KeyUp(new KeyboardEventArgs() { Key = "2" }); + Assert.Equal(2, cut.FindAll(".dropdown-item").Count); } class AutoFillNullStringMock -- Gitee