diff --git a/src/Connected.Components/Components/CheckBox.razor b/src/Connected.Components/Components/CheckBox.razor index 5afe120..2e67d1a 100644 --- a/src/Connected.Components/Components/CheckBox.razor +++ b/src/Connected.Components/Components/CheckBox.razor @@ -3,7 +3,7 @@ @inherits InputBase; diff --git a/src/Connected.Components/Components/CheckBox.razor.cs b/src/Connected.Components/Components/CheckBox.razor.cs index a9f51d3..06ee16d 100644 --- a/src/Connected.Components/Components/CheckBox.razor.cs +++ b/src/Connected.Components/Components/CheckBox.razor.cs @@ -4,20 +4,11 @@ using Microsoft.AspNetCore.Components; namespace Connected.Components; public partial class CheckBox : InputBase { - [CascadingParameter] - public CheckBoxGroup? ParentCheckBoxGroup { get; set; } - - private bool _checked { get; set; } - [Parameter] - public bool? Checked - { - get => _checked; - set => _checked = (bool)value; - } + public bool Checked { get; set; } [Parameter, EditorRequired] - public string Id { get; set; } + public string? Id { get; set; } [Parameter] public EventCallback CheckedChanged { get; set; } @@ -25,18 +16,7 @@ public partial class CheckBox : InputBase public async Task OnChange() { Checked = !Checked; - CheckedChanged.InvokeAsync((bool)Checked); - } - protected override async Task OnParametersSetAsync() - { - await base.OnInitializedAsync(); - if (ParentCheckBoxGroup.Disabled) Disabled = true; - if (!InputAttributes.ContainsKey("disabled")) - InputAttributes.Add("disabled", Disabled); - if (!InputAttributes.ContainsKey("checked")) - InputAttributes.Add("checked", Checked); - StateHasChanged(); + await CheckedChanged.InvokeAsync(Checked); } - } diff --git a/src/Connected.Components/Components/CheckBoxGroup.razor b/src/Connected.Components/Components/CheckBoxGroup.razor deleted file mode 100644 index 79e7232..0000000 --- a/src/Connected.Components/Components/CheckBoxGroup.razor +++ /dev/null @@ -1,13 +0,0 @@ -@using Connected.Models; - - -
- @if (!string.IsNullOrEmpty(Name)) - { -
@Name
- } -
- @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 deleted file mode 100644 index 289ef29..0000000 --- a/src/Connected.Components/Components/CheckBoxGroup.razor.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Components; - -namespace Connected.Components; -public partial class CheckBoxGroup -{ - [Parameter, EditorRequired] - public string Name { get; set; } - - [Parameter] - public bool Disabled { get; set; } - - [Parameter] - public RenderFragment ChildContent { get; set; } -} diff --git a/src/Connected.Components/Components/Glyph.razor.cs b/src/Connected.Components/Components/Glyph.razor.cs index 62d88d3..3dfe54e 100644 --- a/src/Connected.Components/Components/Glyph.razor.cs +++ b/src/Connected.Components/Components/Glyph.razor.cs @@ -4,26 +4,26 @@ using Microsoft.AspNetCore.Components.Web; namespace Connected.Components { - public partial class Glyph - { - [Parameter] - public string SVG { get; set; } = string.Empty; + public partial class Glyph + { + [Parameter] + public string SVG { get; set; } = string.Empty; - [Parameter] - public string Color { get; set; } = "#000000"; + [Parameter] + public string Color { get; set; } = "#000000"; - [Parameter] - public int Width { get; set; } = 24; - [Parameter] - public int Height { get; set; } = 24; + [Parameter] + public int Width { get; set; } = 24; + [Parameter] + public int Height { get; set; } = 24; - private string WidthString + private string WidthString + { + get { - get - { - return Width.ToString() + "px"; - } + return Width.ToString() + "px"; } + } private string HeightString { @@ -34,17 +34,17 @@ namespace Connected.Components } [Parameter] - public string Class { get; set; } = string.Empty; + public string Class { get; set; } = string.Empty; - private string GlyphClassList + private string GlyphClassList + { + get { - get - { - return new CssBuilder() - .AddClass(Class) - .Build(); - } + return new CssBuilder() + .AddClass(Class) + .Build(); } + } [Parameter] /// @@ -54,7 +54,6 @@ namespace Connected.Components protected async Task OnClick(MouseEventArgs e) { await Click.InvokeAsync(e); - } } } \ No newline at end of file diff --git a/src/Connected.Components/Components/Link.razor.cs b/src/Connected.Components/Components/Link.razor.cs index d9562e1..7087d4f 100644 --- a/src/Connected.Components/Components/Link.razor.cs +++ b/src/Connected.Components/Components/Link.razor.cs @@ -4,46 +4,48 @@ using Microsoft.AspNetCore.Components; namespace Connected.Components; public partial class Link { - [Parameter, EditorRequired] - public string Url { get; set; } = string.Empty; - - [Parameter, EditorRequired] - public string Text { get; set; } = string.Empty; - - [Parameter, EditorRequired] - public string Target { get; set; } = "_self"; - - [Parameter] - public string Class { get; set; } = string.Empty; - - private string LinkClassList - { - get - { - return new CssBuilder() - .AddClass(Class) - .Build(); - } - } - - [Parameter] - public string Style { get; set; } = string.Empty; - - private string LinkStyleList - { - get - { - return new StyleBuilder() - .AddStyle(Style) - .Build(); - } - } - - protected override async Task OnParametersSetAsync() - { - if (string.IsNullOrEmpty(Text)) Text = Url; - await base.OnParametersSetAsync(); - } + [Parameter, EditorRequired] + public string Url { get; set; } = string.Empty; + + [Parameter, EditorRequired] + public string Text { get; set; } = string.Empty; + + [Parameter, EditorRequired] + public string Target { get; set; } = "_self"; + + [Parameter] + public string Class { get; set; } = string.Empty; + + private string LinkClassList + { + get + { + return new CssBuilder() + .AddClass(Class) + .Build(); + } + } + + [Parameter] + public string Style { get; set; } = string.Empty; + + private string LinkStyleList + { + get + { + return new StyleBuilder() + .AddStyle(Style) + .Build(); + } + } + + protected override async Task OnParametersSetAsync() + { + if (string.IsNullOrEmpty(Text)) + Text = Url; + + await base.OnParametersSetAsync(); + } } diff --git a/src/Connected.Components/Components/NumberInput.razor.cs b/src/Connected.Components/Components/NumberInput.razor.cs index 17f6a14..ddfecbc 100644 --- a/src/Connected.Components/Components/NumberInput.razor.cs +++ b/src/Connected.Components/Components/NumberInput.razor.cs @@ -5,45 +5,36 @@ using Microsoft.AspNetCore.Components.Web; using System.Numerics; namespace Connected.Components; -public partial class NumberInput:InputBase where NumberType : INumber +public partial class NumberInput : InputBase where NumberType : INumber { - - private double _step =1; [Parameter] - public double Step { - get - { - return _step; - } - set - { - _step=value; - } - } + public double Step { get; set; } = 1; [Parameter] - public bool DisableMouseWheel - { - get; - set; - } = false; + public bool DisableMouseWheel { get; set; } = false; private async Task StepUp() { try { - double num = (double)Convert.ChangeType(Value, typeof(double)); - num += _step; + var num = (double)Convert.ChangeType(Value, typeof(double)); + + num += Step; + if (DecimalPlaces > 0) num = Math.Round(num, DecimalPlaces); + Value = (NumberType)Convert.ChangeType(num, typeof(NumberType)); - if (IsError) ErrorText = string.Empty; + + if (IsError) + ErrorText = string.Empty; } catch { ErrorText = "Error with step up!"; - Value = default(NumberType); + Value = default; } + await ValueChanged.InvokeAsync(Value); } @@ -51,96 +42,101 @@ public partial class NumberInput:InputBase where NumberType : INumbe { try { - double num = (double)Convert.ChangeType(Value, typeof(double)); - - num -= _step; + var num = (double)Convert.ChangeType(Value, typeof(double)); + + num -= Step; + if (DecimalPlaces > 0) num = Math.Round(num, DecimalPlaces); + Value = (NumberType)Convert.ChangeType(num, typeof(NumberType)); - if (IsError) ErrorText = string.Empty; - } catch + + if (IsError) + ErrorText = string.Empty; + } + catch { ErrorText = "Error with step down!"; - Value = default(NumberType); + Value = default; } + await ValueChanged.InvokeAsync(Value); } protected async Task OnMouseWheel(WheelEventArgs args) { - if (DisableMouseWheel == false) + if (DisableMouseWheel) + return; + + if (args.ShiftKey || Disabled || Readonly) + return; + + if (args.DeltaY >= 0) { - if (args.ShiftKey || Disabled || Readonly) - return; - if (args.DeltaY >= 0) - { - await StepDown(); - } - else - { - await StepUp(); - } - } else + await StepDown(); + } + else { - return; + await StepUp(); } } - private string _value; + private string? _value; [Parameter] public NumberType? Value { get { - if (string.IsNullOrEmpty(_value)) return default(NumberType); + if (string.IsNullOrEmpty(_value)) + return default; + return (NumberType)Convert.ChangeType(_value, typeof(NumberType)); } set { - _value = value.ToString(); + _value = value?.ToString(); } } [Parameter] - public int DecimalPlaces { get; set; } =0; + public int DecimalPlaces { get; set; } = 0; [Parameter] - public EventCallback ValueChanged { get; set; } + public EventCallback ValueChanged { get; set; } public async Task GetValueAsync(ChangeEventArgs args) - { + { if (args.Value is not null) { - string newVal = args.Value.ToString(); + var newVal = args.Value.ToString()!; + if (!newVal.Equals("0")) { if (newVal.ToString().Contains("-")) newVal = "-" + newVal.ToString().Replace("-", ""); + if (newVal.ToString().ToLower().Contains("e")) newVal = "e" + newVal.ToString().Replace("e", ""); } + if (string.IsNullOrEmpty(newVal)) - { - await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType((NumberType)Convert.ChangeType(null, typeof(NumberType)), typeof(NumberType))); - } - else - { - if (!newVal.Equals(_value)) - await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType((NumberType)Convert.ChangeType(newVal, typeof(NumberType)), typeof(NumberType))); - } + await ValueChanged.InvokeAsync(default); + + if (!newVal.Equals(_value)) + await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(newVal, typeof(NumberType))); } } public async Task Change(ChangeEventArgs args) { if (args.Value is not null) - { Value = AdjustDecimalPlaces((NumberType)Convert.ChangeType(args.Value, typeof(NumberType))); - } - ValueChanged.InvokeAsync(Value); + + await ValueChanged.InvokeAsync(Value); } - [Parameter] public EventCallback OnKeyDown { get; set; } + [Parameter] + public EventCallback OnKeyDown { get; set; } private bool CheckKey(string key) { @@ -159,7 +155,7 @@ public partial class NumberInput:InputBase where NumberType : INumbe break; } default: - { + { result = false; break; } @@ -172,17 +168,19 @@ public partial class NumberInput:InputBase where NumberType : INumbe private bool _preventDefaultAction = true; public async Task ChangeValue(KeyboardEventArgs args) { - _preventDefaultAction= true; + _preventDefaultAction = true; if (args is not null) { var key = args.Key.ToString().ToLower(); + if (CheckKey(key)) { _preventDefaultAction = false; await OnKeyDown.InvokeAsync(args); } - } else + } + else { args.Key = null; } @@ -222,11 +220,13 @@ public partial class NumberInput:InputBase where NumberType : INumbe { if (typeof(NumberType).Name.ToLower().Contains("int")) { - if (Step - (int)Step > 0) Step = (int)Step; - if (Step < 1) Step = 1; + if (Step - (int)Step > 0) + Step = (int)Step; + if (Step < 1) + Step = 1; } await base.OnParametersSetAsync(); - } + } protected override async Task OnInitializedAsync() { diff --git a/src/Connected.Components/Components/Radio.razor b/src/Connected.Components/Components/Radio.razor index 9c307c6..526e965 100644 --- a/src/Connected.Components/Components/Radio.razor +++ b/src/Connected.Components/Components/Radio.razor @@ -3,7 +3,7 @@ @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 index 0d5bfda..ca4dc90 100644 --- a/src/Connected.Components/Components/Radio.razor.cs +++ b/src/Connected.Components/Components/Radio.razor.cs @@ -7,17 +7,11 @@ public partial class Radio : InputBase [CascadingParameter] public RadioGroup? ParentRadioGroup { get; set; } - private bool _checked { get; set; } - [Parameter] - public bool? Checked - { - get => _checked; - set => _checked = (bool)value; - } + public bool Checked { get; set; } [Parameter, EditorRequired] - public string Id { get; set; } + public string? Id { get; set; } [Parameter] public EventCallback CheckedChanged { get; set; } @@ -25,14 +19,6 @@ public partial class Radio : InputBase public async Task OnChange() { Checked = !Checked; - CheckedChanged.InvokeAsync((bool)Checked); - } - - protected override async Task OnInitializedAsync() - { - await base.OnInitializedAsync(); - if (ParentRadioGroup.Disabled) Disabled = true; - if (!InputAttributes.ContainsKey("disabled")) - InputAttributes.Add("disabled", Disabled); + await CheckedChanged.InvokeAsync(Checked); } } diff --git a/src/Connected.Components/Components/RadioGroup.razor b/src/Connected.Components/Components/RadioGroup.razor index a2e30aa..2a159e3 100644 --- a/src/Connected.Components/Components/RadioGroup.razor +++ b/src/Connected.Components/Components/RadioGroup.razor @@ -1,13 +1,14 @@ @using Connected.Models; -
- @if (!string.IsNullOrEmpty(Name)) - { -
@Name
- } -
- @ChildContent -
-
+
+ @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 index 2ec0b4c..12b8005 100644 --- a/src/Connected.Components/Components/RadioGroup.razor.cs +++ b/src/Connected.Components/Components/RadioGroup.razor.cs @@ -4,12 +4,12 @@ namespace Connected.Components; public partial class RadioGroup { [Parameter, EditorRequired] - public string Name { get; set; } + public string? Name { get; set; } [Parameter] public bool Disabled { get; set; } [Parameter] - public RenderFragment ChildContent { get; set; } + public RenderFragment? ChildContent { get; set; } } \ No newline at end of file diff --git a/src/Connected.Components/Components/SimpleSelect.razor.cs b/src/Connected.Components/Components/SimpleSelect.razor.cs index 456a06b..97d4109 100644 --- a/src/Connected.Components/Components/SimpleSelect.razor.cs +++ b/src/Connected.Components/Components/SimpleSelect.razor.cs @@ -1,5 +1,6 @@ using Connected.Models; using Microsoft.AspNetCore.Components; +using System.Collections.ObjectModel; namespace Connected.Components; public partial class SimpleSelect : InputBase @@ -10,7 +11,7 @@ public partial class SimpleSelect : InputBase [Parameter] public IEnumerable Items { get; set; } - public IEnumerable OriginalItems { get; set; } + public ObservableCollection OriginalItems { get; set; } [Parameter] public bool EnableSearch { get; set; } = true; @@ -59,18 +60,20 @@ public partial class SimpleSelect : InputBase protected override async Task OnParametersSetAsync() { - - OriginalItems = Items; - if (_searchText.Length>0) FilterItems(); + + OriginalItems = new ObservableCollection(Items); + + if (_searchText.Length > 0) + FilterItems(); + await base.OnParametersSetAsync(); } protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - if (Required) - { - if (InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true); - } + + if (Required && InputAttributes.ContainsKey("required")) + InputAttributes.Add("required", true); } } \ No newline at end of file diff --git a/src/Connected.Components/Components/TextInput.razor b/src/Connected.Components/Components/TextInput.razor index cf84831..1750968 100644 --- a/src/Connected.Components/Components/TextInput.razor +++ b/src/Connected.Components/Components/TextInput.razor @@ -5,7 +5,7 @@
@if (NumOfRows==1) { - IsPassword ? "password" : "text"; - [Parameter] - public EventCallback ValueChanged { get; set; } + [Parameter] + public EventCallback ValueChanged { get; set; } - private async Task ChangeValueAsync(ChangeEventArgs args) - { - await ValueChanged.InvokeAsync(args.Value.ToString()); - } + private async Task ChangeValueAsync(ChangeEventArgs args) + { + await ValueChanged.InvokeAsync(args?.Value?.ToString()); + } private async Task Clear() { await ValueChanged.InvokeAsync(string.Empty); - } protected override async Task OnInitializedAsync() - { + { await base.OnInitializedAsync(); - if (Required) - { - if (!InputAttributes.ContainsKey("required")) InputAttributes.Add("required", true); - } - } + if (Required && !InputAttributes.ContainsKey("required")) + InputAttributes.Add("required", true); + } } \ No newline at end of file diff --git a/src/Connected.Components/Models/InputBase.cs b/src/Connected.Components/Models/InputBase.cs index f6bda7a..abfd7b7 100644 --- a/src/Connected.Components/Models/InputBase.cs +++ b/src/Connected.Components/Models/InputBase.cs @@ -30,7 +30,7 @@ public class InputBase : ComponentBase [Parameter] public bool Required { get; set; } = false; - public Dictionary InputAttributes { get; set; } + public Dictionary InputAttributes { get; set; } = new(); /// /// Show clear button. @@ -50,18 +50,11 @@ public class InputBase : ComponentBase [Parameter] public bool ShowCharacterCounter { get; set; } - private string _errorText = string.Empty; + /// + /// + /// [Parameter] - public string ErrorText { - get - { - return _errorText; - } - set - { - _errorText = value; - } - } + public string ErrorText { get; set; } = string.Empty; public bool IsError { @@ -79,15 +72,16 @@ public class InputBase : ComponentBase protected virtual async Task SetTextAsync(string text) { - if (Text != text) - { - Text = text; - await TextChanged.InvokeAsync(text); - } + if (Text == text) + return; + + Text = text; + await TextChanged.InvokeAsync(text); } private string _helperText = string.Empty; + [Parameter] public string HelperText {