From e83e46abc08af54558cb09efb1414117e0a10f01 Mon Sep 17 00:00:00 2001 From: markosteger Date: Thu, 16 Feb 2023 10:40:42 +0100 Subject: [PATCH] Checkbox - working example -- NumberInput,TextInput - InputAttributes initialization moved to InputBase class --- .../Components/CheckBox.razor | 13 ++++++++++++ .../Components/CheckBox.razor.cs | 21 +++++++++++++++++++ .../Components/CheckBoxGroup.razor | 9 ++++++++ .../Components/CheckBoxGroup.razor.cs | 11 ++++++++++ .../Components/NumberInput.razor.cs | 11 +++++++++- .../Components/TextInput.razor.cs | 13 ++++++------ src/Connected.Components/Models/InputBase.cs | 7 +++++-- 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 src/Connected.Components/Components/CheckBox.razor create mode 100644 src/Connected.Components/Components/CheckBox.razor.cs create mode 100644 src/Connected.Components/Components/CheckBoxGroup.razor create mode 100644 src/Connected.Components/Components/CheckBoxGroup.razor.cs diff --git a/src/Connected.Components/Components/CheckBox.razor b/src/Connected.Components/Components/CheckBox.razor new file mode 100644 index 0000000..899ccaf --- /dev/null +++ b/src/Connected.Components/Components/CheckBox.razor @@ -0,0 +1,13 @@ +@using Connected.Models; + +@inherits InputBase; + +
+@if (Checked) +{ + +} else { + +} + +
\ No newline at end of file diff --git a/src/Connected.Components/Components/CheckBox.razor.cs b/src/Connected.Components/Components/CheckBox.razor.cs new file mode 100644 index 0000000..12bd057 --- /dev/null +++ b/src/Connected.Components/Components/CheckBox.razor.cs @@ -0,0 +1,21 @@ +using Connected.Models; +using Microsoft.AspNetCore.Components; + +namespace Connected.Components; +public partial class CheckBox: InputBase +{ + [Parameter] + public bool Checked { get; set; } = false; + + [Parameter, EditorRequired] + public string Id { get; set; } + + [Parameter] + public EventCallback CheckedChange { get; set; } + public async Task OnChange(ChangeEventArgs args) + { + Checked = (bool)args.Value; + CheckedChange.InvokeAsync(Checked); + } + +} diff --git a/src/Connected.Components/Components/CheckBoxGroup.razor b/src/Connected.Components/Components/CheckBoxGroup.razor new file mode 100644 index 0000000..68efc7c --- /dev/null +++ b/src/Connected.Components/Components/CheckBoxGroup.razor @@ -0,0 +1,9 @@ +@using Connected.Models; + +@inherits InputBase; + +
+
+ @ChildContent +
+
\ No newline at end of file diff --git a/src/Connected.Components/Components/CheckBoxGroup.razor.cs b/src/Connected.Components/Components/CheckBoxGroup.razor.cs new file mode 100644 index 0000000..d1512cb --- /dev/null +++ b/src/Connected.Components/Components/CheckBoxGroup.razor.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Components; + +namespace Connected.Components; +public partial class CheckBoxGroup +{ + [Parameter, EditorRequired] + public string Id { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } +} diff --git a/src/Connected.Components/Components/NumberInput.razor.cs b/src/Connected.Components/Components/NumberInput.razor.cs index 01a81c8..17f6a14 100644 --- a/src/Connected.Components/Components/NumberInput.razor.cs +++ b/src/Connected.Components/Components/NumberInput.razor.cs @@ -228,5 +228,14 @@ public partial class NumberInput:InputBase where NumberType : INumbe await base.OnParametersSetAsync(); } - #endregion + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + if (Required) + { + if (!InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true); + } + } + + #endregion } \ No newline at end of file diff --git a/src/Connected.Components/Components/TextInput.razor.cs b/src/Connected.Components/Components/TextInput.razor.cs index e7e5354..b8d4268 100644 --- a/src/Connected.Components/Components/TextInput.razor.cs +++ b/src/Connected.Components/Components/TextInput.razor.cs @@ -56,14 +56,13 @@ public partial class TextInput: InputBase } - protected override async Task OnParametersSetAsync() - { - if (base.InputAttributes is null) base.InputAttributes = new(); - if (base.Required) + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + if (Required) { - if (base.InputAttributes.ContainsKey("required")) base.InputAttributes.Add("required", true); + if (!InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true); } - await base.OnInitializedAsync(); - } + } } \ No newline at end of file diff --git a/src/Connected.Components/Models/InputBase.cs b/src/Connected.Components/Models/InputBase.cs index a95722c..f6bda7a 100644 --- a/src/Connected.Components/Models/InputBase.cs +++ b/src/Connected.Components/Models/InputBase.cs @@ -1,7 +1,5 @@ using Connected.Utilities; using Microsoft.AspNetCore.Components; -using static Connected.Colors; -using static System.Net.Mime.MediaTypeNames; namespace Connected.Models; public class InputBase : ComponentBase @@ -124,4 +122,9 @@ public class InputBase : ComponentBase [Parameter] public string Placeholder { get; set; } = string.Empty; + + protected override async Task OnInitializedAsync() + { + if (InputAttributes is null) InputAttributes = new(); + } }