From 33311058119b06c00436f18709a2c4f77c8f29c4 Mon Sep 17 00:00:00 2001 From: markosteger Date: Fri, 24 Mar 2023 13:42:47 +0100 Subject: [PATCH] Added Base component with ID used for state management. All other components must inherit from it To get Id property --- .../Program.cs | 1 + src/Connected.Components/Classes/InputBase.cs | 7 +- src/Connected.Components/Classes/State.cs | 18 +++++ .../Components/Base.razor | 5 ++ .../Components/Base.razor.cs | 20 ++++++ .../Components/Button.razor | 4 +- .../Components/Button.razor.cs | 2 +- .../Components/CheckBox.razor | 2 +- .../Components/CheckBox.razor.cs | 7 -- .../Components/Chip.razor | 4 +- .../Components/Chip.razor.cs | 6 +- .../Components/DatePicker.razor | 2 + .../Components/DatePicker.razor.cs | 2 +- .../Components/Dropdown.razor | 6 +- .../Components/Dropdown.razor.cs | 1 - .../Components/FormWizard.razor | 6 +- .../Components/FormWizard.razor.cs | 12 ++-- .../Components/FormWizardStep.razor | 4 +- .../Components/FormWizardStep.razor.cs | 9 +-- .../Components/Glyph.razor | 4 +- .../Components/Glyph.razor.cs | 2 +- .../Components/Grid.razor | 2 + .../Components/Grid.razor.cs | 3 +- .../Components/GridRow.razor | 2 + .../Components/GridRow.razor.cs | 2 +- .../Components/Link.razor | 4 +- .../Components/Link.razor.cs | 3 +- .../Components/ModalDialog.razor | 3 + .../Components/ModalDialog.razor.cs | 3 +- .../Components/NumberStepper.razor | 2 + .../Components/NumberStepper.razor.cs | 2 +- .../Components/Radio.razor.cs | 3 - .../Components/RadioGroup.razor | 2 + .../Components/RadioGroup.razor.cs | 2 +- .../Components/ToggleInput.razor.cs | 3 - .../Services/ServiceCollectionExtensions.cs | 3 + .../Services/StateManagerService.cs | 71 +++++++++++++++++++ 37 files changed, 178 insertions(+), 56 deletions(-) create mode 100644 src/Connected.Components/Classes/State.cs create mode 100644 src/Connected.Components/Components/Base.razor create mode 100644 src/Connected.Components/Components/Base.razor.cs create mode 100644 src/Connected.Components/Services/StateManagerService.cs diff --git a/src/Connected.Components.Showcase.Runner/Program.cs b/src/Connected.Components.Showcase.Runner/Program.cs index 370ca59..2230c89 100644 --- a/src/Connected.Components.Showcase.Runner/Program.cs +++ b/src/Connected.Components.Showcase.Runner/Program.cs @@ -14,6 +14,7 @@ internal class Program builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddModalDialogService(); + builder.Services.AddStateManagementService(); await builder.Build().RunAsync(); } diff --git a/src/Connected.Components/Classes/InputBase.cs b/src/Connected.Components/Classes/InputBase.cs index 60ff5e3..44ef62c 100644 --- a/src/Connected.Components/Classes/InputBase.cs +++ b/src/Connected.Components/Classes/InputBase.cs @@ -1,8 +1,9 @@ -using Connected.Utilities; +using Connected.Components; +using Connected.Utilities; using Microsoft.AspNetCore.Components; namespace Connected.Models; -public class InputBase : ComponentBase +public class InputBase : Base { [Parameter] public string Class { get; set; } = string.Empty; @@ -72,7 +73,7 @@ public class InputBase : ComponentBase /// Fired when the text value changes. /// [Parameter] public EventCallback TextChanged { get; set; } - public string Text { get; set; } + public string Text { get; set; } = string.Empty; protected virtual async Task SetTextAsync(string text) { diff --git a/src/Connected.Components/Classes/State.cs b/src/Connected.Components/Classes/State.cs new file mode 100644 index 0000000..fce7666 --- /dev/null +++ b/src/Connected.Components/Classes/State.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Components; + +namespace Connected.Classes +{ + public class State + { + public Type? ComponentType { get; set; } + public string Id { get; set; } + public Dictionary? Values { get; set; } + + public State() + { + ComponentType = null; + Id=string.Empty; + Values = new(); + } + } +} \ No newline at end of file diff --git a/src/Connected.Components/Components/Base.razor b/src/Connected.Components/Components/Base.razor new file mode 100644 index 0000000..ebeb06c --- /dev/null +++ b/src/Connected.Components/Components/Base.razor @@ -0,0 +1,5 @@ +@inherits ComponentBase + +@*Base class for all components +It contains unique ID for the component used in +saving state of the component*@ diff --git a/src/Connected.Components/Components/Base.razor.cs b/src/Connected.Components/Components/Base.razor.cs new file mode 100644 index 0000000..dd9a782 --- /dev/null +++ b/src/Connected.Components/Components/Base.razor.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Components; + +namespace Connected.Components +{ + public partial class Base : ComponentBase + { + /// + /// Unique ID for the component used in state management + /// Every component should inherit from Base class + /// + [Parameter, EditorRequired] + public string Id { get; set; } = Guid.NewGuid().ToString(); + + protected override void OnInitialized() + { + if (string.IsNullOrEmpty(Id)) + Id = Guid.NewGuid().ToString(); + } + } +} \ No newline at end of file diff --git a/src/Connected.Components/Components/Button.razor b/src/Connected.Components/Components/Button.razor index 1465f49..078981e 100644 --- a/src/Connected.Components/Components/Button.razor +++ b/src/Connected.Components/Components/Button.razor @@ -1,4 +1,6 @@ -