Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DefaultValue parameter to BitInputBase (#8186) #8742

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<EditForm Model="@TestModel" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
<DataAnnotationsValidator />

<BitCheckbox IsEnabled="@IsEnabled"
<BitCheckbox IsEnabled="IsEnabled"
AriaLabel="@AriaLabel"
CheckmarkIconName="@CheckmarkIconName"
CheckIconName="@CheckmarkIconName"
AriaDescription="@AriaDescription"
AriaLabelledby="@AriaLabelledby"
AriaPositionInSet="@AriaPositionInSet"
AriaSetSize="@AriaSetSize"
AriaPositionInSet="AriaPositionInSet"
AriaSetSize="AriaSetSize"
Name="@Name"
Title="@Title"
DefaultValue="@DefaultValue"
@bind-Value="@TestModel.Value"
@* DefaultValue="DefaultValue" *@
@bind-Value="TestModel.Value"
CheckIconAriaLabel="@CheckIconAriaLabel"
Reversed="@Reversed"
DefaultIndeterminate="@DefaultIndeterminate"
Reversed="Reversed"
DefaultIndeterminate="DefaultIndeterminate"
OnClick="HandleOnClick"
OnChange="HandleOnChange">
<ChildContent>
Expand Down
24 changes: 23 additions & 1 deletion src/BlazorUI/Bit.BlazorUI/Components/Inputs/BitInputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected BitInputBase()



/// <summary>
/// The default value of the input when the value has not been set.
/// </summary>
[Parameter] public TValue? DefaultValue { get; set; }

/// <summary>
/// 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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ public partial class BitCheckbox : BitInputBase<bool>, IDisposable
/// </summary>
[Parameter] public bool? DefaultIndeterminate { get; set; }

/// <summary>
/// Default checkbox state
/// Use this if you want an uncontrolled component, meaning the Checkbox instance maintains its own state.
/// </summary>
[Parameter] public bool? DefaultValue { get; set; }

/// <summary>
/// An indeterminate visual state for checkbox.
/// Setting indeterminate state takes visual precedence over checked given but does not affect on Value state.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public partial class BitChoiceGroup<TItem, TValue> : BitInputBase<TValue> where
/// </summary>
[Parameter] public BitChoiceGroupClassStyles? Classes { get; set; }

/// <summary>
/// Default selected item for ChoiceGroup.
/// </summary>
[Parameter] public TValue? DefaultValue { get; set; }

/// <summary>
/// Renders the items in the ChoiceGroup horizontally.
/// </summary>
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ namespace Bit.BlazorUI;
/// </summary>
[Parameter] public bool Combo { get; set; }

/// <summary>
/// The default key value that will be initially used to set selected item if the Value parameter is not set.
/// </summary>
[Parameter] public TValue? DefaultValue { get; set; }

/// <summary>
/// The default key value that will be initially used to set selected items in multi select mode if the Values parameter is not set.
/// </summary>
Expand Down Expand Up @@ -597,13 +592,6 @@ protected override async Task OnInitializedAsync()
await AssignValues(DefaultValues);
}
}
else
{
if (ValueHasBeenSet is false && DefaultValue is not null)
{
Value = DefaultValue;
}
}

UpdateSelectedItemsFromValues();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public BitNumberField()
/// </summary>
[Parameter] public string DecrementIconName { get; set; } = "ChevronDownSmall";

/// <summary>
/// Initial value of the number field.
/// </summary>
[Parameter] public TValue? DefaultValue { get; set; }

/// <summary>
/// The aria label of the icon for the benefit of screen readers.
/// </summary>
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public partial class BitRating : BitInputBase<double>
/// </summary>
[Parameter] public BitRatingClassStyles? Classes { get; set; }

/// <summary>
/// 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.
/// </summary>
[Parameter] public double? DefaultValue { get; set; }

/// <summary>
/// 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.
/// </summary>
Expand Down Expand Up @@ -60,16 +54,6 @@ public partial class BitRating : BitInputBase<double>



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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ public partial class BitSearchBox : BitTextInputBase<string?>, IAsyncDisposable
/// </summary>
[Parameter] public BitSearchBoxClassStyles? Classes { get; set; }

/// <summary>
/// The default value of the text in the SearchBox, in the case of an uncontrolled component.
/// </summary>
[Parameter] public string? DefaultValue { get; set; }

/// <summary>
/// Whether or not to animate the search box icon on focus.
/// </summary>
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public partial class BitSpinButton : BitInputBase<double>
/// </summary>
[Parameter] public string? DecrementTitle { get; set; }

/// <summary>
/// Initial value of the spin button.
/// </summary>
[Parameter] public double? DefaultValue { get; set; }

/// <summary>
/// The aria label of the icon for the benefit of screen readers.
/// </summary>
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public partial class BitTextField : BitTextInputBase<string?>
/// </summary>
[Parameter] public BitTextFieldClassStyles? Classes { get; set; }

/// <summary>
/// Default value of the text field. Only provide this if the text field is an uncontrolled component; otherwise, use the value property.
/// </summary>
[Parameter] public string? DefaultValue { get; set; }

/// <summary>
/// Description displayed below the text field to provide additional details about what text to enter.
/// </summary>
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public partial class ComponentDemo

private readonly List<ComponentParameter> _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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -101,8 +94,8 @@ public partial class BitChoiceGroupDemo
Type = "BitChoiceGroupNameSelectors<TItem, TValue>?",
DefaultValue = "null",
Description = "Names and selectors of the custom input type properties.",
Href = "#name-selectors",
LinkType = LinkType.Link,
Href = "#name-selectors",
},
new()
{
Expand Down Expand Up @@ -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",
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>",
Expand Down
Loading