Skip to content

Commit

Permalink
🐛 (DataTable): ItemColContent and CellRender cannot be used together
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem committed Jul 2, 2024
1 parent 808781d commit 4edc7e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
38 changes: 30 additions & 8 deletions src/Masa.Blazor/Components/DataTable/Row/ItemColProps.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
namespace Masa.Blazor;

public class ItemColProps<TItem>
public class ItemColProps<TItem>(DataTableHeader<TItem> header, TItem item)
{
public DataTableHeader<TItem> Header { get; set; }
public DataTableHeader<TItem> Header { get; } = header;

public object? Value => Header.ItemValue.Invoke(Item);
public TItem Item { get; } = item;

public TItem Item { get; set; }

public ItemColProps(DataTableHeader<TItem> header, TItem item)
/// <summary>
/// The value of current cell.
/// It's recommended to use <see cref="ValueContent"/>
/// to render cell content as it has better compatibility.
/// </summary>
public object? Value
{
Header = header;
Item = item;
get
{
if (Header.CellRender is not null)
{
var render = Header.CellRender.Invoke(Item);
if (render.IsT0)
{
return render.AsT0;
}

return render.AsT1;
}

return Header.ItemValue.Invoke(Item);
}
}

/// <summary>
/// The render fragment of current cell.
/// </summary>
public RenderFragment ValueContent
=> Value as RenderFragment ?? (RenderFragment)(builder => builder.AddContent(0, Value));
}
28 changes: 7 additions & 21 deletions src/Masa.Blazor/Components/DataTable/Row/MDataTableRow.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,13 @@
<td class="@GetCellClass(header)"
style="@GetCellStyle(header)"
title="@title">
@{
if (HasSlot(props))
{
@SlotContent(props)
}
else if (header.CellRender is not null)
{
var cell = header.CellRender(Item);
if (cell.IsT0)
{
@cell.AsT0
}
else
{
@cell.AsT1
}
}
else
{
@props.Value
}
@if (HasSlot(props))
{
@SlotContent(props)
}
else
{
@props.ValueContent
}
</td>
}
Expand Down

0 comments on commit 4edc7e8

Please sign in to comment.