From 456313da1d045c2decdef15151d7257ba314dbf1 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 20 Dec 2021 14:44:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20BootstrapDynamicComponent=20?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8F=82=E6=95=B0=E5=AE=9A=E4=B9=89=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Dialog/DialogService.cs | 93 ++++++------------- .../Components/Dialog/EditDialog.razor | 2 +- .../Components/Print/PrintService.cs | 2 +- .../Components/SweetAlert/SwalOption.cs | 24 ++--- .../Components/SweetAlert/SweetAlert.razor.cs | 18 ++-- .../SweetAlert/SweetAlertBody.razor.cs | 45 +++------ .../Utils/BootstrapDynamicComponent.cs | 13 ++- 7 files changed, 69 insertions(+), 128 deletions(-) diff --git a/src/BootstrapBlazor/Components/Dialog/DialogService.cs b/src/BootstrapBlazor/Components/Dialog/DialogService.cs index 6a911def4..3ae57b215 100644 --- a/src/BootstrapBlazor/Components/Dialog/DialogService.cs +++ b/src/BootstrapBlazor/Components/Dialog/DialogService.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; -using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -31,7 +30,7 @@ namespace BootstrapBlazor.Components /// 指定弹窗组件 默认为 null 使用 组件内置弹窗组件 public async Task ShowSearchDialog(SearchDialogOption option, Dialog? dialog = null) { - var parameters = new Dictionary + var parameters = new Dictionary { [nameof(SearchDialog.ShowLabel)] = option.ShowLabel, [nameof(SearchDialog.Items)] = option.Items ?? Utility.GenerateColumns(item => item.Searchable), @@ -54,36 +53,14 @@ namespace BootstrapBlazor.Components } }), [nameof(SearchDialog.RowType)] = option.RowType, - [nameof(SearchDialog.LabelAlign)] = option.LabelAlign + [nameof(SearchDialog.LabelAlign)] = option.LabelAlign, + [nameof(ItemsPerRow)] = option.ItemsPerRow, + [nameof(SearchDialog.ResetButtonText)] = option.ResetButtonText, + [nameof(SearchDialog.QueryButtonText)] = option.QueryButtonText, + [nameof(SearchDialog.Model)] = option.Model, + [nameof(SearchDialog.BodyTemplate)] = option.DialogBodyTemplate }; - - if (option.ItemsPerRow.HasValue) - { - parameters.Add(nameof(ItemsPerRow), option.ItemsPerRow); - } - - if (!string.IsNullOrEmpty(option.ResetButtonText)) - { - parameters.Add(nameof(SearchDialog.ResetButtonText), option.ResetButtonText); - } - - if (!string.IsNullOrEmpty(option.QueryButtonText)) - { - parameters.Add(nameof(SearchDialog.QueryButtonText), option.QueryButtonText); - } - - if (option.Model != null) - { - parameters.Add(nameof(SearchDialog.Model), option.Model); - } - - if (option.DialogBodyTemplate != null) - { - parameters.Add(nameof(SearchDialog.BodyTemplate), option.DialogBodyTemplate); - } - option.Component = BootstrapDynamicComponent.CreateComponent>(parameters); - await Invoke(option, dialog); } @@ -94,7 +71,7 @@ namespace BootstrapBlazor.Components /// public async Task ShowEditDialog(EditDialogOption option, Dialog? dialog = null) { - var parameters = new Dictionary + var parameters = new Dictionary { [nameof(EditDialog.ShowLoading)] = option.ShowLoading, [nameof(EditDialog.ShowLabel)] = option.ShowLabel, @@ -119,36 +96,15 @@ namespace BootstrapBlazor.Components [nameof(EditDialog.RowType)] = option.RowType, [nameof(EditDialog.LabelAlign)] = option.LabelAlign, [nameof(EditDialog.IsTracking)] = option.IsTracking, - [nameof(EditDialog.ItemChangedType)] = option.ItemChangedType + [nameof(EditDialog.ItemChangedType)] = option.ItemChangedType, + [nameof(ItemsPerRow)] = option.ItemsPerRow, + [nameof(EditDialog.CloseButtonText)] = option.CloseButtonText, + [nameof(EditDialog.SaveButtonText)] = option.SaveButtonText, + [nameof(EditDialog.Model)] = option.Model, + [nameof(EditDialog.BodyTemplate)] = option.DialogBodyTemplate }; - if (option.ItemsPerRow.HasValue) - { - parameters.Add(nameof(ItemsPerRow), option.ItemsPerRow); - } - - if (!string.IsNullOrEmpty(option.CloseButtonText)) - { - parameters.Add(nameof(EditDialog.CloseButtonText), option.CloseButtonText); - } - - if (!string.IsNullOrEmpty(option.SaveButtonText)) - { - parameters.Add(nameof(EditDialog.SaveButtonText), option.SaveButtonText); - } - - if (option.Model != null) - { - parameters.Add(nameof(EditDialog.Model), option.Model); - } - - if (option.DialogBodyTemplate != null) - { - parameters.Add(nameof(EditDialog.BodyTemplate), option.DialogBodyTemplate); - } - option.Component = BootstrapDynamicComponent.CreateComponent>(parameters); - await Invoke(option, dialog); } @@ -166,17 +122,24 @@ namespace BootstrapBlazor.Components option.BodyTemplate = builder => { - builder.OpenComponent(0, typeof(TDialog)); - builder.AddMultipleAttributes(1, option.ComponentParamters); - builder.AddComponentReferenceCapture(2, com => resultDialog = (IResultDialog)com); + var index = 0; + builder.OpenComponent(index++, typeof(TDialog)); + if (option.ComponentParamters != null) + { + foreach (var p in option.ComponentParamters) + { + builder.AddAttribute(index++, p.Key, p.Value); + } + } + builder.AddComponentReferenceCapture(index++, com => resultDialog = (IResultDialog)com); builder.CloseComponent(); }; - option.FooterTemplate = BootstrapDynamicComponent.CreateComponent(new Dictionary + option.FooterTemplate = BootstrapDynamicComponent.CreateComponent(new Dictionary { - [nameof(ResultDialogFooter.ButtonCloseText)] = option.ButtonCloseText!, - [nameof(ResultDialogFooter.ButtonNoText)] = option.ButtonNoText!, - [nameof(ResultDialogFooter.ButtonYesText)] = option.ButtonYesText!, + [nameof(ResultDialogFooter.ButtonCloseText)] = option.ButtonCloseText, + [nameof(ResultDialogFooter.ButtonNoText)] = option.ButtonNoText, + [nameof(ResultDialogFooter.ButtonYesText)] = option.ButtonYesText, [nameof(ResultDialogFooter.ShowCloseButton)] = option.ShowCloseButton, [nameof(ResultDialogFooter.ButtonCloseColor)] = option.ButtonCloseColor, [nameof(ResultDialogFooter.ButtonCloseIcon)] = option.ButtonCloseIcon, diff --git a/src/BootstrapBlazor/Components/Dialog/EditDialog.razor b/src/BootstrapBlazor/Components/Dialog/EditDialog.razor index 68765bf61..0af614166 100644 --- a/src/BootstrapBlazor/Components/Dialog/EditDialog.razor +++ b/src/BootstrapBlazor/Components/Dialog/EditDialog.razor @@ -5,7 +5,7 @@ @if (BodyTemplate != null) { - @BodyTemplate?.Invoke(Model) + @BodyTemplate.Invoke(Model) @if (!IsTracking) {
diff --git a/src/BootstrapBlazor/Components/Print/PrintService.cs b/src/BootstrapBlazor/Components/Print/PrintService.cs index 39a0ddc1d..b5d6aa59a 100644 --- a/src/BootstrapBlazor/Components/Print/PrintService.cs +++ b/src/BootstrapBlazor/Components/Print/PrintService.cs @@ -27,7 +27,7 @@ namespace BootstrapBlazor.Components /// /// /// - public async Task PrintAsync(Func> parametersFactory) where TComponent : ComponentBase + public async Task PrintAsync(Func> parametersFactory) where TComponent : ComponentBase { var option = new DialogOption(); var parameters = parametersFactory(option); diff --git a/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs b/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs index 7c8d95fe9..26eade5f7 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs @@ -86,24 +86,18 @@ namespace BootstrapBlazor.Components /// 将参数转换为组件属性方法 /// /// - public List> ToAttributes() + public Dictionary ToAttributes() { - var parameters = new List> + var parameters = new Dictionary { - new(nameof(Size), Size.Medium), - new(nameof(ModalDialog.IsCentered), true), - new(nameof(ModalDialog.IsScrolling), false), - new(nameof(ModalDialog.ShowCloseButton), false), - new(nameof(ShowFooter), false) + [nameof(Size)] = Size.Medium, + [nameof(ModalDialog.IsCentered)] = true, + [nameof(ModalDialog.IsScrolling)] = false, + [nameof(ModalDialog.ShowCloseButton)] = false, + [nameof(ShowFooter)] = false, + [nameof(ModalDialog.Title)] = Title, + [nameof(BodyContext)] = BodyContext }; - if (!string.IsNullOrEmpty(Title)) - { - parameters.Add(new(nameof(ModalDialog.Title), Title)); - } - if (BodyContext != null) - { - parameters.Add(new(nameof(BodyContext), BodyContext)); - } return parameters; } diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs index 4f5ffe82f..172413a1f 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs @@ -38,7 +38,7 @@ namespace BootstrapBlazor.Components private CancellationTokenSource? DelayToken { get; set; } [NotNull] - private List>? DialogParameter { get; set; } + private Dictionary? DialogParameter { get; set; } /// /// OnInitialized 方法 @@ -91,7 +91,7 @@ namespace BootstrapBlazor.Components option.Dialog = ModalContainer; var parameters = option.ToAttributes(); - parameters.Add(new KeyValuePair(nameof(ModalDialog.OnClose), new Func(async () => + parameters.Add(nameof(ModalDialog.OnClose), new Func(async () => { if (IsAutoHide && DelayToken != null) { @@ -101,9 +101,9 @@ namespace BootstrapBlazor.Components DialogParameter = null; await ModalContainer.CloseOrPopDialog(); StateHasChanged(); - }))); + })); - parameters.Add(new(nameof(ModalDialog.BodyTemplate), BootstrapDynamicComponent.CreateComponent(SweetAlertBody.Parse(option)).Render())); + parameters.Add(nameof(ModalDialog.BodyTemplate), BootstrapDynamicComponent.CreateComponent(SweetAlertBody.Parse(option)).Render()); DialogParameter = parameters; IsShowDialog = true; @@ -115,9 +115,13 @@ namespace BootstrapBlazor.Components { if (DialogParameter != null) { - builder.OpenComponent(0); - builder.AddMultipleAttributes(1, DialogParameter); - builder.AddComponentReferenceCapture(2, dialog => + var index = 0; + builder.OpenComponent(index++); + foreach (var p in DialogParameter) + { + builder.AddAttribute(index++, p.Key, p.Value); + } + builder.AddComponentReferenceCapture(index++, dialog => { var modal = (ModalDialog)dialog; ModalContainer.ShowDialog(modal); diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs index 772c3c50e..cd7dad1bf 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs @@ -119,39 +119,20 @@ namespace BootstrapBlazor.Components /// /// /// - internal static IDictionary Parse(SwalOption option) + internal static IDictionary Parse(SwalOption option) => new Dictionary() { - var parameters = new Dictionary() - { - [nameof(SweetAlertBody.Category)] = option.Category, - [nameof(SweetAlertBody.ShowClose)] = option.ShowClose, - [nameof(SweetAlertBody.IsConfirm)] = option.IsConfirm, - [nameof(SweetAlertBody.ShowFooter)] = option.ShowFooter, - [nameof(SweetAlertBody.OnClose)] = new Action(async () => await option.Close(false)), - [nameof(SweetAlertBody.OnConfirm)] = new Action(async () => await option.Close(true)) - }; - if (!string.IsNullOrEmpty(option.Title)) - { - parameters.Add(nameof(SweetAlertBody.Title), option.Title); - } - if (!string.IsNullOrEmpty(option.Content)) - { - parameters.Add(nameof(SweetAlertBody.Content), option.Content); - } - if (option.BodyTemplate != null) - { - parameters.Add(nameof(SweetAlertBody.BodyTemplate), option.BodyTemplate); - } - if (option.FooterTemplate != null) - { - parameters.Add(nameof(SweetAlertBody.FooterTemplate), option.FooterTemplate); - } - if (option.ButtonTemplate != null) - { - parameters.Add(nameof(SweetAlertBody.ButtonTemplate), option.ButtonTemplate); - } - return parameters; - } + [nameof(SweetAlertBody.Category)] = option.Category, + [nameof(SweetAlertBody.ShowClose)] = option.ShowClose, + [nameof(SweetAlertBody.IsConfirm)] = option.IsConfirm, + [nameof(SweetAlertBody.ShowFooter)] = option.ShowFooter, + [nameof(SweetAlertBody.OnClose)] = new Action(async () => await option.Close(false)), + [nameof(SweetAlertBody.OnConfirm)] = new Action(async () => await option.Close(true)), + [nameof(SweetAlertBody.Title)] = option.Title, + [nameof(SweetAlertBody.Content)] = option.Content, + [nameof(SweetAlertBody.BodyTemplate)] = option.BodyTemplate, + [nameof(SweetAlertBody.FooterTemplate)] = option.FooterTemplate, + [nameof(SweetAlertBody.ButtonTemplate)] = option.ButtonTemplate + }; /// /// OnInitialized 方法 diff --git a/src/BootstrapBlazor/Utils/BootstrapDynamicComponent.cs b/src/BootstrapBlazor/Utils/BootstrapDynamicComponent.cs index aeb2517f9..af46fb494 100644 --- a/src/BootstrapBlazor/Utils/BootstrapDynamicComponent.cs +++ b/src/BootstrapBlazor/Utils/BootstrapDynamicComponent.cs @@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Components; using System; using System.Collections.Generic; -using System.Linq; namespace BootstrapBlazor.Components { @@ -17,7 +16,7 @@ namespace BootstrapBlazor.Components /// /// 获得/设置 组件参数集合 /// - private IDictionary Parameters { get; set; } + private IDictionary Parameters { get; set; } /// /// 获得/设置 组件类型 @@ -29,7 +28,7 @@ namespace BootstrapBlazor.Components /// /// /// TCom 组件所需要的参数集合 - public BootstrapDynamicComponent(Type componentType, IDictionary parameters) + public BootstrapDynamicComponent(Type componentType, IDictionary parameters) { ComponentType = componentType; Parameters = parameters; @@ -41,14 +40,14 @@ namespace BootstrapBlazor.Components /// /// TCom 组件所需要的参数集合 /// - public static BootstrapDynamicComponent CreateComponent(IDictionary parameters) where TCom : IComponent => new(typeof(TCom), parameters); + public static BootstrapDynamicComponent CreateComponent(IDictionary parameters) where TCom : IComponent => new(typeof(TCom), parameters); /// /// 创建自定义组件方法 /// /// /// - public static BootstrapDynamicComponent CreateComponent() where TCom : IComponent => CreateComponent(new Dictionary()); + public static BootstrapDynamicComponent CreateComponent() where TCom : IComponent => CreateComponent(new Dictionary()); /// /// 创建组件实例并渲染 @@ -58,9 +57,9 @@ namespace BootstrapBlazor.Components { var index = 0; builder.OpenComponent(index++, ComponentType); - if (Parameters.Any()) + foreach (var p in Parameters) { - builder.AddMultipleAttributes(index++, Parameters); + builder.AddAttribute(index++, p.Key, p.Value); } builder.CloseComponent(); }; -- Gitee From b761ef7286e9ac772cbd15e8257055c759ad70af Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Mon, 20 Dec 2021 14:44:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Shared/App.razor.cs | 4 ++-- src/BootstrapBlazor.Shared/Data/Menus.cs | 2 +- src/BootstrapBlazor.Shared/Pages/Layout.razor.cs | 9 +++------ src/BootstrapBlazor.Shared/Samples/Dialogs.razor.cs | 12 ++++++------ src/BootstrapBlazor.Shared/Samples/Prints.razor.cs | 4 ++-- .../Samples/Timelines.razor.cs | 2 +- .../Shared/MainLayout.razor.cs | 3 +++ src/BootstrapBlazor.Shared/Shared/NavMenu.razor.cs | 2 +- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/BootstrapBlazor.Shared/App.razor.cs b/src/BootstrapBlazor.Shared/App.razor.cs index d51cf4a48..71cb0dd10 100644 --- a/src/BootstrapBlazor.Shared/App.razor.cs +++ b/src/BootstrapBlazor.Shared/App.razor.cs @@ -73,9 +73,9 @@ namespace BootstrapBlazor.Shared #if DEBUG IsAutoHide = false, #endif - ChildContent = BootstrapDynamicComponent.CreateComponent(new Dictionary + ChildContent = BootstrapDynamicComponent.CreateComponent(new Dictionary { - [nameof(CommitItem.Item)] = payload.Entry! + [nameof(CommitItem.Item)] = payload.Entry }).Render() }; await Toast.Show(option); diff --git a/src/BootstrapBlazor.Shared/Data/Menus.cs b/src/BootstrapBlazor.Shared/Data/Menus.cs index 0a9228576..213b94a8b 100644 --- a/src/BootstrapBlazor.Shared/Data/Menus.cs +++ b/src/BootstrapBlazor.Shared/Data/Menus.cs @@ -185,7 +185,7 @@ namespace BootstrapBlazor.Shared }; } - private static BootstrapDynamicComponent BuildDynamicComponent() => BootstrapDynamicComponent.CreateComponent(new Dictionary + private static BootstrapDynamicComponent BuildDynamicComponent() => BootstrapDynamicComponent.CreateComponent(new Dictionary { [nameof(Badge.Color)] = Color.Danger, [nameof(Badge.IsPill)] = true, diff --git a/src/BootstrapBlazor.Shared/Pages/Layout.razor.cs b/src/BootstrapBlazor.Shared/Pages/Layout.razor.cs index 90bcf9e15..cddcf9f06 100644 --- a/src/BootstrapBlazor.Shared/Pages/Layout.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Layout.razor.cs @@ -4,11 +4,8 @@ using BootstrapBlazor.Components; using Microsoft.AspNetCore.Components; -using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; using System.Threading.Tasks; namespace BootstrapBlazor.Shared.Pages @@ -41,7 +38,7 @@ namespace BootstrapBlazor.Shared.Pages Title = "测试弹窗", BodyTemplate = builder => { - builder.AddContent(0, BootstrapDynamicComponent.CreateComponent