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

Consider adding an Index to your Iterator #3

Open
kevonh opened this issue Jul 1, 2021 · 1 comment
Open

Consider adding an Index to your Iterator #3

kevonh opened this issue Jul 1, 2021 · 1 comment

Comments

@kevonh
Copy link

kevonh commented Jul 1, 2021

We started down the path of doing things like you are doing, but came up with a slightly different approach for the Iterator and Iterations... Essentially, we tried to keep as much of the razor tags as close to C# as possible. This implementation breaks one thing I like better about yours... and that is you have direct access to the object in the loop. With the below method,, one must understand you have an Item as well as an Index.

public class ComponentContext<T> where T : ComponentBase
{
    public ComponentContext(T @component) => Component = @component;
    public T Component { get; set; }
}

public class ForEach<T> : ComponentBase
{
    [Parameter]
    public IEnumerable<T> Items { get; set; }
    [Parameter]
    public RenderFragment<ForEachContext<T>> ChildContent { get; set; }

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        var index = 0;
        foreach (var item in Items)
            builder?.AddContent(0, ChildContent(new ForEachContext<T>(this, item, index++)));
    }
}

public class ForEachContext<T> : ComponentContext<ForEach<T>>
{
    public ForEachContext(ForEach<T> component, T item, int index) : base(component)
    {
        Item = item;
        Index = index;
    }
    public T Item { get; }
    public int Index { get; }
}
@Zettersten
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants