using System.Diagnostics.CodeAnalysis;
using Connected.Annotations;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components;
public partial class Badge : UIComponent
{
protected string Classname =>
new CssBuilder("mud-badge-root")
.AddClass(Class)
.Build();
protected string WrapperClass =>
new CssBuilder("mud-badge-wrapper")
.AddClass($"mud-badge-{Origin.ToDescriptionString().Replace("-", " ")}")
.Build();
protected string BadgeClassName =>
new CssBuilder("mud-badge")
.AddClass("mud-badge-dot", Dot)
.AddClass("mud-badge-bordered", Bordered)
.AddClass("mud-badge-icon", !string.IsNullOrEmpty(Icon) && !Dot)
.AddClass($"mud-badge-{Origin.ToDescriptionString().Replace("-", " ")}")
.AddClass($"mud-elevation-{Elevation.ToString()}")
.AddClass("mud-theme-" + Color.ToDescriptionString(), Color != ThemeColor.Default)
.AddClass("mud-badge-default", Color == ThemeColor.Default)
.AddClass("mud-badge-overlap", Overlap)
.AddClass(BadgeClass)
.Build();
///
/// The placement of the badge.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public Origin Origin { get; set; } = Origin.TopRight;
///
/// The higher the number, the heavier the drop-shadow.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public int Elevation { set; get; } = 0;
///
/// The visibility of the badge.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public bool Visible { get; set; } = true;
///
/// The color of the badge.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public ThemeColor Color { get; set; } = ThemeColor.Default;
///
/// Aligns the badge to bottom.
///
[ExcludeFromCodeCoverage]
[Obsolete("Use Origin instead.", true)]
[Parameter] public bool Bottom { get; set; }
///
/// Aligns the badge to left.
///
[ExcludeFromCodeCoverage]
[Obsolete("Use Origin instead.", true)]
[Parameter] public bool Left { get => Start; set { Start = value; } }
///
/// Aligns the badge to the start (Left in LTR and right in RTL).
///
[ExcludeFromCodeCoverage]
[Obsolete("Use Origin instead.", true)]
[Parameter] public bool Start { get; set; }
///
/// Reduces the size of the badge and hide any of its content.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public bool Dot { get; set; }
///
/// Overlaps the childcontent on top of the content.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public bool Overlap { get; set; }
///
/// Applies a border around the badge.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public bool Bordered { get; set; }
///
/// Sets the Icon to use in the badge.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public string Icon { get; set; }
///
/// Max value to show when content is integer type.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public int Max { get; set; } = 99;
///
/// Content you want inside the badge. Supported types are string and integer.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public object Content { get; set; }
///
/// Badge class names, separated by space.
///
[Parameter]
[Category(CategoryTypes.Badge.Appearance)]
public string BadgeClass { get; set; }
///
/// Child content of component, the content that the badge will apply to.
///
[Parameter]
[Category(CategoryTypes.Badge.Behavior)]
public RenderFragment ChildContent { get; set; }
///
/// Button click event if set.
///
[Parameter] public EventCallback OnClick { get; set; }
private string _content;
internal Task HandleBadgeClick(MouseEventArgs e)
{
if (OnClick.HasDelegate)
return OnClick.InvokeAsync(e);
return Task.CompletedTask;
}
protected override void OnParametersSet()
{
if (Content is string stringContent)
{
_content = stringContent;
}
else if (Content is int numberContent)
{
if (numberContent > Max)
{
_content = Max + "+";
}
else
{
_content = numberContent.ToString();
}
}
else
{
_content = null;
}
}
}