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

Allow property navigation for ChildComponent rendering when using CascadingTypeParameter #41120

Closed
BrunoBlanes opened this issue Apr 9, 2022 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@BrunoBlanes
Copy link

BrunoBlanes commented Apr 9, 2022

Is your feature request related to a problem? Please describe the problem.

I am trying to simplify my Table component design for the consumer of my library. Currently it is implemented as follows:

Table.razor

@using Microsoft.AspNetCore.Components.Web.Virtualization

@typeparam TItem
@attribute [CascadingTypeParameter(nameof(TItem))]

<CascadingValue IsFixed="true" Value="this">@ChildContent</CascadingValue>

<div class="table-container column">
    <Virtualize Items="Items" Context="item">
        @foreach (var column in columns)
        {
            @column.GetValue(item)
        }
    </Virtualize>
</div>

@code {
    private List<TableColumn<TItem>> columns = new();

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    [Parameter]
    public ICollection<TItem>? Items { get; set; }

    internal void AddTableColumn(TableColumn<TItem> column)
    {
	columns.Add(column);
    }
}

TableColumn.cs

public partial class TableColumn<TItem> : ComponentBase
{
    private Func<TItem, object>? compiledExpression;

    [CascadingParameter]
    public Table<TItem>? Table { get; set; }

    [Parameter]
    public Expression<Func<TItem, object>?>? Expression { get; set; }

    protected override void OnInitialized()
    {
	Table?.AddTableColumn(this);
	base.OnInitialized();
    }

    internal object? GetCellValue(TItem item)
    {
	ArgumentNullException.ThrowIfNull(compiledExpression);
	return compiledExpression(item);
    }
}

Usage

<Table Items="group?.Blocks">
    <TableColumn Expression="block => block.Name" />
    <TableColumn Expression="block => block.Id" />
    <TableColumn Expression="block => block.Group" />
</Table>

Describe the solution you'd like

I'd like for the end user to be able to define the table as follows:

<Table Items="group?.Blocks" Context="block">
    <TableColumn>@block.Name</TableColumn>
    <TableColumn>@block.Id</TableColumn>
    <TableColumn>@block.Group</TableColumn>
</Table>

I've tried a few things, but the way RenderFragment seems to be implemented now doesn't allow this to work.

Additional context

I've been looking around for topics on this issue and found #29379 which is related but I don't think addresses this specificly.

@TanayParikh TanayParikh added the area-blazor Includes: Blazor, Razor Components label Apr 9, 2022
@javiercn
Copy link
Member

@BrunoBlanes thanks for contacting us.

You can already do this by passing a RenderFragment instead of a RenderFragment. It's not clear that we are missing anything here.

Your child content in Table.razor as well as in TableColum.razor need to be RenderFragment<T>, but afterwards, it should work.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Apr 10, 2022
@ghost ghost added the Status: Resolved label Apr 10, 2022
@BrunoBlanes
Copy link
Author

Thanks, with your help and the help of this article I got it working. I'm gonna go dig myself a hole to cry on in fetal position for all the hours I spent on trial and error now...

@BrunoBlanes
Copy link
Author

Quick question, is it possible to know when @ChildContent(T) will return string.Empty or null?

@ghost ghost locked as resolved and limited conversation to collaborators May 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants