Last active
August 17, 2020 17:26
-
-
Save EdCharbeneau/346b69aaf3298ca11316eee3fcf6e9f9 to your computer and use it in GitHub Desktop.
EventCallbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="alert alert-danger"> | |
@ChildContent | |
</div> | |
@code { | |
[Parameter] public RenderFragment ChildContent { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@typeparam TItem | |
@foreach (var item in Items) | |
{ | |
<AlertBox> | |
@AlertTemplate(item) | |
@if (OnDelete.HasDelegate) | |
{ | |
<button @onclick="@(_ => HandleDelete(item))" type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
<span aria-hidden="true">×</span> | |
</button> | |
} | |
</AlertBox> | |
} | |
@code { | |
[Parameter] public RenderFragment<TItem> ItemTemplate { get; set; } | |
[Parameter] public IReadOnlyList<TItem> Items { get; set; } | |
[Parameter] public EventCallback<object> OnDelete { get; set; } | |
void HandleDelete(TItem item) | |
{ | |
OnDelete.InvokeAsync(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@page "/counter" | |
<h1>Counter</h1> | |
<p>Current count: @alerts.Count()</p> | |
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | |
<AlertList Items="alerts" Context="alert" OnDelete="DeleteItem"> | |
<ItemTemplate> | |
<span>This is an alert @alert</span> | |
</ItemTemplate> | |
</AlertList> | |
@code { | |
List<int> alerts = new List<int>(); | |
private void IncrementCount() | |
{ | |
alerts.Add(alerts.Count() +1); | |
} | |
void DeleteItem(object item) | |
{ | |
alerts.Remove((int)item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment