Skip to content

Commit

Permalink
bug fixed: clearing selecting item in JingetDropDownTreeList and Jing…
Browse files Browse the repository at this point in the history
…etDropDownList
  • Loading branch information
vahid committed Aug 5, 2024
1 parent 0a49eea commit eae58c3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 47 deletions.
26 changes: 26 additions & 0 deletions 01-Core/Jinget.Core/ExtensionMethods/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,30 @@ public static bool HasSameValuesAs(this object source, object target)

return JsonConvert.SerializeObject(source, settings) == JsonConvert.SerializeObject(target, settings);
}

/// <summary>
/// Check if an object has default value or not. This extension method works only for value types.
/// For reference types or nullable value types(which has null value) it always returns false.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool HasDefaultValue(this object value)
{
//null is not default value for non-nullable value types
if (value == null)
return false;

Type type = value.GetType();

// can't be, as would be null
if (!type.IsValueType)
return false;

// ditto, Nullable<T>
if (Nullable.GetUnderlyingType(type) != null)
return false;

object defaultValue = Activator.CreateInstance(type);
return value.Equals(defaultValue);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Jinget.Blazor.Components.DropDownList
@using Jinget.Core.ExtensionMethods
@using Microsoft.AspNetCore.Components.Forms
@using MudBlazor
@inherits JingetDropDownListTreeBase;
Expand All @@ -13,14 +14,6 @@
<option value="---" data-parent="" data-level=1>@DefaultText</option>
@foreach (var item in Items)
{
@* bool selected = item.Value != null && Value != null && item.Value.ToString().Equals(Value.ToString()); *@
@* if (selected)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_selectItem", Id, Value).GetAwaiter().GetResult();
});
} *@
@if (item.ParentValue == null)
{
<option value="@item.Value" data-parent="" data-level="@item.Padding">@item.Text</option>
Expand All @@ -30,13 +23,6 @@
<option value="@item.Value" data-parent="@item.ParentValue" data-level="@item.Padding">@item.Text</option>
}
}
@* @if (Value == null)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_clear", Id).GetAwaiter().GetResult();
});
} *@
</select>
@if (!HasSelectedValue())
{
Expand All @@ -53,7 +39,7 @@
await InitComponentAsync("initJingetDropDownListTree");
await base.OnAfterRenderAsync(firstRender);
}
if (Value == null)
if (Value == null || Value.HasDefaultValue())
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_clear", Id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,24 @@
@using Microsoft.AspNetCore.Components.Forms
@using Jinget.Core.ExtensionMethods
@using Microsoft.AspNetCore.Components.Forms
@using MudBlazor
@inherits JingetDropDownListBase;

@if (connected)
{
<select id="@Id"
class="@CssClass"
disabled="@IsDisabled"
required="@IsRequired">
<option value="">@DefaultText</option>
<select id="@Id"
class="@CssClass"
disabled="@IsDisabled"
required="@IsRequired">
<option value="">@DefaultText</option>
@foreach (var item in Items)
{
bool selected = item.Value != null && Value != null && item.Value.ToString().Equals(Value.ToString());
@* if (selected)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_selectItem", Id, Value).GetAwaiter().GetResult();
});
} *@
<option value="@item.Value" selected="@selected">@item.Text</option>
@* <option value="@item.Value">@item.Text</option> *@
}
@* @if (Value == null)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_clear", Id).GetAwaiter().GetResult();
});
}
else
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_selectItem", Id, Value).GetAwaiter().GetResult();
});
} *@
</select>
</select>
@if (!HasSelectedValue())
{
<label style="color:red">@RequiredError</label>
<label style="color:red">@RequiredError</label>
}
}
@code {
Expand All @@ -52,7 +31,7 @@
await InitComponentAsync("initJingetDropDownList");
await base.OnAfterRenderAsync(firstRender);
}
if (Value == null)
if (Value == null || Value.HasDefaultValue())
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_clear", Id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected async Task OnSelectedItemChangedAsync(object? e)
Value = e;

SelectedItem = e == null ? null : Items.FirstOrDefault(x => x.Value?.ToString() == e.ToString());

StateHasChanged();
await OnChange.InvokeAsync(new ChangeEventArgs { Value = e });
}
Expand Down
33 changes: 33 additions & 0 deletions Tests/Jinget.Core.Tests/ExtensionMethods/ObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using Jinget.Core.Tests._BaseData;
using static Jinget.Core.ExtensionMethods.ObjectExtensions;
using System;

namespace Jinget.Core.ExtensionMethods.Tests;

Expand Down Expand Up @@ -125,4 +126,36 @@ public void should_return_true_for_two_objects_with_same_structure_and_value()

Assert.IsTrue(result);
}

[TestMethod()]
public void should_return_true_for_guid_default_value()
{
Guid input = default;
var result = input.HasDefaultValue();
Assert.IsTrue(result);
}

[TestMethod()]
public void should_return_true_for_int_default_value()
{
int input = default;
var result = input.HasDefaultValue();
Assert.IsTrue(result);
}

[TestMethod()]
public void should_return_false_for_nullable_int_default_value()
{
int? input = null;
var result = input.HasDefaultValue();
Assert.IsFalse(result);
}

[TestMethod()]
public void should_return_false_for_reference_type_default_value()
{
TestClass input = new();
var result = input.HasDefaultValue();
Assert.IsFalse(result);
}
}

0 comments on commit eae58c3

Please sign in to comment.