Skip to content

Commit

Permalink
add property type
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Aug 1, 2024
1 parent b4b81d8 commit c5df54a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
51 changes: 32 additions & 19 deletions src/LoreSoft.Blazor.Controls/Data/DataColumn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;

using LoreSoft.Blazor.Controls.Extensions;

Expand All @@ -10,7 +11,6 @@ namespace LoreSoft.Blazor.Controls;
public class DataColumn<TItem> : ComponentBase
{
private Func<TItem, object> _propertyAccessor;
private string _propertyName;

[CascadingParameter(Name = "Grid")]
protected DataGrid<TItem> Grid { get; set; }
Expand Down Expand Up @@ -63,9 +63,6 @@ public class DataColumn<TItem> : ComponentBase
[Parameter]
public bool Visible { get; set; } = true;

public string Name => PropertyName();


[Parameter]
public RenderFragment HeaderTemplate { get; set; }

Expand All @@ -78,6 +75,9 @@ public class DataColumn<TItem> : ComponentBase
[Parameter]
public RenderFragment<QueryFilter> FilterTemplate { get; set; }

public string Name { get; set; }

public Type Type { get; set; }

internal int CurrentSortIndex { get; set; } = -1;

Expand All @@ -99,13 +99,19 @@ protected override void OnInitialized()
Grid.AddColumn(this);
}

protected override void OnParametersSet()
{
base.OnParametersSet();

UpdateProperty();
}

internal string HeaderTitle()
{
if (!string.IsNullOrEmpty(Title))
return Title;

var name = PropertyName();
return name.ToTitle();
return Name.ToTitle();
}

internal string CellValue(TItem data)
Expand Down Expand Up @@ -140,25 +146,32 @@ internal void UpdateSort(int index, bool descending)
CurrentSortDescending = descending;
}

private string PropertyName()
private void UpdateProperty()
{
if (Property == null)
return string.Empty;
MemberInfo memberInfo = null;

if (!string.IsNullOrEmpty(_propertyName))
return _propertyName;
if (Property?.Body is MemberExpression memberExpression)
memberInfo = memberExpression.Member;
else if (Property?.Body is UnaryExpression { Operand: MemberExpression memberOperand })
memberInfo = memberOperand.Member;

_propertyName = Property?.Body switch
if (memberInfo is PropertyInfo propertyInfo)
{
MemberExpression memberExpression => memberExpression.Member.Name,
UnaryExpression { Operand: MemberExpression memberOperand } => memberOperand.Member.Name,
_ => string.Empty
};

return _propertyName;
Name = propertyInfo.Name;
Type = propertyInfo.PropertyType;
}
else if (memberInfo is FieldInfo fieldInfo)
{
Name = fieldInfo.Name;
Type = fieldInfo.FieldType;
}
else
{
Name = memberInfo.Name;
Type = typeof(object);
}
}


internal Dictionary<string, object> ComputeAttributes(TItem data)
{
var attributes = new Dictionary<string, object>();
Expand Down
7 changes: 5 additions & 2 deletions src/LoreSoft.Blazor.Controls/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public static Type GetUnderlyingType(this Type type)
throw new ArgumentNullException(nameof(type));

var isNullable = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
return isNullable ? Nullable.GetUnderlyingType(type) : type;
if (isNullable)
return Nullable.GetUnderlyingType(type) ?? type;

return type;
}

/// <summary>
Expand All @@ -44,7 +47,7 @@ public static bool IsNullable(this Type type)
/// </summary>
/// <param name="type">The type to get a default value for.</param>
/// <returns>A default value the specified <paramref name="type"/>.</returns>
public static object Default(this Type type)
public static object? Default(this Type type)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
Expand Down
1 change: 1 addition & 0 deletions src/LoreSoft.Blazor.Controls/Query/QueryBuilderField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void UpdateProperty()
else
{
Name = memberInfo.Name;
Type = typeof(object);
}
}

Expand Down

0 comments on commit c5df54a

Please sign in to comment.