You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Components/Alert/Alert.razor.cs

144 lines
3.8 KiB

2 years ago
using System.Diagnostics.CodeAnalysis;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components;
public partial class Alert : UIComponent
{
[CascadingParameter(Name = "RightToLeft")]
public bool RightToLeft { get; set; }
/// <summary>
/// Sets the position of the text to the start (Left in LTR and right in RTL).
/// </summary>
[Parameter]
public HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
/// <summary>
/// The callback, when the close button has been clicked.
/// </summary>
[Parameter] public EventCallback<Alert> CloseGlyphClicked { get; set; }
/// <summary>
/// Define the icon used for the close button.
/// </summary>
[Parameter]
public string CloseGlyph { get; set; } = Icons.Material.Filled.Close;
/// <summary>
/// Sets if the alert shows a close icon.
/// </summary>
[Parameter]
public bool CloseGlyphVisible { get; set; }
/// <summary>
/// The higher the number, the heavier the drop-shadow. 0 for no shadow.
/// </summary>
[Parameter]
public int Elevation { set; get; } = 0;
/// <summary>
/// If true, rounded corners are disabled.
/// </summary>
[Parameter]
public bool Square { get; set; }
/// <summary>
/// If true, compact padding will be used.
/// </summary>
[Parameter]
public bool Dense { get; set; }
/// <summary>
/// If true, no alert icon will be used.
/// </summary>
[Parameter]
public bool GlyphVisible { get; set; }
/// <summary>
/// The severity of the alert. This defines the color and icon used.
/// </summary>
[Parameter]
public Severity Severity { get; set; } = Severity.Normal;
/// <summary>
/// The variant to use.
/// </summary>
[Parameter]
public Variant Variant { get; set; } = Variant.Text;
/// <summary>
/// Child content of the component.
/// </summary>
[Parameter]
public RenderFragment? ChildContent { get; set; }
/// <summary>
/// Custom icon, leave unset to use the standard icon which depends on the Severity
/// </summary>
[Parameter]
public string Glyph { get; set; } = default!;
/// <summary>
/// Raised when the alert is clicked
/// </summary>
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
protected string ClassList
{
get
{
return new CssBuilder("alert")
.AddClass($"alert-{Variant.ToDescriptionString()}-{Severity.ToDescriptionString()}")
.AddClass($"dense", Dense)
.AddClass($"square", Square)
.AddClass($"elevation-{Elevation}")
.AddClass(Class)
.Build();
}
}
protected string ClassPosition
{
get
{
return new CssBuilder("alert-position")
.AddClass($"justify-sm-{ConvertHorizontalAlignment(Alignment).ToDescriptionString()}")
.Build();
}
}
private HorizontalAlignment ConvertHorizontalAlignment(HorizontalAlignment contentAlignment)
{
return contentAlignment switch
{
HorizontalAlignment.Right => RightToLeft ? HorizontalAlignment.Start : HorizontalAlignment.End,
HorizontalAlignment.Left => RightToLeft ? HorizontalAlignment.End : HorizontalAlignment.Start,
_ => contentAlignment
};
}
internal async Task OnCloseGlyphClick()
{
if (CloseGlyphClicked.HasDelegate)
{
await CloseGlyphClicked.InvokeAsync(this);
return;
}
await Task.CompletedTask;
}
//If we can check this exception can include the coverage again
[ExcludeFromCodeCoverage]
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(Glyph))
return;
Glyph = Severity switch
{
Severity.Normal => Icons.Material.Outlined.EventNote,
Severity.Info => Icons.Material.Outlined.Info,
Severity.Success => Icons.Custom.Uncategorized.AlertSuccess,
Severity.Warning => Icons.Material.Outlined.ReportProblem,
Severity.Error => Icons.Material.Filled.ErrorOutline,
_ => throw new ArgumentOutOfRangeException(nameof(Severity)),
};
}
}