ant-design-blazor/components/icon/Icon.razor.cs

124 lines
3.0 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
2019-12-11 00:23:10 +08:00
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
namespace AntDesign
2019-12-11 00:23:10 +08:00
{
public partial class Icon : AntDomComponentBase
2019-12-11 00:23:10 +08:00
{
[Parameter]
public bool Spin { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public int Rotate { get; set; } = 0;
2019-12-11 00:23:10 +08:00
[Parameter]
public string Type { get; set; }
2019-12-11 00:23:10 +08:00
/// <summary>
/// 'fill' | 'outline' | 'twotone';
/// </summary>
2019-12-11 00:23:10 +08:00
[Parameter]
public string Theme { get; set; } = IconThemeType.Outline;
2019-12-11 00:23:10 +08:00
[Parameter]
public string TwotoneColor { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public string IconFont { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public string Width { get; set; } = "1em";
2019-12-11 00:23:10 +08:00
[Parameter]
public string Height { get; set; } = "1em";
2019-12-11 00:23:10 +08:00
[Parameter]
public string Fill { get; set; } = "currentColor";
[Parameter]
public string TabIndex { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public bool StopPropagation { get; set; }
2019-12-16 15:33:29 +08:00
[CascadingParameter]
public Button Button { get; set; }
2019-12-16 15:33:29 +08:00
2019-12-11 00:23:10 +08:00
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
2019-12-11 00:23:10 +08:00
[Inject]
public IconService IconService { get; set; }
2019-12-11 00:23:10 +08:00
[Parameter]
public RenderFragment Component { get; set; }
2019-12-11 00:23:10 +08:00
protected string _svgImg;
2019-12-11 00:23:10 +08:00
protected override void Dispose(bool disposing)
{
if (this is Icon icon)
{
Button?.Icons.Remove(icon);
}
base.Dispose(disposing);
}
2019-12-11 00:23:10 +08:00
protected override async Task OnInitializedAsync()
{
if (Type == "loading")
{
Spin = true;
}
await SetupSvgImg();
2019-12-16 15:33:29 +08:00
if (this is Icon icon)
2019-12-16 15:33:29 +08:00
{
Button?.Icons.Add(icon);
}
ClassMapper.Add($"anticon anticon-{Type}")
.If("anticon-spin", () => Spin);
await base.OnInitializedAsync();
2019-12-11 00:23:10 +08:00
}
protected override async Task OnParametersSetAsync()
2019-12-11 00:23:10 +08:00
{
await SetupSvgImg();
await base.OnParametersSetAsync();
2019-12-11 00:23:10 +08:00
}
protected virtual async Task SetupSvgImg()
2019-12-11 00:23:10 +08:00
{
if (Component != null)
{
return;
2020-04-24 18:32:50 +08:00
}
if (!string.IsNullOrEmpty(IconFont))
2020-04-24 18:32:50 +08:00
{
var svg = $"<svg><use xlink:href=#{IconFont} /></svg>";
2020-08-04 15:10:29 +08:00
_svgImg = IconService.GetStyledSvg(svg, Width, Height, Fill, Rotate);
}
else
{
var svg = await IconService.GetIconImg(Type.ToLowerInvariant(), Theme.ToLowerInvariant());
2020-08-04 15:10:29 +08:00
_svgImg = IconService.GetStyledSvg(svg, Width, Height, Fill, Rotate);
}
2019-12-18 16:57:00 +08:00
StateHasChanged();
2019-12-11 00:23:10 +08:00
}
2019-12-16 15:33:29 +08:00
private async Task HandleOnClick(MouseEventArgs args)
2019-12-16 15:33:29 +08:00
{
if (OnClick.HasDelegate)
2019-12-16 15:33:29 +08:00
{
await OnClick.InvokeAsync(args);
2019-12-16 15:33:29 +08:00
}
}
2019-12-11 00:23:10 +08:00
}
}