From 67847fbe553065fb81250908dda56a0f5b83b735 Mon Sep 17 00:00:00 2001 From: Saleh Yusefnejad Date: Thu, 26 Sep 2024 01:44:54 +0330 Subject: [PATCH] add DefaultValue parameter to BitInputBase #8186 --- .../Checkbox/BitCheckboxValidationTest.razor | 16 ++++++------- .../Components/Inputs/BitInputBase.cs | 24 ++++++++++++++++++- .../Inputs/Checkbox/BitCheckbox.razor.cs | 11 --------- .../ChoiceGroup/BitChoiceGroup.razor.cs | 7 ++---- .../Inputs/Dropdown/BitDropdown.razor.cs | 12 ---------- .../NumberField/BitNumberField.razor.cs | 15 ------------ .../Inputs/Rating/BitRating.razor.cs | 16 ------------- .../Inputs/SearchBox/BitSearchBox.razor.cs | 10 -------- .../Inputs/SpinButton/BitSpinButton.razor.cs | 14 ++--------- .../Inputs/TextField/BitTextField.razor.cs | 14 ++--------- .../Components/ComponentDemo.razor.cs | 7 ++++++ .../Inputs/Checkbox/BitCheckboxDemo.razor.cs | 7 ------ .../ChoiceGroup/BitChoiceGroupDemo.razor.cs | 13 +++------- .../Inputs/Dropdown/BitDropdownDemo.razor.cs | 7 ------ 14 files changed, 47 insertions(+), 126 deletions(-) diff --git a/src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/Checkbox/BitCheckboxValidationTest.razor b/src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/Checkbox/BitCheckboxValidationTest.razor index 8048cf4809..82e423fb29 100644 --- a/src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/Checkbox/BitCheckboxValidationTest.razor +++ b/src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/Checkbox/BitCheckboxValidationTest.razor @@ -1,20 +1,20 @@  - diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs index e709c7ba73..34dff5e475 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs @@ -46,6 +46,11 @@ protected BitInputBase() + /// + /// The default value of the input when the value has not been set. + /// + [Parameter] public TValue? DefaultValue { get; set; } + /// /// Gets or sets the display name for this field. /// This value is used when generating error messages when the input value fails to parse correctly. @@ -146,6 +151,11 @@ public override Task SetParametersAsync(ParameterView parameters) parametersDictionary.Remove(parameter.Key); break; + case nameof(DefaultValue): + DefaultValue = (TValue?)parameter.Value; + parametersDictionary.Remove(parameter.Key); + break; + case nameof(DisplayName): DisplayName = (string?)parameter.Value; parametersDictionary.Remove(parameter.Key); @@ -225,11 +235,23 @@ public override Task SetParametersAsync(ParameterView parameters) protected override void OnInitialized() { - ClassBuilder.Register(() => ValueInvalid is true ? "bit-inv" : string.Empty); + if (ShouldUseDefaultValue && ValueHasBeenSet is false && DefaultValue is not null) + { + Value = DefaultValue; + } base.OnInitialized(); } + protected override void RegisterCssClasses() + { + ClassBuilder.Register(() => ValueInvalid is true ? "bit-inv" : string.Empty); + + base.RegisterCssClasses(); + } + + protected virtual bool ShouldUseDefaultValue => true; + protected bool? ValueInvalid diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Checkbox/BitCheckbox.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Checkbox/BitCheckbox.razor.cs index ffd4a57f37..a72913da8c 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Checkbox/BitCheckbox.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Checkbox/BitCheckbox.razor.cs @@ -57,12 +57,6 @@ public partial class BitCheckbox : BitInputBase, IDisposable /// [Parameter] public bool? DefaultIndeterminate { get; set; } - /// - /// Default checkbox state - /// Use this if you want an uncontrolled component, meaning the Checkbox instance maintains its own state. - /// - [Parameter] public bool? DefaultValue { get; set; } - /// /// An indeterminate visual state for checkbox. /// Setting indeterminate state takes visual precedence over checked given but does not affect on Value state. @@ -110,11 +104,6 @@ protected override async Task OnInitializedAsync() OnValueChanged += HandleOnValueChanged; - if (ValueHasBeenSet is false && DefaultValue is not null) - { - Value = DefaultValue.Value; - } - if (IndeterminateHasBeenSet is false && DefaultIndeterminate is not null) { await SetIndeterminate(DefaultIndeterminate.Value); diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/ChoiceGroup/BitChoiceGroup.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/ChoiceGroup/BitChoiceGroup.razor.cs index 53ec44d4fa..53d0a32da2 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/ChoiceGroup/BitChoiceGroup.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/ChoiceGroup/BitChoiceGroup.razor.cs @@ -26,11 +26,6 @@ public partial class BitChoiceGroup : BitInputBase where /// [Parameter] public BitChoiceGroupClassStyles? Classes { get; set; } - /// - /// Default selected item for ChoiceGroup. - /// - [Parameter] public TValue? DefaultValue { get; set; } - /// /// Renders the items in the ChoiceGroup horizontally. /// @@ -162,6 +157,8 @@ protected override void RegisterCssStyles() protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string? validationErrorMessage) => throw new NotSupportedException($"This component does not parse string inputs. Bind to the '{nameof(CurrentValue)}' property, not '{nameof(CurrentValueAsString)}'."); + protected override bool ShouldUseDefaultValue => false; + private void InitDefaultValue() diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs index 8d843136d7..2f8a255301 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Dropdown/BitDropdown.razor.cs @@ -80,11 +80,6 @@ namespace Bit.BlazorUI; /// [Parameter] public bool Combo { get; set; } - /// - /// The default key value that will be initially used to set selected item if the Value parameter is not set. - /// - [Parameter] public TValue? DefaultValue { get; set; } - /// /// The default key value that will be initially used to set selected items in multi select mode if the Values parameter is not set. /// @@ -597,13 +592,6 @@ protected override async Task OnInitializedAsync() await AssignValues(DefaultValues); } } - else - { - if (ValueHasBeenSet is false && DefaultValue is not null) - { - Value = DefaultValue; - } - } UpdateSelectedItemsFromValues(); diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs index 26c5fe1dba..a803903aa4 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs @@ -81,11 +81,6 @@ public BitNumberField() /// [Parameter] public string DecrementIconName { get; set; } = "ChevronDownSmall"; - /// - /// Initial value of the number field. - /// - [Parameter] public TValue? DefaultValue { get; set; } - /// /// The aria label of the icon for the benefit of screen readers. /// @@ -249,16 +244,6 @@ protected override void RegisterCssStyles() StyleBuilder.Register(() => _hasFocus ? Styles?.Focused : string.Empty); } - protected override async Task OnInitializedAsync() - { - if (ValueHasBeenSet is false && DefaultValue is not null) - { - Value = DefaultValue; - } - - await base.OnInitializedAsync(); - } - protected override bool TryParseValueFromString(string? value, [MaybeNullWhen(false)] out TValue result, [NotNullWhen(false)] out string? parsingErrorMessage) { if (NumberFormat is not null) diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Rating/BitRating.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Rating/BitRating.razor.cs index cd6aff21d1..19ef342572 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Rating/BitRating.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/Rating/BitRating.razor.cs @@ -21,12 +21,6 @@ public partial class BitRating : BitInputBase /// [Parameter] public BitRatingClassStyles? Classes { get; set; } - /// - /// Default value. Must be a number between _min and max. - /// Only provide this if the CurrentValue is an uncontrolled component; otherwise, use the Value property. - /// - [Parameter] public double? DefaultValue { get; set; } - /// /// Optional callback to set the aria-label for rating control in readOnly mode. Also used as a fallback aria-label if ariaLabel prop is not provided. /// @@ -60,16 +54,6 @@ public partial class BitRating : BitInputBase - protected override async Task OnInitializedAsync() - { - if (ValueHasBeenSet is false && DefaultValue.HasValue) - { - Value = DefaultValue.Value; - } - - await base.OnInitializedAsync(); - } - protected override string RootElementClass => "bit-rtg"; protected override void RegisterCssClasses() diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBox.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBox.razor.cs index 92d6696df9..ce1a68a6fc 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBox.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SearchBox/BitSearchBox.razor.cs @@ -31,11 +31,6 @@ public partial class BitSearchBox : BitTextInputBase, IAsyncDisposable /// [Parameter] public BitSearchBoxClassStyles? Classes { get; set; } - /// - /// The default value of the text in the SearchBox, in the case of an uncontrolled component. - /// - [Parameter] public string? DefaultValue { get; set; } - /// /// Whether or not to animate the search box icon on focus. /// @@ -181,11 +176,6 @@ protected override async Task OnInitializedAsync() _scrollContainerId = $"BitSearchBox-{UniqueId}-scroll-container"; _inputId = $"BitSearchBox-{UniqueId}-input"; - if (CurrentValue.HasNoValue() && DefaultValue.HasValue()) - { - await SetCurrentValueAsStringAsync(DefaultValue); - } - OnValueChanged += HandleOnValueChanged; _dotnetObj = DotNetObjectReference.Create(this); diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SpinButton/BitSpinButton.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SpinButton/BitSpinButton.razor.cs index 08cadefafb..41bf022426 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SpinButton/BitSpinButton.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/SpinButton/BitSpinButton.razor.cs @@ -66,11 +66,6 @@ public partial class BitSpinButton : BitInputBase /// [Parameter] public string? DecrementTitle { get; set; } - /// - /// Initial value of the spin button. - /// - [Parameter] public double? DefaultValue { get; set; } - /// /// The aria label of the icon for the benefit of screen readers. /// @@ -209,16 +204,11 @@ protected override void RegisterCssStyles() StyleBuilder.Register(() => Styles?.Root); } - protected override async Task OnInitializedAsync() + protected override void OnInitialized() { _inputId = $"BitSpinButton-{UniqueId}-input"; - if (ValueHasBeenSet is false && DefaultValue.HasValue) - { - Value = DefaultValue.Value; - } - - await base.OnInitializedAsync(); + base.OnInitialized(); } protected override async Task OnParametersSetAsync() diff --git a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/TextField/BitTextField.razor.cs b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/TextField/BitTextField.razor.cs index 4de247201c..db04a456c4 100644 --- a/src/BlazorUI/Bit.BlazorUI/Components/Inputs/TextField/BitTextField.razor.cs +++ b/src/BlazorUI/Bit.BlazorUI/Components/Inputs/TextField/BitTextField.razor.cs @@ -34,11 +34,6 @@ public partial class BitTextField : BitTextInputBase /// [Parameter] public BitTextFieldClassStyles? Classes { get; set; } - /// - /// Default value of the text field. Only provide this if the text field is an uncontrolled component; otherwise, use the value property. - /// - [Parameter] public string? DefaultValue { get; set; } - /// /// Description displayed below the text field to provide additional details about what text to enter. /// @@ -215,18 +210,13 @@ protected override void RegisterCssStyles() StyleBuilder.Register(() => _hasFocus ? Styles?.Focused : string.Empty); } - protected override async Task OnInitializedAsync() + protected override void OnInitialized() { _inputId = $"BitTextField-{UniqueId}-input"; _labelId = $"BitTextField-{UniqueId}-label"; _descriptionId = $"BitTextField-{UniqueId}-description"; - if (ValueHasBeenSet is false && DefaultValue is not null) - { - await SetCurrentValueAsStringAsync(DefaultValue, true); - } - - await base.OnInitializedAsync(); + base.OnInitialized(); } protected override async Task OnAfterRenderAsync(bool firstRender) diff --git a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Components/ComponentDemo.razor.cs b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Components/ComponentDemo.razor.cs index cc67574b63..51876f5768 100644 --- a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Components/ComponentDemo.razor.cs +++ b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Components/ComponentDemo.razor.cs @@ -161,6 +161,13 @@ public partial class ComponentDemo private readonly List _inputBaseParameters = [ + new() + { + Name = "DefaultValue", + Type = "TValue?", + DefaultValue = "null", + Description = "The default value of the input when the value has not been set.", + }, new() { Name = "DisplayName", diff --git a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Checkbox/BitCheckboxDemo.razor.cs b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Checkbox/BitCheckboxDemo.razor.cs index cad6fee18b..2ee3e1711f 100644 --- a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Checkbox/BitCheckboxDemo.razor.cs +++ b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Checkbox/BitCheckboxDemo.razor.cs @@ -70,13 +70,6 @@ public partial class BitCheckboxDemo Description = "Default indeterminate visual state for checkbox.", }, new() - { - Name = "DefaultValue", - Type = "bool?", - DefaultValue = "null", - Description = "Use this if you want an uncontrolled component, meaning the Checkbox instance maintains its own state.", - }, - new() { Name = "Indeterminate", Type = "bool", diff --git a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/ChoiceGroup/BitChoiceGroupDemo.razor.cs b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/ChoiceGroup/BitChoiceGroupDemo.razor.cs index 03742bd487..74cbad1a4b 100644 --- a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/ChoiceGroup/BitChoiceGroupDemo.razor.cs +++ b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/ChoiceGroup/BitChoiceGroupDemo.razor.cs @@ -24,15 +24,8 @@ public partial class BitChoiceGroupDemo Type = "BitChoiceGroupClassStyles?", DefaultValue = "null", Description = "Custom CSS classes for different parts of the BitChoiceGroup.", - Href = "#class-styles", LinkType = LinkType.Link, - }, - new() - { - Name = "DefaultValue", - Type = "string?", - DefaultValue = "null", - Description = "Default selected Value for ChoiceGroup." + Href = "#class-styles", }, new() { @@ -101,8 +94,8 @@ public partial class BitChoiceGroupDemo Type = "BitChoiceGroupNameSelectors?", DefaultValue = "null", Description = "Names and selectors of the custom input type properties.", - Href = "#name-selectors", LinkType = LinkType.Link, + Href = "#name-selectors", }, new() { @@ -130,8 +123,8 @@ public partial class BitChoiceGroupDemo Type = "BitChoiceGroupClassStyles?", DefaultValue = "null", Description = "Custom CSS styles for different parts of the BitChoiceGroup.", - Href = "#class-styles", LinkType = LinkType.Link, + Href = "#class-styles", }, ]; diff --git a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs index ab81034c30..a349c77dfb 100644 --- a/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs +++ b/src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/Dropdown/BitDropdownDemo.razor.cs @@ -70,13 +70,6 @@ public partial class BitDropdownDemo Description = "Activates the ComboBox feature in BitDropDown component.", }, new() - { - Name = "DefaultValue", - Type = "string?", - DefaultValue = "null", - Description = "The default key value that will be initially used to set selected item if the Value parameter is not set.", - }, - new() { Name = "DefaultValues", Type = "List",