From 25ba1e9b3c3ebf7f29c74c477bf5f787a17d66f9 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:07:45 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=97=A0=E6=B3=95=E5=88=87=E6=8D=A2=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Select/Select.razor.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/BootstrapBlazor/Components/Select/Select.razor.cs b/src/BootstrapBlazor/Components/Select/Select.razor.cs index 3209f32a4da..9dfdc82250f 100644 --- a/src/BootstrapBlazor/Components/Select/Select.razor.cs +++ b/src/BootstrapBlazor/Components/Select/Select.razor.cs @@ -264,7 +264,7 @@ private SelectedItem? SelectedRow var item = Rows.Find(i => i.Value == CurrentValueAsString) ?? Rows.Find(i => i.Active) ?? Rows.Where(i => !i.IsDisabled).FirstOrDefault() - ?? GetVirtualizeItem(); + ?? GetVirtualizeItem(CurrentValueAsString); if (item != null) { @@ -376,8 +376,8 @@ protected override bool TryParseValueFromString(string value, [MaybeNullWhen(fal private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue result, out string? validationErrorMessage) { - SelectedItem = Items.FirstOrDefault(i => i.Value == value) - ?? GetVirtualizeItem(); + SelectedItem = Rows.FirstOrDefault(i => i.Value == value) + ?? GetVirtualizeItem(value); // support SelectedItem? type result = SelectedItem != null ? (TValue)(object)SelectedItem : default; @@ -385,13 +385,14 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue return SelectedItem != null; } - private SelectedItem? GetVirtualizeItem() + private SelectedItem? GetVirtualizeItem(string value) { - return OnQueryAsync == null ? null : GetSelectedItem(); - - SelectedItem? GetSelectedItem() => ValueType == typeof(SelectedItem) - ? (SelectedItem)(object)Value - : new SelectedItem(CurrentValueAsString, DefaultVirtualizeItemText ?? CurrentValueAsString); + SelectedItem? item = null; + if (_result.Items != null) + { + item = _result.Items.FirstOrDefault(i => i.Value == value); + } + return item ?? new SelectedItem(value, DefaultVirtualizeItemText ?? value); } /// From 9e884532f84d628c18479b0e5bfdaef83a778242 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:15:18 +0800 Subject: [PATCH 2/6] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=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/SelectTest.cs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/test/UnitTest/Components/SelectTest.cs b/test/UnitTest/Components/SelectTest.cs index da97baee2d3..015d83b1a59 100644 --- a/test/UnitTest/Components/SelectTest.cs +++ b/test/UnitTest/Components/SelectTest.cs @@ -747,7 +747,7 @@ public async Task IsVirtualize_OnQueryAsync_Clearable_Ok() } [Fact] - public void IsVirtualize_BindValue() + public async Task IsVirtualize_BindValue() { var value = new SelectedItem("3", "Test 3"); var cut = Context.RenderComponent>(pb => @@ -772,24 +772,21 @@ public void IsVirtualize_BindValue() }); }); - cut.InvokeAsync(() => - { - var input = cut.Find(".form-select"); - Assert.Equal("Test 3", input.GetAttribute("value")); - }); - cut.Contains("Test 3"); + var input = cut.Find(".form-select"); + Assert.Equal("3", input.GetAttribute("value")); + var select = cut.Instance; Assert.Equal("3", select.Value?.Value); - cut.InvokeAsync(() => + var item = cut.Find(".dropdown-item"); + await cut.InvokeAsync(() => { - var item = cut.Find(".dropdown-item"); item.Click(); - Assert.Equal("1", value.Value); - - var input = cut.Find(".form-select"); - Assert.Equal("Test1", input.GetAttribute("value")); }); + Assert.Equal("1", value.Value); + + input = cut.Find(".form-select"); + Assert.Equal("Test1", input.GetAttribute("value")); } [Fact] From 114d3ad2681c0c6a231dd2b89d6903d5d1bb1d05 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:21:02 +0800 Subject: [PATCH 3/6] =?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/Select/Select.razor.cs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/BootstrapBlazor/Components/Select/Select.razor.cs b/src/BootstrapBlazor/Components/Select/Select.razor.cs index 9dfdc82250f..117d63c87b9 100644 --- a/src/BootstrapBlazor/Components/Select/Select.razor.cs +++ b/src/BootstrapBlazor/Components/Select/Select.razor.cs @@ -266,17 +266,14 @@ private SelectedItem? SelectedRow ?? Rows.Where(i => !i.IsDisabled).FirstOrDefault() ?? GetVirtualizeItem(CurrentValueAsString); - if (item != null) + if (_init && DisableItemChangedWhenFirstRender) { - if (_init && DisableItemChangedWhenFirstRender) - { - } - else - { - _ = SelectedItemChanged(item); - _init = false; - } + } + else + { + _ = SelectedItemChanged(item); + _init = false; } return item; } @@ -380,12 +377,12 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue ?? GetVirtualizeItem(value); // support SelectedItem? type - result = SelectedItem != null ? (TValue)(object)SelectedItem : default; + result = (TValue)(object)SelectedItem; validationErrorMessage = ""; return SelectedItem != null; } - private SelectedItem? GetVirtualizeItem(string value) + private SelectedItem GetVirtualizeItem(string value) { SelectedItem? item = null; if (_result.Items != null) From bd5e3ab83b5869f87c6e526db4d55b360eee17d7 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:31:01 +0800 Subject: [PATCH 4/6] =?UTF-8?q?Revert=20"refactor:=20=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E4=BB=A3=E7=A0=81"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 114d3ad2681c0c6a231dd2b89d6903d5d1bb1d05. --- .../Components/Select/Select.razor.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor/Components/Select/Select.razor.cs b/src/BootstrapBlazor/Components/Select/Select.razor.cs index 117d63c87b9..9dfdc82250f 100644 --- a/src/BootstrapBlazor/Components/Select/Select.razor.cs +++ b/src/BootstrapBlazor/Components/Select/Select.razor.cs @@ -266,14 +266,17 @@ private SelectedItem? SelectedRow ?? Rows.Where(i => !i.IsDisabled).FirstOrDefault() ?? GetVirtualizeItem(CurrentValueAsString); - if (_init && DisableItemChangedWhenFirstRender) + if (item != null) { + if (_init && DisableItemChangedWhenFirstRender) + { - } - else - { - _ = SelectedItemChanged(item); - _init = false; + } + else + { + _ = SelectedItemChanged(item); + _init = false; + } } return item; } @@ -377,12 +380,12 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue ?? GetVirtualizeItem(value); // support SelectedItem? type - result = (TValue)(object)SelectedItem; + result = SelectedItem != null ? (TValue)(object)SelectedItem : default; validationErrorMessage = ""; return SelectedItem != null; } - private SelectedItem GetVirtualizeItem(string value) + private SelectedItem? GetVirtualizeItem(string value) { SelectedItem? item = null; if (_result.Items != null) From 1b52b9d74d7b90e40c8d3e4ffebe8afa2e0da231 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:31:30 +0800 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Select/Select.razor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/Select/Select.razor.cs b/src/BootstrapBlazor/Components/Select/Select.razor.cs index 9dfdc82250f..7ebddb0dda0 100644 --- a/src/BootstrapBlazor/Components/Select/Select.razor.cs +++ b/src/BootstrapBlazor/Components/Select/Select.razor.cs @@ -390,9 +390,9 @@ private bool TryParseSelectItem(string value, [MaybeNullWhen(false)] out TValue SelectedItem? item = null; if (_result.Items != null) { - item = _result.Items.FirstOrDefault(i => i.Value == value); + item = _result.Items.FirstOrDefault(i => i.Value == value) ?? new SelectedItem(value, DefaultVirtualizeItemText ?? value); } - return item ?? new SelectedItem(value, DefaultVirtualizeItemText ?? value); + return item; } /// From 4048d6b6bbefdff2b6e8f718b985bb0a8752b694 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Sat, 14 Dec 2024 19:36:40 +0800 Subject: [PATCH 6/6] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=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/SelectTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/UnitTest/Components/SelectTest.cs b/test/UnitTest/Components/SelectTest.cs index 015d83b1a59..58570fe5621 100644 --- a/test/UnitTest/Components/SelectTest.cs +++ b/test/UnitTest/Components/SelectTest.cs @@ -773,7 +773,7 @@ public async Task IsVirtualize_BindValue() }); var input = cut.Find(".form-select"); - Assert.Equal("3", input.GetAttribute("value")); + Assert.Null(input.GetAttribute("value")); var select = cut.Instance; Assert.Equal("3", select.Value?.Value);