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

New default UI for toasts #181

Merged
merged 8 commits into from
Jan 4, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Well, this escolated quickly...
chrissainty committed Sep 19, 2022
commit 2a3eaa465dd28f2cad2f96fe9cb91de052a3c140
8 changes: 4 additions & 4 deletions samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -5,19 +5,19 @@
<h1>Blazored Toasts</h1>

<button class="btn btn-info" @onclick="@(() => toastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" @onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom heading", "Congratulations!"))">Success Toast</button>
<button class="btn btn-success" @onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom heading"))">Success Toast</button>
<button class="btn btn-warning" @onclick="@(() => toastService.ShowWarning("I'm a WARNING message"))">Warning Toast</button>
<button class="btn btn-danger" @onclick="@(() => toastService.ShowError("I'm an ERROR message"))">Error Toast</button>
<button class="btn btn-info" @onclick="@OnShowHtml">Info Toast with HTML</button>
<button class="btn btn-info" @onclick="@(() => toastService.ShowInfo("Click to refresh the page", "Click me!", () => { NavigationManager.NavigateTo("/", true); }))">
<button class="btn btn-info" @onclick="@(() => toastService.ShowInfo("Click to refresh the page", options => options.OnClick = () => NavigationManager.NavigateTo("/", true)))">
Info Toast with custom action on click
</button>
<hr />

<h1>Blazored Toasts - Custom Component</h1>

<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>
<button class="btn btn-primary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(settings => { settings.Timeout = 5; settings.ShowProgressBar = false; }))">Custom Toast</button>
<button class="btn btn-secondary" @onclick="@(() => toastService.ShowToast<MyToastComponent>(_toastParameters, settings => { settings.Timeout = 5; settings.ShowProgressBar = false; }))">Custom Toast with parameters</button>
<hr />

<h1>Blazored Toasts - Remove Toasts</h1>
11 changes: 6 additions & 5 deletions samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
@page "/"
@using Blazored.Toast.Configuration
@inject IToastService ToastService
@inject NavigationManager NavigationManager

<PageTitle>Blazored Toast Samples</PageTitle>

<h1>Blazored Toasts</h1>

<button class="btn btn-info" id="InfoButton" @onclick="@(() => ToastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" id="SuccessButton" @onclick="@(() => ToastService.ShowSuccess("I'm a SUCCESS message with a custom heading", "Congratulations!"))">Success Toast</button>
<button class="btn btn-info" id="InfoButton" @onclick="@(() => ToastService.ShowInfo("I'm an INFO message", settings => settings.IconType = IconType.None))">Info Toast</button>
<button class="btn btn-success" id="SuccessButton" @onclick="@(() => ToastService.ShowSuccess("I'm a SUCCESS message with a lot of text to see what a toast looks like when it's really big."))">Success Toast</button>
<button class="btn btn-warning" @onclick="@(() => ToastService.ShowWarning("I'm a WARNING message"))">Warning Toast</button>
<button class="btn btn-danger" @onclick="@(() => ToastService.ShowError("I'm an ERROR message"))">Error Toast</button>
<button class="btn btn-info" @onclick="@OnShowHtml">Info Toast with HTML</button>
<button class="btn btn-info" @onclick="@(() => ToastService.ShowInfo("Click to refresh the page", "Click me!", () => { NavigationManager.NavigateTo("/", true); }))">
<button class="btn btn-info" @onclick="@(() => ToastService.ShowInfo("Click to refresh the page", settings => settings.OnClick = () => NavigationManager.NavigateTo("/", true)))">
Info Toast with custom action on click
</button>
<hr />
<h1>Blazored Toasts - Custom Component</h1>
<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>
<button class="btn btn-primary" @onclick="@(() => ToastService.ShowToast<MyToastComponent>(settings => { settings.Timeout = 5; settings.ShowProgressBar = false; }))">Custom Toast</button>
<button class="btn btn-secondary" id="CustomButton" @onclick="@(() => ToastService.ShowToast<MyToastComponent>(_toastParameters, settings => { settings.Timeout = 5; settings.ShowProgressBar = true; }))">Custom Toast with parameters</button>
<hr />
<h1>Blazored Toasts - Remove Toasts</h1>
3 changes: 1 addition & 2 deletions samples/BlazorWebAssembly/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -2,8 +2,7 @@
@using Blazored.Toast.Configuration

<BlazoredToasts Position="ToastPosition.BottomRight"
Timeout="60"
IconType="IconType.FontAwesome"
Timeout="120"
ShowProgressBar="true"
SuccessClass="success-toast-override"
ErrorIcon="fa fa-bug"
31 changes: 0 additions & 31 deletions samples/bUnitExample/BlazoredToastTests.cs
Original file line number Diff line number Diff line change
@@ -64,20 +64,6 @@ public void DisplaysToastWithLevel()
Assert.Equal(ToastLevel.Info, toastService.Toasts.Single().ToastLevel);
}

[Fact]
public void DisplaysToastWithHeading()
{
// Arrange
var toastService = this.AddBlazoredToast();
var cut = RenderComponent<Index>();

// Act
cut.Find("#SuccessButton").Click();

// Assert
Assert.Equal("Congratulations!", toastService.Toasts.Single().Heading);
}

[Fact]
public void DisplaysTwoToastsWithLevel()
{
@@ -95,23 +81,6 @@ public void DisplaysTwoToastsWithLevel()
_ => Assert.Equal(ToastLevel.Success, _.ToastLevel));
}

[Fact]
public void DisplaysTwoToastsWithHeading()
{
// Arrange
var toastService = this.AddBlazoredToast();
var cut = RenderComponent<Index>();

// Act
cut.Find("#InfoButton").Click();
cut.Find("#SuccessButton").Click();

// Assert
Assert.Collection(toastService.Toasts,
_ => Assert.Equal("Info", _.Heading),
_ => Assert.Equal("Congratulations!", _.Heading));
}

[Fact]
public void DisplaysToasts()
{
4 changes: 1 addition & 3 deletions src/Blazored.Toast.TestExtensions/InMemoryToast.cs
Original file line number Diff line number Diff line change
@@ -9,14 +9,12 @@ public class InMemoryToast
public Type ToastType { get; set; }
public ToastLevel ToastLevel { get; }
public RenderFragment Message { get; }
public string Heading { get; }

public InMemoryToast(Type toastType, ToastLevel toastLevel, RenderFragment message, string heading)
public InMemoryToast(Type toastType, ToastLevel toastLevel, RenderFragment message)
{
ToastType = toastType;
ToastLevel = toastLevel;
Message = message;
Heading = heading;
}

public InMemoryToast(Type toastType)
112 changes: 45 additions & 67 deletions src/Blazored.Toast.TestExtensions/InMemoryToastService.cs
Original file line number Diff line number Diff line change
@@ -1,102 +1,80 @@
using Blazored.Toast.Services;
using Blazored.Toast.Configuration;
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;

namespace Blazored.Toast.TestExtensions;

public class InMemoryToastService : IToastService
{
private readonly List<InMemoryToast> toasts = new List<InMemoryToast>();
public IReadOnlyList<InMemoryToast> Toasts => toasts;
private readonly List<InMemoryToast> _toasts = new();
public IReadOnlyList<InMemoryToast> Toasts => _toasts;

public event Action<ToastLevel, RenderFragment, Action<ToastSettings>?>? OnShow;
public event Action<Type, ToastParameters?, Action<ToastSettings>?>? OnShowComponent;
public event Action? OnClearAll;
public event Action<ToastLevel>? OnClearToasts;
public event Action? OnClearCustomToasts;

public event Action<ToastLevel, RenderFragment, string, Action> OnShow;
public void ShowToast<TComponent>() where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public event Action<Type, ToastParameters, ToastInstanceSettings> OnShowComponent;
public event Action OnClearAll;
public event Action<ToastLevel> OnClearToasts;
public event Action OnClearCustomToasts;
public void ShowToast<TComponent>(ToastParameters parameters) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>() where TComponent : IComponent
{
toasts.Add(new InMemoryToast(typeof(TComponent)));
}
public void ShowToast<TComponent>(Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastParameters parameters) where TComponent : IComponent
{
toasts.Add(new InMemoryToast(typeof(TComponent)));
}
public void ShowToast<TComponent>(ToastParameters parameters, Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastInstanceSettings settings) where TComponent : IComponent
{
toasts.Add(new InMemoryToast(typeof(TComponent)));
}
public void ShowError(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Error, message, settings);

public void ShowToast<TComponent>(ToastParameters parameters, ToastInstanceSettings settings) where TComponent : IComponent
{
toasts.Add(new InMemoryToast(typeof(TComponent)));
}
public void ShowError(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Error, message, settings);

public void ShowError(string message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Error, message, heading, onClick);
public void ShowInfo(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Info, message, settings);

public void ShowError(RenderFragment message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Error, message, heading, onClick);
public void ShowInfo(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Info, message, settings);

public void ShowInfo(string message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Info, message, heading, onClick);
public void ShowSuccess(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Success, message, settings);

public void ShowInfo(RenderFragment message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Info, message, heading, onClick);
public void ShowSuccess(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Success, message, settings);

public void ShowSuccess(string message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Success, message, heading, onClick);
public void ShowToast(ToastLevel level, string message, Action<ToastSettings>? settings = null)
=> ShowToast(level, builder => builder.AddContent(0, message), settings);

public void ShowSuccess(RenderFragment message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Success, message, heading, onClick);
public void ShowToast(ToastLevel level, RenderFragment message, Action<ToastSettings>? settings = null)
=> _toasts.Add(new InMemoryToast(typeof(ToastInstance), level, message));

public void ShowToast(ToastLevel level, string message, string heading = "", Action onClick = null)
=> ShowToast(level, builder => builder.AddContent(0, message), heading, onClick);
public void ShowWarning(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Warning, message, settings);

public void ShowToast(ToastLevel level, RenderFragment message, string heading = "", Action onClick = null)
=> toasts.Add(new InMemoryToast(typeof(Configuration.ToastInstance), level, message, GetHeading(level, heading)));

public void ShowWarning(string message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Warning, message, heading, onClick);

public void ShowWarning(RenderFragment message, string heading = "", Action onClick = null)
=> ShowToast(ToastLevel.Warning, message, heading, onClick);

private string GetHeading(ToastLevel level, string heading)
{
if (!string.IsNullOrWhiteSpace(heading)) return heading;

return level switch
{
ToastLevel.Error => "Error",
ToastLevel.Info => "Info",
ToastLevel.Success => "Success",
ToastLevel.Warning => "Warning",
_ => throw new InvalidOperationException(),
};
}
public void ShowWarning(RenderFragment message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Warning, message, settings);

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

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

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

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

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

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

public void ClearCustomToasts()
=> toasts.RemoveAll(x => x.ToastType != typeof(Configuration.ToastInstance));
=> _toasts.RemoveAll(x => x.ToastType != typeof(ToastInstance));
}
6 changes: 0 additions & 6 deletions src/Blazored.Toast/Blazored.Toast.csproj
Original file line number Diff line number Diff line change
@@ -30,19 +30,13 @@
</PropertyGroup>

<ItemGroup>
<Content Remove="bundleconfig.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.7" />
</ItemGroup>

<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath="\" />
<None Include="bundleconfig.json" />
</ItemGroup>

<ItemGroup>
Loading