From e83e46abc08af54558cb09efb1414117e0a10f01 Mon Sep 17 00:00:00 2001 From: markosteger Date: Thu, 16 Feb 2023 10:40:42 +0100 Subject: [PATCH 1/4] 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(); + } } From a90cf518a3f70c1b6dafbf18f41d1246f2c5bac3 Mon Sep 17 00:00:00 2001 From: markosteger Date: Thu, 16 Feb 2023 11:32:10 +0100 Subject: [PATCH 2/4] Fixes for InputAttributes in SimpleSelect --- .../Pages/Index.razor | 13 +++++++------ .../Components/SimpleSelect.razor.cs | 15 ++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Connected.Components.Showcase.Runner/Pages/Index.razor b/src/Connected.Components.Showcase.Runner/Pages/Index.razor index 6f94a83..883720b 100644 --- a/src/Connected.Components.Showcase.Runner/Pages/Index.razor +++ b/src/Connected.Components.Showcase.Runner/Pages/Index.razor @@ -6,12 +6,13 @@

SelectedValue: @SelectedValue.ToString()

- - + + + + + + + @code { diff --git a/src/Connected.Components/Components/SimpleSelect.razor.cs b/src/Connected.Components/Components/SimpleSelect.razor.cs index 2baadb7..0490ae6 100644 --- a/src/Connected.Components/Components/SimpleSelect.razor.cs +++ b/src/Connected.Components/Components/SimpleSelect.razor.cs @@ -76,13 +76,18 @@ public partial class SimpleSelect : InputBase protected override async Task OnParametersSetAsync() { - if (base.InputAttributes is null) base.InputAttributes = new(); - if (base.Required) - { - if (base.InputAttributes.ContainsKey("required")) base.InputAttributes.Add("required", true); - } + OriginalItems = Items; if (_searchText.Length>0) await FilterItems(); + await base.OnParametersSetAsync(); + } + + protected override async Task OnInitializedAsync() + { await base.OnInitializedAsync(); + if (Required) + { + if (InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true); + } } } \ No newline at end of file From d688b25589e059f4b7188ca09824cc96db030ad8 Mon Sep 17 00:00:00 2001 From: markosteger Date: Thu, 16 Feb 2023 13:17:06 +0100 Subject: [PATCH 3/4] Radio, RadioGroup --- .../Pages/Index.razor | 23 +++++++++----- .../Components/Radio.razor | 8 +++++ .../Components/Radio.razor.cs | 31 +++++++++++++++++++ .../Components/RadioGroup.razor | 15 +++++++++ .../Components/RadioGroup.razor.cs | 12 +++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/Connected.Components/Components/Radio.razor create mode 100644 src/Connected.Components/Components/Radio.razor.cs create mode 100644 src/Connected.Components/Components/RadioGroup.razor create mode 100644 src/Connected.Components/Components/RadioGroup.razor.cs diff --git a/src/Connected.Components.Showcase.Runner/Pages/Index.razor b/src/Connected.Components.Showcase.Runner/Pages/Index.razor index 883720b..7df32d4 100644 --- a/src/Connected.Components.Showcase.Runner/Pages/Index.razor +++ b/src/Connected.Components.Showcase.Runner/Pages/Index.razor @@ -6,14 +6,21 @@

SelectedValue: @SelectedValue.ToString()

- - - - - - - - + + + + + + + + + + + + + + + @code { int SelectedValue; diff --git a/src/Connected.Components/Components/Radio.razor b/src/Connected.Components/Components/Radio.razor new file mode 100644 index 0000000..18e385e --- /dev/null +++ b/src/Connected.Components/Components/Radio.razor @@ -0,0 +1,8 @@ +@using Connected.Models; + +@inherits InputBase; + +
+ + +
\ No newline at end of file diff --git a/src/Connected.Components/Components/Radio.razor.cs b/src/Connected.Components/Components/Radio.razor.cs new file mode 100644 index 0000000..a86a940 --- /dev/null +++ b/src/Connected.Components/Components/Radio.razor.cs @@ -0,0 +1,31 @@ +using Connected.Models; +using Microsoft.AspNetCore.Components; + +namespace Connected.Components; +public partial class Radio: InputBase +{ + [CascadingParameter] + public RadioGroup? ParentRadioGroup { get; set; } + + [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); + } + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + if (ParentRadioGroup.Disabled) Disabled = true; + if (!InputAttributes.ContainsKey("disabled")) + InputAttributes.Add("disabled", Disabled); + } +} diff --git a/src/Connected.Components/Components/RadioGroup.razor b/src/Connected.Components/Components/RadioGroup.razor new file mode 100644 index 0000000..b842c61 --- /dev/null +++ b/src/Connected.Components/Components/RadioGroup.razor @@ -0,0 +1,15 @@ +@using Connected.Models; + +@inherits InputBase; + + +
+ @if (!string.IsNullOrEmpty(Name)) + { +
@Name
+ } +
+ @ChildContent +
+
+
\ No newline at end of file diff --git a/src/Connected.Components/Components/RadioGroup.razor.cs b/src/Connected.Components/Components/RadioGroup.razor.cs new file mode 100644 index 0000000..7b07360 --- /dev/null +++ b/src/Connected.Components/Components/RadioGroup.razor.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Components; + +namespace Connected.Components; +public partial class RadioGroup +{ + [Parameter, EditorRequired] + public string Name { get; set; } + + [Parameter] + public RenderFragment ChildContent { get; set; } + +} From 7c60aefe3162c8b631122cc9c0e4891b4b65c474 Mon Sep 17 00:00:00 2001 From: markosteger Date: Thu, 16 Feb 2023 14:25:48 +0100 Subject: [PATCH 4/4] SimpleSelect - code cleanup (removed unnecessary blank space, removed unused private methods) --- .../Components/SimpleSelect.razor | 4 +--- .../Components/SimpleSelect.razor.cs | 21 ++----------------- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/src/Connected.Components/Components/SimpleSelect.razor b/src/Connected.Components/Components/SimpleSelect.razor index ec9c163..8fb0f45 100644 --- a/src/Connected.Components/Components/SimpleSelect.razor +++ b/src/Connected.Components/Components/SimpleSelect.razor @@ -7,7 +7,6 @@ @if (Items is not null) {
- @if (IsLabel) @@ -42,8 +41,7 @@ + @bind-value="@SearchText" /> } @foreach (ValueType item in Items) { diff --git a/src/Connected.Components/Components/SimpleSelect.razor.cs b/src/Connected.Components/Components/SimpleSelect.razor.cs index 0490ae6..456a06b 100644 --- a/src/Connected.Components/Components/SimpleSelect.razor.cs +++ b/src/Connected.Components/Components/SimpleSelect.razor.cs @@ -29,30 +29,13 @@ public partial class SimpleSelect : InputBase } } - private string DropDownClass { get; set; } = "drop-down"; - - bool DropDownClicked = false; - - public void DropDownClassToggle() - { - DropDownClicked = !DropDownClicked; - if (DropDownClicked) - { - DropDownClass = ""; - } else - { - DropDownClass = "drop-down"; - } - StateHasChanged(); - } - private async Task SetSelectedItem(ValueType item) { //DropDownClassToggle(); await ValueChanged.InvokeAsync(item); } - private async Task FilterItems() + private void FilterItems() { if (string.IsNullOrEmpty(_searchText)) { @@ -78,7 +61,7 @@ public partial class SimpleSelect : InputBase { OriginalItems = Items; - if (_searchText.Length>0) await FilterItems(); + if (_searchText.Length>0) FilterItems(); await base.OnParametersSetAsync(); }