From 0a8d91d98e184d39b277ae606a8a246622048a1d Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:08:02 +0800 Subject: [PATCH 01/13] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=20checkbox=20=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Checkbox/Checkbox.razor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor index 1a8b24dcc7d..a38b7378184 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor @@ -20,7 +20,7 @@ else @code { RenderFragment RenderCheckbox => @
- + @if (IsShowAfterLabel) { @RenderLabel From d395026b5bc4e578796b3dc0bab2091210fcbda8 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:08:50 +0800 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20TriggerClick?= =?UTF-8?q?=20=E9=80=BB=E8=BE=91=E6=94=B9=E4=B8=BA=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Checkbox/Checkbox.razor.cs | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.cs b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.cs index 9c71e0b5071..864a00cbff5 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.cs +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.cs @@ -157,13 +157,18 @@ protected override async Task OnAfterRenderAsync(bool firstRender) /// /// /// - protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new { Callback = nameof(TriggerOnBeforeStateChanged) }); + protected override Task InvokeInitAsync() => InvokeVoidAsync("init", Id, Interop, new + { + TriggerOnBeforeStateChanged = nameof(TriggerOnBeforeStateChanged), + TriggerClick = nameof(TriggerClick), + SyncStateCallback = nameof(SyncStateCallback) + }); /// /// 触发 OnBeforeStateChanged 回调方法 由 JavaScript 调用 /// [JSInvokable] - public async Task TriggerOnBeforeStateChanged() + public async ValueTask TriggerOnBeforeStateChanged() { if (OnBeforeStateChanged != null) { @@ -171,32 +176,37 @@ public async Task TriggerOnBeforeStateChanged() var ret = await OnBeforeStateChanged(state); if (ret) { - var render = await InternalStateChanged(state); - if (render) - { - StateHasChanged(); - } + await TriggerClick(); } } } /// - /// 点击选择框方法 + /// 同步 值方法 由 JavaScript 调用 + /// + /// + /// + [JSInvokable] + public ValueTask SyncStateCallback(CheckboxState state) + { + State = state; + return ValueTask.CompletedTask; + } + + /// + /// 触发 Click 方法 由 JavaScript 调用 /// - private async Task OnToggleClick() + /// + [JSInvokable] + public async ValueTask TriggerClick() { - if (!IsDisabled) + var render = await InternalStateChanged(State == CheckboxState.Checked ? CheckboxState.UnChecked : CheckboxState.Checked); + if (render) { - var render = await InternalStateChanged(State == CheckboxState.Checked ? CheckboxState.UnChecked : CheckboxState.Checked); - if (render) - { - StateHasChanged(); - } + StateHasChanged(); } } - private bool TriggerClick => !IsDisabled && OnBeforeStateChanged == null; - /// /// 此变量为了提高性能,避免循环更新 /// From edd14885d22f1e8eebc7041067d9e0bd1c6af006 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:09:13 +0800 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E8=84=9A=E6=9C=AC=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Checkbox/Checkbox.razor.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js index 9df7597c479..2715fc4c4d5 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js @@ -8,12 +8,27 @@ export function init(id, invoke, options) { } EventHandler.on(el, 'click', async e => { - e.preventDefault(); + e.stopPropagation(); + const state = el.getAttribute("data-bb-state"); const trigger = el.getAttribute("data-bb-trigger-before"); + + if (state) { + el.removeAttribute('data-bb-state'); + invoke.invokeMethodAsync(options.syncStateCallback, parseInt(state)); + + if (trigger !== "true") { + return; + } + } + if (trigger === 'true') { - await invoke.invokeMethodAsync(options.callback); + e.preventDefault(); + await invoke.invokeMethodAsync(options.triggerOnBeforeStateChanged); + return; } + + await invoke.invokeMethodAsync(options.triggerClick); }); } From 3a8ceea3bf628216062beb6f2a40fab04282344c Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:11:36 +0800 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=88=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Checkbox/Checkbox.razor.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js index 2715fc4c4d5..1dbfbe24e52 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js @@ -15,7 +15,14 @@ export function init(id, invoke, options) { if (state) { el.removeAttribute('data-bb-state'); - invoke.invokeMethodAsync(options.syncStateCallback, parseInt(state)); + await invoke.invokeMethodAsync(options.syncStateCallback, parseInt(state)); + + if (state === "1") { + el.parentElement.classList.add("is-checked"); + } + else { + el.parentElement.classList.remove("is-checked"); + } if (trigger !== "true") { return; From 5ddceaacf6f7b3b4308e84247680ffab9363d44d Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:13:44 +0800 Subject: [PATCH 05/13] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=88=B6?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js index 1dbfbe24e52..3a792af3efa 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js @@ -17,13 +17,6 @@ export function init(id, invoke, options) { el.removeAttribute('data-bb-state'); await invoke.invokeMethodAsync(options.syncStateCallback, parseInt(state)); - if (state === "1") { - el.parentElement.classList.add("is-checked"); - } - else { - el.parentElement.classList.remove("is-checked"); - } - if (trigger !== "true") { return; } From aec7b421e5cad2c4dd54d51167ceaee43cd176eb Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 16:23:12 +0800 Subject: [PATCH 06/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=A0=B7=E5=BC=8F=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js index 3a792af3efa..6e99acb36e9 100644 --- a/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js +++ b/src/BootstrapBlazor/Components/Checkbox/Checkbox.razor.js @@ -17,6 +17,13 @@ export function init(id, invoke, options) { el.removeAttribute('data-bb-state'); await invoke.invokeMethodAsync(options.syncStateCallback, parseInt(state)); + if (state === "1") { + el.parentElement.classList.remove('is-checked'); + } + else { + el.parentElement.classList.add('is-checked'); + } + if (trigger !== "true") { return; } From 1e4b49707eb71d4a2e24f396302db857c8a07f46 Mon Sep 17 00:00:00 2001 From: Argo-AsicoTech Date: Thu, 7 Nov 2024 18:50:37 +0800 Subject: [PATCH 07/13] =?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/CheckboxListTest.cs | 18 ++-- test/UnitTest/Components/ConsoleTest.cs | 5 +- test/UnitTest/Components/TableDialogTest.cs | 10 +- test/UnitTest/Components/TableDrawerTest.cs | 10 +- test/UnitTest/Components/TableTest.cs | 104 +++++++++---------- test/UnitTest/Components/TreeViewTest.cs | 9 +- 6 files changed, 78 insertions(+), 78 deletions(-) diff --git a/test/UnitTest/Components/CheckboxListTest.cs b/test/UnitTest/Components/CheckboxListTest.cs index 999fbac81d3..92db69cc5f6 100644 --- a/test/UnitTest/Components/CheckboxListTest.cs +++ b/test/UnitTest/Components/CheckboxListTest.cs @@ -208,7 +208,7 @@ public void CheckboxItemClass_Ok() } [Fact] - public void StringValue_Ok() + public async Task StringValue_Ok() { var cut = Context.RenderComponent>(builder => { @@ -236,13 +236,13 @@ public void StringValue_Ok() }); }); // 字符串值选中事件 - var item = cut.Find(".form-check-input"); - item.Click(); + var item = cut.FindComponent>(); + await cut.InvokeAsync(item.Instance.TriggerClick); Assert.True(selected); } [Fact] - public void OnSelectedChanged_Ok() + public async Task OnSelectedChanged_Ok() { var selected = false; var foo = Foo.Generate(Localizer); @@ -257,8 +257,8 @@ public void OnSelectedChanged_Ok() }); }); - var item = cut.Find(".form-check-input"); - item.Click(); + var item = cut.FindComponent>(); + await cut.InvokeAsync(item.Instance.TriggerClick); Assert.True(selected); } @@ -274,7 +274,7 @@ public void EnumValue_Ok() } [Fact] - public void IntValue_Ok() + public async Task IntValue_Ok() { var ret = new List(); var selectedIntValues = new List { 1, 2 }; @@ -292,8 +292,8 @@ public void IntValue_Ok() return Task.CompletedTask; }); }); - var item = cut.Find(".form-check-input"); - item.Click(); + var item = cut.FindComponent>(); + await cut.InvokeAsync(item.Instance.TriggerClick); // 选中 2 Assert.Equal(2, ret.First()); diff --git a/test/UnitTest/Components/ConsoleTest.cs b/test/UnitTest/Components/ConsoleTest.cs index ef00230fbf7..a09d805f431 100644 --- a/test/UnitTest/Components/ConsoleTest.cs +++ b/test/UnitTest/Components/ConsoleTest.cs @@ -195,7 +195,7 @@ public void LightTitle_OK() } [Fact] - public void ClickAutoScroll_OK() + public async Task ClickAutoScroll_OK() { var cut = Context.RenderComponent(builder => { @@ -206,7 +206,8 @@ public void ClickAutoScroll_OK() builder.Add(a => a.ShowAutoScroll, true); }); - cut.Find(".card-footer input").Click(); + var item = cut.FindComponent>(); + await cut.InvokeAsync(item.Instance.TriggerClick); var res = cut.Instance.IsAutoScroll; Assert.False(res); } diff --git a/test/UnitTest/Components/TableDialogTest.cs b/test/UnitTest/Components/TableDialogTest.cs index 788a6266629..e867ca70555 100644 --- a/test/UnitTest/Components/TableDialogTest.cs +++ b/test/UnitTest/Components/TableDialogTest.cs @@ -62,8 +62,8 @@ public async Task EditAsync_Ok() var table = cut.FindComponent>(); // 选一个 - var input = cut.Find("tbody tr input"); - await cut.InvokeAsync(() => input.Click()); + var checkbox = cut.FindComponents>()[1]; + await cut.InvokeAsync(checkbox.Instance.TriggerClick); await cut.InvokeAsync(() => table.Instance.EditAsync()); cut.Contains("test-save"); @@ -128,7 +128,7 @@ public async Task EditAsync_Ok() await cut.InvokeAsync(() => table.Instance.AddAsync()); // 编辑弹窗逻辑 - input = cut.Find(".modal-body form input.form-control"); + var input = cut.Find(".modal-body form input.form-control"); await cut.InvokeAsync(() => input.Change("Test_Name")); form = cut.Find(".modal-body form"); @@ -362,8 +362,8 @@ public async Task Required_Ok() var modal = cut.FindComponent(); // 选一个 - var input = cut.Find("tbody tr input"); - await cut.InvokeAsync(() => input.Click()); + var item = cut.FindComponent>(); + await cut.InvokeAsync(item.Instance.TriggerClick); await cut.InvokeAsync(() => table.Instance.AddAsync()); var form = cut.Find(".modal-body form"); diff --git a/test/UnitTest/Components/TableDrawerTest.cs b/test/UnitTest/Components/TableDrawerTest.cs index 75a63c52350..a96d484eb76 100644 --- a/test/UnitTest/Components/TableDrawerTest.cs +++ b/test/UnitTest/Components/TableDrawerTest.cs @@ -43,8 +43,8 @@ public async Task EditAsync_Ok() var table = cut.FindComponent>(); // 选一个 - var input = cut.Find("tbody tr input"); - await cut.InvokeAsync(() => input.Click()); + var checkbox = cut.FindComponents>()[1]; + await cut.InvokeAsync(checkbox.Instance.TriggerClick); await cut.InvokeAsync(() => table.Instance.EditAsync()); // 编辑弹窗逻辑 @@ -102,8 +102,8 @@ public async Task EditAsync_Ok() { pb.Add(a => a.OnSaveAsync, (foo, itemType) => Task.FromResult(false)); }); - input = cut.Find("tbody tr input"); - await cut.InvokeAsync(() => input.Click()); + checkbox = cut.FindComponents>()[1]; + await cut.InvokeAsync(checkbox.Instance.TriggerClick); await cut.InvokeAsync(() => table.Instance.EditAsync()); form = cut.Find("form"); await cut.InvokeAsync(() => form.Submit()); @@ -119,7 +119,7 @@ public async Task EditAsync_Ok() await cut.InvokeAsync(() => table.Instance.AddAsync()); // 编辑弹窗逻辑 - input = cut.Find("form input.form-control"); + var input = cut.Find("form input.form-control"); await cut.InvokeAsync(() => input.Change("Test_Name")); form = cut.Find("form"); diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index d0e3dd92169..f6634013f5c 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. // Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone +using Bunit; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web.Virtualization; @@ -758,9 +759,8 @@ public async Task ShowColumnList_Ok() }); cut.Contains("Test_Column_List"); - var item = cut.Find(".dropdown-item .form-check-input"); - await cut.InvokeAsync(() => item.Click()); - + var item = cut.FindComponents>()[0]; + await cut.InvokeAsync(item.Instance.TriggerClick); Assert.True(show); } @@ -2478,8 +2478,8 @@ public async Task CustomerToolbarButton_Ok() Assert.True(clickWithoutRender); // 选中一行 - var input = cut.Find("tbody tr input"); - await cut.InvokeAsync(() => input.Click()); + var input = cut.FindComponents>()[1]; + await cut.InvokeAsync(input.Instance.TriggerClick); button = cut.FindComponents