using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System.Threading.Tasks;
namespace AntBlazor
{
///
/// Alert component for feedback.
///
public class AntAlertBase : AntDomComponentBase
{
///
/// Called when close animation is finished
///
[Parameter] public EventCallback AfterClose { get; set; }
///
/// Whether to show as banner
///
[Parameter] public bool Banner { get; set; } = false;
///
/// Whether Alert can be closed
///
[Parameter] public bool Closable { get; set; } = false;
///
/// Close text to show
///
[Parameter] public string CloseText { get; set; }
///
/// Additional content of Alert
///
[Parameter] public string Description { get; set; }
///
/// Custom icon, effective when showIcon is true
///
[Parameter] public string Icon { get; set; }
///
/// Content of Aler
///
[Parameter] public string Message { get; set; }
///
/// Whether to show icon.
///
[Parameter] public bool ShowIcon { get; set; }
///
/// Type of Alert styles, options: success, info, warning, error
///
[Parameter] public string Type { get; set; } = AntAlertType.Default;
///
/// Callback when Alert is closed.
///
[Parameter] public EventCallback OnClose { get; set; }
///
/// Additional Content
///
[Parameter] public RenderFragment ChildContent { get; set; }
///
/// Icon to show.
///
protected string iconType => !string.IsNullOrEmpty(Icon) ? Icon
: Type == AntAlertType.Success ? "check-circle"
: Type == AntAlertType.Info ? "info-circle"
: Type == AntAlertType.Warning ? "exclamation-circle"
: Type == AntAlertType.Error ? "close-circle" : null;
///
/// Indicator if the component is closed or not.
///
protected bool isClosed = false;
///
/// Just before we close the component we set this indicator to show a closing animation.
///
protected bool isClosing = false;
///
/// Sets the default classes.
///
protected void SetClassMap()
{
string prefixName = "ant-alert";
ClassMapper.Clear()
.Add("ant-alert")
.If($"{prefixName}-{Type}", () => !string.IsNullOrEmpty(Type))
.If($"{prefixName}-no-icon", () => !ShowIcon)
.If($"{prefixName}-closable", () => Closable)
.If($"{prefixName}-banner", () => Banner)
.If($"{prefixName}-with-description", () => !string.IsNullOrEmpty(Description))
.If($"{prefixName}-slide-up-leave", () => isClosing)
;
}
///
/// Triggered each time a parameter is changed.
///
protected override void OnParametersSet()
{
base.OnParametersSet();
SetClassMap();
}
///
/// Start-up code.
///
protected override void OnInitialized()
{
base.OnInitialized();
CheckBannerMode();
SetClassMap();
}
private void CheckBannerMode()
{
if (Banner && string.IsNullOrEmpty(Type))
{
ShowIcon = false;
}
}
///
/// Handles the close callback.
///
///
///
protected async Task OnCloseHandler(MouseEventArgs args)
{
isClosing = true;
if (OnClose.HasDelegate)
{
await OnClose.InvokeAsync(args);
}
await Task.Delay(300);
isClosed = true;
await AfterCloseHandler(args);
}
///
/// Handles the after close callback.
///
///
///
protected async Task AfterCloseHandler(MouseEventArgs args)
{
if (AfterClose.HasDelegate)
{
await AfterClose.InvokeAsync(args);
}
}
}
}