Skip to content

Commit

Permalink
Added ForEachWithIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpmorris committed Aug 3, 2022
1 parent f417a99 commit 8990104
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 32 deletions.
9 changes: 9 additions & 0 deletions Source/Lib/Morris.Blazor.ControlFlow/ForEachWithIndex.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@typeparam TItem
@{
int i = 0;
foreach (TItem value in In ?? Enumerable.Empty<TItem>())
{
var item = new ValueWithIndex<TItem>(value!, i++);
@ChildContent!(item);
}
}
16 changes: 16 additions & 0 deletions Source/Lib/Morris.Blazor.ControlFlow/ForEachWithIndex.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;

namespace Morris.Blazor.ControlFlow;

public partial class ForEachWithIndex<TItem>
{
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter] public IEnumerable<TItem>? In { get; set; }
#if NET6_0_OR_GREATER
[EditorRequired]
#endif
[Parameter] public RenderFragment<ValueWithIndex<TItem>>? ChildContent { get; set; }
}
13 changes: 13 additions & 0 deletions Source/Lib/Morris.Blazor.ControlFlow/ValueWithIndex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Morris.Blazor.ControlFlow;

public readonly struct ValueWithIndex<T>
{
public T Value { get; } = default!;
public int Index { get; } = 0;

public ValueWithIndex(T value, int index)
{
Index = index;
Value = value;
}
}
87 changes: 55 additions & 32 deletions Source/Tutorials/ForEachExample/Pages/FetchData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,62 @@

<p>This component demonstrates fetching data from a service.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<ForEach Context=forecast In=@forecasts>
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
</ForEach>
</tbody>
</table>
}
<If Condition=@(forecasts is null)>
<Then>
<p><em>Loading...</em></p>
</Then>
<Else>
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<ForEach Context=forecast In=@forecasts>
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
</ForEach>
</tbody>
</table>
<hr />
<table class="table">
<thead>
<tr>
<th>Index</th>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<ForEachWithIndex Context=item In=@forecasts>
<tr>
<td>@item.Index</td>
<td>@item.Value.Date.ToShortDateString()</td>
<td>@item.Value.TemperatureC</td>
<td>@item.Value.TemperatureF</td>
<td>@item.Value.Summary</td>
</tr>
</ForEachWithIndex>
</tbody>
</table>
</Else>
</If>

@code {
private WeatherForecast[] forecasts;
private WeatherForecast[] forecasts;

protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
protected override async Task OnInitializedAsync()
{
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}

0 comments on commit 8990104

Please sign in to comment.