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

Fix https://github.com/chanan/BlazorStrap/issues/456 #458

Merged
merged 2 commits into from
Jul 28, 2021
Merged
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
30 changes: 22 additions & 8 deletions src/BlazorStrap/Components/Forms/BSInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class BSInput<T> : InputBase<T>
// Private variables
private bool _clean = true;
private bool _touched = false;


// protected variables
protected string Classname =>
Expand Down Expand Up @@ -189,9 +189,9 @@ protected override void OnInitialized()

protected override string FormatValueAsString(T value)
{
if(typeof(T) == typeof(bool?))
if (typeof(T) == typeof(bool?))
{
if(value == null)
if (value == null)
{
return "";
}
Expand Down Expand Up @@ -276,7 +276,6 @@ protected void OnClick(MouseEventArgs e)
if (InputType == InputType.Radio)
{
Value = (T)(object)(RadioValue);
ValueChanged.InvokeAsync(Value);
}
else
{
Expand All @@ -287,12 +286,10 @@ protected void OnClick(MouseEventArgs e)
if (Value != null)
{
Value = default(T);
ValueChanged.InvokeAsync(Value);
}
else
{
Value = CheckValue;
ValueChanged.InvokeAsync(Value);
}
}
else
Expand All @@ -309,8 +306,20 @@ protected void OnClick(MouseEventArgs e)
}
else
{
var tmp = (bool)(object)Value;
Value = (T)(object)(!tmp);
if (Value == null)
{
Type underlyingType = Nullable.GetUnderlyingType(typeof(T));
// T is a Nullable<>
if (underlyingType != null)
{
Value = (T)GetDefault(underlyingType);
}
}
if (Value != null && Value is bool)
{
var tmp = (bool)(object)Value;
Value = (T)(object)(!tmp);
}
}
ValueChanged.InvokeAsync(Value);
}
Expand Down Expand Up @@ -400,5 +409,10 @@ private void ValidateField(FieldIdentifier fieldIdentifier)
OnFieldChanged?.DynamicInvoke(new object[] { EditContext, new FieldChangedEventArgs(fieldIdentifier) });
StateHasChanged();
}

private object GetDefault(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
}
10 changes: 8 additions & 2 deletions src/SampleCore/Pages/Forms.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
<BSInput Id="exampleInputPassword1" InputType="InputType.Password" PlaceHolder="Password" @bind-Value="formsModelVal.Blank" />
</BSFormGroup>
<BSFormGroup IsCheck="true">
<BSInput Id="exampleCheck1" InputType="InputType.Checkbox" @bind-Value="formsModelVal.Valid" />
<BSInput Id="exampleCheck1"
InputType="InputType.Checkbox"
T="bool?"
Value="formsModelVal.BoolNullable"
ValueChanged=@((v) => { formsModelVal.BoolNullable = v; StateHasChanged(); })
ValueExpression=@(() => formsModelVal.BoolNullable) />
<BSLabel IsCheck="true" For="exampleCheck1">Check me out</BSLabel>
<p class="text-muted">Value: @formsModelVal.BoolNullable</p>
</BSFormGroup>
<BSButton Color="Color.Primary" ButtonType="ButtonType.Submit">Submit</BSButton>
</BSForm>
Expand Down Expand Up @@ -285,7 +291,7 @@

public class FormsModel
{

public bool? BoolNullable { get; set; }
public string Valid { get; set; }
public DateTime Date { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Oh noes! that name is already taken")]
Expand Down