Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adds ability to clear all toasts or all toast types #137

Merged
merged 6 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

<button class="btn btn-primary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(new ToastInstanceSettings(5, true)))">Custom Toast</button>
<button class="btn btn-secondary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(_toastParameters, new ToastInstanceSettings(5, true)))">Custom Toast with parameters</button>
<hr />

<h1>Blazored Toasts - Remove Toasts</h1>

<button class="btn btn-primary" @onclick="ClearAll">Clear All Toasts</button>
<button class="btn btn-warning" @onclick="ClearWarnings">Clear Warning Toasts</button>
<button class="btn btn-info" @onclick="ClearInfos">Clear Info Toasts</button>
<button class="btn btn-secondary" @onclick="ClearCustom">Clear Custom Toasts</button>

@code
{
Expand All @@ -32,7 +40,20 @@

private void OnShowHtml()
{
RenderFragment message =@<text>I'm an <em>INFO</em> message with some <strong>bold</strong> text</text>;
RenderFragment message =@<text>I'm an <em>INFO</em> message with some <strong>bold</strong> text</text>
;
toastService.ShowToast(ToastLevel.Info, message);
}

private void ClearAll()
=> toastService.ClearAll();

private void ClearWarnings()
=> toastService.ClearToasts(ToastLevel.Warning);

private void ClearInfos()
=> toastService.ClearInfoToasts();

private void ClearCustom()
=> toastService.ClearCustomToasts();
}
23 changes: 22 additions & 1 deletion samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

<button class="btn btn-primary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(new ToastInstanceSettings(5, true)))">Custom Toast</button>
<button class="btn btn-secondary" id="CustomButton" @onclick="@(() => toastService.ShowToast<MyToastComponent>(_toastParameters, new ToastInstanceSettings(5, true)))">Custom Toast with parameters</button>
<hr />

<h1>Blazored Toasts - Remove Toasts</h1>

<button class="btn btn-primary" @onclick="ClearAll">Clear All Toasts</button>
<button class="btn btn-warning" @onclick="ClearWarnings">Clear Warning Toasts</button>
<button class="btn btn-info" @onclick="ClearInfos">Clear Info Toasts</button>
<button class="btn btn-secondary" @onclick="ClearCustom">Clear Custom Toasts</button>

@code
{
Expand All @@ -32,7 +40,20 @@

private void OnShowHtml()
{
RenderFragment message =@<text>I'm an <em>INFO</em> message with some <strong>bold</strong> text</text>;
RenderFragment message =@<text>I'm an <em>INFO</em> message with some <strong>bold</strong> text</text>
;
toastService.ShowToast(ToastLevel.Info, message);
}

private void ClearAll()
=> toastService.ClearAll();

private void ClearWarnings()
=> toastService.ClearToasts(ToastLevel.Warning);

private void ClearInfos()
=> toastService.ClearInfoToasts();

private void ClearCustom()
=> toastService.ClearCustomToasts();
}
24 changes: 24 additions & 0 deletions src/Blazored.Toast.TestExtensions/InMemoryToastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class InMemoryToastService : IToastService
public event Action<ToastLevel, RenderFragment, string, Action> OnShow;

public event Action<Type, ToastParameters, ToastInstanceSettings> OnShowComponent;
public event Action OnClearAll;
public event Action<ToastLevel> OnClearToasts;
public event Action OnClearCustomToasts;

public void ShowToast<TComponent>() where TComponent : IComponent
{
Expand Down Expand Up @@ -77,5 +80,26 @@ private string GetHeading(ToastLevel level, string heading)
_ => throw new InvalidOperationException(),
};
}

public void ClearAll()
=> toasts.Clear();

public void ClearToasts(ToastLevel toastLevel)
=> toasts.RemoveAll(x => x.ToastType == typeof(Configuration.ToastInstance) && x.ToastLevel == toastLevel);

public void ClearWarningToasts()
=> toasts.RemoveAll(x => x.ToastType == typeof(Configuration.ToastInstance) && x.ToastLevel == ToastLevel.Warning);

public void ClearInfoToasts()
=> toasts.RemoveAll(x => x.ToastType == typeof(Configuration.ToastInstance) && x.ToastLevel == ToastLevel.Info);

public void ClearSuccessToasts()
=> toasts.RemoveAll(x => x.ToastType == typeof(Configuration.ToastInstance) && x.ToastLevel == ToastLevel.Success);

public void ClearErrorToasts()
=> toasts.RemoveAll(x => x.ToastType == typeof(Configuration.ToastInstance) && x.ToastLevel == ToastLevel.Error);

public void ClearCustomToasts()
=> toasts.RemoveAll(x => x.ToastType != typeof(Configuration.ToastInstance));
}
}
40 changes: 36 additions & 4 deletions src/Blazored.Toast/BlazoredToasts.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected override void OnInitialized()
{
ToastService.OnShow += ShowToast;
ToastService.OnShowComponent += ShowToast;
ToastService.OnClearAll += ClearAll;
ToastService.OnClearToasts += ClearToasts;
ToastService.OnClearCustomToasts += ClearCustomToasts;


if (RemoveToastsOnNavigation)
Expand All @@ -60,6 +63,8 @@ protected override void OnInitialized()
}
}



public void RemoveToast(Guid toastId)
{
InvokeAsync(() =>
Expand Down Expand Up @@ -94,19 +99,19 @@ private ToastSettings BuildToastSettings(ToastLevel level, RenderFragment messag
switch (level)
{
case ToastLevel.Error:
return new ToastSettings(string.IsNullOrWhiteSpace(heading) ? "Error" : heading, message, IconType,
return new ToastSettings(ToastLevel.Error, string.IsNullOrWhiteSpace(heading) ? "Error" : heading, message, IconType,
"blazored-toast-error", ErrorClass, ErrorIcon, ShowProgressBar, MaxToastCount, onclick);

case ToastLevel.Info:
return new ToastSettings(string.IsNullOrWhiteSpace(heading) ? "Info" : heading, message, IconType,
return new ToastSettings(ToastLevel.Info, string.IsNullOrWhiteSpace(heading) ? "Info" : heading, message, IconType,
"blazored-toast-info", InfoClass, InfoIcon, ShowProgressBar, MaxToastCount, onclick);

case ToastLevel.Success:
return new ToastSettings(string.IsNullOrWhiteSpace(heading) ? "Success" : heading, message, IconType,
return new ToastSettings(ToastLevel.Success, string.IsNullOrWhiteSpace(heading) ? "Success" : heading, message, IconType,
"blazored-toast-success", SuccessClass, SuccessIcon, ShowProgressBar, MaxToastCount, onclick);

case ToastLevel.Warning:
return new ToastSettings(string.IsNullOrWhiteSpace(heading) ? "Warning" : heading, message, IconType,
return new ToastSettings(ToastLevel.Warning, string.IsNullOrWhiteSpace(heading) ? "Warning" : heading, message, IconType,
"blazored-toast-warning", WarningClass, WarningIcon, ShowProgressBar, MaxToastCount, onclick);
}

Expand Down Expand Up @@ -181,5 +186,32 @@ private void ShowToast(Type contentComponent, ToastParameters parameters, ToastI
StateHasChanged();
});
}

private void ClearAll()
{
InvokeAsync(() =>
{
ToastList.Clear();
StateHasChanged();
});
}

private void ClearToasts(ToastLevel toastLevel)
{
InvokeAsync(() =>
{
ToastList.RemoveAll(x => x.BlazoredToast == null && x.ToastSettings.ToastLevel == toastLevel);
StateHasChanged();
});
}

private void ClearCustomToasts()
{
InvokeAsync(() =>
{
ToastList.RemoveAll(x => x.BlazoredToast is object);
StateHasChanged();
});
}
}
}
4 changes: 4 additions & 0 deletions src/Blazored.Toast/Configuration/ToastSettings.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;

namespace Blazored.Toast.Configuration
{
public class ToastSettings
{
public ToastSettings(
ToastLevel toastLevel,
string heading,
RenderFragment message,
IconType? iconType,
Expand All @@ -16,6 +18,7 @@ public ToastSettings(
int maxItemsShown,
Action? onClick)
{
ToastLevel = toastLevel;
Heading = heading;
Message = message;
IconType = iconType;
Expand All @@ -31,6 +34,7 @@ public ToastSettings(
}
}

public ToastLevel ToastLevel { get; set; }
public string Heading { get; set; }
public RenderFragment Message { get; set; }
public string BaseClass { get; set; }
Expand Down
51 changes: 51 additions & 0 deletions src/Blazored.Toast/Services/IToastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ public interface IToastService
/// </summary>
event Action<ToastLevel, RenderFragment, string, Action> OnShow;

/// <summary>
/// A event that will be invoked to clear all toasts
/// </summary>
event Action OnClearAll;

/// <summary>
/// A event that will be invoked to clear toast of specified level
/// </summary>
event Action<ToastLevel> OnClearToasts;

/// <summary>
/// A event that will be invoked to clear custom toast components
/// </summary>
event Action OnClearCustomToasts;

/// <summary>
/// A event that will be invoked when showing a toast with a custom comonent
/// </summary>
Expand Down Expand Up @@ -110,5 +125,41 @@ public interface IToastService
/// <param name="parameters">Key/Value collection of parameters to pass to component being displayed</param>
/// <param name="settings">Key/Settings to pass to component being displayed</param>
void ShowToast<TComponent>(ToastParameters parameters, ToastInstanceSettings settings) where TComponent : IComponent;

/// <summary>
/// Removes all toasts
/// </summary>
void ClearAll();

/// <summary>
/// Removes all toasts with a specified <paramref name="toastLevel"/>.
/// </summary>
void ClearToasts(ToastLevel toastLevel);

/// <summary>
/// Removes all toasts with toast level warning
/// </summary>
void ClearWarningToasts();

/// <summary>
/// Removes all toasts with toast level Info
/// </summary>
void ClearInfoToasts();

/// <summary>
/// Removes all toasts with toast level Success
/// </summary>
void ClearSuccessToasts();

/// <summary>
/// Removes all toasts with toast level Error
/// </summary>
void ClearErrorToasts();

/// <summary>
/// Removes all custom toast components
/// </summary>
void ClearCustomToasts();

}
}
Loading